diff --git a/CHANGELOG.md b/CHANGELOG.md index a8be951c..14e7958f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +### Added +- Direct build on macOS 27: when you expand the bar and some of the revealed icons do not fit (on a notched MacBook, right of the notch), macOS puts them into its own « button. Hidden Bar now says so once, after an expand by click or hotkey: "Some icons do not fit in the menu bar, so macOS moved them into its own « button. Click « to see them. To make room, move icons you rarely use to the always-hidden section." It never shows again, and the collapse notice from before stays separate. + ### Changed - Settings › Advanced: the switch "Show all icons while macOS hides one right of the arrow" is now "Pause hiding while macOS shows Now Playing, Focus and similar items", with a note that Hidden Bar hides nothing while one of them shows. Same stored setting, same behavior ([#446](https://github.com/dwarvesf/hidden/issues/446)). diff --git a/HiddenTests/SystemOverflowTests.swift b/HiddenTests/SystemOverflowTests.swift index f32ef6fd..8e5163cc 100644 --- a/HiddenTests/SystemOverflowTests.swift +++ b/HiddenTests/SystemOverflowTests.swift @@ -191,6 +191,111 @@ final class SystemOverflowTests: XCTestCase { XCTAssertFalse(policy.shouldShow()) XCTAssertFalse(SystemOverflowNoticePolicy(defaults: defaults).shouldShow()) } + + // MARK: expand notice: the decision + + func test_expandNoticeShowsOnlyWhenExpandedAndItemInOverflowAndNotShown() { + for isExpanded in [true, false] { + for inOverflow in [true, false] { + for alreadyShown in [true, false] { + let show = SystemOverflowNoticePolicy.shouldShowAfterExpand( + isExpanded: isExpanded, hiddenItemInOverflow: inOverflow, alreadyShown: alreadyShown) + XCTAssertEqual(show, isExpanded && inOverflow && !alreadyShown, + "expanded \(isExpanded), in overflow \(inOverflow), shown \(alreadyShown)") + } + } + } + } + + func test_expandNoticeFlagIsSeparateFromTheCollapseNotice() { + let suite = "SystemOverflowTests.\(UUID().uuidString)" + let defaults = UserDefaults(suiteName: suite)! + defer { defaults.removePersistentDomain(forName: suite) } + let policy = SystemOverflowNoticePolicy(defaults: defaults) + policy.recordShown() + XCTAssertTrue(policy.shouldShowAfterExpand()) + policy.recordExpandShown() + XCTAssertFalse(policy.shouldShowAfterExpand()) + XCTAssertFalse(SystemOverflowNoticePolicy(defaults: defaults).shouldShowAfterExpand()) + + let other = UserDefaults(suiteName: suite + ".other")! + defer { other.removePersistentDomain(forName: suite + ".other") } + let fresh = SystemOverflowNoticePolicy(defaults: other) + fresh.recordExpandShown() + XCTAssertTrue(fresh.shouldShow(), "the expand notice does not silence the collapse notice") + } + + // MARK: expand notice: which hidden apps are in « + + private func item(_ bundle: String, _ axFrame: CGRect, unreadable: Bool = false) -> MenuBarInventoryItem { + MenuBarInventoryItem(bundleIdentifier: bundle, frame: axFrame, isUnreadable: unreadable) + } + + private func overflowed(hidden: Set, running: Set? = nil, + inventory: [MenuBarInventoryItem], indicators: [SystemOverflowButton]) -> Set { + SystemOverflowExpandMatcher.bundlesInOverflow( + hidden: hidden, running: running ?? hidden, inventory: inventory, indicators: indicators, + displays: MenuBarDisplay.displays(screenFrames: [main, portrait]), primaryScreenMaxY: primaryMaxY) + } + + private let onBar = CGRect(x: 900, y: 0, width: 24, height: 24) + + func test_hiddenAppMissingFromAccessibilityWhileButtonExistsIsInOverflow() { + let result = overflowed(hidden: ["a.app", "b.app"], inventory: [item("a.app", onBar)], indicators: [mainButton]) + XCTAssertEqual(result, ["b.app"]) + } + + func test_nothingIsInOverflowWithoutAButton() { + XCTAssertEqual(overflowed(hidden: ["a.app"], inventory: [], indicators: []), []) + } + + func test_everyHiddenAppOnTheBarMeansNoOverflow() { + let result = overflowed(hidden: ["a.app", "b.app"], + inventory: [item("a.app", onBar), item("b.app", onBar.offsetBy(dx: 30, dy: 0))], + indicators: [mainButton]) + XCTAssertEqual(result, []) + } + + func test_hiddenAppListedUnderTheButtonIsInOverflow() { + let result = overflowed(hidden: ["a.app"], inventory: [item("a.app", mainAXFrame)], indicators: [mainButton]) + XCTAssertEqual(result, ["a.app"]) + } + + func test_hiddenAppListedOnNoDisplayIsInOverflow() { + let off = CGRect(x: 0, y: 5000, width: 24, height: 24) + let result = overflowed(hidden: ["a.app"], inventory: [item("a.app", off)], indicators: [mainButton]) + XCTAssertEqual(result, ["a.app"]) + } + + func test_oneOverflowedItemOfSeveralCounts() { + let result = overflowed(hidden: ["a.app"], + inventory: [item("a.app", onBar), item("a.app", mainAXFrame)], + indicators: [mainButton]) + XCTAssertEqual(result, ["a.app"]) + } + + func test_appThatDidNotAnswerAccessibilityIsNotJudged() { + let result = overflowed(hidden: ["a.app"], inventory: [item("a.app", .zero, unreadable: true)], indicators: [mainButton]) + XCTAssertEqual(result, []) + } + + func test_appThatIsNotRunningIsNotCounted() { + let result = overflowed(hidden: ["a.app", "b.app"], running: ["a.app"], + inventory: [item("a.app", onBar)], indicators: [mainButton]) + XCTAssertEqual(result, []) + } + + func test_systemExtrasAreNotCounted() { + let result = overflowed(hidden: ["com.apple.controlcenter", "com.apple.MenuBarAgent"], + inventory: [], indicators: [mainButton]) + XCTAssertEqual(result, []) + } + + func test_buttonWithoutFrameStillCountsAnAppMissingFromTheList() { + let unreadable = SystemOverflowButton(state: .closed, frame: nil) + XCTAssertEqual(overflowed(hidden: ["a.app"], inventory: [], indicators: [unreadable]), ["a.app"]) + XCTAssertEqual(overflowed(hidden: ["a.app"], inventory: [item("a.app", mainAXFrame)], indicators: [unreadable]), []) + } } #endif diff --git a/docs/MANUAL.md b/docs/MANUAL.md index dd53f941..6f70d709 100644 --- a/docs/MANUAL.md +++ b/docs/MANUAL.md @@ -167,6 +167,7 @@ To undo any of them: `defaults delete com.dwarvesv.minimalbar `. | Login item missing after denying it once | System Settings > General > Login Items: re-enable Hidden Bar, then toggle the pref off/on | | The old "LauncherApplication" login item from a version before 1.11 (System Settings > General > Login Items & Extensions lists it inside "Hidden Bar" under Allow in the Background; a "Hidden Bar" row there is normal while launch at login is on) | With "Open Hidden Bar at login" on, launch the current version once; it removes the old item. Otherwise turn "Open Hidden Bar at login" off and on in Settings | | App language stuck | See the `AppleLanguages` command above, or System Settings > General > Language & Region > Applications | +| A second arrow « appears when I expand (macOS 27 direct build) | The icons Hidden Bar reveals do not all fit in the menu bar (on a notched MacBook, right of the notch), so macOS puts some into its own « button. macOS offers no setting to turn this off. Click « to see them, and move icons you rarely use to the always-hidden section (Settings > Icons) to make room. Hidden Bar says this once, the first time an expand overflows | | Nothing hides on a macOS 27 beta | Known (issue #360); the menu bar re-architecture broke the hiding mechanism, fix under investigation | | A new or just-updated app's icon shows up already hidden | macOS 26 and earlier, or the App Store build: expected, see "Why new icons start hidden" below; ⌘-drag it to the right of the separator once. macOS 27 direct build: new and relaunched apps start visible (Pinned sections, beta) | | An app jumps into the hidden section by itself (BitBar, SwiftBar) | macOS 27 direct build: in Settings > Icons, tick "Ignore drags" on that app; it then keeps the section you picked | diff --git a/hidden/Features/StatusBar/Engine/MenuBarEngine.swift b/hidden/Features/StatusBar/Engine/MenuBarEngine.swift index 51639c04..1ff1395a 100644 --- a/hidden/Features/StatusBar/Engine/MenuBarEngine.swift +++ b/hidden/Features/StatusBar/Engine/MenuBarEngine.swift @@ -43,6 +43,10 @@ protocol MenuBarEngine: AnyObject { // can tell a menu-bar app's popup window. Empty when the engine reads none. var menuBarItemOwners: Set { get } + // Bundles the last read put in the hidden section: what an expand brings back + // onto the bar. Empty when the engine reads none. + var hiddenSectionBundles: Set { get } + // The user expanded or collapsed the bar, so a clock click no longer // collapses it when Notification Center closes. func userChangedBar() @@ -69,6 +73,7 @@ extension MenuBarEngine { var isArrangementKnown: Bool { return true } var defersAutoCollapse: Bool { return false } var menuBarItemOwners: Set { return [] } + var hiddenSectionBundles: Set { return [] } func userChangedBar() {} func separatorsHiddenForCollapse(current: Bool) -> Bool { return current } var offersShowEverything: Bool { return false } diff --git a/hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift b/hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift index f241e653..c165f0cc 100644 --- a/hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift +++ b/hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift @@ -659,6 +659,10 @@ final class NativeVisibilityEngine: MenuBarEngine { return Set(read + complete) } + var hiddenSectionBundles: Set { + return Set(layout?.bundles(in: [.hidden]) ?? []) + } + // Set by the controller: the palette's reveal session also keeps a bar the user changed. var onUserChangedBar: (() -> Void)? diff --git a/hidden/Features/StatusBar/StatusBarController.swift b/hidden/Features/StatusBar/StatusBarController.swift index 230a6d24..e3619c32 100644 --- a/hidden/Features/StatusBar/StatusBarController.swift +++ b/hidden/Features/StatusBar/StatusBarController.swift @@ -409,7 +409,7 @@ class StatusBarController: MenuBarItemProvider { if AccessibilityPermission.shared.arrowClickOpensPreferences(isCollapsed: isCollapsed) { Util.showPrefWindow() } - self.isCollapsed ? self.expandMenubar() : self.collapseMenuBar(userInitiated: true) + self.isCollapsed ? self.expandMenubar(userInitiated: true) : self.collapseMenuBar(userInitiated: true) DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in self?.isToggle = false } @@ -483,8 +483,12 @@ class StatusBarController: MenuBarItemProvider { #endif } - private func expandMenubar(activatingApp: Bool = true) { + private func expandMenubar(activatingApp: Bool = true, userInitiated: Bool = false) { guard self.isCollapsed else {return} + #if HIDDENBAR_NATIVE_VISIBILITY + // Read before the engine expands: it is what the bar hid until now. + let hiddenBundles = userInitiated ? menuBarEngine.hiddenSectionBundles : [] + #endif menuBarEngine.expand() if let button = btnExpandCollapse.button { button.image = Assets.collapseImage @@ -499,6 +503,10 @@ class StatusBarController: MenuBarItemProvider { } #if HIDDENBAR_NATIVE_VISIBILITY presentationController?.barExpanded() + if userInitiated { + SystemOverflowNotice.shared.checkAfterExpand(hiddenBundles: hiddenBundles, + isExpanded: { [weak self] in !(self?.isCollapsed ?? true) }) + } #endif } diff --git a/hidden/Features/SystemOverflow/SystemOverflowNotice.swift b/hidden/Features/SystemOverflow/SystemOverflowNotice.swift index afae47d4..1853d643 100644 --- a/hidden/Features/SystemOverflow/SystemOverflowNotice.swift +++ b/hidden/Features/SystemOverflow/SystemOverflowNotice.swift @@ -14,6 +14,8 @@ import AppKit // hotkey still works, but a click user sees the arrow vanish. Say so once. struct SystemOverflowNoticePolicy { static let shownKey = "systemOverflowNoticeShown" + // The expand notice has its own flag: seeing one does not silence the other. + static let expandShownKey = "systemOverflowExpandNoticeShown" let defaults: UserDefaults @@ -24,6 +26,63 @@ struct SystemOverflowNoticePolicy { func recordShown() { defaults.set(true, forKey: Self.shownKey) } + + func shouldShowAfterExpand() -> Bool { + !defaults.bool(forKey: Self.expandShownKey) + } + + func recordExpandShown() { + defaults.set(true, forKey: Self.expandShownKey) + } + + /// The notice shows only for a bar that is expanded, with a revealed icon that + /// macOS put into «, and only until it was shown once. + static func shouldShowAfterExpand(isExpanded: Bool, hiddenItemInOverflow: Bool, alreadyShown: Bool) -> Bool { + isExpanded && hiddenItemInOverflow && !alreadyShown + } +} + +// After an expand the icons that were hidden are back on the bar. Those that do +// not fit right of the notch (or on a narrow display) go into «, and the user +// then needs a second click. macOS 27 lists only the items that fit, so such an +// app is missing from Accessibility, or its item sits under « or on no display. +enum SystemOverflowExpandMatcher { + /// The bundles of `hidden` that macOS moved into «. Only running third-party + /// apps count: system extras follow their own rules, and an app that is not + /// running has no icon to lose. + static func bundlesInOverflow(hidden: Set, + running: Set, + inventory: [MenuBarInventoryItem], + indicators: [SystemOverflowButton], + displays: [MenuBarDisplay], + primaryScreenMaxY: CGFloat?) -> Set { + guard !indicators.isEmpty else { return [] } + let system = MenuBarLayoutResolver.systemItemOwners + return hidden.filter { bundle in + guard running.contains(bundle), !system.contains(bundle), + bundle != MenuBarLayoutResolver.systemExtrasBundle else { return false } + let items = inventory.filter { $0.bundleIdentifier == bundle } + if items.isEmpty { return true } + // An app that did not answer cannot be judged. + if items.contains(where: { $0.isUnreadable }) { return false } + return items.contains { isInOverflow($0.frame, indicators: indicators, displays: displays, primaryScreenMaxY: primaryScreenMaxY) } + } + } + + // `frame` is an Accessibility frame (top-left origin, global). + private static func isInOverflow(_ frame: CGRect, + indicators: [SystemOverflowButton], + displays: [MenuBarDisplay], + primaryScreenMaxY: CGFloat?) -> Bool { + if !displays.isEmpty, MenuBarDisplay.containing(accessibilityFrame: frame, in: displays) == nil { return true } + guard let maxY = primaryScreenMaxY else { return false } + let cocoa = SystemOverflowMatcher.cocoaFrame(fromAccessibilityFrame: frame, primaryScreenMaxY: maxY) + let centre = CGPoint(x: cocoa.midX, y: cocoa.midY) + return indicators.contains { button in + guard let buttonFrame = button.frame else { return false } + return buttonFrame.insetBy(dx: -SystemOverflowMatcher.slack, dy: -SystemOverflowMatcher.slack).contains(centre) + } + } } final class SystemOverflowNotice { @@ -37,6 +96,7 @@ final class SystemOverflowNotice { private let indicator = SystemOverflowIndicator() private let policy = SystemOverflowNoticePolicy(defaults: AppDefaults.store) private var isChecking = false + private var isCheckingExpand = false /// Call after a collapse the user asked for (click or hotkey). `isCollapsed` /// is asked again when the delayed check runs, so an expand in between cancels it. @@ -46,6 +106,41 @@ final class SystemOverflowNotice { check(arrow: arrow, isCollapsed: isCollapsed, after: Self.settleDelay, retriesLeft: 1) } + /// Call after an expand the user asked for (click or hotkey). `hiddenBundles` is + /// what the bar hid before, read now; `isExpanded` is asked again before the + /// notice, so a collapse in between cancels it. + func checkAfterExpand(hiddenBundles: Set, isExpanded: @escaping () -> Bool, + inventory: MenuBarInventoryProviding = AccessibilityMenuBarInventory()) { + guard MenuBarEngineFactory.usesNativeEngine(), policy.shouldShowAfterExpand(), + !hiddenBundles.isEmpty, !isCheckingExpand, inventory.isAuthorized else { return } + isCheckingExpand = true + // The revealed icons need a moment to settle before macOS has placed or overflowed them. + DispatchQueue.main.asyncAfter(deadline: .now() + Self.settleDelay) { [weak self] in + guard let self = self else { return } + guard isExpanded() else { self.isCheckingExpand = false; return } + self.indicator.readButtons { [weak self] buttons in + guard let self = self else { return } + // No « means nothing overflowed; the inventory walk is skipped. + guard isExpanded(), !buttons.isEmpty else { self.isCheckingExpand = false; return } + inventory.snapshot { [weak self] items in + guard let self = self else { return } + let running = Set(NSWorkspace.shared.runningApplications.compactMap { $0.bundleIdentifier }) + let moved = SystemOverflowExpandMatcher.bundlesInOverflow( + hidden: hiddenBundles, running: running, inventory: items, indicators: buttons, + displays: MenuBarDisplay.current(), primaryScreenMaxY: NSScreen.screens.first?.frame.maxY) + guard SystemOverflowNoticePolicy.shouldShowAfterExpand(isExpanded: isExpanded(), + hiddenItemInOverflow: !moved.isEmpty, + alreadyShown: !self.policy.shouldShowAfterExpand()) else { + self.isCheckingExpand = false + return + } + NSLog("SystemOverflow: expanded; macOS moved \(moved.sorted()) into «") + self.presentExpandNotice() + } + } + } + } + private func check(arrow: NSStatusItem, isCollapsed: @escaping () -> Bool, after delay: TimeInterval, retriesLeft: Int) { DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self, weak arrow] in guard let self = self else { return } @@ -78,17 +173,41 @@ final class SystemOverflowNotice { // The run loop block does not. private func present() { RunLoop.main.perform(inModes: [.default]) { [weak self] in - let alert = NSAlert() - alert.alertStyle = .informational - alert.messageText = "Your menu bar is full".localized - alert.informativeText = "macOS moved the Hidden Bar arrow into the « button. Click « to reach it, or use the Hidden Bar hotkey. To keep the arrow in view, move some icons to the left of the arrow.".localized - alert.addButton(withTitle: "OK".localized) - NSApp.activate(ignoringOtherApps: true) - alert.runModal() + self?.runAlert(Self.makeAlert(title: "Your menu bar is full".localized, + text: "macOS moved the Hidden Bar arrow into the « button. Click « to reach it, or use the Hidden Bar hotkey. To keep the arrow in view, move some icons to the left of the arrow.".localized)) self?.policy.recordShown() self?.isChecking = false } } + + private func presentExpandNotice() { + RunLoop.main.perform(inModes: [.default]) { [weak self] in + // Recorded before the alert opens: the bar may collapse behind it, and a + // second expand must not stack another one. + self?.policy.recordExpandShown() + self?.runAlert(Self.makeExpandAlert()) + self?.isCheckingExpand = false + } + } + + static func makeExpandAlert() -> NSAlert { + makeAlert(title: "Some icons are in the « button".localized, + text: "Some icons do not fit in the menu bar, so macOS moved them into its own « button. Click « to see them. To make room, move icons you rarely use to the always-hidden section.".localized) + } + + private static func makeAlert(title: String, text: String) -> NSAlert { + let alert = NSAlert() + alert.alertStyle = .informational + alert.messageText = title + alert.informativeText = text + alert.addButton(withTitle: "OK".localized) + return alert + } + + private func runAlert(_ alert: NSAlert) { + NSApp.activate(ignoringOtherApps: true) + alert.runModal() + } } #endif diff --git a/hidden/en.lproj/Localizable.strings b/hidden/en.lproj/Localizable.strings index 5ba7624b..9dd7c7a9 100644 --- a/hidden/en.lproj/Localizable.strings +++ b/hidden/en.lproj/Localizable.strings @@ -194,6 +194,8 @@ Steps to view always hidden icons: "Show Everything" = "Show Everything"; "Your menu bar is full" = "Your menu bar is full"; "macOS moved the Hidden Bar arrow into the « button. Click « to reach it, or use the Hidden Bar hotkey. To keep the arrow in view, move some icons to the left of the arrow." = "macOS moved the Hidden Bar arrow into the « button. Click « to reach it, or use the Hidden Bar hotkey. To keep the arrow in view, move some icons to the left of the arrow."; +"Some icons are in the « button" = "Some icons are in the « button"; +"Some icons do not fit in the menu bar, so macOS moved them into its own « button. Click « to see them. To make room, move icons you rarely use to the always-hidden section." = "Some icons do not fit in the menu bar, so macOS moved them into its own « button. Click « to see them. To make room, move icons you rarely use to the always-hidden section."; "Also used by %@" = "Also used by %@"; "Show or hide icons" = "Show or hide icons"; "Ask Me" = "Ask Me";