diff --git a/CLAUDE.md b/CLAUDE.md index 35ea3aad..7871ebbd 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,7 +9,9 @@ Build features so failures are *debuggable without a rebuild*. This is not optio - **HTTP/IPC boundaries:** check `resp.status().is_success()` before parsing, and put the response body in the error so a 401/403/quota failure reads as itself, not a generic "missing field". Same for subprocess: capture stderr and surface the structured error kind. - **Diagnose with logs, not hypotheses.** If a bug's cause isn't provable from data in hand, add the logging that would capture it, have the user reproduce, then fix from what the logs show. Don't guess-and-rebuild in a loop. -## Build + reinstall workflow (macOS) +## Build + reinstall workflow (macOS only) + +As of 2026-09-11 the dev machine is Omarchy Linux (rebuilt from Windows 11 on 2026-09-01). The macOS steps below (codesign, tccutil, `/Applications` copy, `.app` bundles) are not runnable here; they are kept for when a Mac is available. HEAD is on branch `feat/windows-voice-actions`. Windows path: `docs/WINDOWS.md` describes a work-in-progress NSIS installer built by the GitHub Actions **Windows** workflow (`src-tauri/tauri.windows.conf.json`, targets `nsis`); no fully supported Windows release yet. No Linux build path documented. **Signing identity (stable — this is why permissions now persist).** `tauri.conf.json` → `bundle.macOS.signingIdentity` is set to the SHA-1 `F6FD1D39BE4E52054A6B72E1EC5E90A03F5E5B77`, a self-signed **"Echo Scribe Local Dev"** code-signing identity in the login keychain. Because every build is signed with the *same* cert, its designated requirement is constant (`identifier "com.echoscribe.app" and certificate root = H"f6fd…"`), so macOS keeps TCC grants (including Screen Recording) across reinstalls — no more per-rebuild re-granting. Do **not** revert the committed config to `"-"` (ad-hoc), which changes the signature every build and drops grants. @@ -79,6 +81,9 @@ bun run test:e2e # blocks publishing on failure. Locally (after a build): tar -czf EchoScribe-aarch64.tar.gz -C src-tauri/target/release/bundle/macos "Echo Scribe.app" bash scripts/smoke-test.sh EchoScribe-aarch64.tar.gz + +# Installer test: exercises install.sh against a fixture tarball in a throwaway dir +bun run test:installer # = bash scripts/test-installer.sh ``` Don't run `bun tauri dev` from a subagent — it spawns a window that won't terminate cleanly. diff --git a/docs/WINDOWS.md b/docs/WINDOWS.md index 6c672475..3b1897ed 100644 --- a/docs/WINDOWS.md +++ b/docs/WINDOWS.md @@ -40,9 +40,9 @@ calendar matching can then be added back with Windows-native implementations. Minimum porting checklist: 1. Move macOS-only dependencies behind `cfg(target_os = "macos")`. -2. Add Windows equivalents or feature fallbacks for global hotkeys, focus context, - microphone permission UX, meeting audio capture, screen recording, calendar - lookup, and credential storage. +2. Add Windows equivalents or feature fallbacks for focus context, microphone + permission UX, meeting audio capture, screen recording, calendar lookup, and + credential storage. (Global hotkeys are done — see "Hotkeys on Windows" below.) 3. Replace the unconditional Metal LLM build with a Windows-compatible llama.cpp backend, likely CPU first, then optional CUDA/Vulkan later. 4. Add Windows sidecar binaries or disable the features that require the current @@ -51,6 +51,26 @@ Minimum porting checklist: 6. Publish a Windows installer from Tauri, typically an NSIS `*-setup.exe` or an MSI package. +## Hotkeys on Windows + +Windows has no CGEventTap, so the configured bindings are registered with the +global-shortcut plugin (`RegisterHotKey`) instead of the macOS event tap. Two +consequences the UI has to live with: + +- **A shortcut must contain a regular key.** `RegisterHotKey` cannot register a + bare modifier, so the macOS default (Right Control on its own) is not + expressible. Settings reports this rather than silently doing nothing. +- **Left/right modifiers are not distinguished.** "Right Alt + Slash" registers + as "Alt + Slash". +- **Registration is first-come-first-served across the whole system.** If another + running application already owns the combination, registration fails and Echo + Scribe surfaces the problem in Settings next to that shortcut. + +Because a second instance of the app would lose that race against the first and +end up with no working hotkey at all, the app registers +`tauri-plugin-single-instance` and focuses the existing window instead of +starting a second process. + ## Validation commands for a Windows developer On a Windows development machine: diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 58806a07..825237a3 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1331,7 +1331,9 @@ dependencies = [ "tauri-plugin-dialog", "tauri-plugin-global-shortcut", "tauri-plugin-notification", + "tauri-plugin-opener", "tauri-plugin-shell", + "tauri-plugin-single-instance", "tauri-plugin-store", "tempfile", "thiserror 1.0.69", @@ -1343,6 +1345,7 @@ dependencies = [ "ulid", "url", "uuid", + "windows 0.58.0", ] [[package]] @@ -5504,6 +5507,28 @@ dependencies = [ "url", ] +[[package]] +name = "tauri-plugin-opener" +version = "2.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17e1bea14edce6b793a04e2417e3fd924b9bc4faae83cdee7d714156cceeed29" +dependencies = [ + "dunce", + "glob", + "objc2-app-kit", + "objc2-foundation", + "open", + "schemars 0.8.22", + "serde", + "serde_json", + "tauri", + "tauri-plugin", + "thiserror 2.0.18", + "url", + "windows 0.61.3", + "zbus", +] + [[package]] name = "tauri-plugin-shell" version = "2.3.5" @@ -5525,6 +5550,22 @@ dependencies = [ "tokio", ] +[[package]] +name = "tauri-plugin-single-instance" +version = "2.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3214becf9ef5783c0ae99a3bb25adf5353a7a16ebf53e74b909e29205735c6c" +dependencies = [ + "serde", + "serde_json", + "tauri", + "thiserror 2.0.18", + "tokio", + "tracing", + "windows-sys 0.60.2", + "zbus", +] + [[package]] name = "tauri-plugin-store" version = "2.4.2" @@ -6745,6 +6786,16 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +dependencies = [ + "windows-core 0.58.0", + "windows-targets 0.52.6", +] + [[package]] name = "windows" version = "0.61.3" @@ -6789,6 +6840,19 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-core" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +dependencies = [ + "windows-implement 0.58.0", + "windows-interface 0.58.0", + "windows-result 0.2.0", + "windows-strings 0.1.0", + "windows-targets 0.52.6", +] + [[package]] name = "windows-core" version = "0.61.2" @@ -6837,6 +6901,17 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "windows-implement" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "windows-implement" version = "0.60.2" @@ -6859,6 +6934,17 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "windows-interface" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "windows-interface" version = "0.59.3" @@ -6901,6 +6987,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-result" version = "0.3.4" @@ -6919,6 +7014,16 @@ dependencies = [ "windows-link 0.2.1", ] +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result 0.2.0", + "windows-targets 0.52.6", +] + [[package]] name = "windows-strings" version = "0.4.2" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 51e00374..7db78a3e 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -20,6 +20,10 @@ tauri-build = { version = "2", features = [] } [dependencies] tauri = { version = "2", features = ["protocol-asset", "tray-icon", "image-png", "macos-private-api"] } tauri-plugin-shell = "2" +# Cross-platform URL / mailto / path opening for voice actions. Replaces +# shelling out to macOS `open`, which made launch_app/draft_email/open_url fail +# with "program not found" on Windows. +tauri-plugin-opener = "2" tauri-plugin-dialog = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -46,6 +50,8 @@ tauri-plugin-store = "2" tauri-plugin-autostart = "2" tauri-plugin-notification = "2" tauri-plugin-global-shortcut = "2" +# Keeps a second launch from stealing/losing the global hotkey registration. +tauri-plugin-single-instance = "2" # ASR / Parakeet via ONNX Runtime. CPU-only — `ort-coreml` was dropped after # logs showed CoreML accepted only 15/39 encoder ops and 4/35 decoder ops, and # spent ~50s on first-run graph compilation while still running most of the @@ -107,6 +113,16 @@ block2 = "0.6" core-graphics = "0.24" core-foundation = "0.10" +[target.'cfg(target_os = "windows")'.dependencies] +# UI Automation, for reading the focused control's text selection without +# synthesizing Ctrl+C (see src/input/uia.rs). Win32 counterpart to the +# objc2-application-services AXUIElement usage on macOS. +windows = { version = "0.58", features = [ + "Win32_Foundation", + "Win32_System_Com", + "Win32_UI_Accessibility", +] } + [target.'cfg(not(target_os = "macos"))'.dependencies] # Non-macOS starts with a CPU-compatible llama.cpp baseline. GPU backends # such as CUDA or Vulkan should be enabled only after Windows CI is green. diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index f76e117e..5b1f080e 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -31,6 +31,7 @@ use crate::input::binding::{ code_from_key, key_from_code, Binding, ModifierKind, ModifierSide, SerKey, }; use crate::input::hotkeys::{spawn_listener, HotkeyEvent}; +use crate::input::trigger::HotkeyProblem; use crate::llm::{self, rag, GenerateRequest, Llm, LlmDownloadProgress, LlmModelEntry}; use crate::permissions::{self, CameraAccessOutcome, MicAccessOutcome, PermissionsStatus, SettingsPane}; use crate::settings::SettingsStore; @@ -62,6 +63,12 @@ pub struct AppState { pub action_binding: Arc>, pub edit_selection_binding: Arc>, pub hotkey_started: AtomicBool, + /// Friendly, user-facing reasons the configured hotkeys couldn't be + /// registered (Windows/Linux only — macOS uses the event tap and never + /// registers anything). Refreshed on every `sync_hotkeys` call and read by + /// the settings UI via [`get_hotkey_problems`], so a shortcut that another + /// app already owns shows up in the UI instead of only in the log. + pub hotkey_problems: Arc>>, /// When `true`, the coordinator drops Pressed/Released events. Toggled /// from the tray menu (Pause/Resume hotkeys). The hotkey listeners stay /// running so the toggle is instant — only the coordinator filters. @@ -344,6 +351,36 @@ pub fn request_screen_recording_access() -> Result { Ok(granted) } +/// Re-register the global shortcuts from the current bindings and store any +/// user-facing problems for [`get_hotkey_problems`]. +/// +/// No-op on macOS, where the CGEventTap listeners read the shared `Binding` +/// directly on every key event and so pick up rebinds with nothing to re-register. +pub fn sync_hotkeys(state: &AppState, app: &AppHandle) { + #[cfg(not(target_os = "macos"))] + { + let problems = crate::input::trigger::sync_shortcuts(app, state); + if let Ok(mut guard) = state.hotkey_problems.write() { + *guard = problems; + } + } + #[cfg(target_os = "macos")] + { + let _ = (state, app); + } +} + +/// Hotkeys that could not be registered (empty on macOS, and empty on +/// Windows/Linux when everything registered cleanly). +#[tauri::command] +pub fn get_hotkey_problems(state: State<'_, AppState>) -> Vec { + state + .hotkey_problems + .read() + .map(|g| g.clone()) + .unwrap_or_default() +} + #[tauri::command] pub fn get_voice_at_cursor_binding(state: State<'_, AppState>) -> JsBinding { let b = state @@ -357,6 +394,7 @@ pub fn get_voice_at_cursor_binding(state: State<'_, AppState>) -> JsBinding { #[tauri::command] pub fn update_voice_at_cursor_binding( state: State<'_, AppState>, + app: AppHandle, binding: JsBinding, ) -> Result<(), String> { let parsed: Binding = binding @@ -366,11 +404,14 @@ pub fn update_voice_at_cursor_binding( .settings .set_voice_at_cursor_binding(parsed.clone()) .map_err(|e| e.to_string())?; - let mut guard = state - .binding - .write() - .map_err(|_| "binding lock poisoned".to_string())?; - *guard = parsed; + { + let mut guard = state + .binding + .write() + .map_err(|_| "binding lock poisoned".to_string())?; + *guard = parsed; + } + sync_hotkeys(&state, &app); Ok(()) } @@ -387,6 +428,7 @@ pub fn get_log_capture_binding(state: State<'_, AppState>) -> JsBinding { #[tauri::command] pub fn update_log_capture_binding( state: State<'_, AppState>, + app: AppHandle, binding: JsBinding, ) -> Result<(), String> { let parsed: Binding = binding @@ -396,11 +438,14 @@ pub fn update_log_capture_binding( .settings .set_log_capture_binding(parsed.clone()) .map_err(|e| e.to_string())?; - let mut guard = state - .log_capture_binding - .write() - .map_err(|_| "binding lock poisoned".to_string())?; - *guard = parsed; + { + let mut guard = state + .log_capture_binding + .write() + .map_err(|_| "binding lock poisoned".to_string())?; + *guard = parsed; + } + sync_hotkeys(&state, &app); Ok(()) } @@ -415,7 +460,11 @@ pub fn get_action_binding(state: State<'_, AppState>) -> JsBinding { } #[tauri::command] -pub fn update_action_binding(state: State<'_, AppState>, binding: JsBinding) -> Result<(), String> { +pub fn update_action_binding( + state: State<'_, AppState>, + app: AppHandle, + binding: JsBinding, +) -> Result<(), String> { let parsed: Binding = binding .try_into() .map_err(|e: BindingConversionError| e.to_string())?; @@ -423,11 +472,14 @@ pub fn update_action_binding(state: State<'_, AppState>, binding: JsBinding) -> .settings .set_action_binding(parsed.clone()) .map_err(|e| e.to_string())?; - let mut guard = state - .action_binding - .write() - .map_err(|_| "action_binding lock poisoned".to_string())?; - *guard = parsed; + { + let mut guard = state + .action_binding + .write() + .map_err(|_| "action_binding lock poisoned".to_string())?; + *guard = parsed; + } + sync_hotkeys(&state, &app); Ok(()) } @@ -444,6 +496,7 @@ pub fn get_edit_selection_binding(state: State<'_, AppState>) -> JsBinding { #[tauri::command] pub fn update_edit_selection_binding( state: State<'_, AppState>, + app: AppHandle, binding: JsBinding, ) -> Result<(), String> { let parsed: Binding = binding @@ -453,11 +506,14 @@ pub fn update_edit_selection_binding( .settings .set_edit_selection_binding(parsed.clone()) .map_err(|e| e.to_string())?; - let mut guard = state - .edit_selection_binding - .write() - .map_err(|_| "edit_selection_binding lock poisoned".to_string())?; - *guard = parsed; + { + let mut guard = state + .edit_selection_binding + .write() + .map_err(|_| "edit_selection_binding lock poisoned".to_string())?; + *guard = parsed; + } + sync_hotkeys(&state, &app); Ok(()) } @@ -705,16 +761,10 @@ pub fn ensure_pipeline_started(state: &AppState, app: &AppHandle) { spawn_listener(Arc::clone(&state.edit_selection_binding), es_tx, Arc::clone(&state.rebinding)); // macOS drives the coordinator from the CGEventTap listeners above. - // Non-macOS has no tap, so register the global-shortcut trigger. - #[cfg(not(target_os = "macos"))] - { - if let Err(e) = crate::input::trigger::register_default_dictation_shortcut( - app, - coord_tx.clone(), - ) { - tracing::warn!(target: "trigger", %e, "dictation hotkey unavailable"); - } - } + // Non-macOS has no tap, so register the configured bindings as global + // shortcuts. Must run after `coord_tx` is stored in state — the shortcut + // handlers forward into that channel. + sync_hotkeys(state, app); // Adapter tasks: tag + forward into the coordinator channel. We use // `tokio::spawn` rather than a dedicated thread because these are pure diff --git a/src-tauri/src/coordinator.rs b/src-tauri/src/coordinator.rs index 3267a17c..8d3d995e 100644 --- a/src-tauri/src/coordinator.rs +++ b/src-tauri/src/coordinator.rs @@ -160,6 +160,12 @@ pub fn spawn( info!(?action, "hotkey Pressed dropped: paused via tray"); continue; } + // Everything between here and `recorder.start()` returning + // is time the user hears the start cue but the microphone + // is not yet capturing — i.e. speech that never reaches the + // transcript. That silently eats the FIRST word, which is + // where the trigger word lives. + let pressed_at = std::time::Instant::now(); // Mid-recording upgrade: if LogCapture fires while // VoiceAtCursor is already recording, promote the @@ -270,6 +276,12 @@ pub fn spawn( } notify_recorder_failure(&app, &e, preferred.as_deref()); } else { + info!( + target: "trigger", + ?action, + press_to_capture_ms = pressed_at.elapsed().as_millis() as u64, + "recorder live" + ); if matches!(action, Action::VoiceAtCursor) { let _ = app.emit("voice:recording_started", ()); } @@ -849,6 +861,10 @@ async fn try_intercept_action( // Prefix-Based Routing if !trigger_enabled { // Option 2 fallback bypass: standard voice typing completely bypasses LLM + info!( + target: "trigger", + "trigger word routing is disabled in settings; pasting as plain text" + ); return InterceptOutcome::Passthrough; } @@ -872,6 +888,11 @@ async fn try_intercept_action( match matched_trigger { Some(len) => { + info!( + target: "trigger", + trigger = %trigger_lower, + "trigger word matched; routing dictation to the action launcher" + ); if text_lower.len() == len { String::new() } else { @@ -882,6 +903,21 @@ async fn try_intercept_action( } None => { // No trigger prefix found: bypass the LLM entirely (instant paste!) + // + // Log what we actually saw. Without this, a miss is + // indistinguishable from "the hotkey never fired" in the log, + // and the two have completely different fixes: the trigger word + // is spoken FIRST, so it is exactly the word most likely to be + // clipped by microphone start-up latency. `head` is the prefix + // we compared, so a log showing `head="open google."` proves the + // word never reached the transcript rather than failing to match. + info!( + target: "trigger", + trigger = %trigger_lower, + head = %text_lower.chars().take(24).collect::(), + chars = text_trimmed.len(), + "no trigger word at start of dictation; pasting as plain text" + ); return InterceptOutcome::Passthrough; } } diff --git a/src-tauri/src/input/focus.rs b/src-tauri/src/input/focus.rs index 5156aadc..194c7b0f 100644 --- a/src-tauri/src/input/focus.rs +++ b/src-tauri/src/input/focus.rs @@ -91,7 +91,9 @@ impl FocusElement { /// How a text selection was captured. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SelectionMethod { - /// Read directly via the Accessibility `AXSelectedText` attribute. + /// Read directly through the platform accessibility API — `AXSelectedText` + /// on macOS, UI Automation `TextPattern` on Windows. Clean: no keystrokes + /// synthesized, clipboard untouched. Ax, /// Read by synthesizing Cmd+C and reading the clipboard. Copy, @@ -260,8 +262,35 @@ pub fn capture_selection(element: Option<&FocusElement>) -> Option) -> Option { + let text = crate::input::uia::selected_text()?; + tracing::info!(target: "edit", chars = text.len(), "capture_selection: via UIA TextPattern"); + Some(SelectionSnapshot { text, method: SelectionMethod::Ax }) +} + +/// No accessibility-based selection reader on this platform. Refusing beats a +/// synthetic copy, which leaks the held hotkey's auto-repeat into the focused +/// app (see the Windows arm above); the coordinator aborts cleanly and tells +/// the user there is nothing to edit. +#[cfg(not(any(target_os = "macos", target_os = "windows")))] pub fn capture_selection(_element: Option<&FocusElement>) -> Option { + tracing::warn!( + target: "edit", + "capture_selection: not supported on this platform — needs an \ + accessibility selection reader (AXSelectedText / UIA TextPattern)" + ); None } diff --git a/src-tauri/src/input/mod.rs b/src-tauri/src/input/mod.rs index 601474cf..f5335a57 100644 --- a/src-tauri/src/input/mod.rs +++ b/src-tauri/src/input/mod.rs @@ -3,3 +3,5 @@ pub mod focus; pub mod hotkeys; pub mod paste; pub mod trigger; +#[cfg(target_os = "windows")] +pub mod uia; diff --git a/src-tauri/src/input/paste.rs b/src-tauri/src/input/paste.rs index da379eb1..9982479c 100644 --- a/src-tauri/src/input/paste.rs +++ b/src-tauri/src/input/paste.rs @@ -132,6 +132,12 @@ fn synthesize_cmd_v() -> Result<(), PasteError> { /// Cmd+C, return the selected text — or `None` if the clipboard did not change /// (which we treat as "nothing was selected"). Kept pure so it is unit-testable /// without a live app. +/// +/// ⚠️ Unsound when `before` is `None`: a failed pre-read makes *any* stale +/// clipboard content compare as "changed". Prefer +/// [`selection_from_sentinel`], which does not depend on reading the +/// user's clipboard at all. Retained because it is the right check when the +/// pre-read is known to have succeeded. pub fn selection_from_clipboard_delta(before: Option<&str>, after: Option<&str>) -> Option { match after { Some(a) if !a.is_empty() && Some(a) != before => Some(a.to_string()), @@ -139,10 +145,35 @@ pub fn selection_from_clipboard_delta(before: Option<&str>, after: Option<&str>) } } -/// Fallback selection capture: synthesize Cmd+C, read the clipboard, then -/// restore the user's original clipboard. Returns the selected text, or `None` -/// if nothing changed / the copy failed. macOS-only. -#[cfg(target_os = "macos")] +/// Marker written to the clipboard immediately before the synthetic copy. +/// +/// Deliberately not empty: `set_text("")` is unreliable across platforms +/// (some clipboards drop the entry entirely rather than storing an empty +/// string), which would put us right back to guessing. +const COPY_SENTINEL: &str = "\u{200B}echo-scribe-copy-probe\u{200B}"; + +/// Pure decision helper: did the synthetic copy actually replace our sentinel? +/// +/// This is the sound version of [`selection_from_clipboard_delta`]. Because we +/// *wrote* the pre-state ourselves, "unchanged" is provable rather than +/// inferred, so a clipboard whose pre-read failed can no longer be mistaken for +/// a fresh selection. Fixes real Windows behaviour: `capture_selection` grabbed +/// a URL copied minutes earlier and handed it to the editor as if the user had +/// selected it. +pub fn selection_from_sentinel(after: Option<&str>) -> Option { + match after { + Some(a) if !a.is_empty() && a != COPY_SENTINEL => Some(a.to_string()), + _ => None, + } +} + +/// Fallback selection capture: synthesize the platform copy chord, read the +/// clipboard, then restore the user's original clipboard. Returns the selected +/// text, or `None` if nothing changed / the copy failed. +/// +/// Platform-neutral: only [`synthesize_cmd_c`] differs per OS (CoreGraphics on +/// macOS, enigo elsewhere). On Windows this is the *only* selection path, since +/// the AX-based `focus::capture_selection` has no Win32 equivalent yet. pub fn capture_selection_via_copy() -> Option { use arboard::Clipboard; let mut clipboard = match Clipboard::new() { @@ -153,28 +184,58 @@ pub fn capture_selection_via_copy() -> Option { } }; let before = clipboard.get_text().ok(); + + // Overwrite with a known marker so "nothing was copied" is provable. Without + // this, a failed `before` read (routine on Windows — the clipboard is one + // global resource that browsers and clipboard managers lock constantly) makes + // leftover content look like a brand-new selection. + if let Err(e) = clipboard.set_text(COPY_SENTINEL) { + warn!(target: "edit", ?e, "capture_selection_via_copy: failed to write probe sentinel"); + return None; + } + if let Err(e) = synthesize_cmd_c() { - warn!(target: "edit", ?e, "capture_selection_via_copy: Cmd+C synthesis failed"); + warn!(target: "edit", ?e, "capture_selection_via_copy: copy synthesis failed"); + restore_clipboard(&mut clipboard, before.as_deref()); return None; } // Give the frontmost app time to service the copy and write the pasteboard. thread::sleep(Duration::from_millis(120)); let after = clipboard.get_text().ok(); - let result = selection_from_clipboard_delta(before.as_deref(), after.as_deref()); - // Best-effort restore of the user's original clipboard. - if let Some(orig) = before { - if let Err(e) = clipboard.set_text(&orig) { - warn!(target: "edit", ?e, "capture_selection_via_copy: failed to restore clipboard"); - } + let result = selection_from_sentinel(after.as_deref()); + if result.is_none() { + info!( + target: "edit", + sentinel_intact = after.as_deref() == Some(COPY_SENTINEL), + after_readable = after.is_some(), + "capture_selection_via_copy: nothing was copied (no active selection?)" + ); } + restore_clipboard(&mut clipboard, before.as_deref()); result } -#[cfg(not(target_os = "macos"))] -pub fn capture_selection_via_copy() -> Option { - None +/// Put the user's clipboard back. Always attempted, including on the failure +/// paths — we overwrote it with a sentinel, so bailing out early without this +/// would leave the probe marker sitting in their clipboard. +fn restore_clipboard(clipboard: &mut arboard::Clipboard, before: Option<&str>) { + match before { + Some(orig) => { + if let Err(e) = clipboard.set_text(orig) { + warn!(target: "edit", ?e, "capture_selection_via_copy: failed to restore clipboard"); + } + } + None => { + // We never managed to read it, so we cannot put it back. Clearing is + // still better than leaving our sentinel behind. + if let Err(e) = clipboard.set_text("") { + warn!(target: "edit", ?e, "capture_selection_via_copy: failed to clear sentinel"); + } + } + } } + /// Synthesize Cmd+C via CoreGraphics (same deterministic approach as /// [`synthesize_cmd_v`], with the Command flag set directly on the keydown). #[cfg(target_os = "macos")] @@ -200,10 +261,63 @@ fn synthesize_cmd_c() -> Result<(), PasteError> { Ok(()) } +/// Synthesize Ctrl+C. Mirrors [`synthesize_cmd_v`]'s non-macOS enigo path so +/// the two chords behave identically on Windows/Linux. +#[cfg(not(target_os = "macos"))] +fn synthesize_cmd_c() -> Result<(), PasteError> { + use enigo::{Direction, Enigo, Key, Keyboard, Settings}; + let mut enigo = + Enigo::new(&Settings::default()).map_err(|e| PasteError::Init(e.to_string()))?; + enigo + .key(Key::Control, Direction::Press) + .map_err(|e| PasteError::Keystroke(e.to_string()))?; + enigo + .key(Key::Unicode('c'), Direction::Click) + .map_err(|e| PasteError::Keystroke(e.to_string()))?; + enigo + .key(Key::Control, Direction::Release) + .map_err(|e| PasteError::Keystroke(e.to_string()))?; + Ok(()) +} + #[cfg(test)] mod tests { use super::*; + /// The bug this replaces: a failed `before` read made stale clipboard + /// content look like a fresh selection. Observed on Windows as a URL copied + /// minutes earlier being handed to the selection editor. + #[test] + fn sentinel_intact_means_nothing_was_copied() { + assert_eq!(selection_from_sentinel(Some(COPY_SENTINEL)), None); + } + + #[test] + fn sentinel_replaced_means_real_selection() { + assert_eq!( + selection_from_sentinel(Some("the highlighted sentence")), + Some("the highlighted sentence".to_string()) + ); + } + + #[test] + fn sentinel_unreadable_or_empty_is_not_a_selection() { + assert_eq!(selection_from_sentinel(None), None); + assert_eq!(selection_from_sentinel(Some("")), None); + } + + /// Regression guard on the old helper: it cannot distinguish stale content + /// from a real selection once `before` is unknown. Documents *why* + /// `capture_selection_via_copy` no longer uses it. + #[test] + fn clipboard_delta_is_unsound_without_a_known_before() { + assert_eq!( + selection_from_clipboard_delta(None, Some("https://github.com/")), + Some("https://github.com/".to_string()), + "documents the unsoundness that motivated the sentinel probe" + ); + } + #[test] fn paste_error_display_messages() { let e = PasteError::Clipboard("test".into()); diff --git a/src-tauri/src/input/trigger.rs b/src-tauri/src/input/trigger.rs index 1b77e0e8..e32b6326 100644 --- a/src-tauri/src/input/trigger.rs +++ b/src-tauri/src/input/trigger.rs @@ -1,10 +1,17 @@ //! Cross-platform dictation trigger helpers. //! //! macOS uses the CGEventTap listener in `hotkeys.rs`. Windows/Linux have no -//! event tap, so we drive the coordinator from the global-shortcut plugin and -//! the in-app Record button. Both funnel into `CoordinatorMsg::Hotkey`. +//! event tap, so we drive the coordinator from the global-shortcut plugin. +//! Both funnel into `CoordinatorMsg::Hotkey`. +//! +//! On Windows/Linux the shortcuts are derived from the *user's configured +//! bindings* (the same ones the Settings UI edits), not a hardcoded default. +//! [`sync_shortcuts`] is idempotent: it clears every registration and rebuilds +//! it from the current bindings, so it can be called at pipeline start and +//! again after any rebind. -use crate::input::hotkeys::HotkeyEvent; +use crate::input::binding::{code_from_key, Binding, ModifierKind}; +use crate::input::hotkeys::{is_modifier, HotkeyEvent}; /// Map a "is the trigger currently active?" boolean to a coordinator hotkey /// transition. `true` => `Pressed` (start capture), `false` => `Released` @@ -17,45 +24,415 @@ pub fn shortcut_state_to_hotkey(pressed: bool) -> HotkeyEvent { } } -/// Register the default Windows/Linux dictation hotkey (Ctrl+Alt+Space, -/// push-to-talk) with the global-shortcut plugin, forwarding Pressed/Released -/// into the coordinator as `Action::VoiceAtCursor`. +/// A hotkey that could not be registered, tagged with the action it belongs to +/// so the settings UI can show the message next to the shortcut that caused it +/// instead of dumping every problem under all four rebinders. +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub struct HotkeyProblem { + /// Stable action slug: "voice-at-cursor" | "log-capture" | "action" | + /// "edit-selection". Matches the `action` prop of `HotkeyRebinder`. + pub action: String, + /// Friendly, already-formatted explanation for the user. + pub message: String, +} + +/// Why a configured binding cannot become a Windows/Linux global shortcut. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum UnsupportedBinding { + /// The primary key is a bare modifier (the macOS default is Right Control + /// on its own). `RegisterHotKey` requires a non-modifier key, so this + /// shape is unrepresentable on Windows. + ModifierOnly, + /// Another Echo Scribe action already claimed this exact combination. + /// The macOS event tap happily lets two actions share a binding; + /// `RegisterHotKey` rejects the duplicate, so we have to say which of our + /// own shortcuts is in the way rather than blaming another app. + DuplicateOf(&'static str), + /// The key has no DOM-code mapping, so we can't translate it. + UnmappableKey, +} + +impl UnsupportedBinding { + /// A short, human-readable explanation for the settings UI. `label` names + /// the action, e.g. "voice-at-cursor". + pub fn message(self, label: &str) -> String { + match self { + Self::ModifierOnly => format!( + "The {label} shortcut is a single modifier key. Windows can only \ + register a shortcut that includes a regular key — add a letter, \ + digit or function key to the combination." + ), + Self::UnmappableKey => { + format!("The {label} shortcut uses a key Windows can't register. Pick another key.") + } + Self::DuplicateOf(other) => format!( + "The {label} shortcut is the same combination as the {other} shortcut. Windows \ + needs a different combination for each action." + ), + } + } +} + +/// Split a binding into the DOM `KeyboardEvent.code` of its primary key plus +/// the distinct modifier kinds it requires. /// -/// Windows can't swallow the keystroke the way the macOS CGEventTap does, so -/// the default is a deliberately non-conflicting combo. Rebinding on Windows is -/// deferred; v1 ships this fixed default. +/// Pure and platform-independent so it unit-tests on every host, including the +/// macOS CI runner. Note that `ModifierSide` is deliberately discarded: +/// Windows' `RegisterHotKey` has no notion of left-vs-right modifiers, so a +/// binding of "Right Alt + Slash" registers as "Alt + Slash" there. +pub fn shortcut_parts( + binding: &Binding, +) -> Result<(&'static str, Vec), UnsupportedBinding> { + if is_modifier(binding.primary.0) { + return Err(UnsupportedBinding::ModifierOnly); + } + let code = code_from_key(binding.primary.0).ok_or(UnsupportedBinding::UnmappableKey)?; + + let mut kinds: Vec = Vec::new(); + for (kind, _side) in &binding.modifiers { + if !kinds.contains(kind) { + kinds.push(*kind); + } + } + // Canonical order, so two bindings that require the same modifiers compare + // equal regardless of the order the user pressed them in. Duplicate + // detection in `sync_shortcuts` relies on this. + kinds.sort_by_key(|k| modifier_rank(*k)); + Ok((code, kinds)) +} + +fn modifier_rank(kind: ModifierKind) -> u8 { + match kind { + ModifierKind::Control => 0, + ModifierKind::Shift => 1, + ModifierKind::Alt => 2, + ModifierKind::Meta => 3, + } +} + #[cfg(not(target_os = "macos"))] -pub fn register_default_dictation_shortcut( - app: &tauri::AppHandle, - coord_tx: tokio::sync::mpsc::UnboundedSender, -) -> Result<(), String> { - use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState}; - - let shortcut = Shortcut::new(Some(Modifiers::CONTROL | Modifiers::ALT), Code::Space); - - app.global_shortcut() - .on_shortcut(shortcut, move |_app, _shortcut, event| { - let ev = shortcut_state_to_hotkey(matches!(event.state(), ShortcutState::Pressed)); - if let Err(e) = coord_tx.send(crate::coordinator::CoordinatorMsg::Hotkey( - crate::coordinator::Action::VoiceAtCursor, - ev, - )) { - tracing::warn!(target: "trigger", ?e, "failed to forward global shortcut to coordinator"); - } +pub use platform::sync_shortcuts; + +#[cfg(not(target_os = "macos"))] +mod platform { + use tauri::AppHandle; + use tauri_plugin_global_shortcut::{ + Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState, + }; + + use super::{shortcut_parts, shortcut_state_to_hotkey, HotkeyProblem}; + use crate::commands::AppState; + use crate::coordinator::{Action, CoordinatorMsg}; + use crate::input::binding::{Binding, ModifierKind}; + + /// The bindings we register, paired with the action they drive and the + /// label used in user-facing problem messages. + fn configured(state: &AppState) -> Vec<(Action, &'static str, Binding)> { + let read = |lock: &std::sync::RwLock| lock.read().ok().map(|g| g.clone()); + let mut out = Vec::new(); + if let Some(b) = read(&state.binding) { + out.push((Action::VoiceAtCursor, "voice-at-cursor", b)); + } + if let Some(b) = read(&state.log_capture_binding) { + out.push((Action::LogCapture, "log-capture", b)); + } + if let Some(b) = read(&state.action_binding) { + out.push((Action::ActionCommand, "action", b)); + } + if let Some(b) = read(&state.edit_selection_binding) { + out.push((Action::EditSelection, "edit-selection", b)); + } + out + } + + fn modifiers_for(kinds: &[ModifierKind]) -> Modifiers { + let mut mods = Modifiers::empty(); + for kind in kinds { + mods |= match kind { + ModifierKind::Control => Modifiers::CONTROL, + ModifierKind::Shift => Modifiers::SHIFT, + ModifierKind::Alt => Modifiers::ALT, + ModifierKind::Meta => Modifiers::META, + }; + } + mods + } + + /// DOM `KeyboardEvent.code` string -> `Code`. + /// + /// Explicit rather than `Code::from_str` so an unmappable key is a + /// `None` we report, never a panic or a silently-wrong key. The input + /// strings come from `binding::code_from_key`, so this table mirrors it. + fn code_for(code: &str) -> Option { + Some(match code { + "Space" => Code::Space, + "Tab" => Code::Tab, + "Escape" => Code::Escape, + "Enter" => Code::Enter, + "Backspace" => Code::Backspace, + "KeyA" => Code::KeyA, + "KeyB" => Code::KeyB, + "KeyC" => Code::KeyC, + "KeyD" => Code::KeyD, + "KeyE" => Code::KeyE, + "KeyF" => Code::KeyF, + "KeyG" => Code::KeyG, + "KeyH" => Code::KeyH, + "KeyI" => Code::KeyI, + "KeyJ" => Code::KeyJ, + "KeyK" => Code::KeyK, + "KeyL" => Code::KeyL, + "KeyM" => Code::KeyM, + "KeyN" => Code::KeyN, + "KeyO" => Code::KeyO, + "KeyP" => Code::KeyP, + "KeyQ" => Code::KeyQ, + "KeyR" => Code::KeyR, + "KeyS" => Code::KeyS, + "KeyT" => Code::KeyT, + "KeyU" => Code::KeyU, + "KeyV" => Code::KeyV, + "KeyW" => Code::KeyW, + "KeyX" => Code::KeyX, + "KeyY" => Code::KeyY, + "KeyZ" => Code::KeyZ, + "Digit0" => Code::Digit0, + "Digit1" => Code::Digit1, + "Digit2" => Code::Digit2, + "Digit3" => Code::Digit3, + "Digit4" => Code::Digit4, + "Digit5" => Code::Digit5, + "Digit6" => Code::Digit6, + "Digit7" => Code::Digit7, + "Digit8" => Code::Digit8, + "Digit9" => Code::Digit9, + "F1" => Code::F1, + "F2" => Code::F2, + "F3" => Code::F3, + "F4" => Code::F4, + "F5" => Code::F5, + "F6" => Code::F6, + "F7" => Code::F7, + "F8" => Code::F8, + "F9" => Code::F9, + "F10" => Code::F10, + "F11" => Code::F11, + "F12" => Code::F12, + "Period" => Code::Period, + "Comma" => Code::Comma, + "Semicolon" => Code::Semicolon, + "Quote" => Code::Quote, + "BracketLeft" => Code::BracketLeft, + "BracketRight" => Code::BracketRight, + "Backslash" => Code::Backslash, + "Slash" => Code::Slash, + "Minus" => Code::Minus, + "Equal" => Code::Equal, + "Backquote" => Code::Backquote, + _ => return None, }) - .map_err(|e| format!("failed to register dictation hotkey: {e}"))?; + } + + /// Re-register every global shortcut from the user's current bindings. + /// + /// Returns one friendly message per binding that could NOT be registered — + /// either because Windows can't express it, or because another running + /// application already owns the combination. The caller stores these so the + /// settings UI can show them; the technical detail goes to the log. + /// + /// Safe to call repeatedly: it unregisters everything first. + pub fn sync_shortcuts(app: &AppHandle, state: &AppState) -> Vec { + let mut problems: Vec = Vec::new(); + let problem = |action: &str, message: String| HotkeyProblem { + action: action.to_string(), + message, + }; + + if let Err(e) = app.global_shortcut().unregister_all() { + tracing::warn!(target: "trigger", ?e, "failed to clear existing global shortcuts"); + } + + let coord_tx = match state.coord_tx.lock() { + Ok(guard) => guard.clone(), + Err(_) => { + tracing::warn!(target: "trigger", "coordinator channel lock poisoned"); + None + } + }; + let Some(coord_tx) = coord_tx else { + // The pipeline hasn't started yet; ensure_pipeline_started will + // call us again once the channel exists. + tracing::info!(target: "trigger", "no coordinator channel yet; skipping shortcut sync"); + return problems; + }; + + // (code, modifiers) pairs already taken, with the action that took + // them — so a self-collision reports the real culprit. + let mut claimed: Vec<(&'static str, Vec, &'static str)> = Vec::new(); + + for (action, label, binding) in configured(state) { + let (code, kinds) = match shortcut_parts(&binding) { + Ok(parts) => parts, + Err(e) => { + tracing::warn!(target: "trigger", %label, ?e, "binding unsupported on this platform"); + problems.push(problem(label, e.message(label))); + continue; + } + }; + // Reject a duplicate before asking the OS, so the message names our + // own conflicting shortcut instead of implying a foreign app. + if let Some((_, _, other)) = claimed + .iter() + .find(|(c, k, _)| *c == code && *k == kinds) + { + let e = super::UnsupportedBinding::DuplicateOf(other); + tracing::warn!(target: "trigger", %label, %other, "two actions share one binding"); + problems.push(problem(label, e.message(label))); + continue; + } + claimed.push((code, kinds.clone(), label)); + + let Some(code) = code_for(code) else { + let e = super::UnsupportedBinding::UnmappableKey; + tracing::warn!(target: "trigger", %label, %code, "no Code mapping for key"); + problems.push(problem(label, e.message(label))); + continue; + }; + + let shortcut = Shortcut::new(Some(modifiers_for(&kinds)), code); + // Rendered before `shortcut` is handed to the plugin so the log + // lines below don't depend on it still being ours to borrow. + let desc = format!("{shortcut:?}"); + let tx = coord_tx.clone(); + let result = app + .global_shortcut() + .on_shortcut(shortcut, move |_app, _shortcut, event| { + let ev = shortcut_state_to_hotkey(matches!(event.state(), ShortcutState::Pressed)); + if let Err(e) = tx.send(CoordinatorMsg::Hotkey(action, ev)) { + tracing::warn!(target: "trigger", ?e, "failed to forward global shortcut to coordinator"); + } + }); + + match result { + Ok(()) => { + tracing::info!(target: "trigger", %label, %desc, "registered global shortcut"); + } + Err(e) => { + // The overwhelmingly common cause on Windows is another + // running app already owning the combination — + // RegisterHotKey is first-come-first-served process-wide. + tracing::error!(target: "trigger", %label, %desc, %e, "failed to register global shortcut"); + problems.push(problem( + label, + format!( + "Couldn't register the {label} shortcut. Another application is \ + probably already using that key combination — pick a different one." + ), + )); + } + } + } - tracing::info!(target: "trigger", "registered default dictation hotkey Ctrl+Alt+Space"); - Ok(()) + problems + } } #[cfg(test)] mod tests { use super::*; + use crate::input::binding::{ModifierSide, SerKey}; + use rdev::Key; #[test] fn maps_pressed_and_released() { assert_eq!(shortcut_state_to_hotkey(true), HotkeyEvent::Pressed); assert_eq!(shortcut_state_to_hotkey(false), HotkeyEvent::Released); } + + #[test] + fn combo_splits_into_code_and_modifiers() { + let b = Binding { + primary: SerKey(Key::Slash), + modifiers: vec![(ModifierKind::Alt, ModifierSide::Right)], + }; + let (code, kinds) = shortcut_parts(&b).expect("combo should be supported"); + assert_eq!(code, "Slash"); + assert_eq!(kinds, vec![ModifierKind::Alt]); + } + + /// Left/right is meaningless to RegisterHotKey, so the same modifier bound + /// on both sides must collapse to one entry rather than being applied twice. + #[test] + fn duplicate_modifier_kinds_collapse() { + let b = Binding { + primary: SerKey(Key::KeyK), + modifiers: vec![ + (ModifierKind::Control, ModifierSide::Left), + (ModifierKind::Control, ModifierSide::Right), + ], + }; + let (_, kinds) = shortcut_parts(&b).expect("supported"); + assert_eq!(kinds, vec![ModifierKind::Control]); + } + + /// THE Windows gap: the shipped macOS default (bare Right Control) can't + /// be a global shortcut, so it must be reported rather than silently + /// dropped — that silence is what made the mic look dead on Windows. + #[test] + fn bare_modifier_primary_is_unsupported() { + assert_eq!( + shortcut_parts(&Binding::single(Key::ControlRight)), + Err(UnsupportedBinding::ModifierOnly) + ); + assert_eq!( + shortcut_parts(&Binding::single(Key::AltGr)), + Err(UnsupportedBinding::ModifierOnly) + ); + } + + #[test] + fn single_non_modifier_key_is_supported() { + let (code, kinds) = shortcut_parts(&Binding::single(Key::F8)).expect("supported"); + assert_eq!(code, "F8"); + assert!(kinds.is_empty()); + } + + /// Modifier order must not affect equality: Echo Scribe's own default + /// bindings collided on Windows (`RegisterHotKey` rejects duplicates where + /// the macOS event tap allowed them), and duplicate detection compares + /// these vectors. + #[test] + fn modifier_order_is_canonical() { + let ctrl_shift = Binding { + primary: SerKey(Key::KeyD), + modifiers: vec![ + (ModifierKind::Shift, ModifierSide::Either), + (ModifierKind::Control, ModifierSide::Either), + ], + }; + let shift_ctrl = Binding { + primary: SerKey(Key::KeyD), + modifiers: vec![ + (ModifierKind::Control, ModifierSide::Either), + (ModifierKind::Shift, ModifierSide::Either), + ], + }; + assert_eq!( + shortcut_parts(&ctrl_shift).unwrap(), + shortcut_parts(&shift_ctrl).unwrap() + ); + } + + #[test] + fn duplicate_message_names_the_conflicting_action() { + let msg = UnsupportedBinding::DuplicateOf("log-capture").message("voice-at-cursor"); + assert!(msg.contains("log-capture"), "got {msg}"); + assert!(msg.contains("voice-at-cursor"), "got {msg}"); + } + + #[test] + fn messages_name_the_action() { + let msg = UnsupportedBinding::ModifierOnly.message("voice-at-cursor"); + assert!(msg.contains("voice-at-cursor"), "got {msg}"); + } } diff --git a/src-tauri/src/input/uia.rs b/src-tauri/src/input/uia.rs new file mode 100644 index 00000000..1040e82e --- /dev/null +++ b/src-tauri/src/input/uia.rs @@ -0,0 +1,168 @@ +//! Windows UI Automation selection reader — the Win32 counterpart to macOS +//! `AXSelectedText`. +//! +//! Reads the focused element's live text selection through +//! `IUIAutomationTextPattern::GetSelection`, synthesizing **no input at all**. +//! That property is the whole point of this module. +//! +//! The edit-selection hotkey captures at PRESS time, while the user is still +//! physically holding the trigger. The obvious shortcut — synthesize Ctrl+C and +//! read the clipboard — is unusable there: `enigo` ends its chord by releasing +//! Ctrl, so Windows sees Ctrl go up, the still-held primary key stops matching +//! the `RegisterHotKey` combination, and its auto-repeat is delivered to the +//! focused app as a stream of characters typed into the user's document. That +//! path was tried, shipped, and reverted (commit 9856d20). Reading the +//! selection out of band is the fix. +//! +//! Caveat worth knowing when reading logs: `TextPattern` is a per-control +//! opt-in. Standard Win32 edits, RichEdit, UWP/WinUI text, Office, and +//! Chromium-based apps (Chrome, Edge, Electron, VS Code) expose it; a few +//! custom-drawn editors (some terminals, some Java/Qt apps) do not. When it is +//! missing we return `None` and the coordinator shows its "select text first" +//! hint, which is why every failure step below logs a distinct reason. + +use std::sync::mpsc; +use std::time::Duration; + +use tracing::{info, warn}; +use windows::Win32::System::Com::{ + CoCreateInstance, CoInitializeEx, CoUninitialize, CLSCTX_INPROC_SERVER, + COINIT_APARTMENTTHREADED, +}; +use windows::Win32::UI::Accessibility::{ + CUIAutomation, IUIAutomation, IUIAutomationTextPattern, UIA_TextPatternId, +}; + +/// Hard deadline for the whole UIA round-trip. A hung or busy provider (an app +/// pumping a modal loop, a slow Electron a11y server) must never stall the +/// hotkey — the press handler is on the coordinator's critical path. +const UIA_DEADLINE: Duration = Duration::from_millis(600); + +/// Read the current selection from the focused UI element. +/// +/// Runs the COM work on a throwaway thread so a wedged provider costs us +/// `UIA_DEADLINE` and nothing more. Only the resulting `String` crosses the +/// thread boundary; every COM pointer is created, used, and dropped inside the +/// worker, which is what keeps this sound despite COM interfaces being `!Send`. +pub fn selected_text() -> Option { + let (tx, rx) = mpsc::channel(); + std::thread::spawn(move || { + let _ = tx.send(read_selection()); + }); + + match rx.recv_timeout(UIA_DEADLINE) { + Ok(result) => result, + Err(_) => { + warn!( + target: "edit", + deadline_ms = UIA_DEADLINE.as_millis() as u64, + "uia: selection read timed out; treating as no selection" + ); + None + } + } +} + +/// COM lifecycle wrapper. Initializes an STA on this worker thread (UIA +/// clients are documented to work from either apartment; STA matches what the +/// providers we talk to expect) and always uninitializes, including on the +/// error paths. +fn read_selection() -> Option { + unsafe { + if let Err(e) = CoInitializeEx(None, COINIT_APARTMENTTHREADED).ok() { + warn!(target: "edit", error = %e, "uia: CoInitializeEx failed"); + return None; + } + let result = read_selection_com(); + CoUninitialize(); + result + } +} + +unsafe fn read_selection_com() -> Option { + let automation: IUIAutomation = + match CoCreateInstance(&CUIAutomation, None, CLSCTX_INPROC_SERVER) { + Ok(a) => a, + Err(e) => { + warn!(target: "edit", error = %e, "uia: CoCreateInstance(CUIAutomation) failed"); + return None; + } + }; + + let element = match automation.GetFocusedElement() { + Ok(el) => el, + Err(e) => { + info!(target: "edit", error = %e, "uia: GetFocusedElement failed"); + return None; + } + }; + + // `GetCurrentPatternAs` does the QueryInterface for us, so an element that + // simply doesn't implement TextPattern surfaces as an Err here rather than + // as a silently-null IUnknown from the plain `GetCurrentPattern`. + let text_pattern: IUIAutomationTextPattern = + match element.GetCurrentPatternAs(UIA_TextPatternId) { + Ok(p) => p, + Err(e) => { + info!( + target: "edit", + error = %e, + "uia: focused element does not support TextPattern" + ); + return None; + } + }; + + let ranges = match text_pattern.GetSelection() { + Ok(r) => r, + Err(e) => { + info!(target: "edit", error = %e, "uia: GetSelection failed"); + return None; + } + }; + let count = ranges.Length().unwrap_or(0); + if count <= 0 { + info!(target: "edit", "uia: no selection ranges on focused element"); + return None; + } + + // Disjoint selections (a multi-select grid, a column selection) come back + // as several ranges. Join them so we edit everything the user highlighted + // rather than silently dropping all but the first block. + let mut out = String::new(); + for i in 0..count { + let Ok(range) = ranges.GetElement(i) else { + continue; + }; + // -1 = no length cap. The coordinator enforces the real limit via + // `llm::edit::within_length_limit`, which reports it to the user. + let Ok(bstr) = range.GetText(-1) else { + continue; + }; + let chunk = bstr.to_string(); + if chunk.is_empty() { + continue; + } + if !out.is_empty() { + out.push('\n'); + } + out.push_str(&chunk); + } + + if out.trim().is_empty() { + info!( + target: "edit", + ranges = count, + "uia: selection ranges are empty (caret only, no highlighted text)" + ); + return None; + } + + info!( + target: "edit", + ranges = count, + chars = out.len(), + "uia: read selection via TextPattern" + ); + Some(out) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e302bd3b..9910a03f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -59,6 +59,7 @@ use crate::commands::{ get_format_templates, get_item, get_llm_unload_secs, get_log_capture_binding, get_mute_while_recording, get_onboarding_completed, get_project_auto_tagging_enabled, get_recording_project, get_screenrec_audio_prefs, get_trigger_word_routing_enabled, + get_hotkey_problems, get_voice_at_cursor_binding, hide_countdown_overlay, import_editor_background, @@ -107,16 +108,28 @@ use crate::ui::tray::TrayHandle; /// Resolve the directory crash logs are rotated into. Public so that the /// `diagnostics_log_dir` Tauri command (Settings → Diagnostics) can return /// the same path the appender writes to. +#[cfg(target_os = "macos")] pub fn log_dir() -> std::path::PathBuf { dirs::home_dir() .map(|h| h.join("Library/Logs/EchoScribe")) .unwrap_or_else(std::env::temp_dir) } +/// Non-macOS hosts have no `~/Library`; use the platform's local-data dir +/// (`%LOCALAPPDATA%\EchoScribe\logs` on Windows) so logs land somewhere the +/// OS actually expects rather than in a macOS-shaped path under the home dir. +#[cfg(not(target_os = "macos"))] +pub fn log_dir() -> std::path::PathBuf { + dirs::data_local_dir() + .map(|d| d.join("EchoScribe").join("logs")) + .unwrap_or_else(std::env::temp_dir) +} + #[cfg(test)] mod tests { use super::*; + #[cfg(target_os = "macos")] #[test] fn log_dir_resolves_under_library_logs_when_home_present() { // We can't easily mock `dirs::home_dir()`, but on the host where @@ -136,6 +149,24 @@ mod tests { assert_eq!(p, std::env::temp_dir()); } } + + /// Windows/Linux must not put logs in a macOS-shaped `~/Library` path. + #[cfg(not(target_os = "macos"))] + #[test] + fn log_dir_resolves_under_local_data_dir() { + let p = log_dir(); + let s = p.to_string_lossy(); + if let Some(local) = dirs::data_local_dir() { + assert!( + s.starts_with(local.to_string_lossy().as_ref()), + "log dir {s} should sit under local data dir {}", + local.display() + ); + assert!(!s.contains("Library"), "got a macOS-shaped path: {s}"); + } else { + assert_eq!(p, std::env::temp_dir()); + } + } } #[cfg_attr(mobile, tauri::mobile_entry_point)] @@ -176,6 +207,19 @@ pub fn run() { info!(accelerator = %OrtAccelerator::CpuOnly, "ORT accelerator selected"); tauri::Builder::default() + // Must be registered first (plugin requirement). Without it a second + // launch starts a second process that loses the global-shortcut race + // against the first — on Windows `RegisterHotKey` is first-come- + // first-served, so the new instance silently has NO working hotkey. + // Focus the existing window instead. + .plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| { + use tauri::Manager; + if let Some(window) = app.get_webview_window("main") { + let _ = window.show(); + let _ = window.unminimize(); + let _ = window.set_focus(); + } + })) .on_window_event(|window, event| { if window.label() == "main" { if let tauri::WindowEvent::CloseRequested { api, .. } = event { @@ -206,6 +250,7 @@ pub fn run() { } }) .plugin(tauri_plugin_shell::init()) + .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_store::Builder::default().build()) .plugin(tauri_plugin_notification::init()) @@ -230,6 +275,7 @@ pub fn run() { request_screen_recording_access, get_voice_at_cursor_binding, update_voice_at_cursor_binding, + get_hotkey_problems, get_log_capture_binding, update_log_capture_binding, get_action_binding, @@ -678,6 +724,7 @@ pub fn run() { action_binding, edit_selection_binding, hotkey_started: AtomicBool::new(false), + hotkey_problems: Arc::new(std::sync::RwLock::new(Vec::new())), paused_hotkeys: Arc::clone(&paused_hotkeys), rebinding, coord_tx: Mutex::new(None), diff --git a/src-tauri/src/llm/action_launcher.rs b/src-tauri/src/llm/action_launcher.rs index 028aecff..3d09c6fd 100644 --- a/src-tauri/src/llm/action_launcher.rs +++ b/src-tauri/src/llm/action_launcher.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use std::process::Command; use tauri::{AppHandle, Manager}; +use tauri_plugin_opener::OpenerExt; use tracing::{debug, info, warn}; @@ -143,7 +143,59 @@ fn parse_raw_action(raw: &str) -> Result { serde_json::from_str::(slice).map_err(|e| e.to_string()) } -/// Execute a classified action on macOS on behalf of the user +/// Launch a desktop application by user-facing name. +/// +/// This is the one action with no cross-platform primitive: `open -a` is a macOS +/// idiom, and the opener plugin opens *paths and URLs*, not app names. So it +/// stays cfg-split. +#[cfg(target_os = "macos")] +fn launch_app_by_name(app_name: &str) -> Result<(), ActionError> { + let status = std::process::Command::new("open") + .arg("-a") + .arg(app_name) + .status() + .map_err(|e| ActionError::Execute(e.to_string()))?; + if !status.success() { + return Err(ActionError::Execute(format!( + "Failed to launch app '{app_name}'. Check if it is installed." + ))); + } + Ok(()) +} + +/// Windows equivalent of `open -a`: `start` resolves names registered under +/// App Paths (e.g. "chrome", "notepad", "excel") as well as anything on PATH. +/// +/// The empty `""` argument is the window *title* that `start` otherwise steals +/// from a quoted program name — omit it and `start "Chrome"` opens a console +/// titled "Chrome" instead of launching anything. +#[cfg(target_os = "windows")] +fn launch_app_by_name(app_name: &str) -> Result<(), ActionError> { + let status = std::process::Command::new("cmd") + .args(["/C", "start", "", app_name]) + .status() + .map_err(|e| ActionError::Execute(e.to_string()))?; + if !status.success() { + return Err(ActionError::Execute(format!( + "Failed to launch app '{app_name}'. Check if it is installed." + ))); + } + Ok(()) +} + +#[cfg(not(any(target_os = "macos", target_os = "windows")))] +fn launch_app_by_name(app_name: &str) -> Result<(), ActionError> { + Err(ActionError::Execute(format!( + "Launching applications by name isn't supported on this platform (wanted '{app_name}')." + ))) +} + +/// Execute a classified action on behalf of the user. +/// +/// URLs and `mailto:` drafts go through the opener plugin, which is +/// cross-platform and — unlike shelling out to `cmd /C start` on Windows — +/// does not treat the `&` between `?subject=` and `&body=` as a command +/// separator and silently truncate the email. pub fn execute_action(app: &AppHandle, cmd: &ActionCommand) -> Result { let action_type = cmd.action_type.as_deref().unwrap_or(""); info!(action_type, "executing voice action command"); @@ -154,19 +206,7 @@ pub fn execute_action(app: &AppHandle, cmd: &ActionCommand) -> Result Result) + .map_err(|e| { + ActionError::Execute(format!("Failed to open mail client draft: {e}")) + })?; increment_stats(app)?; @@ -218,14 +255,11 @@ pub fn execute_action(app: &AppHandle, cmd: &ActionCommand) -> Result) + .map_err(|e| { + ActionError::Execute(format!("Failed to open URL '{sanitized_url}': {e}")) + })?; increment_stats(app)?; diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index f998b5f5..f9df1bc5 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -48,7 +48,9 @@ "resources/tray_recording.png", "resources/tray_recording_dark.png", "resources/tray_transcribing.png", - "resources/tray_transcribing_dark.png" + "resources/tray_transcribing_dark.png", + "resources/tray_thinking.png", + "resources/tray_thinking_dark.png" ], "icon": [ "icons/32x32.png", diff --git a/src/components/HotkeyRebinder.tsx b/src/components/HotkeyRebinder.tsx index 754a9c6b..c20b5ab7 100644 --- a/src/components/HotkeyRebinder.tsx +++ b/src/components/HotkeyRebinder.tsx @@ -1,6 +1,7 @@ import { useEffect, useRef, useState } from "react"; import { invoke } from "@tauri-apps/api/core"; import { + getHotkeyProblems, getVoiceAtCursorBinding, updateVoiceAtCursorBinding, type JsBinding, @@ -25,6 +26,9 @@ type Props = { /// voice-at-cursor binding for backwards compatibility. load?: () => Promise; save?: (b: JsBinding) => Promise; + /// Action slug used to match this rebinder against `getHotkeyProblems()` + /// entries. Defaults to voice-at-cursor, matching `load`/`save`. + action?: string; }; function buildBinding( @@ -58,16 +62,34 @@ function buildBinding( return { primary: primaryCode, modifiers }; } -export default function HotkeyRebinder({ onChange, load, save }: Props) { +export default function HotkeyRebinder({ + onChange, + load, + save, + action = "voice-at-cursor", +}: Props) { const [current, setCurrent] = useState(null); const [loadError, setLoadError] = useState(null); const [capture, setCapture] = useState({ kind: "idle" }); const [saveError, setSaveError] = useState(null); const [saving, setSaving] = useState(false); + const [problem, setProblem] = useState(null); const loader = load ?? getVoiceAtCursorBinding; const saver = save ?? updateVoiceAtCursorBinding; + // Whether the OS accepted this shortcut. On Windows a combination another + // app already owns fails to register, and without this the app looks healthy + // while the hotkey does nothing at all. + const refreshProblem = async () => { + try { + const problems = await getHotkeyProblems(); + setProblem(problems.find((p) => p.action === action)?.message ?? null); + } catch { + // Diagnostics only — never block the rebinder on this. + } + }; + // Load current binding on mount useEffect(() => { let cancelled = false; @@ -79,6 +101,7 @@ export default function HotkeyRebinder({ onChange, load, save }: Props) { if (!cancelled) setLoadError(e instanceof Error ? e.message : String(e)); } + if (!cancelled) await refreshProblem(); })(); return () => { cancelled = true; @@ -152,6 +175,9 @@ export default function HotkeyRebinder({ onChange, load, save }: Props) { setCurrent(capture.binding); onChange?.(capture.binding); setCapture({ kind: "idle" }); + // The backend re-registers on save, so ask whether the new combination + // was actually accepted by the OS. + await refreshProblem(); } catch (e) { const msg = e instanceof Error ? e.message : String(e); setSaveError(`Unsupported key — try a different one. (${msg})`); @@ -229,6 +255,9 @@ export default function HotkeyRebinder({ onChange, load, save }: Props) { )} + {problem && capture.kind === "idle" ? ( +

