Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: scripts/test-native.sh
- run: scripts/test-shortcut-registration.sh
- run: scripts/build-app.sh
- run: python3 scripts/check-distribution.py
worker:
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Never commit credentials, actual service endpoints, account IDs, local machine p

```sh
scripts/test-native.sh
scripts/test-shortcut-registration.sh
scripts/build-app.sh
python3 -m unittest discover -s local-stt -v
python3 scripts/check-distribution.py
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ python3.12 local-stt/run.py --configure-app /Applications/ClearFlow.app

The configuration command sends the token directly into macOS Keychain without displaying it or putting it in command-line arguments. Run it from your logged-in macOS desktop terminal so Keychain is available. Restart ClearFlow after configuring it. No Cloudflare account or Tailscale installation is needed for same-Mac use.

### Change the shortcut

In **Settings → General**, click the shortcut beside the pencil. Choose modifiers and a key, then click **Apply shortcut**. Changes take effect immediately and survive a restart; no service configuration or separate Save is required. Include Control, Option, or Command. Key names refer to US keyboard positions. If macOS reports a conflict, ClearFlow keeps the previous shortcut. Avoid combinations already used by your editor or system; some app-local shortcuts cannot be detected. **Use default**, then Apply, restores Option–Shift–Space. Finish or cancel active dictation before editing.

### 3. Dictate

1. Open ClearFlow. In Settings → General, allow Microphone and Accessibility through the normal macOS prompts.
Expand Down Expand Up @@ -77,6 +81,7 @@ Deploy the supplied Worker in **your own account**, generate your own secret, an

```sh
scripts/test-native.sh
scripts/test-shortcut-registration.sh
scripts/build-app.sh
python3 -m unittest discover -s local-stt -v
python3 scripts/check-distribution.py
Expand Down
14 changes: 10 additions & 4 deletions Sources/ClearFlow/ClearFlowApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import ClearFlowCore
let menu = NSMenu()
statusMenuItem = NSMenuItem(title: model.status, action: nil, keyEquivalent: "")
menu.addItem(statusMenuItem); menu.addItem(.separator())
startItem = NSMenuItem(title: "Start dictation ⌥⇧Space", action: #selector(toggle), keyEquivalent: "")
startItem = NSMenuItem(title: "Start dictation", action: #selector(toggle), keyEquivalent: "")
startItem.target = self; menu.addItem(startItem)
let open = NSMenuItem(title: "Open ClearFlow…", action: #selector(showWindow), keyEquivalent: "")
open.target = self; menu.addItem(open)
Expand All @@ -77,16 +77,22 @@ import ClearFlowCore
overlay.contentView = NSHostingView(rootView: OverlayView(model: model))
model.showWindow = { [weak self] in self?.showWindow() }
shortcut.onPress = { [weak self] in self?.model.toggle() }
model.shortcutAvailable = shortcut.register()
subscription = model.$mode.combineLatest(model.$status).sink { [weak self] _, _ in
model.shortcutAvailable = shortcut.register(model.shortcut)
model.registerShortcut = { [weak self] value in
guard let self else { return false }
return self.shortcut.register(value)
}
subscription = model.$mode.combineLatest(model.$status, model.$shortcut).sink { [weak self] _, _, _ in
DispatchQueue.main.async { self?.refresh() }
}
refresh()
// Open the controls at launch; recording still requires an explicit action.
showWindow()
}
private func refresh() {
statusMenuItem.title = String(model.status.prefix(85))
startItem.title = model.mode == .recording ? "Finish dictation ⌥⇧Space" : model.busy ? "Cancel ⌥⇧Space" : "Start dictation ⌥⇧Space"
let action = model.mode == .recording ? "Finish dictation" : model.busy ? "Cancel" : "Start dictation"
startItem.title = action + " " + model.shortcut.display
statusItem.button?.image = NSImage(systemSymbolName: model.mode == .recording ? "mic.fill" : model.mode == .transcribing ? "hourglass" : "waveform",
accessibilityDescription: "ClearFlow")
if model.busy {
Expand Down
13 changes: 13 additions & 0 deletions Sources/ClearFlow/DictationModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ import ClearFlowCore
@Published var hasAccessibility = AXIsProcessTrusted()
@Published var microphoneGranted = AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
@Published var shortcutAvailable = true
@Published private(set) var shortcut = AppSettings.shortcut
var registerShortcut: ((Shortcut) -> Bool)?
func changeShortcut(_ value: Shortcut) -> String? {
guard !busy else { return "Finish or cancel dictation before changing the shortcut." }
guard value.isValid else { return "Include Control, Option, or Command with a key." }
guard let registerShortcut, registerShortcut(value) else {
return "Could not register this shortcut. It may be used by another app or macOS. Choose another combination; your previous shortcut is unchanged."
}
AppSettings.shortcut = value
shortcut = value; shortcutAvailable = true
return nil
}
var showWindow: (() -> Void)?
var lastExternalApp: NSRunningApplication?
private var demoMode = false
Expand All @@ -39,6 +51,7 @@ import ClearFlowCore
init(demo: Bool = false) {
if demo {
demoMode = true
shortcut = .default
endpoint = "http://127.0.0.1:8768"; vocabulary = ""; token = ""
saveHistory = false; hasAccessibility = true; microphoneGranted = true
history = [Transcript(id: UUID(), date: Date(timeIntervalSince1970: 1780322400),
Expand Down
36 changes: 30 additions & 6 deletions Sources/ClearFlow/GlobalShortcut.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import Carbon
import ClearFlowCore

@MainActor final class GlobalShortcut {
private var reference: EventHotKeyRef?
private var handler: EventHandlerRef?
private var current: Shortcut?
private var generation: UInt32 = 0
var onPress: (() -> Void)?
func register() -> Bool {
// Register the replacement first: a conflict must never discard the working shortcut.
func register(_ value: Shortcut) -> Bool {
guard value.isValid else { return false }
if current == value, reference != nil { return true }
guard installHandler() else { return false }
var modifiers: UInt32 = 0
if value.modifiers.contains(.control) { modifiers |= UInt32(controlKey) }
if value.modifiers.contains(.option) { modifiers |= UInt32(optionKey) }
if value.modifiers.contains(.shift) { modifiers |= UInt32(shiftKey) }
if value.modifiers.contains(.command) { modifiers |= UInt32(cmdKey) }
let next = generation &+ 1
var candidate: EventHotKeyRef?
let id = EventHotKeyID(signature: 0x43464c57, id: next)
guard RegisterEventHotKey(value.keyCode, modifiers, id, GetApplicationEventTarget(), 0, &candidate) == noErr else { return false }
if let reference { UnregisterEventHotKey(reference) }
reference = candidate; current = value; generation = next
return true
}
private func installHandler() -> Bool {
if handler != nil { return true }
var type = EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyPressed))
let pointer = Unmanaged.passUnretained(self).toOpaque()
let status = InstallEventHandler(GetApplicationEventTarget(), { _, event, userData in
Expand All @@ -14,16 +36,18 @@ import Carbon
nil, MemoryLayout<EventHotKeyID>.size, nil, &id)
guard id.signature == 0x43464c57 else { return OSStatus(eventNotHandledErr) }
let shortcut = Unmanaged<GlobalShortcut>.fromOpaque(userData).takeUnretainedValue()
Task { @MainActor in shortcut.onPress?() }
let receivedID = id.id
Task { @MainActor in
guard shortcut.reference != nil, shortcut.generation == receivedID else { return }
shortcut.onPress?()
}
return noErr
}, 1, &type, pointer, &handler)
guard status == noErr else { return false }
let id = EventHotKeyID(signature: 0x43464c57, id: 1)
return RegisterEventHotKey(UInt32(kVK_Space), UInt32(optionKey | shiftKey), id, GetApplicationEventTarget(), 0, &reference) == noErr
return status == noErr
}
func unregister() {
if let reference { UnregisterEventHotKey(reference) }
if let handler { RemoveEventHandler(handler) }
reference = nil; handler = nil
reference = nil; handler = nil; current = nil
}
}
4 changes: 4 additions & 0 deletions Sources/ClearFlow/SettingsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import ClearFlowCore

struct AppSettings {
static let defaults = UserDefaults.standard
static var shortcut: Shortcut {
get { Shortcut.load(defaults.data(forKey: "dictationShortcut")) }
set { defaults.set(newValue.encoded, forKey: "dictationShortcut") }
}
static var endpoint: String {
get { defaults.string(forKey: "endpoint") ?? (Bundle.main.object(forInfoDictionaryKey: "ClearFlowEndpoint") as? String ?? "http://127.0.0.1:8768") }
set { defaults.set(newValue, forKey: "endpoint") }
Expand Down
66 changes: 57 additions & 9 deletions Sources/ClearFlow/Views.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import ClearFlowCore

private enum FlowTheme {
static let shell = Color(red: 0.965, green: 0.960, blue: 0.945)
Expand Down Expand Up @@ -66,9 +67,9 @@ struct MainView: View {
Spacer()
VStack(alignment: .leading, spacing: 12) {
Text("Make yourself heard").font(.system(size: 12, weight: .semibold))
Text("Press Option–Shift–Space\nto start and finish.")
Text("Press \(model.shortcut.display)\nto start and finish.")
.font(.system(size: 12)).foregroundStyle(FlowTheme.muted).lineSpacing(4)
HStack(spacing: 5) { keycap("⌥"); keycap("⇧"); keycap("space") }
HStack(spacing: 3) { ForEach(model.shortcut.keycaps, id: \.self) { keycap($0) } }
}.padding(15).frame(maxWidth: .infinity, alignment: .leading)
.background(.white, in: RoundedRectangle(cornerRadius: 13))
.overlay(RoundedRectangle(cornerRadius: 13).stroke(FlowTheme.line))
Expand Down Expand Up @@ -98,7 +99,7 @@ struct MainView: View {
}
private func keycap(_ text: String) -> some View {
Text(text).font(.system(size: 12, weight: .medium))
.padding(.horizontal, 9).padding(.vertical, 5)
.padding(.horizontal, 5).padding(.vertical, 5)
.background(FlowTheme.shell, in: RoundedRectangle(cornerRadius: 5))
.overlay(RoundedRectangle(cornerRadius: 5).stroke(FlowTheme.line))
}
Expand Down Expand Up @@ -150,7 +151,7 @@ struct MainView: View {
Text(model.mode == .recording ? "Go on. We’re listening." : model.mode == .transcribing ? "Finding your words." : "Less typing.\nMore you.")
.font(.system(size: 29, weight: .regular, design: .serif))
.foregroundStyle(Color(red: 0.98, green: 0.97, blue: 0.91)).lineSpacing(0)
Text(model.mode == .recording ? "Press Option–Shift–Space when you’re done." : model.mode == .transcribing ? "Your transcript will be ready in a moment." : "Speak naturally, wherever you write.")
Text(model.mode == .recording ? "Press \(model.shortcut.display) when you’re done." : model.mode == .transcribing ? "Your transcript will be ready in a moment." : "Speak naturally, wherever you write.")
.font(.system(size: 12)).foregroundStyle(.white.opacity(0.80))
HStack(spacing: 9) {
Button { model.toggle() } label: {
Expand Down Expand Up @@ -208,7 +209,7 @@ struct MainView: View {
Image(systemName: query.isEmpty ? "text.bubble" : "magnifyingglass").font(.system(size: 25)).foregroundStyle(FlowTheme.muted)
Text(query.isEmpty ? "Your next thought starts here" : "No matching dictations")
.font(.system(size: 16, weight: .medium))
Text(query.isEmpty ? "Press ⌥ ⇧ Space, say what’s on your mind,\nthen press it again. Your words will appear here." : "Try another word or phrase.")
Text(query.isEmpty ? "Press \(model.shortcut.display), say what’s on your mind,\nthen press it again. Your words will appear here." : "Try another word or phrase.")
.font(.system(size: 12)).foregroundStyle(FlowTheme.muted).multilineTextAlignment(.center).lineSpacing(4)
}.frame(maxWidth: .infinity).padding(.vertical, 38)
} else {
Expand Down Expand Up @@ -263,6 +264,7 @@ private struct FlowSettings: View {
@ObservedObject var model: DictationModel
@Environment(\.dismiss) private var dismiss
@State private var section = "General"
@State private var editingShortcut = false
var body: some View {
HStack(spacing: 0) {
VStack(alignment: .leading, spacing: 6) {
Expand All @@ -272,7 +274,7 @@ private struct FlowSettings: View {
sectionButton("Transcription", "waveform")
sectionButton("Data & privacy", "hand.raised")
Spacer()
Text("ClearFlow 0.3").font(.system(size: 11)).foregroundStyle(FlowTheme.muted).padding(14)
Text("ClearFlow " + (Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "")).font(.system(size: 11)).foregroundStyle(FlowTheme.muted).padding(14)
}.padding(10).frame(width: 174).background(FlowTheme.shell)
VStack(alignment: .leading, spacing: 24) {
HStack {
Expand Down Expand Up @@ -324,10 +326,15 @@ private struct FlowSettings: View {
Text("Press once to speak. Press again to finish.").font(.system(size: 12)).foregroundStyle(FlowTheme.muted)
}
Spacer()
Text("⌥ ⇧ Space").font(.system(size: 14, weight: .medium)).padding(11)
.background(FlowTheme.selected, in: RoundedRectangle(cornerRadius: 8))
Button { editingShortcut = true } label: {
HStack { Text(model.shortcut.display); Image(systemName: "pencil") }
}.buttonStyle(FlowButton()).disabled(model.busy)
.accessibilityLabel("Change shortcut")
.popover(isPresented: $editingShortcut) {
ShortcutEditor(model: model, draft: model.shortcut)
}
}
if !model.shortcutAvailable { Text("This shortcut is in use by another app. Close that app and restart ClearFlow, or use Start dictation.")
if !model.shortcutAvailable { Text("This shortcut is unavailable. Choose another combination above, or use Start dictation.")
.font(.caption).foregroundStyle(.orange).padding(.top, 8) }
rule()
row("Microphone", "Use your Mac’s default input.", granted: model.microphoneGranted) { model.requestMicrophone() }
Expand Down Expand Up @@ -375,6 +382,47 @@ private struct FlowSettings: View {
}
}
}
private struct ShortcutEditor: View {
@ObservedObject var model: DictationModel
@State var draft: Shortcut
@State private var error = ""
@Environment(\.dismiss) private var dismiss
var body: some View {
VStack(alignment: .leading, spacing: 16) {
Text("Change shortcut").font(.headline)
Text("Choose modifiers and a key. Applies immediately.")
.font(.caption).foregroundStyle(.secondary)
HStack {
modifier("⌃ Control", .control)
modifier("⌥ Option", .option)
modifier("⇧ Shift", .shift)
modifier("⌘ Command", .command)
}
Picker("Key (US keyboard positions)", selection: $draft.keyCode) {
ForEach(Shortcut.keys, id: \.code) { key in Text(key.name).tag(key.code) }
}
.onChange(of: draft.keyCode) { _, _ in error = "" }
Text(draft.display).font(.title3.monospaced())
if !draft.isValid { Text("Include Control, Option, or Command.").font(.caption).foregroundStyle(.orange) }
if !error.isEmpty { Text(error).font(.caption).foregroundStyle(.red).fixedSize(horizontal: false, vertical: true) }
HStack {
Button("Use default") { draft = .default; error = "" }
Spacer()
Button("Cancel") { dismiss() }
Button("Apply shortcut") {
if let issue = model.changeShortcut(draft) { error = issue }
else { dismiss() }
}.disabled(!draft.isValid || model.busy)
}
}.padding(22).frame(width: 470)
}
private func modifier(_ title: String, _ flag: Shortcut.Modifiers) -> some View {
Toggle(title, isOn: Binding(get: { draft.modifiers.contains(flag) }, set: { enabled in
if enabled { draft.modifiers.insert(flag) } else { draft.modifiers.remove(flag) }
error = ""
})).toggleStyle(.checkbox).font(.caption)
}
}
struct Waveform: View {
let samples: [Double]
var body: some View {
Expand Down
4 changes: 2 additions & 2 deletions Sources/ClearFlowCore/SessionGate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public enum DestinationPolicy {
}
public static func failure(accessibility: Bool, hasApp: Bool, hasField: Bool, secure: Bool, editable: Bool) -> String? {
guard accessibility else { return "Enable Accessibility for ClearFlow in System Settings to insert text automatically." }
guard hasApp else { return "Select a text field in another app, then start with Option–Shift–Space." }
guard hasField else { return "The selected app did not expose a focused text field. Click its editor, then start with Option–Shift–Space." }
guard hasApp else { return "Select a text field in another app, then press your dictation shortcut." }
guard hasField else { return "The selected app did not expose a focused text field. Click its editor, then press your dictation shortcut." }
guard !secure else { return "Automatic insertion is disabled in password fields." }
guard editable else { return "The focused control is not an editable text field. Click inside the editor first." }
return nil
Expand Down
Loading
Loading