Repository Public License
1import AppKit2import NavigatorCore3import NavigatorFeatures4import NavigatorUI5import SwiftUI67@main8struct NavigatorMacApp: App {9 @NSApplicationDelegateAdaptor(AppDelegate.self) private var delegate1011 init() {12 // Features plug into the shell before the first menu or window is built.13 NavigatorFeatures.registerAll(in: AppEnvironment.shared.registry)14 }1516 var body: some Scene {17 WindowGroup(id: "browser", for: WindowID.self) { $windowID in18 NavigatorSceneView(windowID: $windowID)19 .frame(minWidth: 520, minHeight: 420)20 }21 .windowStyle(.hiddenTitleBar)22 .defaultSize(width: 1280, height: 820)23 // The session is restored from the app's own database, not from AppKit's window restoration.24 .restorationBehavior(.disabled)25 // Links opened from other apps go to the active window (see AppDelegate), never to a new scene.26 .handlesExternalEvents(matching: [])27 .commands { BrowserCommands() }28 }29}3031@MainActor32final class AppDelegate: NSObject, NSApplicationDelegate {33 private var isReadyToTerminate = false34 private var terminationSource: (any DispatchSourceSignal)?35 func applicationWillFinishLaunching(_ notification: Notification) {36 NSWindow.allowsAutomaticWindowTabbing = false37 let environment = AppEnvironment.shared38 environment.configureStore = { store in39 environment.macSupport = MacPlatformSupport(store: store)40 }41 environment.start()42 // `kill <pid>` (scripts, logout tools) quits like the Quit command: the session is saved and marked clean.43 signal(SIGTERM, SIG_IGN)44 let source = DispatchSource.makeSignalSource(signal: SIGTERM, queue: .main)45 // Not from inside the handler: it runs while the main queue is being drained, and the loop AppKit spins46 // while waiting for the session to be saved could then not deliver main-actor work (the queue is not47 // re-entrant). A run-loop block runs outside the drain.48 source.setEventHandler { RunLoop.main.perform { MainActor.assumeIsolated { NSApp.terminate(nil) } } }49 source.resume()50 terminationSource = source51 }5253 func application(_ application: NSApplication, open urls: [URL]) {54 AppEnvironment.shared.open(urls: urls)55 }5657 func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {58 guard !isReadyToTerminate, AppEnvironment.shared.store != nil else { return .terminateNow }59 AppEnvironment.shared.store?.isTerminating = true60 // The session is written by actors: answer once it is on disk and marked clean.61 Task {62 await AppEnvironment.shared.suspend()63 isReadyToTerminate = true64 sender.reply(toApplicationShouldTerminate: true)65 }66 return .terminateLater67 }68}69