{problem}

+ ) : null} {saveError ? (

{saveError}

) : null} diff --git a/src/lib/api.ts b/src/lib/api.ts index f274f5fc..027a30f5 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -70,6 +70,14 @@ export const requestScreenRecordingAccess = (): Promise => export const getVoiceAtCursorBinding = (): Promise => invoke("get_voice_at_cursor_binding"); +/** A configured hotkey the OS refused to register, tagged by action slug. */ +export type HotkeyProblem = { action: string; message: string }; + +// Always empty on macOS (the event tap registers nothing); on Windows/Linux a +// combination another app already owns shows up here. +export const getHotkeyProblems = (): Promise => + invoke("get_hotkey_problems"); + export const updateVoiceAtCursorBinding = (binding: JsBinding): Promise => invoke("update_voice_at_cursor_binding", { binding }); diff --git a/src/views/Settings.tsx b/src/views/Settings.tsx index ce8e15ce..c0f415de 100644 --- a/src/views/Settings.tsx +++ b/src/views/Settings.tsx @@ -336,6 +336,7 @@ function LogCapturePage() { subtitle="Hold this key combination to capture a thought, idea, or task — classified locally and saved to your log." > @@ -1773,6 +1774,7 @@ function AppLauncherSettingsSection() {
@@ -1789,6 +1791,7 @@ function AppLauncherSettingsSection() {