vOOice/VoiceInk/AppDelegate.swift

49 lines
1.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Cocoa
import SwiftUI
import UniformTypeIdentifiers
class AppDelegate: NSObject, NSApplicationDelegate {
weak var menuBarManager: MenuBarManager?
func applicationDidFinishLaunching(_ notification: Notification) {
menuBarManager?.applyActivationPolicy()
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag, let menuBarManager = menuBarManager, !menuBarManager.isMenuBarOnly {
if WindowManager.shared.showMainWindow() != nil {
return false
}
}
return true
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return false
}
// Stash URL when app cold-starts to avoid spawning a new window/tab
var pendingOpenFileURL: URL?
func application(_ application: NSApplication, open urls: [URL]) {
guard let url = urls.first(where: { SupportedMedia.isSupported(url: $0) }) else {
return
}
NSApplication.shared.activate(ignoringOtherApps: true)
if WindowManager.shared.currentMainWindow() == nil {
// Cold start: do NOT create a window here to avoid extra window/tab.
// Defer to SwiftUIs WindowGroup-created ContentView and let it process this later.
pendingOpenFileURL = url
} else {
// Running: focus current window and route in-place to Transcribe Audio
menuBarManager?.focusMainWindow()
NotificationCenter.default.post(name: .navigateToDestination, object: nil, userInfo: ["destination": "Transcribe Audio"])
DispatchQueue.main.async {
NotificationCenter.default.post(name: .openFileForTranscription, object: nil, userInfo: ["url": url])
}
}
}
}