78 lines
2.7 KiB
Swift
78 lines
2.7 KiB
Swift
import Cocoa
|
||
import SwiftUI
|
||
import UniformTypeIdentifiers
|
||
|
||
class AppDelegate: NSObject, NSApplicationDelegate {
|
||
func applicationDidFinishLaunching(_ notification: Notification) {
|
||
updateActivationPolicy()
|
||
cleanupLegacyUserDefaults()
|
||
}
|
||
|
||
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
||
updateActivationPolicy()
|
||
|
||
if !flag {
|
||
createMainWindowIfNeeded()
|
||
}
|
||
return true
|
||
}
|
||
|
||
func applicationDidBecomeActive(_ notification: Notification) {
|
||
updateActivationPolicy()
|
||
}
|
||
|
||
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
||
return false
|
||
}
|
||
|
||
private func updateActivationPolicy() {
|
||
let isMenuBarOnly = UserDefaults.standard.bool(forKey: "IsMenuBarOnly")
|
||
if isMenuBarOnly {
|
||
NSApp.setActivationPolicy(.accessory)
|
||
} else {
|
||
NSApp.setActivationPolicy(.regular)
|
||
}
|
||
}
|
||
|
||
private func createMainWindowIfNeeded() {
|
||
if NSApp.windows.isEmpty {
|
||
let contentView = ContentView()
|
||
let hostingView = NSHostingView(rootView: contentView)
|
||
let window = WindowManager.shared.createMainWindow(contentView: hostingView)
|
||
window.makeKeyAndOrderFront(nil)
|
||
} else {
|
||
NSApp.windows.first?.makeKeyAndOrderFront(nil)
|
||
}
|
||
}
|
||
|
||
private func cleanupLegacyUserDefaults() {
|
||
let defaults = UserDefaults.standard
|
||
defaults.removeObject(forKey: "defaultPowerModeConfigV2")
|
||
defaults.removeObject(forKey: "isPowerModeEnabled")
|
||
}
|
||
|
||
// 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
|
||
}
|
||
|
||
NSApp.activate(ignoringOtherApps: true)
|
||
|
||
if NSApp.windows.isEmpty {
|
||
// Cold start: do NOT create a window here to avoid extra window/tab.
|
||
// Defer to SwiftUI’s WindowGroup-created ContentView and let it process this later.
|
||
pendingOpenFileURL = url
|
||
} else {
|
||
// Running: focus current window and route in-place to Transcribe Audio
|
||
NSApp.windows.first?.makeKeyAndOrderFront(nil)
|
||
NotificationCenter.default.post(name: .navigateToDestination, object: nil, userInfo: ["destination": "Transcribe Audio"])
|
||
DispatchQueue.main.async {
|
||
NotificationCenter.default.post(name: .openFileForTranscription, object: nil, userInfo: ["url": url])
|
||
}
|
||
}
|
||
}
|
||
}
|