Files
helder/Sources/HelderUI/Shell/AppDelegate.swift
T
2026-10-10 14:54:39 +02:00

66 lines
2.8 KiB
Swift

import AppKit
import HelderServices
/// App-level setup that SwiftUI has no hook for.
@MainActor
public final class AppDelegate: NSObject, NSApplicationDelegate {
public override init() {
super.init()
}
public func applicationWillFinishLaunching(_ notification: Notification) {
Log.setSink(FileLogger(directory: FileLogger.defaultDirectory))
FontRegistry.registerBundledFonts()
// `swift run` starts a bare executable, which macOS treats as a background tool without these two.
NSApp.setActivationPolicy(.regular)
NSApp.appearance = NSAppearance(named: .darkAqua)
// Every window is one project; tabs would put two projects in one window.
NSWindow.allowsAutomaticWindowTabbing = false
// One window per process: a quit must not leave windows for the next launch to reopen.
// Set in the app's domain, which wins over the system-wide "Close windows when quitting" choice.
UserDefaults.standard.set(false, forKey: "NSQuitAlwaysKeepsWindows")
// The View menu carries its own full-screen item; AppKit's automatic one would be a duplicate.
UserDefaults.standard.set(false, forKey: "NSFullScreenMenuItemEverywhere")
}
public func applicationDidFinishLaunching(_ notification: Notification) {
PointerPolicy.installCursorUpdateMonitor()
NSApp.activate(ignoringOtherApps: true)
}
public func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
false
}
/// ⌘Q asks about unsaved files in every window first, and writes each note and session before quitting.
public func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
let models = WorkspaceRegistry.models
guard !models.isEmpty else { return .terminateNow }
Task { @MainActor in
for model in models where !(await model.prepareToClose()) {
sender.reply(toApplicationShouldTerminate: false)
return
}
for model in models { await model.closeProject() }
sender.reply(toApplicationShouldTerminate: true)
}
return .terminateLater
}
/// The Dock's right-click menu: the project's name, then New Window (a new process).
public func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
let menu = NSMenu()
menu.autoenablesItems = false
if let model = WorkspaceRegistry.current, model.root != nil {
menu.addItem(withTitle: model.title, action: nil, keyEquivalent: "").isEnabled = false
menu.addItem(.separator())
}
menu.addItem(withTitle: "New Window", action: #selector(newWindow), keyEquivalent: "").target = self
return menu
}
@objc private func newWindow() {
LiveSystemActions().openNewInstance(nil)
}
}