Files
simpletopbar/Sources/SimpleTopBar/App.swift
T
TZGyn 83cb12eddc Request Accessibility at launch so the tray can populate
The Menu Bar section silently stayed empty because nothing prompted for
Accessibility permission until a button inside the panel was clicked, so
the app looked inert. Prompt once on first launch instead.

💘 Generated with Crush

Assisted-by: Crush:deepseek-v4.1-flash
2026-09-26 23:39:20 +08:00

76 lines
2.3 KiB
Swift

import SwiftUI
import AppKit
import ServiceManagement
@main
struct SimpleTopBarApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
var body: some Scene {
MenuBarExtra("SimpleTopBar", systemImage: "line.3.horizontal") {
TopBarView(
model: AppListModel.shared,
menuBar: MenuBarItemManager.shared,
hider: MenuBarHider.shared
)
}
.menuBarExtraStyle(.window)
}
}
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
if CommandLine.arguments.contains("--self-test") {
runSelfTest()
return
}
// Single instance: stale copies keep a menu bar icon around after an
// update and swallow clicks meant for the new instance.
let myPid = ProcessInfo.processInfo.processIdentifier
let others = NSRunningApplication.runningApplications(withBundleIdentifier: "dev.tzgyn.simpletopbar")
.filter { $0.processIdentifier != myPid }
for other in others {
other.forceTerminate()
}
AppListModel.shared.start()
MenuBarItemManager.shared.start()
MenuBarHider.shared.prepare()
// The tray is empty without Accessibility permission, so surface the
// system prompt once at launch instead of waiting for the user to
// open the panel and find the button.
if !AccessibilityPermission.isGranted,
!UserDefaults.standard.bool(forKey: "didRequestAccessibility") {
UserDefaults.standard.set(true, forKey: "didRequestAccessibility")
AccessibilityPermission.request()
}
}
func applicationWillTerminate(_ notification: Notification) {
// Never leave the menu bar resized behind us.
MenuBarHider.shared.restore()
}
}
enum LoginItem {
static var isEnabled: Bool {
SMAppService.mainApp.status == .enabled
}
static func toggle() -> Bool {
let service = SMAppService.mainApp
do {
if service.status == .enabled {
try service.unregister()
} else {
try service.register()
}
return true
} catch {
return false
}
}
}