SPB Git

Repository Public

AppDelegate.swift 3.2 KB 61 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/AppDelegate.swift3// Description: Application delegate: opened files, Dock menu, wake from sleep, orderly termination.4import AppKit5import SwiftUI6import SPMusicCore78final class AppDelegate: NSObject, NSApplicationDelegate {9    var model: AppModel?10    private var pendingFiles: [URL] = []1112    func applicationDidFinishLaunching(_ notification: Notification) {13        NSWindow.allowsAutomaticWindowTabbing = false14        Notifier.requestAuthorization()15        // After sleep the output device may have changed format; the engine handles the notification, the session is re-checked.16        NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in17            Task { @MainActor in18                guard let model = self?.model else { return }19                model.refreshOutputs()20                if model.connectivity.isOnline { await model.syncNow() }21            }22        }23    }2425    /// Files opened from the Finder (Open With, double click, drop on the Dock icon) are played without being imported.26    func application(_ application: NSApplication, open urls: [URL]) {27        if let model, model.phase != .launching { model.openFiles(urls) } else { pendingFiles += urls }28    }29    func flushPendingFiles() { if !pendingFiles.isEmpty { model?.openFiles(pendingFiles); pendingFiles = [] } }3031    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false }3233    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {34        if !flag { for w in sender.windows where w.canBecomeMain { w.makeKeyAndOrderFront(nil); return false } }35        return true36    }3738    /// Dock menu: the transport, without bringing the app forward.39    func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {40        guard let model, model.playback.currentTrack != nil else { return nil }41        let menu = NSMenu()42        let title = NSMenuItem(title: "\(model.playback.currentTrack?.title ?? "") — \(model.playback.currentTrack?.artistName ?? "")", action: nil, keyEquivalent: "")43        title.isEnabled = false44        menu.addItem(title); menu.addItem(.separator())45        for (label, action) in [(model.playback.isPlaying ? "Pause" : "Lecture", #selector(dockToggle)), ("Suivant", #selector(dockNext)), ("Précédent", #selector(dockPrevious))] {46            let item = NSMenuItem(title: label, action: action, keyEquivalent: ""); item.target = self; menu.addItem(item)47        }48        return menu49    }50    @objc private func dockToggle() { model?.playback.togglePlay() }51    @objc private func dockNext() { model?.playback.next() }52    @objc private func dockPrevious() { model?.playback.previous() }5354    func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {55        // Let the database record where downloads stand; partial files are resumed at the next launch.56        guard let session = model?.session else { return .terminateNow }57        Task { await session.downloads.stop(); try? await Task.sleep(nanoseconds: 150_000_000); NSApp.reply(toApplicationShouldTerminate: true) }58        return .terminateLater59    }60}61
Files main
Menu

Theme