diff --git a/HiddenBarTests/ExpandCollapseActionTests.swift b/HiddenBarTests/ExpandCollapseActionTests.swift index 02b8c75a..b56b7c3a 100644 --- a/HiddenBarTests/ExpandCollapseActionTests.swift +++ b/HiddenBarTests/ExpandCollapseActionTests.swift @@ -70,3 +70,41 @@ final class ExpandCollapseActionTests: XCTestCase { XCTAssertEqual(ExpandCollapseActionResolver.action(eventType: .otherMouseUp, optionPressed: false), .toggleSeparators) } } + +final class FullBarActivationTests: XCTestCase { + + private func run(isMacOS14OrLater: Bool, stillWanted: Bool) -> [String] { + var log: [String] = [] + var pending: [() -> Void] = [] + FullBarActivation.switchToRegularAndActivate( + isMacOS14OrLater: isMacOS14OrLater, + setPolicy: { log.append("policy:\($0 == .regular ? "regular" : "other")") }, + deferToNextTurn: { pending.append($0) }, + stillWanted: { stillWanted }, + activate: { log.append("activate:\($0)") } + ) + log.append("turn-boundary") + pending.forEach { $0() } + return log + } + + func testPolicyChangesBeforeTheTurnAndActivationComesAfter() { + XCTAssertEqual(run(isMacOS14OrLater: true, stillWanted: true), + ["policy:regular", "turn-boundary", "activate:cooperative"]) + } + + func testMacOS13KeepsTheLegacyCall() { + XCTAssertEqual(run(isMacOS14OrLater: false, stillWanted: true), + ["policy:regular", "turn-boundary", "activate:legacy"]) + } + + func testCollapsedBeforeTheNextTurnSkipsActivation() { + XCTAssertEqual(run(isMacOS14OrLater: true, stillWanted: false), + ["policy:regular", "turn-boundary"]) + } + + func testRequestKindPerOSVersion() { + XCTAssertEqual(FullBarActivation.request(isMacOS14OrLater: true), .cooperative) + XCTAssertEqual(FullBarActivation.request(isMacOS14OrLater: false), .legacy) + } +} diff --git a/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-0.3s-left.png b/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-0.3s-left.png new file mode 100644 index 00000000..0a00e11f Binary files /dev/null and b/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-0.3s-left.png differ diff --git a/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-1s-left.png b/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-1s-left.png new file mode 100644 index 00000000..0a00e11f Binary files /dev/null and b/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-1s-left.png differ diff --git a/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-1s-right.png b/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-1s-right.png new file mode 100644 index 00000000..71535506 Binary files /dev/null and b/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-1s-right.png differ diff --git a/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-2s-left.png b/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-2s-left.png new file mode 100644 index 00000000..0a00e11f Binary files /dev/null and b/docs/implementation-notes/shift-click-full-bar-proof/fix-plain-2s-left.png differ diff --git a/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-0.3s-left.png b/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-0.3s-left.png new file mode 100644 index 00000000..0a00e11f Binary files /dev/null and b/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-0.3s-left.png differ diff --git a/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-1s-left.png b/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-1s-left.png new file mode 100644 index 00000000..0a00e11f Binary files /dev/null and b/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-1s-left.png differ diff --git a/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-1s-right.png b/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-1s-right.png new file mode 100644 index 00000000..71535506 Binary files /dev/null and b/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-1s-right.png differ diff --git a/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-2s-left.png b/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-2s-left.png new file mode 100644 index 00000000..0a00e11f Binary files /dev/null and b/docs/implementation-notes/shift-click-full-bar-proof/fix-shift-2s-left.png differ diff --git a/docs/implementation-notes/shift-click-full-bar.md b/docs/implementation-notes/shift-click-full-bar.md index 9125e585..4dcfe2b4 100644 --- a/docs/implementation-notes/shift-click-full-bar.md +++ b/docs/implementation-notes/shift-click-full-bar.md @@ -23,6 +23,17 @@ dump-state `useFullStatusBarOnExpand` read false at every step. - The same path serves the stored "use the full menu bar on expand" setting, so that setting likely shows the same on macOS 27; not run here. - How step f was told apart from noise: other Debug Hidden Bar builds (test hosts of a parallel session) also answer the broadcast hook and wrote the same dump file, which gave two false triggers. Only this build's dump has `fullBarThisExpansion`, so the trigger was that key being true; the capture retried the dump until that key was present. +## Fix attempt (#496): activation on the next runloop turn + +| Click (real, by Han) | Trigger | Frontmost app, sampled +0.12 s to +5.5 s | End state (dump-state / NSRunningApplication) | Result | Crops | +|---|---|---|---|---|---| +| Shift-click, setting off (01:49:40) | `fullBarThisExpansion` true | Orca (pid 17272) at all 30 samples | expanded, regular / regular, `isActive` false | FAIL: Hidden Bar never frontmost, app menu not shown | `fix-shift-0.3s-left.png`, `fix-shift-1s-left.png`, `fix-shift-2s-left.png`, `fix-shift-1s-right.png` | +| Plain click, `full-menu-bar on` (01:50:39) | state expanded | Orca (pid 17272) at all 30 samples | expanded, regular / regular, `isActive` false | FAIL: same; the stored setting fails the same way | `fix-plain-0.3s-left.png`, `fix-plain-1s-left.png`, `fix-plain-2s-left.png`, `fix-plain-1s-right.png` | + +- Build: a local merge of `origin/develop` (`f8f2726`) and `fix/full-bar-activation` (`027be9b`), Debug-Direct, Developer ID signed, from /Applications (pid 25611); the branch had no Debug hooks of its own. The installed v1.12.0 was moved aside and back: CDHash identical, login items identical, prefs identical after a re-import (the `full-menu-bar` hook writes the stored setting). +- Trigger trusted only this pid (frontmost) or a state the poll read from this build; no other Hidden Bar process ran at either trigger. The frontmost app was sampled with `lsappinfo front` in a loop meant for every 0.1 s; real spacing was about 0.17 s, 30 samples over 5.5 s. The left crops at +0.3, +1 and +2 s are byte-identical per click (Orca's menus). +- So deferring `NSApp.activate()` one runloop turn after `setActivationPolicy(.regular)` does not make Hidden Bar active on macOS 27 from a status item click, Shift or plain. + ## Not proven here - The app menu in the menu bar after a Shift-click: not seen on macOS 27 (finding above). Shift detection itself is proven on screen by step f (`fullBarThisExpansion` true after a real click). diff --git a/hidden/Features/StatusBar/ExpandCollapseAction.swift b/hidden/Features/StatusBar/ExpandCollapseAction.swift index a4fe086e..659ce8af 100644 --- a/hidden/Features/StatusBar/ExpandCollapseAction.swift +++ b/hidden/Features/StatusBar/ExpandCollapseAction.swift @@ -42,3 +42,45 @@ enum ExpandCollapseActionResolver { } } } + +// Switching to .regular and activating in the same runloop turn does not make the +// app active on macOS 14+, where activation is a cooperative request and +// activate(ignoringOtherApps:) is deprecated and ignored. The full menu bar needs +// Hidden Bar frontmost, so the request goes out on the next turn, after the policy +// change has registered, and is dropped if the bar collapsed in the meantime. +enum FullBarActivation { + enum Request: Equatable { + case cooperative + case legacy + } + + static func request(isMacOS14OrLater: Bool) -> Request { + return isMacOS14OrLater ? .cooperative : .legacy + } + + static func switchToRegularAndActivate( + isMacOS14OrLater: Bool = ProcessInfo.processInfo.isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 14, minorVersion: 0, patchVersion: 0)), + setPolicy: (NSApplication.ActivationPolicy) -> Void = { _ = NSApp.setActivationPolicy($0) }, + deferToNextTurn: (@escaping () -> Void) -> Void = { DispatchQueue.main.async(execute: $0) }, + stillWanted: @escaping () -> Bool, + activate: @escaping (Request) -> Void = FullBarActivation.perform + ) { + setPolicy(.regular) + let request = request(isMacOS14OrLater: isMacOS14OrLater) + deferToNextTurn { + guard stillWanted() else { return } + activate(request) + } + } + + static func perform(_ request: Request) { + switch request { + case .cooperative: + if #available(macOS 14.0, *) { + NSApp.activate() + } + case .legacy: + NSApp.activate(ignoringOtherApps: true) + } + } +} diff --git a/hidden/Features/StatusBar/StatusBarController.swift b/hidden/Features/StatusBar/StatusBarController.swift index 0fc901d1..ad0c6388 100644 --- a/hidden/Features/StatusBar/StatusBarController.swift +++ b/hidden/Features/StatusBar/StatusBarController.swift @@ -395,9 +395,10 @@ class StatusBarController: MenuBarItemProvider { autoCollapseIfNeeded() if activatingApp && wantsFullBar { - NSApp.setActivationPolicy(.regular) - NSApp.activate(ignoringOtherApps: true) - + FullBarActivation.switchToRegularAndActivate(stillWanted: { [weak self] in + guard let self else { return false } + return !self.isCollapsed && self.wantsFullBar + }) } }