Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions HiddenBarTests/ExpandCollapseActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
32 changes: 32 additions & 0 deletions hidden/Features/StatusBar/ExpandCollapseAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
45 changes: 43 additions & 2 deletions hidden/Features/StatusBar/StatusBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class StatusBarController: MenuBarItemProvider {

deinit {
timer?.invalidate()
pendingDeactivation?.invalidate()
NotificationCenter.default.removeObserver(self)
pinnedSectionsObserver.map(NotificationCenter.default.removeObserver)
hoverDwellTimer?.invalidate()
Expand Down Expand Up @@ -491,6 +492,7 @@ class StatusBarController: MenuBarItemProvider {
}

private func didFailToCollapseMenuBar() {
cancelPendingDeactivation()
if let button = btnExpandCollapse.button {
button.image = Assets.collapseImage
}
Expand All @@ -517,8 +519,7 @@ class StatusBarController: MenuBarItemProvider {
button.image = collapsedArrowImage
}
if wantsFullBar {
NSApp.setActivationPolicy(.accessory)
NSApp.deactivate()
deactivateAfterCollapse()
}
fullBarThisExpansion = false
revealController.barDidCollapse()
Expand All @@ -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 : []
Expand Down Expand Up @@ -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 }
Expand Down