Skip to content

feat: native menu-bar hiding on macOS 27 (direct build) - #403

Merged
phucledien merged 5 commits into
refactor/menu-bar-enginefrom
feat/native-visibility-engine
Sep 18, 2026
Merged

phucledien merged 5 commits into
refactor/menu-bar-enginefrom
feat/native-visibility-engine

Conversation

@phucledien

Copy link
Copy Markdown
Collaborator

Stacked on #402 (review that first; this diff is against its branch). Part of #360, related #366.

Important

This uses private Apple API (MenuBarClientCore), compiled only into a new non-sandboxed direct-download build. The App Store build is unchanged and contains none of it.

Why

On macOS 27 an over-wide status item is ejected from the menu bar, so widening the separator hides nothing (#360). Measured on 27.0: the widest separator macOS 27 still lays out (~939pt on a 1920pt display) displaces icons but doesn't hide them, and the bar reorders itself between collapses. Width-based fixes (#382, #392, #396, #397) pick a fraction of the display width, sometimes with spacer items. They still depend on display width, the notch and the frontmost app's menus.

macOS 27 can hide menu-bar items itself. Its assessment (exam lockdown) mode restricts the bar to an allow-list of apps and reflows it. This PR drives that directly, the same mechanism Pelmet uses (behavior only; no code taken from it, it's GPL).

What's this PR do?

  • NativeVisibilityEngine (macOS 27+, direct build only). On collapse it reads which apps sit left of the arrow (Accessibility), then activates a restriction that keeps only Hidden Bar, the visible apps and macOS's own items. Expand drops the restriction, or keeps the always-hidden section hidden while "hide separators" is on. A new restriction is activated before the old one is dropped, so there's no flash.
  • The arrow is the boundary. The regular separator is taken out of the bar on 27; the always-hidden separator still marks the third section.
  • HBNativeVisibilityShim (Objective-C) is the only file that touches the private framework. It resolves MBAssessmentModeConfiguration / MBAssessmentModeAssertion at runtime, checks every selector and catches exceptions. It compiles only with HIDDENBAR_NATIVE_VISIBILITY.
  • Debug-Direct / Release-Direct configurations set that flag and sign with Hidden-Direct.entitlements (no App Sandbox).
  • MenuBarLayoutResolver is a pure function that maps item frames to visible / hidden / always-hidden. When one app has several icons, the most visible section wins.
  • AccessibilityMenuBarInventory lists other apps' status items. It queries only app bundles, with a 0.1s timeout per app, off the main thread. The first version blocked the main thread for ~18s on WebKit helpers.
  • Arrangement checks moved into the engines, since which item is the boundary depends on the engine.
  • Fail open: if the API is missing, Accessibility isn't granted or activation fails, the bar stays expanded and the controller shows it as expanded. The restriction is process-bound, so a crash or quit restores the bar.
  • Unhosted HiddenBarTests target with 17 tests. They cover the resolver (LTR/RTL, grouping, exclusions) and the engine's state transitions against fake inventory and visibility, including late activations after an expand, not re-reading while hidden, and failure paths.

Measured on macOS 27.0 before relying on it

  • Sandbox: activation works sandboxed and unsandboxed. But the App Sandbox denies mach-lookup com.apple.axserver even with Accessibility granted, so a sandboxed app can't read other apps' items. That's why this is direct-build only.
  • API shape: the configuration takes NSArrays (an NSSet raises). System items are numeric IDs (2 clock, 5 sound, 6 Wi-Fi, 8 Control Center); unknown IDs up to 63 are ignored.
  • Stale positions: while a restriction is active, hidden items report stale frames, so sections are read only from an unrestricted bar.
  • Own icons: the allow-list matches an app's own items only for the copy LaunchServices resolves, i.e. the one in /Applications. A copy run from elsewhere hides its own arrow.

Limits

  • Hiding is per app: an app's icons move together.
  • macOS's own items (clock, Wi-Fi, Sound, Control Center) always stay visible.
  • An app launched while collapsed stays hidden until the next collapse re-reads the sections.
  • App Store build on macOS 27 is still broken. Tracked in docs/BACKLOG.md.

Open decisions

  • Bundle ID for the direct build. I tested as com.dwarvesv.minimalbar.direct; the project still uses com.dwarvesv.minimalbar.
  • Tutorial text. The in-app tutorial still describes the | separator, which the direct build on 27 no longer uses.
  • App Store stance. Using private API in an App Store build would likely be rejected under guideline 2.5.1, so it's kept out of it.

Validation

  • xcodebuild ... -configuration Debug test: 17 tests pass
  • Debug and Debug-Direct build; the App Store (Debug) binaries contain no MenuBarClientCore / MBAssessmentMode strings
  • macOS 27.0, 1920pt display, Direct build from /Applications with Accessibility:
    • collapse hides the hidden-section apps
    • visible apps, system items and the arrow stay
    • expand restores them; repeated toggles work
    • ⌘-dragging icons across the arrow changes what hides
    • no leftover separator gap
  • Not tested: notched display, multiple displays / hot-plug, RTL, always-hidden section on 27, sleep/wake, notarized Release-Direct

🤖 Generated with Claude Code

phucledien and others added 5 commits September 18, 2026 15:00
macOS 27 ejects an inflated separator from the menu bar, so length-based
hiding stops working there (#360). Add NativeVisibilityEngine: it reads which
apps the user placed in each section (Accessibility, relative to the
separators) and asks macOS to keep only the allowed apps visible through the
menu-bar visibility restriction behind assessment mode. macOS hides the rest
and reflows the bar, independent of display width, the notch and the
frontmost app's menus.

That restriction lives in MenuBarClientCore, a private framework. All use of
it sits in HBNativeVisibilityShim (Objective-C, runtime-resolved, exceptions
caught) and compiles only with HIDDENBAR_NATIVE_VISIBILITY, which the new
Debug-Direct / Release-Direct configurations set. Those configurations sign
with Hidden-Direct.entitlements (no App Sandbox): on macOS 27 the sandbox
denies the mach-lookup Accessibility needs (com.apple.axserver) even with
the permission granted, so the App Store build cannot read the sections and
keeps LegacyLengthEngine.

Measured on 27.0 before building this:
- activation works sandboxed and unsandboxed; invalidate, or process exit,
  restores the bar
- the configuration takes NSArrays; an NSSet raises
- system items are numeric ids (2 clock, 5 sound, 6 wifi, 8 control center
  on the test Mac); unknown ids up to 63 are ignored
- while a restriction is active, hidden items report stale positions, so
  sections are read only from an unrestricted bar

Behavior: collapse allows Hidden Bar plus the visible section; expand drops
the restriction, or keeps the always-hidden section hidden while "hide
separators" is on. A new restriction is activated before the old one is
dropped. Every failure (API missing, no Accessibility, activation error)
leaves the bar expanded and reports .unavailable; the controller shows it
as expanded. Separators stay at their normal width.

Limits: hiding is per app (an app's icons move together, most visible wins);
macOS's own items stay visible; an app launched while collapsed stays hidden
until the next collapse re-reads the sections.

Adds an unhosted HiddenBarTests target: layout resolver (LTR/RTL, grouping,
exclusions) and engine transitions against fake inventory and visibility.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Found testing the direct build on 27.0:
- The Accessibility walk blocked the collapse for up to ~18s: every running
  process was queried with the default timeout, and dozens of WebKit/XPC
  helpers never answer. Query only app bundles (the only status-item
  owners seen), with a 0.1s timeout, off the main thread (~1s total).
- The separators sat idle beside the arrow while collapsed. They drop to
  zero width once hiding succeeds and return on expand (zero width rather
  than isVisible, which would forget their position).
- Log the resolved sections, and every distinct reason hiding is
  unavailable, instead of only the first.

Also measured: the allow-list matches an app's own items only for the copy
LaunchServices resolves for its bundle id, which for an installed app is the
one in /Applications. Other copies of Hidden Bar (build products, archives)
have their items hidden.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
With native hiding the separator no longer has to widen, so it only
duplicated the arrow. The native engine now reads sections relative to the
arrow (apps on its left are hidden) and keeps the regular separator at zero
width, not removed, so macOS keeps its saved position for the legacy engine.
The always-hidden separator still marks the third section while expanded.

The "is the arrangement usable" checks move from the controller into the
engines, since they depend on which item is the boundary: the legacy engine
keeps the separator-left-of-arrow rule, the native engine accepts any
arrangement and checks the always-hidden separator against the arrow.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Zero width still left a gap beside the arrow, since macOS pads every
status item. The native engine hides the regular separator outright, and
the controller creates the engine at launch so it never flashes on screen.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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