112 lines
5.8 KiB
Swift
112 lines
5.8 KiB
Swift
import SwiftUI
|
|
import AppKit
|
|
|
|
/// 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(energyEnabled: PanelResourceAudit.omitted != "energy")
|
|
private let awake = KeepAwakeService()
|
|
private let presentation = SettingsPresentation()
|
|
private let appearance = AppearancePreferences()
|
|
private let language = LanguagePreferences()
|
|
private var openSettingsAction: (() -> Void)?
|
|
private var scenes: AnyObject?
|
|
private var menuBar: MenuPanelController?
|
|
private var workspaceTokens: [NSObjectProtocol] = []
|
|
|
|
func applicationWillFinishLaunching(_ notification: Notification) {
|
|
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 settings = NSHostingSceneRepresentation {
|
|
Settings {
|
|
AppLanguageScope(preferences: language, onChange: { self.configureMainMenu() }) { [self] in
|
|
AppearanceSettingsView(preferences: appearance, language: language, sampling: metrics.sampling, presentation: presentation)
|
|
}
|
|
}
|
|
}
|
|
NSApp.addSceneRepresentation(settings)
|
|
scenes = settings
|
|
openSettingsAction = { settings.environment.openSettings() }
|
|
configureMainMenu()
|
|
menuBar = MenuPanelController(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
|
|
if PanelResourceAudit.omitted == "text" {
|
|
TextMonitoringAuditView(metrics: self.metrics, appearance: self.appearance)
|
|
} else if PanelResourceAudit.omitted == "all" {
|
|
Text(verbatim: "Kymo").frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
} else {
|
|
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()
|
|
})
|
|
}
|
|
}
|
|
}
|
|
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) {
|
|
workspaceTokens.forEach(NSWorkspace.shared.notificationCenter.removeObserver)
|
|
workspaceTokens.removeAll()
|
|
menuBar?.shutdown()
|
|
awake.disable()
|
|
menuBar = nil
|
|
openSettingsAction = nil
|
|
scenes = nil
|
|
}
|
|
|
|
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false }
|
|
}
|