Skip to content

Add automatic lid control with keyboard dimming and lock-on-open - #16

Open
ziyad-aljohani wants to merge 6 commits into
domzilla:masterfrom
ziyad-aljohani:feature/protected-lid-session
Open

Add automatic lid control with keyboard dimming and lock-on-open#16
ziyad-aljohani wants to merge 6 commits into
domzilla:masterfrom
ziyad-aljohani:feature/protected-lid-session

Conversation

@ziyad-aljohani

@ziyad-aljohani ziyad-aljohani commented Sep 6, 2026

Copy link
Copy Markdown

Normal idle-sleep prevention does not keep a Mac awake when its lid closes. This adds an Automatic lid control checkbox that follows Caffeine's existing activation toggle:

  • Closing the lid sets the built-in display and keyboard brightness to zero while background work continues without requesting a lock.
  • Opening the lid requests the native macOS lock. A black privacy cover stays in place and brightness remains zero until the session reports locked continuously for 750 ms, then the saved brightness levels return.
  • Caffeine remains active after authentication for the next lid cycle. Unchecking the option keeps ordinary Caffeine available.
  • At 10% battery or less while unplugged, Caffeine pauses quietly and resumes when external power is detected. It shows a specific battery reason in the menu/tooltip and manually opened preferences. Overheating has its own message and resumes after cooling. Neither pause opens preferences; manual deactivation and the original timer deadline cancel automatic resume. A pause during a protected lid cycle retains the cover and lock-on-open handling.

A limited privileged helper requests administrator approval on first installation and manages the system sleep override through a renewable lease. It restores ordinary sleep after deactivation, lease expiration, or client failure, and recovers its recorded override after a helper restart. Rapid off/on cycles wait for the previous session to finish cleanup. The version-2 helper fixes intermittent deactivation caused by reading the clock before a concurrently renewed lease timestamp, and publishes status atomically. Lease renewal runs on a dedicated serial queue with App Nap disabled during the session, so a delayed UI loop does not expire the lease. Existing version-1 installations need one administrator approval for the helper upgrade; later toggles do not prompt. The change also replaces the intermittent idle-sleep assertion with a continuous assertion and adds Arabic and English strings, build/uninstall scripts, and implementation documentation.

Distribution and compatibility

This implementation is a direct-distribution fork: it disables App Sandbox and upstream Sparkle updates, and its build script uses a separate bundle identifier. The Xcode project is unchanged. It uses private Apple brightness and lock interfaces, targets macOS 14.6+, and the supplied build script produces an ad-hoc-signed universal app, not a notarized release. These distribution choices will need maintainer review before inclusion in upstream releases.

The helper authorizes the installing user rather than a specific app signature, supports one installing account, and uses a global power setting. Existing external sleep overrides are treated as conflicts. The black cover and lock confirmation reduce the reopening exposure, but hardware and OS timing still require physical validation; this is not a universal zero-frame privacy guarantee. See docs/PROTECTED-LID.md for recovery, removal, and limitations.

Validation

  • Universal Release build for Apple Silicon and Intel succeeded; ad-hoc signature verification passed.
  • All 19 watchdog/client tests passed (18 together, followed by the added future-timestamp rejection test). Coverage includes a reproduced clock/renewal race, blocking the Swift main actor for 13 seconds while the lease stays active, rapid off/on cycles, cancelled startup, expired leases, and client/helper crashes. Power operations are mocked in these tests.
  • Lid transition and lock-reveal timing tests passed, including unknown lock state, interrupted confirmation, and reclosure.
  • The real view model passed power-pause tests with inert hardware adapters: quiet low-battery activation and threshold crossing, charger resume below 10%, unknown/fluctuating battery readings, separate thermal reasons, wake handling, privacy-retaining suspension, manual cancellation, expired timers, and queued-start races. No real battery drain or overheating was induced.
  • SwiftFormat lint, localization property-list validation, and whitespace checks passed.
  • Prior manual testing on one Mac confirmed the lid behavior and reopening privacy fix. The heartbeat and power-pause corrections passed their regression tests and universal builds; extended physical use of this revision and other hardware/OS versions remain unverified.

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