import SwiftUI import AppKit import IOKit.ps import notify /// AppKit owns launch only; MenuBar owns windows, Core owns sampling and preferences. @main @MainActor struct kymoApp { static func main() { let application = NSApplication.shared let lifecycle = ApplicationLifecycle() application.delegate = lifecycle application.setActivationPolicy(.accessory) withExtendedLifetime(lifecycle) { application.run() } } } @MainActor private final class ApplicationLifecycle: NSObject, NSApplicationDelegate { private let metrics = MetricsStore() private let awake = KeepAwakeService() private let presentation = SettingsPresentation() private let appearance = AppearancePreferences() private let menuBarPreferences = MenuBarPreferences() private let language = LanguagePreferences() private var openSettingsAction: (() -> Void)? private var scenes: AnyObject? private var menuBar: MenuPanelController? private var powerSourceToken: Int32? private var workspaceTokens: [NSObjectProtocol] = [] func applicationWillFinishLaunching(_ notification: Notification) { var token: Int32 = 0 if notify_register_dispatch(kIOPSNotifyPowerSource, &token, .main, { [weak self] _ in MainActor.assumeIsolated { self?.metrics.powerSourceDidChange() } }) == NOTIFY_STATUS_OK { powerSourceToken = token } let center = NSWorkspace.shared.notificationCenter for (name, sleeping) in [(NSWorkspace.willSleepNotification, true), (NSWorkspace.didWakeNotification, false)] { workspaceTokens.append(center.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in MainActor.assumeIsolated { self?.metrics.setSystemSleeping(sleeping) } }) } for (name, sleeping) in [(NSWorkspace.screensDidSleepNotification, true), (NSWorkspace.screensDidWakeNotification, false)] { workspaceTokens.append(center.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in MainActor.assumeIsolated { self?.metrics.setDisplaySleeping(sleeping) } }) } let readings = { [metrics, menuBarPreferences, language] () -> [MenuBarReading] in let selected = menuBarPreferences.selected guard !selected.isEmpty else { return [] } let locale = language.locale return MenuBarReading.readings(selection: selected, snapshot: metrics.snapshot) { AppLocalization.string($0.titleKey, locale: locale) } } menuBarPreferences.onDemandChange = { [metrics] in metrics.setMenuBarEnabled($0) } metrics.setMenuBarEnabled(!menuBarPreferences.selected.isEmpty) let settings = NSHostingSceneRepresentation { Settings { AppLanguageScope(preferences: language, onChange: { self.configureMainMenu() }) { [self] in AppearanceSettingsView(preferences: appearance, language: language, sampling: metrics.sampling, menuBar: menuBarPreferences, readings: readings, presentation: presentation) } } } NSApp.addSceneRepresentation(settings) scenes = settings openSettingsAction = { settings.environment.openSettings() } configureMainMenu() metrics.setDetailDemand(processes: true, slowMetrics: true) menuBar = MenuPanelController(readings: readings, visibility: { [metrics] in metrics.setPanelVisible($0) }, menuTitle: { [language] key in AppLocalization.string("status." + key, locale: language.locale, table: "StatusItem") }, openSettings: { [weak self] in self?.settingsCommand() }) { [self] panel in AppLanguageScope(preferences: language) { MenuPanelView(metrics: self.metrics, preferences: self.appearance, keepAwake: self.awake, settingsPresentation: self.presentation, presentation: panel, onOpenSettings: { [weak self] in self?.menuBar?.hide() settings.environment.openSettings() }, onPreferredHeight: { [weak self] height in self?.menuBar?.setPreferredContentHeight(height) }) } } if let menuBar { PanelResourceAudit.run(controller: menuBar, metrics: metrics) } } private func configureMainMenu() { let root = NSMenu() let application = NSMenu() let appItem = NSMenuItem(); appItem.submenu = application; root.addItem(appItem) let settings = NSMenuItem(title: AppLocalization.string("action.settings", locale: language.locale), action: #selector(settingsCommand), keyEquivalent: ",") settings.target = self application.addItem(settings) application.addItem(.separator()) let quit = NSMenuItem(title: AppLocalization.string("action.quit", locale: language.locale), action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q") quit.target = NSApp; application.addItem(quit) let file = NSMenu() let fileItem = NSMenuItem(title: AppLocalization.string("action.file", locale: language.locale), action: nil, keyEquivalent: "") fileItem.submenu = file; root.addItem(fileItem) file.addItem(NSMenuItem(title: AppLocalization.string("action.closeWindow", locale: language.locale), action: #selector(NSWindow.performClose(_:)), keyEquivalent: "w")) let edit = NSMenu() let editItem = NSMenuItem(title: AppLocalization.string("action.edit", locale: language.locale), action: nil, keyEquivalent: "") editItem.submenu = edit; root.addItem(editItem) for (key, action, shortcut) in [("undo", "undo:", "z"), ("redo", "redo:", "Z"), ("cut", "cut:", "x"), ("copy", "copy:", "c"), ("paste", "paste:", "v"), ("selectAll", "selectAll:", "a")] { edit.addItem(NSMenuItem(title: AppLocalization.string("action." + key, locale: language.locale), action: Selector(action), keyEquivalent: shortcut)) } NSApp.mainMenu = root } @objc private func settingsCommand() { NSApp.setActivationPolicy(.regular) presentation.request() menuBar?.hide() openSettingsAction?() } func applicationWillTerminate(_ notification: Notification) { if let powerSourceToken { notify_cancel(powerSourceToken) } workspaceTokens.forEach(NSWorkspace.shared.notificationCenter.removeObserver) workspaceTokens.removeAll() menuBar?.shutdown() awake.disable() menuBar = nil openSettingsAction = nil scenes = nil } // Finder or Spotlight remains a settings entry point when macOS hides a wide status item. func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { if !flag { settingsCommand() } return true } func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false } }