Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2312e09105 | ||
|
|
9c5416d27e | ||
|
|
61d2689cbc | ||
|
|
8df3e2cd94 | ||
|
|
d9eddf799f | ||
|
|
dec8d60aa3 |
@@ -42,6 +42,10 @@ executable SwiftPM package, no external dependencies (all Apple frameworks).
|
||||
including ones not in the bar, which are parked off-screen at `y≈982`.
|
||||
`enumerate()` drops items whose AX position `y > 60` and de-duplicates by
|
||||
title per app, otherwise the tray fills with identical "Control Center" tiles.
|
||||
- Icons: every Control Center module shares the same app icon, so
|
||||
`symbolName(for:)` maps titles (Wi‑Fi, Battery, Now Playing, …) to distinct SF
|
||||
Symbols, falling back to the owning app's icon. Note the non-breaking hyphen
|
||||
in "Wi‑Fi" (`\u{2011}`) is normalised before matching.
|
||||
- Reading another app's UI is the only TCC permission needed; there is no Screen
|
||||
Recording or Apple Events usage.
|
||||
|
||||
@@ -119,7 +123,8 @@ no control over this, so the app creates its own `NSStatusItem` and:
|
||||
## Hiding other menu bar items
|
||||
|
||||
`MenuBarHider` creates a zero-width spacer `NSStatusItem` at launch and grows its
|
||||
length to `hiddenLength` (10_000) to push the icons on its left off-screen. Its
|
||||
length to `hiddenLength` (10_000) to push the icons on its left off-screen. It is
|
||||
**on by default** (`hideMenuBarItems` defaults to true, then persisted). Its
|
||||
preferred position controls where the hidden/kept boundary lands:
|
||||
|
||||
- Seed **250** (default) puts the boundary just left of Wi-Fi, so every
|
||||
@@ -129,6 +134,20 @@ preferred position controls where the hidden/kept boundary lands:
|
||||
- Verified with `--diag-hide` across positions 100–400. Below ~200 Wi-Fi gets
|
||||
hidden too; above ~300 app icons (Now Playing, TextInput) leak back.
|
||||
- `debug.spacerPos` (Double) overrides the seed for tuning without rebuilding.
|
||||
- Pressing a tray tile calls `MenuBarItemManager.press`. A hidden item sits
|
||||
off-screen, and its menu opens off-screen too (verified with `--diag-press`:
|
||||
the menu window lands at negative x). So press briefly `suspend()`s the hider,
|
||||
waits ~0.2s for the layout to settle, performs the accessibility action
|
||||
(`kAXPressAction`, then `kAXShowMenuAction`), and `resume()`s ~0.1s later. The
|
||||
menu is its own window anchored at the item's on-screen spot, so it survives
|
||||
re-hiding.
|
||||
- **Cannot reveal a single icon.** The spacer hides a contiguous block to its
|
||||
left, so a press can only reveal the whole block. Isolating one item would
|
||||
need either per-item ordering control (not available) or covering the icons
|
||||
with an overlay window instead of moving them off-screen.
|
||||
- **Order matters**: `AppDelegate` must call `setupStatusItem()` *before*
|
||||
`MenuBarHider.shared.prepare()`. If the hider contracts the bar first, the
|
||||
hamburger is created into the collapsed space and lands off-screen.
|
||||
- **Wi-Fi/Spotlight/Clock/Battery/Control Center cannot be hidden**: macOS pins
|
||||
them to the far right and a third-party item can never be inserted to their
|
||||
right, so the spacer has nothing to push them with. This is a macOS
|
||||
|
||||
@@ -28,8 +28,9 @@ still works.
|
||||
macOS has no public API to move another app's status item. The **Hide items from
|
||||
the menu bar** toggle uses the spacer technique: a zero-width status item is
|
||||
created at launch whose length is expanded to push the other icons off-screen so
|
||||
they only live in this panel. It is off by default and always undone on quit; if
|
||||
the app dies, macOS removes the spacer automatically.
|
||||
they only live in this panel. It is **on by default** (the choice is
|
||||
remembered), always undone on quit, and if the app dies macOS removes the spacer
|
||||
automatically.
|
||||
|
||||
What it can and cannot hide:
|
||||
|
||||
|
||||
@@ -43,8 +43,31 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
|
||||
AppListModel.shared.start()
|
||||
MenuBarItemManager.shared.start()
|
||||
MenuBarHider.shared.prepare()
|
||||
// Create the hamburger before the hider so it is placed in the visible
|
||||
// part of the bar; the hider then contracts the space around it.
|
||||
setupStatusItem()
|
||||
MenuBarHider.shared.prepare()
|
||||
|
||||
if CommandLine.arguments.contains("--diag-press") {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
|
||||
MenuBarHider.shared.suspend()
|
||||
}
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 2.25) {
|
||||
if let item = MenuBarItemManager.enumerate().first(where: {
|
||||
$0.title.lowercased().contains("wireguard")
|
||||
}) {
|
||||
NSLog("diag-press: pressing \(item.appName) after brief reveal")
|
||||
_ = AXUIElementPerformAction(item.element, kAXPressAction as CFString)
|
||||
}
|
||||
}
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 2.45) {
|
||||
MenuBarHider.shared.resume()
|
||||
}
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 4.5) {
|
||||
dumpWindows()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if CommandLine.arguments.contains("--diag") {
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
|
||||
|
||||
@@ -8,7 +8,7 @@ struct MenuBarItem: Identifiable, Equatable {
|
||||
let pid: pid_t
|
||||
let appName: String
|
||||
let title: String
|
||||
let appIcon: NSImage
|
||||
let icon: NSImage
|
||||
let element: AXUIElement
|
||||
|
||||
static func == (lhs: MenuBarItem, rhs: MenuBarItem) -> Bool {
|
||||
@@ -90,7 +90,7 @@ final class MenuBarItemManager: ObservableObject {
|
||||
pid: pid,
|
||||
appName: app.localizedName ?? app.bundleIdentifier ?? "Unknown",
|
||||
title: title,
|
||||
appIcon: app.icon ?? NSImage(named: NSImage.applicationIconName) ?? NSImage(),
|
||||
icon: Self.icon(forTitle: title, app: app),
|
||||
element: child
|
||||
))
|
||||
}
|
||||
@@ -103,16 +103,83 @@ final class MenuBarItemManager: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the real menu bar item by pressing its accessibility element, so
|
||||
/// its menu/popover appears at its true screen location.
|
||||
@discardableResult
|
||||
func press(_ item: MenuBarItem) -> Bool {
|
||||
/// Opens the real menu bar item, so its menu/popover appears.
|
||||
///
|
||||
/// A hidden item sits off-screen, and its menu would open off-screen too.
|
||||
/// So briefly reveal the items, let the layout settle, press the item, then
|
||||
/// hide again shortly after. The reveal is momentary rather than lasting the
|
||||
/// whole time the panel is open.
|
||||
func press(_ item: MenuBarItem) {
|
||||
let hider = MenuBarHider.shared
|
||||
let needsReveal = hider.isHiding
|
||||
guard needsReveal else {
|
||||
_ = performPress(item)
|
||||
return
|
||||
}
|
||||
hider.suspend()
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
|
||||
_ = self.performPress(item)
|
||||
// The menu is its own window, positioned at the item's on-screen
|
||||
// spot, so the bar can be re-hidden almost immediately.
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||||
hider.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func performPress(_ item: MenuBarItem) -> Bool {
|
||||
if AXUIElementPerformAction(item.element, kAXPressAction as CFString) == .success {
|
||||
return true
|
||||
}
|
||||
return AXUIElementPerformAction(item.element, kAXShowMenuAction as CFString) == .success
|
||||
}
|
||||
|
||||
// MARK: - Icons
|
||||
|
||||
/// Picks an icon for a menu bar item. Control Center owns many unrelated
|
||||
/// modules, and they all share the Control Center app icon, so map their
|
||||
/// titles to distinct SF Symbols for readability. Anything unknown falls
|
||||
/// back to the owning app's icon.
|
||||
nonisolated static func icon(forTitle title: String, app: NSRunningApplication) -> NSImage {
|
||||
if let symbol = symbolName(for: title),
|
||||
let image = NSImage(systemSymbolName: symbol, accessibilityDescription: title) {
|
||||
image.isTemplate = true
|
||||
return image
|
||||
}
|
||||
return app.icon ?? NSImage(named: NSImage.applicationIconName) ?? NSImage()
|
||||
}
|
||||
|
||||
/// Keyword table mapping a menu bar item's title/description to an SF Symbol.
|
||||
nonisolated static func symbolName(for title: String) -> String? {
|
||||
// Control Center uses a non-breaking hyphen in "Wi‑Fi".
|
||||
let text = title.lowercased()
|
||||
.replacingOccurrences(of: "\u{2010}", with: "-")
|
||||
.replacingOccurrences(of: "\u{2011}", with: "-")
|
||||
let table: [(matches: [String], symbol: String)] = [
|
||||
(["wi-fi", "wifi"], "wifi"),
|
||||
(["battery"], "battery.100"),
|
||||
(["clock", "time"], "clock"),
|
||||
(["now playing"], "play.circle"),
|
||||
(["focus"], "moon.fill"),
|
||||
(["sound", "volume", "audio"], "speaker.wave.2"),
|
||||
(["airdrop"], "dot.radiowaves.left.and.right"),
|
||||
(["bluetooth"], "dot.radiowaves.left.and.right"),
|
||||
(["mirroring"], "rectangle.on.rectangle"),
|
||||
(["search", "spotlight"], "magnifyingglass"),
|
||||
(["siri"], "waveform"),
|
||||
(["keyboard"], "keyboard"),
|
||||
(["stage manager"], "rectangle.3.group"),
|
||||
(["brightness", "display"], "sun.max"),
|
||||
(["control center"], "switch.2"),
|
||||
(["notification"], "bell"),
|
||||
(["vpn"], "lock.shield"),
|
||||
]
|
||||
for entry in table where entry.matches.contains(where: { text.contains($0) }) {
|
||||
return entry.symbol
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MARK: - AX helpers
|
||||
|
||||
nonisolated static func extrasMenuBar(of app: AXUIElement) -> AXUIElement? {
|
||||
@@ -151,10 +218,10 @@ final class MenuBarItemManager: ObservableObject {
|
||||
}
|
||||
|
||||
/// Best-effort menu bar item hiding. A zero-width spacer status item is created
|
||||
/// at launch; expanding its length to a huge value pushes the items to its left
|
||||
/// at launch; expanding its length to a huge value pushes the other apps' icons
|
||||
/// off the visible menu bar, so they can only be reached through SimpleTopBar's
|
||||
/// panel. Reliable only when SimpleTopBar starts at login (its spacer then sits
|
||||
/// to the right of later-launched apps' items). Always restored on quit.
|
||||
/// panel. On by default; the choice is persisted. Always restored on quit, and
|
||||
/// a crash cleans up automatically because status items die with the process.
|
||||
@MainActor
|
||||
final class MenuBarHider: ObservableObject {
|
||||
static let shared = MenuBarHider()
|
||||
@@ -162,17 +229,24 @@ final class MenuBarHider: ObservableObject {
|
||||
/// Width that pushes the neighbouring items off the visible menu bar.
|
||||
static let hiddenLength: CGFloat = 10_000
|
||||
|
||||
private static let defaultsKey = "hideMenuBarItems"
|
||||
|
||||
@Published private(set) var isHiding = false
|
||||
@Published var enabled = false {
|
||||
@Published var enabled: Bool {
|
||||
didSet {
|
||||
guard enabled != oldValue else { return }
|
||||
UserDefaults.standard.set(enabled, forKey: Self.defaultsKey)
|
||||
apply(enabled)
|
||||
}
|
||||
}
|
||||
|
||||
private var spacer: NSStatusItem?
|
||||
|
||||
private init() {}
|
||||
private init() {
|
||||
// Hide by default: the menu bar then shows only SimpleTopBar's hamburger
|
||||
// plus the Apple items macOS will not let us move.
|
||||
enabled = UserDefaults.standard.object(forKey: Self.defaultsKey) as? Bool ?? true
|
||||
}
|
||||
|
||||
/// Create the spacer early (at launch) so it lands as far right as possible.
|
||||
func prepare() {
|
||||
@@ -191,19 +265,35 @@ final class MenuBarHider: ObservableObject {
|
||||
item.autosaveName = name
|
||||
item.button?.isHidden = true
|
||||
spacer = item
|
||||
apply(enabled)
|
||||
}
|
||||
|
||||
private func apply(_ hiding: Bool) {
|
||||
prepare()
|
||||
spacer?.length = hiding ? Self.hiddenLength : 0
|
||||
isHiding = hiding
|
||||
}
|
||||
|
||||
/// Put the menu bar back the way we found it. Called on quit.
|
||||
/// Temporarily reveal the items (e.g. while the panel is open) so pressing
|
||||
/// a tray icon opens its menu at its real, on-screen position. Keeps the
|
||||
/// user's `enabled` preference untouched.
|
||||
func suspend() {
|
||||
guard enabled else { return }
|
||||
spacer?.length = 0
|
||||
isHiding = false
|
||||
}
|
||||
|
||||
/// Restore hiding after a `suspend()`.
|
||||
func resume() {
|
||||
guard enabled else { return }
|
||||
spacer?.length = Self.hiddenLength
|
||||
isHiding = true
|
||||
}
|
||||
|
||||
/// Put the menu bar back the way we found it. Called on quit. Leaves the
|
||||
/// user's preference intact.
|
||||
func restore() {
|
||||
spacer?.length = 0
|
||||
isHiding = false
|
||||
enabled = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,28 @@
|
||||
import AppKit
|
||||
import ApplicationServices
|
||||
import CoreGraphics
|
||||
|
||||
/// Diagnostic used by `open SimpleTopBar.app --args --diag-press`. Presses a
|
||||
/// hidden item's accessibility element without revealing it and dumps the
|
||||
/// resulting high-level windows, to see where its menu actually opens.
|
||||
func dumpWindows() {
|
||||
var lines: [String] = []
|
||||
if let list = CGWindowListCopyWindowInfo([], kCGNullWindowID) as? [[String: Any]] {
|
||||
for window in list {
|
||||
let layer = window[kCGWindowLayer as String] as? Int ?? -1
|
||||
guard layer > 20 else { continue }
|
||||
let owner = window[kCGWindowOwnerName as String] as? String ?? "?"
|
||||
let bounds = window[kCGWindowBounds as String] as? [String: Any] ?? [:]
|
||||
let x = bounds["X"] as? Double ?? 0
|
||||
let y = bounds["Y"] as? Double ?? 0
|
||||
let width = bounds["Width"] as? Double ?? 0
|
||||
let height = bounds["Height"] as? Double ?? 0
|
||||
lines.append("\(owner) layer=\(layer) frame=\(Int(x)),\(Int(y)) \(Int(width))x\(Int(height))")
|
||||
}
|
||||
}
|
||||
try? lines.joined(separator: "\n").write(toFile: "/tmp/simpletopbar-windows.txt", atomically: true, encoding: .utf8)
|
||||
exit(0)
|
||||
}
|
||||
|
||||
/// Diagnostic used by `open SimpleTopBar.app --args --dump-items`. Writes the
|
||||
/// live Accessibility view of the menu bar to /tmp so it can be inspected
|
||||
@@ -80,12 +103,20 @@ func runSelfTest() {
|
||||
check(MenuBarItemManager.children(of: systemWide) == nil, "no children on a non-app element")
|
||||
|
||||
let element = AXUIElementCreateSystemWide()
|
||||
let first = MenuBarItem(id: "1-0", pid: 1, appName: "A", title: "T", appIcon: NSImage(), element: element)
|
||||
let same = MenuBarItem(id: "1-0", pid: 1, appName: "A", title: "T", appIcon: NSImage(), element: element)
|
||||
let different = MenuBarItem(id: "1-1", pid: 1, appName: "A", title: "T", appIcon: NSImage(), element: element)
|
||||
let first = MenuBarItem(id: "1-0", pid: 1, appName: "A", title: "T", icon: NSImage(), element: element)
|
||||
let same = MenuBarItem(id: "1-0", pid: 1, appName: "A", title: "T", icon: NSImage(), element: element)
|
||||
let different = MenuBarItem(id: "1-1", pid: 1, appName: "A", title: "T", icon: NSImage(), element: element)
|
||||
check(first == same, "menu bar items compare by id and title")
|
||||
check(first != different, "different ids are not equal")
|
||||
|
||||
// Control Center items map to distinct symbols instead of one shared icon.
|
||||
check(MenuBarItemManager.symbolName(for: "Wi\u{2011}Fi, connected, 3 bars") == "wifi", "wi-fi maps to wifi symbol")
|
||||
check(MenuBarItemManager.symbolName(for: "Battery") == "battery.100", "battery maps to battery symbol")
|
||||
check(MenuBarItemManager.symbolName(for: "Now Playing") == "play.circle", "now playing maps to play symbol")
|
||||
check(MenuBarItemManager.symbolName(for: "Control Center") == "switch.2", "control center maps to switch symbol")
|
||||
check(MenuBarItemManager.symbolName(for: "Search") == "magnifyingglass", "search maps to magnifier symbol")
|
||||
check(MenuBarItemManager.symbolName(for: "cmux") == nil, "unknown titles fall back to the app icon")
|
||||
|
||||
let authorized = AXIsProcessTrusted()
|
||||
print("accessibility authorized=\(authorized)")
|
||||
if authorized {
|
||||
|
||||
@@ -245,8 +245,11 @@ private struct MenuBarTile: View {
|
||||
Button {
|
||||
menuBar.press(item)
|
||||
} label: {
|
||||
Image(nsImage: item.appIcon)
|
||||
// Preserve the symbol's aspect ratio: forcing wide symbols like the
|
||||
// battery into a square stretches them into a blocky shape.
|
||||
Image(nsImage: item.icon)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(width: 20, height: 20)
|
||||
.padding(6)
|
||||
.background(
|
||||
|
||||
Reference in New Issue
Block a user