interplane.Get Omega
Read in your language

hop

Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.

Brought by drakestapleton

branch main Branches Tags History

hop / Sources / VoiceView.swift

156 lines · 10574 bytes Raw Blame History
1import SwiftUI
2import AppKit
3
4/// Dictation: what state it is in, how to use it, your own words, and what you said lately.
5struct VoiceView: View {
6 @ObservedObject var m: AppModel
7 @ObservedObject var d: Dictation
8 @State private var heard = ""
9 @State private var write = ""
10 @State private var say = ""
11 @State private var insert = ""
12 @State private var tick = 0
13
14 private var status: (ok: Bool, text: String, action: String?, run: () -> Void) {
15 _ = tick
16 if !d.settings.enabled {
17 return (false, "Dictation is off. Turning it on lets macOS fetch Apple's speech model once (a download from Apple). After that, your voice is turned into text on this Mac; the audio is not sent anywhere. The text goes where you point it: typed at your cursor, or saved locally in the parking lot when you dictate into Hop's Agent screen. Sending a request or sorting ideas uses your configured agent.",
18 "Turn on dictation", { d.settings.enabled = true })
19 }
20 if !d.setupProblem.isEmpty { return (false, d.setupProblem, "Try again", { Task { await d.warmUp() } }) }
21 if !d.modelReady { return (false, "Getting the speech model ready…", nil, {}) }
22 if SpeechEngine.microphoneUndecided { return (false, "Hop needs the microphone.", "Allow microphone…", { Task { _ = await SpeechEngine.askForMicrophone(); tick += 1 } }) }
23 if !SpeechEngine.microphoneAllowed { return (false, "The microphone is switched off for Hop.", "Open settings…", { open("Privacy_Microphone") }) }
24 if !AXIsProcessTrusted() { return (false, "Hop needs the paste permission to hear the Fn key and type for you.", "Open settings…", { open("Privacy_Accessibility") }) }
25 if d.otherDictationAppRunning && !d.settings.takeOverFromWispr {
26 return (false, "Wispr Flow is still answering the Fn key, so Hop is standing by.", "Use Hop instead", { d.settings.takeOverFromWispr = true })
27 }
28 return (true, "Ready. Hold Fn and talk. Your voice is turned into text on this Mac; the text goes where you point it.", nil, {})
29 }
30 private func open(_ pane: String) {
31 if let u = URL(string: "x-apple.systempreferences:com.apple.preference.security?\(pane)") { NSWorkspace.shared.open(u) }
32 }
33
34 var body: some View {
35 let s = status
36 HStack(alignment: .top, spacing: 0) {
37 ScrollView {
38 VStack(alignment: .leading, spacing: 12) {
39 HStack(spacing: 8) {
40 Circle().fill(s.ok ? .green : .orange).frame(width: 9, height: 9)
41 Text(s.text).font(.callout.weight(.semibold)).fixedSize(horizontal: false, vertical: true)
42 Spacer()
43 if let a = s.action { Button(a) { s.run() }.buttonStyle(.borderedProminent).controlSize(.small) }
44 }
45 Grid(alignment: .leading, horizontalSpacing: 14, verticalSpacing: 4) {
46 ForEach([("Hold Fn", "Talk, let go, it types where your cursor is"),
47 ("Fn then Space", "Hands-free. Fn again to finish (or tap Fn twice)"),
48 ("Fn + Control", "Say what to do: rewrites selected text, or Hop's agent does it"),
49 ("Esc", "Cancel"),
50 ("⌃⌘V ⌃⌘C", "Paste or copy your last dictation"),
51 ("⌥1", "Polish the selected text"),
52 ("In Hop", "Hold Fn on the Agent screen to park an idea locally. Send a request or sort ideas when you are ready")], id: \.0) { k, v in
53 GridRow {
54 Text(k).font(.caption.monospaced().weight(.semibold))
55 Text(v).font(.caption).foregroundStyle(.secondary)
56 }
57 }
58 }
59 Divider()
60 Toggle("Dictation on (hold Fn and talk)", isOn: $d.settings.enabled)
61 Toggle("Tidy up what I say (drop um, repeated words, false starts)", isOn: Binding(get: { d.settings.cleanup != "off" }, set: { d.settings.cleanup = $0 ? "light" : "off" }))
62 Toggle("Sounds", isOn: $d.settings.sounds)
63 Toggle("Mute music and video while I talk", isOn: $d.settings.muteAudioWhileDictating)
64 Toggle("Read answers aloud when I ask by voice", isOn: $d.settings.speakReplies)
65 Toggle("Keep Wispr Flow closed (Hop owns the Fn key)", isOn: $d.settings.takeOverFromWispr)
66 Divider()
67 Text("Your words").font(.headline)
68 Text("When Hop hears the first, it writes the second. Leave the first empty to just teach it a name.").font(.caption).foregroundStyle(.secondary)
69 HStack {
70 TextField("Hears (e.g. kuda)", text: $heard)
71 TextField("Writes (e.g. CUDA)", text: $write)
72 Button("Add") {
73 let h = heard.trimmingCharacters(in: .whitespaces), w = write.trimmingCharacters(in: .whitespaces)
74 guard !w.isEmpty else { return }
75 if h.isEmpty { if !d.settings.words.contains(w) { d.settings.words.append(w) } }
76 else { d.settings.fixes.removeAll { $0.id == h.lowercased() }; d.settings.fixes.append(.init(heard: h, write: w)) }
77 heard = ""; write = ""
78 }.disabled(write.trimmingCharacters(in: .whitespaces).isEmpty)
79 }.textFieldStyle(.roundedBorder)
80 FlowChips(items: d.settings.fixes.map { ("\($0.heard) → \($0.write)", $0.id) }) { id in d.settings.fixes.removeAll { $0.id == id } }
81 FlowChips(items: d.settings.words.map { ($0, $0) }) { w in d.settings.words.removeAll { $0 == w } }
82 Toggle("Learn from my corrections (when I fix a word Hop typed, it writes it my way next time)", isOn: $d.settings.learnFromFixes)
83 let learned = d.settings.learned.filter { l in d.settings.fixes.contains { $0.id == l.id && $0.write == l.write } }
84 if !learned.isEmpty {
85 Text("Learned from your fixes. Remove one to undo it; Hop will not learn it again.").font(.caption).foregroundStyle(.secondary)
86 FlowChips(items: learned.map { ("\($0.heard) → \($0.write)", $0.id + "→" + $0.write) }) { key in
87 if let l = learned.first(where: { $0.id + "→" + $0.write == key }) { d.undoLearned(l) }
88 }
89 }
90 Text("Snippets").font(.headline)
91 HStack {
92 TextField("When I say…", text: $say)
93 TextField("Type this…", text: $insert)
94 Button("Add") {
95 d.settings.snippets.removeAll { $0.id == say.lowercased() }
96 d.settings.snippets.append(.init(say: say.trimmingCharacters(in: .whitespaces), insert: insert)); say = ""; insert = ""
97 }.disabled(say.trimmingCharacters(in: .whitespaces).isEmpty || insert.isEmpty)
98 }.textFieldStyle(.roundedBorder)
99 FlowChips(items: d.settings.snippets.map { ("“\($0.say)” → \($0.insert.prefix(28))", $0.id) }) { id in d.settings.snippets.removeAll { $0.id == id } }
100 }
101 .controlSize(.small).padding(14)
102 }
103 .frame(maxWidth: .infinity)
104 Divider()
105 VStack(alignment: .leading, spacing: 6) {
106 Text("What you said").font(.headline)
107 let rows = d.recent.filter { m.query.isEmpty || $0.text.localizedCaseInsensitiveContains(m.query) }
108 if rows.isEmpty {
109 Text(d.recent.isEmpty ? "Your dictations show up here, and in Clipboard so you can paste them again." : "No match.")
110 .font(.callout).foregroundStyle(.secondary)
111 Spacer()
112 } else {
113 ScrollView {
114 VStack(alignment: .leading, spacing: 6) {
115 ForEach(rows) { e in
116 VStack(alignment: .leading, spacing: 2) {
117 Text(e.text).font(.callout).lineLimit(4).textSelection(.enabled)
118 HStack {
119 Text([e.app, e.date.formatted(date: .omitted, time: .shortened)].compactMap { $0 }.joined(separator: " · "))
120 .font(.caption2).foregroundStyle(.tertiary)
121 Spacer()
122 Button("Copy") { let pb = NSPasteboard.general; pb.clearContents(); pb.setString(e.text, forType: .string) }
123 .controlSize(.mini)
124 }
125 }
126 .padding(8).background(Color.primary.opacity(0.05), in: RoundedRectangle(cornerRadius: 8))
127 }
128 }
129 }
130 }
131 }
132 .padding(14).frame(width: 300)
133 }
134 .task { while !Task.isCancelled { try? await Task.sleep(nanoseconds: 2_000_000_000); tick += 1 } } // notice permission changes
135 }
136}
137
138/// Small removable tags that wrap onto as many lines as they need.
139struct FlowChips: View {
140 let items: [(label: String, id: String)]
141 let remove: (String) -> Void
142 var body: some View {
143 if !items.isEmpty {
144 LazyVGrid(columns: [GridItem(.adaptive(minimum: 150), spacing: 6, alignment: .leading)], alignment: .leading, spacing: 6) {
145 ForEach(items, id: \.id) { item in
146 HStack(spacing: 4) {
147 Text(item.label).font(.caption).lineLimit(1)
148 Button { remove(item.id) } label: { Image(systemName: "xmark").font(.system(size: 8, weight: .bold)) }.buttonStyle(.plain).foregroundStyle(.secondary)
149 }
150 .padding(.horizontal, 7).padding(.vertical, 3)
151 .background(Color.primary.opacity(0.07), in: Capsule())
152 }
153 }
154 }
155 }
156}