EPOD Window Bar: a window strip above the macOS Dock
EPOD Window Bar is a macOS agent app. It shows a thin strip above the Dock with one tab per window of the frontmost application. One click raises the window. Build from source with XcodeGen and Xcode. Signing uses Config.xcconfig (ad-hoc by default; a local Local.xcconfig can set a team ID). The app needs Accessibility and Screen Recording permissions, and runs in a reduced mode without Screen Recording. See README.md and docs/ for details.
This commit is contained in:
160
Sources/App/MenuBarController.swift
Normal file
160
Sources/App/MenuBarController.swift
Normal file
@@ -0,0 +1,160 @@
|
||||
import AppKit
|
||||
|
||||
// MenuBarController owns the NSStatusItem and the menu (spec section 7.12).
|
||||
// The menu refreshes its checkmarks each time the user opens the menu. The
|
||||
// button shows a warning symbol if a permission is missing.
|
||||
@MainActor
|
||||
final class MenuBarController: NSObject, NSMenuDelegate {
|
||||
|
||||
private let statusItem: NSStatusItem
|
||||
private let permissions: PermissionManager
|
||||
private weak var control: AppControl?
|
||||
private let settings: Settings
|
||||
|
||||
private let menu = NSMenu()
|
||||
private let pauseItem = NSMenuItem()
|
||||
private let constrainItem = NSMenuItem()
|
||||
private let launchItem = NSMenuItem()
|
||||
private let permissionsItem = NSMenuItem()
|
||||
|
||||
private var lastState: PermissionState
|
||||
|
||||
init(permissions: PermissionManager, control: AppControl, settings: Settings) {
|
||||
self.permissions = permissions
|
||||
self.control = control
|
||||
self.settings = settings
|
||||
self.lastState = permissions.current
|
||||
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
||||
super.init()
|
||||
configureButton()
|
||||
buildMenu()
|
||||
updatePermissionState(permissions.current)
|
||||
}
|
||||
|
||||
private func configureButton() {
|
||||
guard let button = statusItem.button else { return }
|
||||
button.image = NSImage(systemSymbolName: "rectangle.split.3x1",
|
||||
accessibilityDescription: "EPOD Window Bar")
|
||||
button.image?.isTemplate = true
|
||||
}
|
||||
|
||||
private func buildMenu() {
|
||||
menu.delegate = self
|
||||
|
||||
pauseItem.title = "Pause EPOD Window Bar"
|
||||
pauseItem.action = #selector(togglePause)
|
||||
pauseItem.target = self
|
||||
menu.addItem(pauseItem)
|
||||
|
||||
constrainItem.title = "Keep windows above strip"
|
||||
constrainItem.action = #selector(toggleConstrain)
|
||||
constrainItem.target = self
|
||||
menu.addItem(constrainItem)
|
||||
|
||||
menu.addItem(.separator())
|
||||
|
||||
let excluded = NSMenuItem(title: "Excluded apps…",
|
||||
action: #selector(openSettings),
|
||||
keyEquivalent: "")
|
||||
excluded.target = self
|
||||
menu.addItem(excluded)
|
||||
|
||||
launchItem.title = "Launch at login"
|
||||
launchItem.action = #selector(toggleLaunchAtLogin)
|
||||
launchItem.target = self
|
||||
menu.addItem(launchItem)
|
||||
|
||||
let dump = NSMenuItem(title: "Dump windows to log",
|
||||
action: #selector(dumpWindows),
|
||||
keyEquivalent: "")
|
||||
dump.target = self
|
||||
menu.addItem(dump)
|
||||
|
||||
let copyDump = NSMenuItem(title: "Copy window dump",
|
||||
action: #selector(copyWindowDump),
|
||||
keyEquivalent: "")
|
||||
copyDump.target = self
|
||||
menu.addItem(copyDump)
|
||||
|
||||
menu.addItem(.separator())
|
||||
|
||||
permissionsItem.title = "Permissions: unknown"
|
||||
permissionsItem.isEnabled = false
|
||||
menu.addItem(permissionsItem)
|
||||
|
||||
menu.addItem(.separator())
|
||||
|
||||
let quit = NSMenuItem(title: "Quit EPOD Window Bar",
|
||||
action: #selector(quit),
|
||||
keyEquivalent: "q")
|
||||
quit.target = self
|
||||
menu.addItem(quit)
|
||||
|
||||
statusItem.menu = menu
|
||||
}
|
||||
|
||||
// updatePermissionState sets the status text and the button symbol.
|
||||
// The button shows a warning symbol if a permission is missing.
|
||||
func updatePermissionState(_ state: PermissionState) {
|
||||
lastState = state
|
||||
|
||||
let text: String
|
||||
if state.allGranted {
|
||||
text = "Permissions: all granted"
|
||||
} else if state.accessibility {
|
||||
text = "Permissions: Screen Recording missing (reduced mode)"
|
||||
} else {
|
||||
text = "Permissions: Accessibility missing"
|
||||
}
|
||||
permissionsItem.title = text
|
||||
|
||||
guard let button = statusItem.button else { return }
|
||||
let symbol = state.allGranted ? "rectangle.split.3x1" : "exclamationmark.triangle"
|
||||
button.image = NSImage(systemSymbolName: symbol,
|
||||
accessibilityDescription: "EPOD Window Bar")
|
||||
button.image?.isTemplate = true
|
||||
}
|
||||
|
||||
// refreshState sets the checkmarks from the current app state. The menu
|
||||
// calls this function each time the user opens the menu.
|
||||
private func refreshState() {
|
||||
pauseItem.state = (control?.isPaused ?? false) ? .on : .off
|
||||
constrainItem.state = (control?.constrainWindows ?? true) ? .on : .off
|
||||
launchItem.state = LaunchAtLogin.isEnabled ? .on : .off
|
||||
updatePermissionState(permissions.current)
|
||||
}
|
||||
|
||||
// menuNeedsUpdate refreshes the menu state before macOS shows the menu.
|
||||
func menuNeedsUpdate(_ menu: NSMenu) {
|
||||
refreshState()
|
||||
}
|
||||
|
||||
@objc private func togglePause() {
|
||||
control?.togglePause()
|
||||
}
|
||||
|
||||
@objc private func toggleConstrain() {
|
||||
let now = control?.constrainWindows ?? true
|
||||
control?.setConstrainWindows(!now)
|
||||
}
|
||||
|
||||
@objc private func openSettings() {
|
||||
control?.openSettings()
|
||||
}
|
||||
|
||||
@objc private func toggleLaunchAtLogin() {
|
||||
LaunchAtLogin.setEnabled(!LaunchAtLogin.isEnabled)
|
||||
}
|
||||
|
||||
@objc private func dumpWindows() {
|
||||
control?.dumpWindowsToLog()
|
||||
}
|
||||
|
||||
@objc private func copyWindowDump() {
|
||||
control?.copyWindowDump()
|
||||
}
|
||||
|
||||
@objc private func quit() {
|
||||
NSApp.terminate(nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user