Skip to content
Merged
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
42 changes: 41 additions & 1 deletion HiddenBarTests/MenuBarEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,7 @@ final class PinnedSectionsTests: XCTestCase {
private var observed: InMemorySectionMemory!
private var table: InMemorySectionMemory!
private var pins = true
private var dragLocked: Set<String> = []

override func setUp() {
super.setUp()
Expand All @@ -1574,6 +1575,7 @@ final class PinnedSectionsTests: XCTestCase {
observed = InMemorySectionMemory()
table = InMemorySectionMemory()
pins = true
dragLocked = []
}

private func makeEngine() -> NativeVisibilityEngine {
Expand All @@ -1585,7 +1587,8 @@ final class PinnedSectionsTests: XCTestCase {
isLTR: { true },
sectionMemory: observed,
pinnedSections: table,
pinsSections: { [unowned self] in pins })
pinsSections: { [unowned self] in pins },
ignoredDrags: { [unowned self] in dragLocked })
}

@discardableResult
Expand Down Expand Up @@ -1617,6 +1620,32 @@ final class PinnedSectionsTests: XCTestCase {
XCTAssertEqual(collapse(engine), [own, "com.visible"])
}

// Given an app that recreates its status item without relaunching (BitBar, SwiftBar)
// When "Ignore drags" is on for it and its icon lands on the hidden side
// Then it stays in its pinned section; an app without the flag still follows the move
func testAnAppIgnoringDragsKeepsItsSectionWhenItsIconMoves() {
table.sections = ["com.visible": .visible, "com.hidden": .hidden]
dragLocked = ["com.visible"]
let engine = makeEngine()
collapse(engine)
engine.expand()
move("com.visible", to: 1120)
move("com.hidden", to: 1450)
XCTAssertEqual(collapse(engine), [own, "com.hidden", "com.visible"])
XCTAssertEqual(table.sections["com.visible"], .visible, "the flagged app keeps its pinned section")
XCTAssertEqual(table.sections["com.hidden"], .visible, "an app without the flag follows the drag")
}

func testTheDragLocksPersistAsABundleList() throws {
let suite = "HiddenBarTests.dragLocks.\(UUID().uuidString)"
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
defer { defaults.removePersistentDomain(forName: suite) }
XCTAssertEqual(UserDefaultsDragLocks(defaults: defaults).bundles, [])
UserDefaultsDragLocks(defaults: defaults).bundles = ["com.b", "com.a"]
XCTAssertEqual(UserDefaultsDragLocks(defaults: defaults).bundles, ["com.a", "com.b"])
XCTAssertEqual(defaults.stringArray(forKey: UserDefaultsDragLocks.key), ["com.a", "com.b"])
}

// Without the beta, the same relaunch hides the app (v1.11.2, the drift #366 is about).
func testWithTheBetaOffARelaunchedAppGoesWhereMacOSPutsIt() {
pins = false
Expand Down Expand Up @@ -1859,6 +1888,17 @@ final class PinnedSectionsTests: XCTestCase {
XCTAssertEqual(table, resolved)
XCTAssertEqual(observed, ["moved": .visible, "still": .hidden, "relaunched": .hidden, "new": .hidden, "running": .alwaysHidden])
}

func testResolverKeepsTheSectionOfAnAppIgnoringDrags() {
var observed: [String: MenuBarSection] = ["locked": .visible, "free": .visible]
var table: [String: MenuBarSection] = ["locked": .visible, "free": .visible]
let resolved = PinnedSectionResolver.resolve(
read: ["locked": .hidden, "free": .hidden, "lockedNew": .hidden],
launchedSinceRead: [], ignoringDrags: ["locked", "lockedNew"], observed: &observed, table: &table)
XCTAssertEqual(resolved, ["locked": .visible, "free": .hidden, "lockedNew": .hidden],
"a flagged app with no entry still gets its first read as its section")
XCTAssertEqual(observed["locked"], .hidden, "the observed position still follows every read")
}
}

// MARK: - Legacy engine
Expand Down
11 changes: 8 additions & 3 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ flowchart TD
The controller calls `collapse` / `expand` / `updateAlwaysHiddenSection` /
`invalidateLayout` and never writes a status-item length itself.
`MenuBarEngineFactory` is the one place that picks an engine for the running
OS; today that is always `LegacyLengthEngine` (the length trick below).
OS and build: `NativeVisibilityEngine` on macOS 27 and later in the direct
build (`HIDDENBAR_NATIVE_VISIBILITY`), `LegacyLengthEngine` (the length trick
below) everywhere else, the App Store build on any OS included.
- **`Preferences`** (facade enum): typed accessors over `UserDefaults`; setters
post `NotificationCenter` notifications that the controller and prefs window
observe. There is no other state store.
Expand Down Expand Up @@ -113,9 +115,12 @@ the RUNBOOK for how that split is built and checked.
- **The notch**: hidden icons sit "under" the notch area on notched Macs; the
trick cannot reveal them there. The real fix is a spillover/second-bar design
(tracked in issues #357/#341/#148; candidate implementations in PRs #350/#358).
- **macOS 27**: the menu bar re-architecture in macOS 27 betas
- **macOS 27**: the menu bar re-architecture in macOS 27
(`NSMenuBarNavigationSceneExtension`) breaks length-inflation hiding entirely
(issue #360). A different mechanism may be required.
(issue #360). The direct build hides natively there (`NativeVisibilityEngine`,
which reads the sections over Accessibility); the App Store build cannot,
because the sandbox blocks those reads, and keeps `LegacyLengthEngine`, which
does not hide on 27.
- **Other apps' open menus**: interaction-awareness is pointer-position-based;
a pointer deep inside another app's open dropdown is below the menubar band,
so the collapse can still fire there.
15 changes: 9 additions & 6 deletions docs/BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ collapse state machine) are HIGH RISK and require a mandatory review-team pass.
- **#242 permanent icon-loss repro.** Needs a throwaway defaults profile (a live repro
risks losing real menu-bar icons). Required before the always-hidden decouple lands.

## Architectural epic (Option D, #366)
## Architectural epic (#366, decided: option B, pinned sections)

- **Managed-overflow / second-bar redesign.** The real fix behind the icon-drift,
always-hidden, notch and macOS 26/27 clusters. Replaces length-inflation with a managed
overflow bar, likely via Accessibility. Needs a design decision from Han. The cluster
list lives in the epic body; still open from it today: #107 #174 #224 #225 #233 #242
#327 #330 #334 (`gh issue view 366`, then `gh issue list`).
- **Decided.** Option B, pinned sections, shipped in 6e33618 (beta, macOS 27 direct build):
a per-app section table in place of positions. Brief: `docs/briefs/DECISION-BRIEF-366-overflow-bar.md`;
notes: `docs/implementation-notes/pinned-sections.md`. The cluster list lives in the epic
body (`gh issue view 366`, then `gh issue list`).
- **Remains.** Hidden-items menu (phase 4, thin A). Per-app drag lock for apps that recreate
their icon without relaunching (this PR, #107). macOS 26 run of the legacy patches
(always-hidden decoupled from "hide separators", "Show everything"). A 27.3 inventory dump
for #469.
- **What already landed that the epic builds on.** Native hiding on 27 (#403), notch
overflow menu (#420, in #468). The community second-bar PR (#358) was closed as
superseded: its window capture no longer works on macOS 27, so the idea continues here.
Expand Down
25 changes: 16 additions & 9 deletions docs/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,26 @@ To undo any of them: `defaults delete com.dwarvesv.minimalbar <key>`.
| A ghost "LauncherApplication" login item from old versions | Launch the current version once; it deauthorizes the legacy item automatically |
| App language stuck | See the `AppleLanguages` command above, or System Settings > General > Language & Region > Applications |
| 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 | Expected, see "Why new icons start hidden" below; ⌘-drag it to the right of the separator once |
| 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 > Menu Bar Apps, tick "Ignore drags" on that app; it then keeps the section you picked |
| Date disappears from the clock when collapsed (macOS 26 and earlier) | The collapsed bar takes the space macOS measures; set System Settings > Control Center > Clock > Show date to Always. macOS 27 with the direct build is not affected |

### Why new icons start hidden

Hidden Bar hides icons by widening its separator so everything to the *left* of
it slides off-screen. macOS always inserts a brand-new menu-bar icon at the
far-left slot, which is inside that hidden zone, so a freshly launched or updated
app can appear "swallowed". This is macOS positioning behavior, not Hidden Bar
moving your icon: there is no way for one app to reposition another app's menu-bar
icon. The one-time fix is to ⌘-drag the icon to the right of the separator;
macOS remembers that placement per app. A built-in way to keep chosen icons
pinned is being explored as part of the larger menu-bar redesign.
On macOS 26 and earlier, and in the App Store build, Hidden Bar hides icons by
widening its separator so everything to the *left* of it slides off-screen. macOS
always inserts a brand-new menu-bar icon at the far-left slot, which is inside that
hidden zone, so a freshly launched or updated app can appear "swallowed". This is
macOS positioning behavior, not Hidden Bar moving your icon: there is no way for
one app to reposition another app's menu-bar icon. The one-time fix is to ⌘-drag
the icon to the right of the separator; macOS remembers that placement per app.

On macOS 27 with the direct build, Hidden Bar keeps a section per app instead
(Pinned sections, beta, on by default; Settings > Menu Bar Apps). A new or
relaunched app starts visible and stays in the section you pick, also after it
restarts. A ⌘-drag across the arrow still moves it. An app that recreates its
icon without relaunching (BitBar, SwiftBar) can look like a drag; tick "Ignore
drags" on its row to keep it where you put it.

## Requirements

Expand Down
29 changes: 27 additions & 2 deletions hidden/Features/Settings/MenuBarAppsSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protocol MenuBarAppsStore {
func entries() -> [PinnedSectionsList.Entry]
func icon(for bundle: String) -> NSImage?
func set(_ section: MenuBarSection, for bundle: String)
func setIgnoresDrags(_ ignores: Bool, for bundle: String)
func resetAllToVisible()
func enable()
}
Expand All @@ -23,7 +24,8 @@ struct LiveMenuBarAppsStore: MenuBarAppsStore {
var isEnabled: Bool { Preferences.pinnedSectionsEnabled }

func entries() -> [PinnedSectionsList.Entry] {
PinnedSectionsList.entries(table: UserDefaultsPinnedSections().sections, name: Self.appName)
PinnedSectionsList.entries(table: UserDefaultsPinnedSections().sections, name: Self.appName,
ignoringDrags: UserDefaultsDragLocks().bundles)
}

// "Safari", without ".app" even when Finder shows extensions.
Expand All @@ -40,6 +42,11 @@ struct LiveMenuBarAppsStore: MenuBarAppsStore {
PinnedSectionEdits.set(section, for: bundle, in: UserDefaultsPinnedSections())
}

func setIgnoresDrags(_ ignores: Bool, for bundle: String) {
let locks = UserDefaultsDragLocks()
if ignores { locks.bundles.insert(bundle) } else { locks.bundles.remove(bundle) }
}

func resetAllToVisible() {
PinnedSectionEdits.resetAllToVisible(in: UserDefaultsPinnedSections())
}
Expand All @@ -59,6 +66,10 @@ final class MenuBarAppsSettingsViewController: SettingsPaneController {
static func popupIdentifier(for bundle: String) -> String {
"\(UserDefaultsPinnedSections.key).\(bundle)"
}
/// Identifier of each row's "Ignore drags" checkbox.
static func lockIdentifier(for bundle: String) -> String {
"\(UserDefaultsDragLocks.key).\(bundle)"
}
static let maxListHeight: CGFloat = 360

private let store: MenuBarAppsStore
Expand Down Expand Up @@ -159,7 +170,15 @@ final class MenuBarAppsSettingsViewController: SettingsPaneController {
popup.target = self
popup.action = #selector(sectionPicked(_:))

let row = SettingsForm.row(entry.name, control: popup)
let lock = NSButton(checkboxWithTitle: "Ignore drags".localized, target: self, action: #selector(lockToggled(_:)))
lock.controlSize = .small
lock.state = entry.ignoresDrags ? .on : .off
lock.identifier = NSUserInterfaceItemIdentifier(Self.lockIdentifier(for: entry.bundle))
lock.toolTip = "Keep this app in its section even when its icon moves on its own, as BitBar and SwiftBar do. Dragging no longer changes it; the popup still does.".localized
let controls = NSStackView(views: [lock, popup])
controls.spacing = 10

let row = SettingsForm.row(entry.name, control: controls)
row.toolTip = entry.bundle
let icon = NSImageView(image: store.icon(for: entry.bundle) ?? NSWorkspace.shared.icon(for: .applicationBundle))
icon.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -184,6 +203,11 @@ final class MenuBarAppsSettingsViewController: SettingsPaneController {
store.set(section, for: String(raw.dropFirst(UserDefaultsPinnedSections.key.count + 1)))
}

@objc private func lockToggled(_ sender: NSButton) {
guard let raw = sender.identifier?.rawValue else { return }
store.setIgnoresDrags(sender.state == .on, for: String(raw.dropFirst(UserDefaultsDragLocks.key.count + 1)))
}

@objc private func resetAll() {
store.resetAllToVisible()
}
Expand Down Expand Up @@ -220,6 +244,7 @@ struct SnapshotMenuBarAppsStore: MenuBarAppsStore {

func icon(for bundle: String) -> NSImage? { LiveMenuBarAppsStore().icon(for: bundle) }
func set(_ section: MenuBarSection, for bundle: String) {}
func setIgnoresDrags(_ ignores: Bool, for bundle: String) {}
func resetAllToVisible() {}
func enable() {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum MenuBarEngineFactory {
workspaceNotifications: NSWorkspace.shared.notificationCenter,
pinnedSections: UserDefaultsPinnedSections(),
pinsSections: { Preferences.pinnedSectionsEnabled },
ignoredDrags: { UserDefaultsDragLocks().bundles },
agentLaunches: AgentLaunchNotifications(isActive: {
AgentLaunchNotifications.isActive(pinnedSectionsEnabled: Preferences.pinnedSectionsEnabled)
}))
Expand Down
4 changes: 4 additions & 0 deletions hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ final class NativeVisibilityEngine: MenuBarEngine {
// and a read only edits it (PinnedSectionResolver). Off, sections come from
// positions as in v1.11.2.
private let pinnedSections: MenuBarSectionMemory
private let ignoredDrags: () -> Set<String>
private let pinsSections: () -> Bool

// Items right of the arrow that the allow-list cannot keep visible: an item
Expand Down Expand Up @@ -140,8 +141,10 @@ final class NativeVisibilityEngine: MenuBarEngine {
workspaceNotifications: NotificationCenter? = nil,
pinnedSections: MenuBarSectionMemory = InMemorySectionMemory(),
pinsSections: @escaping () -> Bool = { false },
ignoredDrags: @escaping () -> Set<String> = { [] },
agentLaunches: NotificationCenter? = nil) {
self.items = items
self.ignoredDrags = ignoredDrags
self.agentLaunches = agentLaunches
self.pinnedSections = pinnedSections
self.pinsSections = pinsSections
Expand Down Expand Up @@ -374,6 +377,7 @@ final class NativeVisibilityEngine: MenuBarEngine {
var table = self.pinnedSections.sections
layout.sections = PinnedSectionResolver.resolve(read: layout.sections,
launchedSinceRead: self.launchedSinceRead,
ignoringDrags: self.ignoredDrags(),
observed: &observed, table: &table)
self.sectionMemory.sections = observed
self.pinnedSections.sections = table
Expand Down
29 changes: 25 additions & 4 deletions hidden/Features/StatusBar/Layout/MenuBarLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ final class UserDefaultsPinnedSections: MenuBarSectionMemory {
}
}

// Apps that recreate their status item without relaunching (BitBar, SwiftBar)
// read as dragged on every recreation; a bundle in this set keeps its pinned
// section whatever position is observed.
final class UserDefaultsDragLocks {
static let key = "pinnedDragLocks"
private let defaults: UserDefaults

init(defaults: UserDefaults = .standard) {
self.defaults = defaults
}

var bundles: Set<String> {
get { Set(defaults.stringArray(forKey: Self.key) ?? []) }
set { defaults.set(newValue.sorted(), forKey: Self.key) }
}
}

enum PinnedSectionResolver {
// Where an app that starts with no pinned section goes.
static let newAppSection: MenuBarSection = .visible
Expand All @@ -145,9 +162,11 @@ enum PinnedSectionResolver {
// from it. A position is the user's choice only when it changed since the
// app was last read and the app did not start meanwhile: macOS puts a new
// or relaunched icon far left, wherever the user had it. `observed` is the
// section each app was last read in; it follows every read.
// section each app was last read in; it follows every read. An app in
// `ignoringDrags` never takes a moved position.
static func resolve(read: [String: MenuBarSection],
launchedSinceRead: Set<String>,
ignoringDrags: Set<String> = [],
observed: inout [String: MenuBarSection],
table: inout [String: MenuBarSection]) -> [String: MenuBarSection] {
var resolved: [String: MenuBarSection] = [:]
Expand All @@ -157,7 +176,7 @@ enum PinnedSectionResolver {
switch table[bundle] {
case nil:
table[bundle] = launched ? newAppSection : position
case .some(_) where moved && !launched:
case .some(_) where moved && !launched && !ignoringDrags.contains(bundle):
table[bundle] = position
case .some:
break
Expand Down Expand Up @@ -194,12 +213,14 @@ enum PinnedSectionsList {
let bundle: String
let name: String
let section: MenuBarSection
var ignoresDrags = false
}

static func entries(table: [String: MenuBarSection], name: (String) -> String?) -> [Entry] {
static func entries(table: [String: MenuBarSection], name: (String) -> String?,
ignoringDrags: Set<String> = []) -> [Entry] {
return table.map { bundle, section in
let label = bundle == MenuBarLayoutResolver.systemExtrasBundle ? "macOS extras (Focus, Now Playing...)" : (name(bundle) ?? bundle)
return Entry(bundle: bundle, name: label, section: section)
return Entry(bundle: bundle, name: label, section: section, ignoresDrags: ignoringDrags.contains(bundle))
}.sorted {
let order = $0.name.localizedCaseInsensitiveCompare($1.name)
return order == .orderedSame ? $0.bundle < $1.bundle : order == .orderedAscending
Expand Down
Loading