Voxley / app · voxley.si / Sources / Voxley / VoxleyApp.swift
| 1 | import AppKit |
| 2 | import SwiftUI |
| 3 | |
| 4 | @main |
| 5 | @MainActor |
| 6 | struct VoxleyApp: App { |
| 7 | @NSApplicationDelegateAdaptor(VoxleyDelegate.self) private var delegate |
| 8 | |
| 9 | var body: some Scene { |
| 10 | MenuBarExtra("Voxley", systemImage: menuIcon) { |
| 11 | MenuCommands(controller: delegate.controller, openOnboarding: delegate.openOnboarding, |
| 12 | openAbout: delegate.openAbout, openRefine: delegate.openRefine) |
| 13 | } |
| 14 | |
| 15 | Settings { |
| 16 | VoxleySettings(controller: delegate.controller, launchAtLogin: delegate.launchAtLogin, |
| 17 | openOnboarding: delegate.openOnboarding, openAbout: delegate.openAbout) |
| 18 | } |
| 19 | .commands { |
| 20 | CommandGroup(replacing: .appInfo) { |
| 21 | Button("About Voxley", action: delegate.openAbout) |
| 22 | } |
| 23 | CommandGroup(after: .appInfo) { |
| 24 | Button("Voxley Onboarding…", action: delegate.openOnboarding) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | private var menuIcon: String { |
| 30 | if delegate.controller.phase == .recording { return "mic.fill" } |
| 31 | return delegate.controller.pasteNeedsAttention ? "exclamationmark.circle" : "waveform" |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | @MainActor |
| 36 | final class VoxleyDelegate: NSObject, NSApplicationDelegate { |
| 37 | let controller = DictationController() |
| 38 | let launchAtLogin = LaunchAtLogin() |
| 39 | private let recorder = RecorderPanel() |
| 40 | private let refine = RefineWindow() |
| 41 | private let setup = SetupWindow() |
| 42 | private let about = AboutWindow() |
| 43 | |
| 44 | func applicationWillFinishLaunching(_ notification: Notification) { |
| 45 | NSApp.appearance = NSAppearance(named: .darkAqua) |
| 46 | } |
| 47 | |
| 48 | func applicationDidFinishLaunching(_ notification: Notification) { |
| 49 | launchAtLogin.configureDefault() |
| 50 | controller.activate() |
| 51 | recorder.connect(controller) |
| 52 | controller.onRefineReady = { [unowned self] text, target in |
| 53 | refine.present(text, from: target, locale: controller.preferences.locale) |
| 54 | } |
| 55 | if CommandLine.arguments.contains("--refine-preview") { |
| 56 | refine.openEmpty(locale: controller.preferences.locale) |
| 57 | } |
| 58 | if CommandLine.arguments.contains("--about") { openAbout() } |
| 59 | #if VOXLEY_SPARKLE |
| 60 | VoxleyUpdates.shared.start(dictation: controller) |
| 61 | #endif |
| 62 | if CommandLine.arguments.contains("--onboarding") || |
| 63 | !UserDefaults.standard.bool(forKey: "didCompleteVoxleySetup") { |
| 64 | openOnboarding() |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func applicationDidBecomeActive(_ notification: Notification) { |
| 69 | launchAtLogin.refresh() |
| 70 | controller.refreshPermissions() |
| 71 | } |
| 72 | |
| 73 | func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { |
| 74 | openOnboarding() |
| 75 | return true |
| 76 | } |
| 77 | |
| 78 | func openOnboarding() { |
| 79 | // Let the menu finish closing before asking AppKit to activate the window. |
| 80 | DispatchQueue.main.async { self.setup.show(self.controller) } |
| 81 | } |
| 82 | |
| 83 | func openRefine() { |
| 84 | DispatchQueue.main.async { self.refine.openEmpty(locale: self.controller.preferences.locale) } |
| 85 | } |
| 86 | |
| 87 | func openAbout() { DispatchQueue.main.async { self.about.show() } } |
| 88 | } |
| 89 | |
| 90 | private struct MenuCommands: View { |
| 91 | let controller: DictationController |
| 92 | let openOnboarding: () -> Void |
| 93 | let openAbout: () -> Void |
| 94 | let openRefine: () -> Void |
| 95 | @Environment(\.openSettings) private var openSettings |
| 96 | |
| 97 | var body: some View { |
| 98 | Button("Voxley Onboarding…", action: openOnboarding) |
| 99 | Divider() |
| 100 | Text("Tap or hold \(controller.preferences.shortcutKey.label) to dictate") |
| 101 | if controller.preferences.refineEnabled { |
| 102 | Text(controller.refineShortcutReady |
| 103 | ? "Tap or hold \(controller.preferences.refineShortcutKey.label) to dictate and refine" |
| 104 | : "Refine shortcut unavailable. Choose another in Settings.") |
| 105 | } |
| 106 | Button("Open Refine Window…", action: openRefine) |
| 107 | |
| 108 | if controller.phase.isCapturing { |
| 109 | Button("Cancel Dictation") { controller.cancel() } |
| 110 | } |
| 111 | if let transcript = controller.mostRecentTranscript { |
| 112 | Button("Copy Last Transcript") { Destination.copy(transcript.text) } |
| 113 | } |
| 114 | if let message = controller.message { |
| 115 | Text(message) |
| 116 | } |
| 117 | if controller.pasteNeedsAttention, !controller.accessibilityReady { |
| 118 | Button("Enable Automatic Paste…") { controller.allowAccessibility() } |
| 119 | } |
| 120 | if !controller.shortcutReady { |
| 121 | Text("Choose an available shortcut in Voxley Onboarding.") |
| 122 | } |
| 123 | Divider() |
| 124 | Button("Settings…") { |
| 125 | NSApp.activate() |
| 126 | openSettings() |
| 127 | } |
| 128 | .keyboardShortcut(",") |
| 129 | |
| 130 | #if VOXLEY_SPARKLE |
| 131 | Button("Check for Updates…", action: VoxleyUpdates.shared.check) |
| 132 | .disabled(!VoxleyUpdates.shared.canCheck || controller.phase != .idle) |
| 133 | #endif |
| 134 | Button("About Voxley", action: openAbout) |
| 135 | Button("Quit Voxley") { NSApp.terminate(nil) } |
| 136 | .keyboardShortcut("q") |
| 137 | } |
| 138 | } |