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
49 changes: 49 additions & 0 deletions HiddenBarTests/MenuBarEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ private func systemExtra(_ identifier: String?, x: CGFloat) -> MenuBarInventoryI
systemIdentifier: identifier)
}

private func outsideApp(_ bundle: String, x: CGFloat) -> MenuBarInventoryItem {
return MenuBarInventoryItem(bundleIdentifier: bundle, frame: CGRect(x: x, y: 0, width: 24, height: 24),
ownerName: "iStat Menus", runsOutsideApplicationsFolder: true)
}

final class FailOpenClassificationTests: XCTestCase {
private func resolve(_ inventory: [MenuBarInventoryItem], isLTR: Bool = true) -> MenuBarLayout {
return MenuBarLayoutResolver.resolve(inventory: inventory, separatorFrame: separator,
Expand Down Expand Up @@ -452,6 +457,35 @@ final class FailOpenClassificationTests: XCTestCase {
XCTAssertNil(left.reasonToKeepBarWhole(unkeepableSystemItems: unkeepable))
}

// iStat Menus 7 runs its menu bar from ~/Library/Application Support (#521):
// macOS 27.0.1 hides it although its bundle id is allowed.
func testAppRunFromOutsideApplicationsRightOfArrowIsUnkeepable() {
let layout = resolve([outsideApp("com.bjango.istatmenus.status", x: 1400), item("com.app", x: 1100)])
XCTAssertEqual(layout.sections["com.bjango.istatmenus.status"], .visible)
XCTAssertEqual(layout.unkeepableVisibleApps, ["iStat Menus"])
XCTAssertNotNil(layout.reasonToKeepBarWhole(unkeepableSystemItems: []))
}

func testAppRunFromOutsideApplicationsLeftOfArrowCanBeHidden() {
let layout = resolve([outsideApp("com.bjango.istatmenus.status", x: 1100)])
XCTAssertTrue(layout.unkeepableVisibleApps.isEmpty)
XCTAssertNil(layout.reasonToKeepBarWhole(unkeepableSystemItems: []))
}

func testWhereAnAppRunsDecidesWhetherMacOSCanKeepIt() {
let folders = [URL(fileURLWithPath: "/Applications")]
func outside(_ path: String?) -> Bool {
AccessibilityMenuBarInventory.runsOutsideApplicationsFolder(path.map { URL(fileURLWithPath: $0) },
applicationFolders: folders)
}
XCTAssertTrue(outside("/Users/me/Library/Application Support/iStat Menus 7/iStat Menus Status.app"))
XCTAssertTrue(outside("/Users/me/Downloads/Some.app"))
XCTAssertFalse(outside("/Applications/iStat Menus.app/Contents/Library/LoginItems/iStat Menus Status.app"))
XCTAssertFalse(outside("/System/Library/CoreServices/Spotlight.app"), "macOS's own apps live in /System")
XCTAssertFalse(outside("/usr/libexec/sharingd"), "macOS's own daemons live in /usr (seen on 27.0.1)")
XCTAssertFalse(outside(nil), "a bare executable has no bundle id to allow anyway")
}

func testMissingSystemItemsAreThoseNoLongerListed() {
let missing = MenuBarLayoutResolver.missingSystemItems(
["com.apple.menuextra.focusmode", "com.apple.menuextra.now-playing"],
Expand Down Expand Up @@ -662,6 +696,21 @@ final class UnkeepableItemsOptInTests: XCTestCase {
XCTAssertEqual(result, .unavailable)
}

func testOptInKeepsAppRunFromOutsideApplicationsRightOfArrowShown() {
let (requests, result) = collapse([outsideApp("com.bjango.istatmenus.status", x: 1400), item("com.app", x: 1100)],
optIn: true)
XCTAssertEqual(requests, 0, "macOS would hide iStat Menus although it is allowed")
XCTAssertEqual(result, .unavailable)
XCTAssertTrue(NativeVisibilityEngine.knownUnkeepableApps.contains("iStat Menus"))
}

func testDefaultHidesAppRunFromOutsideApplicationsAndListsIt() {
let (requests, _) = collapse([outsideApp("com.bjango.istatmenus.status", x: 1400), item("com.app", x: 1100)],
optIn: false)
XCTAssertEqual(requests, 1, "off by default: hiding goes on as before")
XCTAssertTrue(NativeVisibilityEngine.knownUnkeepableApps.contains("iStat Menus"), "Preferences names it")
}

func testOptInStillCollapsesWhenTheOwnerIsLeftOfArrow() {
let (requests, _) = collapse([item(nil, x: 1100), item("com.app", x: 1100)], optIn: true)
XCTAssertEqual(requests, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ final class AdvancedSettingsViewController: SettingsPaneController {

private func refreshMacOSAlsoHides() {
let names = NativeVisibilityEngine.knownUnkeepableSystemItems.map(NativeVisibilityEngine.displayName(ofSystemItem:))
+ NativeVisibilityEngine.knownUnkeepableApps
let help = "macOS 27 hides these items whenever an app hides icons. While one shows, Hidden Bar hides nothing.".localized
macOSAlsoHidesCaption?.stringValue = names.isEmpty
? help
Expand Down
12 changes: 12 additions & 0 deletions hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ import AppKit
// one run from elsewhere (Downloads, a build folder): it hides that app's
// icons although they are allowed, Hidden Bar's own arrow included, leaving no
// arrow to expand with. Outside an Applications folder nothing is hidden.
// Another app run from elsewhere (iStat Menus 7 from Application Support,
// #521) is hidden the same way although it is allowed; Preferences lists it
// with the system items macOS will not keep, and the opt-in keeps the bar
// whole while one sits right of the arrow.
// Everything one held restriction allows besides Hidden Bar itself. Two holds are
// the same only when every part matches, so a swap between lists that name the
// same apps but different system items is not skipped.
Expand Down Expand Up @@ -486,6 +490,12 @@ final class NativeVisibilityEngine: MenuBarEngine {
if !layout.unnameableVisibleItems.isEmpty || !layout.unprovenVisibleSystemItems.isEmpty {
NSLog("NativeVisibility: visible without a bundle id \(layout.unnameableVisibleItems); visible system extras to check \(layout.unprovenVisibleSystemItems)")
}
let unkeepableApps = layout.unkeepableVisibleApps
if !unkeepableApps.isEmpty, !Set(unkeepableApps).isSubset(of: Self.knownUnkeepableApps) {
NSLog("NativeVisibility: macOS hides \(unkeepableApps) although they are allowed: they run from outside an Applications folder")
Self.knownUnkeepableApps = Array(Set(Self.knownUnkeepableApps).union(unkeepableApps)).sorted()
NotificationCenter.default.post(name: .unkeepableSystemItemsChanged, object: nil)
}
self.layout = layout
body(layout)
}
Expand Down Expand Up @@ -1133,6 +1143,8 @@ final class NativeVisibilityEngine: MenuBarEngine {

// What Preferences shows as "macOS 27 also hides".
static private(set) var knownUnkeepableSystemItems: [String] = []
// Same, for apps run from outside an Applications folder, by name.
static private(set) var knownUnkeepableApps: [String] = []

private func rememberUnkeepable(_ identifiers: [String]) {
unkeepableSystemItems.formUnion(identifiers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class AccessibilityMenuBarInventory: MenuBarInventoryProviding {
// more time, so a busy app is not dropped from the allow list and hidden.
private static let retryTimeout: Float = 0.5

private typealias App = (pid: pid_t, bundleIdentifier: String?, name: String?)
private typealias App = (pid: pid_t, bundleIdentifier: String?, name: String?, runsOutsideApplicationsFolder: Bool)

private var hasRequestedAuthorization = false

Expand All @@ -42,7 +42,8 @@ final class AccessibilityMenuBarInventory: MenuBarInventoryProviding {
let ownPID = ProcessInfo.processInfo.processIdentifier
let apps: [App] = NSWorkspace.shared.runningApplications
.filter { $0.processIdentifier != ownPID && Self.mayOwnStatusItems(bundleURL: $0.bundleURL, bundleIdentifier: $0.bundleIdentifier) }
.map { (pid: $0.processIdentifier, bundleIdentifier: $0.bundleIdentifier, name: $0.localizedName) }
.map { (pid: $0.processIdentifier, bundleIdentifier: $0.bundleIdentifier, name: $0.localizedName,
runsOutsideApplicationsFolder: Self.runsOutsideApplicationsFolder($0.bundleURL)) }
DispatchQueue.global(qos: .userInitiated).async {
let start = Date()
let items = Self.statusItems(of: apps)
Expand All @@ -51,6 +52,21 @@ final class AccessibilityMenuBarInventory: MenuBarInventoryProviding {
}
}

// macOS 27 hides an app run from outside an Applications folder although its
// bundle id is allowed (#521), as it does Hidden Bar's own arrow. Measured
// with iStat Menus 7, whose LaunchAgent runs it from ~/Library/Application
// Support: hidden on 27.0.1 and 27.2.1, kept when run from /Applications.
// macOS's own processes are left out: SystemUIServer's extras are kept by
// bundle, and sharingd (/usr/libexec) lists an item no one sees.
static func runsOutsideApplicationsFolder(_ bundleURL: URL?,
applicationFolders: [URL] = FileManager.default.urls(for: .applicationDirectory,
in: .localDomainMask)) -> Bool {
guard let url = bundleURL else { return false }
let path = url.resolvingSymlinksInPath().standardizedFileURL.path
if ["/System/", "/usr/", "/Library/Apple/"].contains(where: path.hasPrefix) { return false }
return !NativeVisibilityEngine.isInApplicationsFolder(url, applicationFolders: applicationFolders)
}

// Apps and bare executables (scripts, helpers without an Info.plist) can own
// status items. Skipping XPC services and extensions (dozens of WebKit
// helpers, each of which would sit out the timeout) cuts the walk from ~2.8s
Expand Down Expand Up @@ -94,7 +110,8 @@ final class AccessibilityMenuBarInventory: MenuBarInventoryProviding {
let app = apps[index]
guard app.bundleIdentifier != nil else { return [] }
return [MenuBarInventoryItem(bundleIdentifier: app.bundleIdentifier, frame: .zero,
isUnreadable: true, ownerName: app.name)]
isUnreadable: true, ownerName: app.name,
runsOutsideApplicationsFolder: app.runsOutsideApplicationsFolder)]
})
}

Expand Down Expand Up @@ -152,11 +169,13 @@ final class AccessibilityMenuBarInventory: MenuBarInventoryProviding {
return .items(children.map { child in
guard let frame = frame(of: child) else {
return MenuBarInventoryItem(bundleIdentifier: app.bundleIdentifier, frame: .zero,
isUnreadable: true, ownerName: app.name)
isUnreadable: true, ownerName: app.name,
runsOutsideApplicationsFolder: app.runsOutsideApplicationsFolder)
}
return MenuBarInventoryItem(bundleIdentifier: app.bundleIdentifier, frame: frame,
systemIdentifier: isSystemOwner ? identifier(of: child) : nil,
ownerName: app.name)
ownerName: app.name,
runsOutsideApplicationsFolder: app.runsOutsideApplicationsFolder)
})
}

Expand Down
19 changes: 19 additions & 0 deletions hidden/Features/StatusBar/Layout/MenuBarLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ struct MenuBarInventoryItem: Equatable {
// The owner did not answer Accessibility in time, so the frame is unknown.
var isUnreadable: Bool = false
var ownerName: String? = nil
// Run from outside an Applications folder (iStat Menus 7 runs its menu bar
// from Application Support, #521) on a macOS that then hides it although
// its bundle id is allowed. Set by the inventory only where that holds.
var runsOutsideApplicationsFolder: Bool = false
}

// One connected display. Every display has its own menu bar, but AppKit hands
Expand Down Expand Up @@ -254,6 +258,10 @@ struct MenuBarLayout: Equatable {
// Visible-section system extras that no system item identifier covers. Only
// their bundle can try to keep them, so the engine checks they stay shown.
var unprovenVisibleSystemItems: [String] = []
// Apps macOS will not keep although their bundle id is allowed, by bundle,
// with the name Preferences shows. Kept whatever section they are in, as
// pinned sections may move them after the read.
var appsMacOSHides: [String: String] = [:]
// The active display's menu bar overflowed (a narrow display), and macOS
// lists only the items that fit, so apps are missing from `sections`.
var isIncomplete = false
Expand All @@ -262,13 +270,19 @@ struct MenuBarLayout: Equatable {
func fillingIn(from earlier: MenuBarLayout) -> MenuBarLayout {
var layout = self
layout.sections.merge(earlier.sections) { current, _ in current }
layout.appsMacOSHides.merge(earlier.appsMacOSHides) { current, _ in current }
return layout
}

func bundles(in wanted: Set<MenuBarSection>) -> [String] {
return sections.filter { wanted.contains($0.value) }.map(\.key).sorted()
}

// Names of the visible-section apps macOS hides whatever the allow-list says.
var unkeepableVisibleApps: [String] {
return bundles(in: [.visible]).compactMap { appsMacOSHides[$0] }.sorted()
}

// Why nothing may be hidden with this layout: fail open rather than hide an
// item the user put in the visible section.
func reasonToKeepBarWhole(unkeepableSystemItems: Set<String>) -> String? {
Expand All @@ -279,6 +293,9 @@ struct MenuBarLayout: Equatable {
if !unkeepable.isEmpty {
return "macOS hides these system items while hiding anything, and they sit right of the arrow: \(unkeepable)"
}
if !unkeepableVisibleApps.isEmpty {
return "macOS hides these apps run from outside an Applications folder although they are allowed, and they sit right of the arrow: \(unkeepableVisibleApps)"
}
return nil
}
}
Expand Down Expand Up @@ -363,6 +380,8 @@ enum MenuBarLayoutResolver {
layout.unprovenVisibleSystemItems.append(identifier)
}
key = systemExtrasBundle
} else if item.runsOutsideApplicationsFolder {
layout.appsMacOSHides[key] = item.ownerName ?? key
}
// One app can own several items, but hiding applies to the whole
// bundle. The most visible item wins so an icon the user kept visible
Expand Down