Skip to content

Add battery-aware auto-off - #3

Open
MaximKotliar wants to merge 3 commits into
daemonphantom:mainfrom
MaximKotliar:pr/battery-auto-off
Open

Add battery-aware auto-off#3
MaximKotliar wants to merge 3 commits into
daemonphantom:mainfrom
MaximKotliar:pr/battery-auto-off

Conversation

@MaximKotliar

Copy link
Copy Markdown

What

Adds an opt-in battery-aware auto-off: when the Mac is running on battery and
charge drops below a chosen threshold, Awayke turns itself off; when charging back
up on AC it turns itself back on. This backs up the existing "keep the laptop on AC"
guidance with an actual safety net.

Behavior

  • New right-click submenu "Auto-off on low battery" with presets: Off / 10% / 20% / 30% (persisted in UserDefaults).
  • Only triggers on battery — on AC a low charge never turns Awayke off.
  • After an auto-off, plugging in and charging to threshold + 5% (hysteresis, no flapping) turns it back on.
  • Manually toggling Awayke always overrides and clears the battery suspension.
  • macOS notifications on both the auto-off and the auto-back-on (one-time permission prompt on first use).
  • While suspended, the menu shows "Awayke: Paused (low battery)" and the icon returns to its inactive state with an explanatory tooltip.

Design

State is split into intent (does the user want Awayke on?) and suspendedForBattery
(is auto-off holding it off?); the system sees intent && !suspendedForBattery.

  • AutoOffPolicy — pure decision type (suspend / resume / none), unit-tested.
  • BatteryMonitor — thin, event-driven IOKit power-source wrapper (IOPSNotificationCreateRunLoopSource, no polling).
  • AppDelegate — wires monitor → policy → the existing PowerManager, owns the submenu, posts notifications.

Testing

  • AutoOffPolicy covered by unit tests over the full transition table (boundary, hysteresis, on-AC never suspends, manual override clears suspension, threshold 0 disables).
  • App builds clean and launches without crashing (real IOKit path exercised).
  • Live suspend/resume against real battery drain: verified manually.

@MaximKotliar
MaximKotliar marked this pull request as ready for review July 23, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant