From b7f4c43277977f606d028dddc8c98e379c2a1d90 Mon Sep 17 00:00:00 2001 From: DragonNameLeo <15801369+DragonNameLeo@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:28:46 +0900 Subject: [PATCH] fix(fullbar): wait for the pointer to leave a self-hiding menu bar before deactivating With "Make room for icons while they are shown" on and the macOS menu bar set to hide by itself, a click on the arrow to collapse handed focus back to the previous app while the pointer was still in the bar. macOS then started hiding the menu bar and brought it straight back down. The collapse now keeps Hidden Bar active until the pointer leaves the bar (at most 10 s), where the bar hides anyway. A bar that stays on screen deactivates at once, as before. An expand, a failed collapse or a toggle of the option cancels the wait. Co-Authored-By: Claude Opus 5.5 --- .../ExpandCollapseActionTests.swift | 56 +++++++++++++++++++ .../StatusBar/ExpandCollapseAction.swift | 32 +++++++++++ .../StatusBar/StatusBarController.swift | 45 ++++++++++++++- 3 files changed, 131 insertions(+), 2 deletions(-) diff --git a/HiddenBarTests/ExpandCollapseActionTests.swift b/HiddenBarTests/ExpandCollapseActionTests.swift index 516fa315..ef53bc7c 100644 --- a/HiddenBarTests/ExpandCollapseActionTests.swift +++ b/HiddenBarTests/ExpandCollapseActionTests.swift @@ -168,3 +168,59 @@ final class FullBarActivationTests: XCTestCase { XCTAssertEqual(app.calls, [FullBarActivation.usesCooperativeActivation ? "fromFrontApp" : "ignoringOtherApps"]) } } + +final class FullBarDeactivationTests: XCTestCase { + + private typealias Screen = FullBarActivation.Screen + + // A 1920x1200 main screen and a 4K screen to its right, set higher. + private let mainFrame = CGRect(x: 0, y: 0, width: 1920, height: 1200) + private let sideFrame = CGRect(x: 1920, y: 430, width: 3840, height: 2160) + + private func selfHiding(_ frame: CGRect) -> Screen { + Screen(frame: frame, visibleFrame: frame) + } + + private func barReserved(_ frame: CGRect, height: CGFloat = 24) -> Screen { + var visible = frame + visible.size.height -= height + return Screen(frame: frame, visibleFrame: visible) + } + + private func waits(_ pointer: CGPoint, _ screens: [Screen], barHeight: CGFloat = 24) -> Bool { + FullBarActivation.waitsBeforeDeactivating(pointer: pointer, screens: screens, barHeight: barHeight) + } + + func testWaitsWhileThePointerIsInAMenuBarThatHidesItself() { + XCTAssertTrue(waits(CGPoint(x: 1300, y: 1190), [selfHiding(mainFrame)])) + } + + func testWaitsAtTheVeryTopRow() { + XCTAssertTrue(waits(CGPoint(x: 1300, y: 1200), [selfHiding(mainFrame)])) + } + + func testDoesNotWaitOnceThePointerLeftTheBar() { + XCTAssertFalse(waits(CGPoint(x: 1300, y: 1100), [selfHiding(mainFrame)])) + } + + func testDoesNotWaitWhenTheMenuBarStaysOnScreen() { + XCTAssertFalse(waits(CGPoint(x: 1300, y: 1190), [barReserved(mainFrame)])) + } + + func testUsesTheScreenThePointerIsOn() { + let screens = [barReserved(mainFrame), selfHiding(sideFrame)] + XCTAssertTrue(waits(CGPoint(x: 3000, y: sideFrame.maxY - 5), screens)) + // The main screen's bar height on the side screen is not its menu bar. + XCTAssertFalse(waits(CGPoint(x: 3000, y: mainFrame.maxY - 5), screens)) + } + + func testATallerNotchMenuBarCountsAsTheBar() { + XCTAssertTrue(waits(CGPoint(x: 1300, y: 1200 - 30), [selfHiding(mainFrame)], barHeight: 37)) + XCTAssertFalse(waits(CGPoint(x: 1300, y: 1200 - 30), [selfHiding(mainFrame)], barHeight: 24)) + } + + func testDoesNotWaitWithNoScreenOrNoBarHeight() { + XCTAssertFalse(waits(CGPoint(x: 1300, y: 1190), [])) + XCTAssertFalse(waits(CGPoint(x: 1300, y: 1190), [selfHiding(mainFrame)], barHeight: 0)) + } +} diff --git a/hidden/Features/StatusBar/ExpandCollapseAction.swift b/hidden/Features/StatusBar/ExpandCollapseAction.swift index dbdac173..8c61d1cd 100644 --- a/hidden/Features/StatusBar/ExpandCollapseAction.swift +++ b/hidden/Features/StatusBar/ExpandCollapseAction.swift @@ -119,4 +119,36 @@ enum FullBarActivation { app.activateIgnoringOtherApps() } } + + // A screen as the deactivation check needs it (AppKit coordinates). + struct Screen: Equatable { + var frame: CGRect + var visibleFrame: CGRect + } + + // How long a collapse may keep Hidden Bar active while the pointer stays in + // the menu bar. Past this the app hands focus back anyway. + static let deactivationWaitLimit: TimeInterval = 10 + + // With the macOS menu bar set to hide by itself, handing focus back to the + // previous app while the pointer is still in the bar makes macOS start + // hiding the bar and bring it straight back down. A collapse therefore waits + // until the pointer leaves the bar, where the bar hides anyway. + // + // A bar that hides by itself reserves no space: the screen's visible frame + // reaches its top. The bar's strip is then the top `barHeight` of the screen. + // A bar that stays on screen never waits: its menus come back at once, as before. + static func waitsBeforeDeactivating(pointer: CGPoint, screens: [Screen], barHeight: CGFloat) -> Bool { + guard barHeight > 0, + let screen = screens.first(where: { $0.frame.contains(pointer) || Self.isOnTopEdge(pointer, of: $0.frame) }) + else { return false } + let hidesItself = screen.visibleFrame.maxY >= screen.frame.maxY - 0.5 + return hidesItself && pointer.y >= screen.frame.maxY - barHeight + } + + // NSEvent.mouseLocation reads y == frame.maxY at the very top row, which + // CGRect.contains leaves out. + private static func isOnTopEdge(_ point: CGPoint, of frame: CGRect) -> Bool { + return point.y == frame.maxY && point.x >= frame.minX && point.x < frame.maxX + } } diff --git a/hidden/Features/StatusBar/StatusBarController.swift b/hidden/Features/StatusBar/StatusBarController.swift index 4c10b4f6..6ba061a0 100644 --- a/hidden/Features/StatusBar/StatusBarController.swift +++ b/hidden/Features/StatusBar/StatusBarController.swift @@ -188,6 +188,7 @@ class StatusBarController: MenuBarItemProvider { deinit { timer?.invalidate() + pendingDeactivation?.invalidate() NotificationCenter.default.removeObserver(self) pinnedSectionsObserver.map(NotificationCenter.default.removeObserver) hoverDwellTimer?.invalidate() @@ -491,6 +492,7 @@ class StatusBarController: MenuBarItemProvider { } private func didFailToCollapseMenuBar() { + cancelPendingDeactivation() if let button = btnExpandCollapse.button { button.image = Assets.collapseImage } @@ -517,8 +519,7 @@ class StatusBarController: MenuBarItemProvider { button.image = collapsedArrowImage } if wantsFullBar { - NSApp.setActivationPolicy(.accessory) - NSApp.deactivate() + deactivateAfterCollapse() } fullBarThisExpansion = false revealController.barDidCollapse() @@ -530,8 +531,47 @@ class StatusBarController: MenuBarItemProvider { #endif } + // Set while a collapse waits for the pointer to leave a menu bar that hides + // by itself before handing focus back (FullBarActivation.waitsBeforeDeactivating). + private var pendingDeactivation: Timer? + + private func cancelPendingDeactivation() { + pendingDeactivation?.invalidate() + pendingDeactivation = nil + } + + private func deactivateAfterCollapse() { + cancelPendingDeactivation() + guard NSApp.isActive, pointerIsInSelfHidingMenuBar else { return deactivateNow() } + let deadline = Date().addingTimeInterval(FullBarActivation.deactivationWaitLimit) + NSLog("FullBar: the pointer is in a menu bar that hides by itself; handing focus back once it leaves") + pendingDeactivation = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [weak self] _ in + guard let self = self else { return } + // Another app took focus meanwhile: nothing would switch on screen now. + guard NSApp.isActive, self.pointerIsInSelfHidingMenuBar, Date() < deadline else { + self.cancelPendingDeactivation() + // An expand meanwhile keeps the full bar; it cancels this timer, but check anyway. + if self.isCollapsed { self.deactivateNow() } + return + } + } + } + + private func deactivateNow() { + NSApp.setActivationPolicy(.accessory) + NSApp.deactivate() + } + + private var pointerIsInSelfHidingMenuBar: Bool { + let screens = NSScreen.screens.map { FullBarActivation.Screen(frame: $0.frame, visibleFrame: $0.visibleFrame) } + // The arrow's window is as tall as the menu bar, also under a notch. + let barHeight = max(btnExpandCollapse.button?.window?.frame.height ?? 0, NSStatusBar.system.thickness) + return FullBarActivation.waitsBeforeDeactivating(pointer: NSEvent.mouseLocation, screens: screens, barHeight: barHeight) + } + private func expandMenubar(activatingApp: Bool = true, userInitiated: Bool = false) { guard self.isCollapsed else {return} + cancelPendingDeactivation() #if HIDDENBAR_NATIVE_VISIBILITY // Read before the engine expands: it is what the bar hid until now. let hiddenBundles = userInitiated ? menuBarEngine.hiddenSectionBundles : [] @@ -560,6 +600,7 @@ class StatusBarController: MenuBarItemProvider { // Expand/collapse skips the policy change when the option is off, so sync it on toggle. @objc private func syncActivationPolicyWithFullStatusBar() { + cancelPendingDeactivation() let wantsRegular = wantsFullBar && !isCollapsed let policy: NSApplication.ActivationPolicy = wantsRegular ? .regular : .accessory guard NSApp.activationPolicy() != policy else { return }