Voxley / app · voxley.si / Sources / Voxley / VoxleySettings.swift
| 1 | import AppKit |
| 2 | import Carbon |
| 3 | import SwiftUI |
| 4 | import UniformTypeIdentifiers |
| 5 | |
| 6 | struct VoxleySettings: View { |
| 7 | @Bindable var controller: DictationController |
| 8 | let launchAtLogin: LaunchAtLogin |
| 9 | let openOnboarding: () -> Void |
| 10 | let openAbout: () -> Void |
| 11 | @State private var ruleApp = "" |
| 12 | @State private var ruleMode = WritingMode.clean |
| 13 | |
| 14 | var body: some View { |
| 15 | VStack(spacing: 0) { |
| 16 | TabView { |
| 17 | settings.tabItem { Label("General", systemImage: "gearshape") } |
| 18 | modes.tabItem { Label("Modes", systemImage: "slider.horizontal.3") } |
| 19 | dictionary.tabItem { Label("Dictionary", systemImage: "text.book.closed") } |
| 20 | VoxleyAbout().tabItem { Label("About", systemImage: "info.circle") } |
| 21 | } |
| 22 | if let message = controller.message { |
| 23 | Text(message) |
| 24 | .font(.callout) |
| 25 | .foregroundStyle(.secondary) |
| 26 | .textSelection(.enabled) |
| 27 | .padding(12) |
| 28 | } |
| 29 | } |
| 30 | .frame(width: 590, height: 450) |
| 31 | .background(.black) |
| 32 | .tint(.voxleyAccent) |
| 33 | .onAppear { |
| 34 | launchAtLogin.refresh() |
| 35 | controller.refreshPermissions() |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | private var modes: some View { |
| 40 | Form { |
| 41 | SwiftUI.Section("Default mode") { |
| 42 | Picker("Writing mode", selection: $controller.preferences.mode) { |
| 43 | ForEach(WritingMode.allCases) { mode in Text(mode.rawValue).tag(mode) } |
| 44 | } |
| 45 | Text(controller.preferences.mode == .verbatim |
| 46 | ? "Keep Apple Speech’s output without an AI rewrite." |
| 47 | : controller.preferences.mode.instruction) |
| 48 | .foregroundStyle(.secondary) |
| 49 | } |
| 50 | SwiftUI.Section("Modes by app") { |
| 51 | Text("Set a different mode for apps you dictate into. These rules use an app’s bundle identifier.") |
| 52 | .foregroundStyle(.secondary) |
| 53 | HStack { |
| 54 | TextField("e.g. com.apple.mail", text: $ruleApp) |
| 55 | .accessibilityLabel("App bundle identifier") |
| 56 | Picker("Mode", selection: $ruleMode) { |
| 57 | ForEach(WritingMode.allCases) { Text($0.rawValue).tag($0) } |
| 58 | }.labelsHidden() |
| 59 | Button("Choose App…") { chooseApp() } |
| 60 | Button("Add") { |
| 61 | controller.preferences.appModes[ruleApp.trimmingCharacters(in: .whitespacesAndNewlines)] = ruleMode |
| 62 | ruleApp = "" |
| 63 | }.disabled(ruleApp.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) |
| 64 | } |
| 65 | ForEach(controller.preferences.appModes.keys.sorted(), id: \.self) { id in |
| 66 | LabeledContent(id) { |
| 67 | Text(controller.preferences.appModes[id]?.rawValue ?? "") |
| 68 | Button("Remove", systemImage: "minus.circle") { |
| 69 | controller.preferences.appModes.removeValue(forKey: id) |
| 70 | }.labelStyle(.iconOnly).buttonStyle(.borderless) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | }.formStyle(.grouped) |
| 75 | } |
| 76 | |
| 77 | private func chooseApp() { |
| 78 | let panel = NSOpenPanel() |
| 79 | panel.directoryURL = URL(filePath: "/Applications") |
| 80 | panel.allowedContentTypes = [.applicationBundle] |
| 81 | if panel.runModal() == .OK, let url = panel.url, let id = Bundle(url: url)?.bundleIdentifier { |
| 82 | ruleApp = id |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private var dictionary: some View { |
| 87 | Form { |
| 88 | SwiftUI.Section("Vocabulary") { |
| 89 | Text("Add names, spellings, or phrases one per line. Voxley sends them as hints to Apple Speech when you dictate.") |
| 90 | .foregroundStyle(.secondary) |
| 91 | TextEditor(text: $controller.preferences.vocabulary) |
| 92 | .font(.body).frame(height: 132) |
| 93 | .accessibilityLabel("Custom vocabulary") |
| 94 | let count = controller.preferences.vocabularyEntries.count |
| 95 | HStack { |
| 96 | Button("Import Word List…", action: controller.importWordList) |
| 97 | .help("Import a text or CSV file. Separate entries with new lines, commas, semicolons, or tabs.") |
| 98 | Spacer() |
| 99 | Text("\(min(count, Preferences.vocabularyLimit)) of \(Preferences.vocabularyLimit) hints") |
| 100 | .foregroundStyle(.secondary) |
| 101 | } |
| 102 | if count > Preferences.vocabularyLimit { |
| 103 | Text("Apple Speech uses the first \(Preferences.vocabularyLimit) entries.") |
| 104 | .foregroundStyle(.secondary) |
| 105 | } |
| 106 | } |
| 107 | SwiftUI.Section("Replacements") { |
| 108 | ForEach($controller.preferences.replacements) { $replacement in |
| 109 | HStack { |
| 110 | TextField("Spoken text", text: $replacement.spoken) |
| 111 | Image(systemName: "arrow.right").foregroundStyle(.secondary) |
| 112 | TextField("Replace with", text: $replacement.written) |
| 113 | Button("Remove", systemImage: "minus.circle") { |
| 114 | controller.preferences.replacements.removeAll { $0.id == replacement.id } |
| 115 | }.labelStyle(.iconOnly).buttonStyle(.borderless) |
| 116 | } |
| 117 | } |
| 118 | Button("Add replacement", systemImage: "plus") { |
| 119 | controller.preferences.replacements.append(WordReplacement()) |
| 120 | } |
| 121 | } |
| 122 | }.formStyle(.grouped) |
| 123 | } |
| 124 | |
| 125 | private var settings: some View { |
| 126 | Form { |
| 127 | SwiftUI.Section("Startup") { |
| 128 | Toggle("Launch at Login", isOn: Binding( |
| 129 | get: { launchAtLogin.isRegistered }, |
| 130 | set: launchAtLogin.setEnabled |
| 131 | )) |
| 132 | if launchAtLogin.status == .requiresApproval { |
| 133 | Text("Allow Voxley in Login Items to open it automatically when you sign in.") |
| 134 | .foregroundStyle(.secondary) |
| 135 | Button("Open Login Items…", action: launchAtLogin.openSystemSettings) |
| 136 | } |
| 137 | if let message = launchAtLogin.errorMessage { |
| 138 | Text(message).foregroundStyle(.secondary) |
| 139 | } |
| 140 | } |
| 141 | SwiftUI.Section("Dictation") { |
| 142 | ShortcutRecorder(controller: controller) |
| 143 | Button("Voxley Onboarding…", action: openOnboarding) |
| 144 | Text("Tap to start or stop. Hold to speak, then release to finish.") |
| 145 | .foregroundStyle(.secondary) |
| 146 | Picker("Language", selection: $controller.preferences.locale) { |
| 147 | let names = Set(controller.locales.map { $0.identifier(.bcp47) }).union([controller.preferences.locale]) |
| 148 | ForEach(names.sorted(), id: \.self) { name in |
| 149 | Text(Locale.current.localizedString(forIdentifier: name) ?? name).tag(name) |
| 150 | } |
| 151 | } |
| 152 | Toggle("Save transcript history", isOn: $controller.preferences.retainHistory) |
| 153 | } |
| 154 | SwiftUI.Section("Refine") { |
| 155 | Toggle("Open Refine with a shortcut", isOn: Binding( |
| 156 | get: { controller.preferences.refineEnabled }, |
| 157 | set: controller.setRefineEnabled |
| 158 | )) |
| 159 | if controller.preferences.refineEnabled { |
| 160 | ShortcutRecorder(controller: controller, kind: .refine) |
| 161 | if !controller.refineShortcutReady { |
| 162 | Text("This shortcut couldn’t be registered. Record a different one.") |
| 163 | .foregroundStyle(.secondary) |
| 164 | } |
| 165 | } |
| 166 | Text("Dictate with the Refine shortcut and Voxley opens a small window instead of pasting. Edit by typing, chat with Apple Intelligence to refine the idea, then copy it into any app.") |
| 167 | .foregroundStyle(.secondary) |
| 168 | } |
| 169 | #if VOXLEY_SPARKLE |
| 170 | SwiftUI.Section("Updates") { |
| 171 | Toggle("Automatically install updates", isOn: Binding( |
| 172 | get: { VoxleyUpdates.shared.automaticUpdates }, |
| 173 | set: VoxleyUpdates.shared.setAutomaticUpdates |
| 174 | )) |
| 175 | Button("Check for Updates…", action: VoxleyUpdates.shared.check) |
| 176 | .disabled(!VoxleyUpdates.shared.canCheck || controller.phase != .idle) |
| 177 | } |
| 178 | #endif |
| 179 | SwiftUI.Section("Models and permissions") { |
| 180 | LabeledContent("Apple Speech", value: controller.speechReady ? "Supported on this Mac" : "Unavailable") |
| 181 | LabeledContent("Apple Intelligence", value: controller.modelStatus) |
| 182 | LabeledContent("Microphone") { |
| 183 | if controller.microphoneReady { Text("Allowed") } |
| 184 | else { Button("Allow microphone…") { controller.allowMicrophone() } } |
| 185 | } |
| 186 | LabeledContent("Automatic Paste") { |
| 187 | if controller.accessibilityReady { Text("Allowed") } |
| 188 | else { Button("Enable Automatic Paste…") { controller.allowAccessibility() } } |
| 189 | } |
| 190 | Button("Refresh status") { controller.refreshPermissions() } |
| 191 | } |
| 192 | SwiftUI.Section("Backup") { |
| 193 | Text("Export settings, app modes, dictionary and transcript history to a JSON file.") |
| 194 | .foregroundStyle(.secondary) |
| 195 | HStack { |
| 196 | Button("Export backup…") { controller.exportBackup() } |
| 197 | Button("Restore backup…") { controller.importBackup() } |
| 198 | }.disabled(controller.phase != .idle) |
| 199 | } |
| 200 | SwiftUI.Section("About") { |
| 201 | Button("About Voxley", action: openAbout) |
| 202 | } |
| 203 | }.formStyle(.grouped) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | struct ShortcutRecorder: View { |
| 208 | let controller: DictationController |
| 209 | var kind = ShortcutKind.dictation |
| 210 | var windowForRecording: (() -> NSWindow?)? = nil |
| 211 | @State private var monitor: Any? |
| 212 | @State private var instruction = "Press your shortcut…" |
| 213 | @State private var recordingWindow: NSWindow? |
| 214 | @State private var candidate: Shortcut? |
| 215 | @State private var modifierCapture = ModifierChordCapture() |
| 216 | @State private var anchor = NSView() |
| 217 | |
| 218 | var body: some View { |
| 219 | HStack { |
| 220 | Text(kind == .dictation ? "Keyboard shortcut" : "Refine shortcut") |
| 221 | Spacer() |
| 222 | if monitor == nil { |
| 223 | Text(controller.shortcutLabel(kind)) |
| 224 | .monospaced() |
| 225 | Button("Record Shortcut…", action: begin) |
| 226 | .disabled(controller.phase != .idle) |
| 227 | } else { |
| 228 | Text(instruction).foregroundStyle(.secondary) |
| 229 | Button("Cancel") { finish(nil) } |
| 230 | } |
| 231 | } |
| 232 | .background(ShortcutWindowAnchor(view: anchor)) |
| 233 | .onDisappear { if monitor != nil { finish(nil) } } |
| 234 | .onReceive(NotificationCenter.default.publisher(for: NSWindow.didResignKeyNotification)) { event in |
| 235 | if monitor != nil, event.object as? NSWindow === recordingWindow { finish(nil) } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private func begin() { |
| 240 | guard let window = windowForRecording?() ?? anchor.window ?? NSApp.keyWindow else { |
| 241 | controller.message = "Click inside the Voxley window and try Record Shortcut again." |
| 242 | return |
| 243 | } |
| 244 | window.makeKeyAndOrderFront(nil) |
| 245 | NSApp.activate() |
| 246 | recordingWindow = window |
| 247 | instruction = "Hold the keys together…" |
| 248 | candidate = nil |
| 249 | modifierCapture = ModifierChordCapture() |
| 250 | controller.beginShortcutRecording(kind) |
| 251 | monitor = NSEvent.addLocalMonitorForEvents(matching: [.keyDown, .keyUp, .flagsChanged]) { event in |
| 252 | guard (event.window ?? NSApp.keyWindow) === window else { return event } |
| 253 | switch event.type { |
| 254 | case .flagsChanged: |
| 255 | recordModifiers(event.modifierFlags) |
| 256 | case .keyDown: |
| 257 | if !event.isARepeat { |
| 258 | modifierCapture.ordinaryKeyDown() |
| 259 | recordKeyDown(event) |
| 260 | } |
| 261 | case .keyUp: |
| 262 | if let candidate, candidate.code == UInt32(event.keyCode) { finish(candidate) } |
| 263 | default: break |
| 264 | } |
| 265 | return nil |
| 266 | } |
| 267 | if monitor == nil { |
| 268 | recordingWindow = nil |
| 269 | controller.cancelShortcutRecording(kind) |
| 270 | controller.message = "Couldn’t start shortcut capture. Try again." |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | private func recordModifiers(_ flags: NSEvent.ModifierFlags) { |
| 275 | guard candidate == nil else { return } |
| 276 | let held = Shortcut.chordModifiers(for: flags) |
| 277 | if let recorded = modifierCapture.modifiersChanged(held) { |
| 278 | finish(recorded) |
| 279 | } else if let modifierChord = modifierCapture.pending { |
| 280 | instruction = "\(modifierChord.label) Release to save or press another key" |
| 281 | } else if held != 0 { |
| 282 | instruction = "\(Shortcut.modifierLabel(for: flags)) Hold another key" |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | private func recordKeyDown(_ event: NSEvent) { |
| 287 | if event.keyCode == kVK_Escape, Shortcut.modifierLabel(for: event.modifierFlags).isEmpty { |
| 288 | finish(nil) |
| 289 | } else if candidate == nil { |
| 290 | guard let shortcut = Shortcut(event: event) else { |
| 291 | instruction = "Add a modifier or use a function key." |
| 292 | return |
| 293 | } |
| 294 | guard !shortcut.isStandardEditingShortcut else { |
| 295 | instruction = "\(shortcut.label) is reserved for editing. Try another." |
| 296 | return |
| 297 | } |
| 298 | candidate = shortcut |
| 299 | instruction = "\(shortcut.label) Release to save" |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | private func finish(_ shortcut: Shortcut?) { |
| 304 | if let shortcut, !controller.saveRecordedShortcut(shortcut, kind: kind) { |
| 305 | candidate = nil |
| 306 | instruction = "\(shortcut.label) is unavailable. Try another." |
| 307 | return |
| 308 | } |
| 309 | if let monitor { NSEvent.removeMonitor(monitor) } |
| 310 | monitor = nil |
| 311 | recordingWindow = nil |
| 312 | candidate = nil |
| 313 | if shortcut == nil { controller.cancelShortcutRecording(kind) } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private struct ShortcutWindowAnchor: NSViewRepresentable { |
| 318 | let view: NSView |
| 319 | func makeNSView(context: Context) -> NSView { view } |
| 320 | func updateNSView(_ view: NSView, context: Context) {} |
| 321 | } |