SPB Git

Repository Public

AppDelegate.swift 3.1 KB 45 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/AppDelegate.swift3// Description: Application lifecycle: SP Drive keeps syncing from the menu bar when its window is closed, resumes after sleep, and re-locks after a period away when the biometric lock is on.4import AppKit5import Quartz67@MainActor8final class AppDelegate: NSObject, NSApplicationDelegate {9    weak var model: AppModel?10    /// Opens the main window (set by the views, which own SwiftUI's window actions).11    static var openMainWindow: (() -> Void)?12    /// Opens the "Demander à mon Drive" window.13    static var openAskWindow: (() -> Void)?14    /// Opens the Settings window (SwiftUI's own action, captured by a view).15    static var openSettings: (() -> Void)?16    private var hasMainWindow: Bool { NSApp.windows.contains { $0.isVisible && $0.canBecomeMain } }17    /// Brings a main window forward, opening one when none is left.18    static func showMainWindow() {19        if let w = NSApp.windows.first(where: { $0.isVisible && $0.canBecomeMain }) { w.makeKeyAndOrderFront(nil) } else { openMainWindow?() }20        NSApp.activate(ignoringOtherApps: true)21    }2223    // The Quick Look panel looks for its controller along the responder chain, which ends here.24    override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool { true }25    override func beginPreviewPanelControl(_ panel: QLPreviewPanel!) { MainActor.assumeIsolated { QuickLookCoordinator.shared.adopt(panel) } }26    override func endPreviewPanelControl(_ panel: QLPreviewPanel!) { MainActor.assumeIsolated { QuickLookCoordinator.shared.release(panel) } }2728    func applicationDidFinishLaunching(_ notification: Notification) {29        NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in30            Task { @MainActor in await self?.model?.engine?.transfers.kick(); await self?.model?.syncNow() } // back from sleep: look at the server now31        }32        // a launch by the person shows the window even when the system restored none; a launch at login stays in the menu bar33        let atLogin = NSAppleEventManager.shared().currentAppleEvent?.paramDescriptor(forKeyword: keyAEPropData)?.enumCodeValue == keyAELaunchedAsLogInItem34        if !atLogin { DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) { [weak self] in if self?.hasMainWindow == false { AppDelegate.openMainWindow?() } } }35    }36    /// Closing the window does not stop syncing: the menu bar item stays (unless the person hid it, then the app quits with its last window).37    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { !(model?.settings.showMenuBarIcon ?? true) }38    func applicationDidBecomeActive(_ notification: Notification) { model?.appBecameActive() }39    func applicationDidResignActive(_ notification: Notification) { model?.appResignedActive() }40    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {41        if !hasMainWindow, let open = AppDelegate.openMainWindow { open(); return false }42        return true43    }44}45
Files main
Menu

Theme