From 659da9075844a3064c43201fab9df9df14981afd Mon Sep 17 00:00:00 2001 From: kstorm12 Date: Sat, 25 Jul 2026 20:09:21 -0400 Subject: [PATCH 1/7] fix(windows): drive global hotkeys from the configured bindings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Windows/Linux the hotkey listener in `input/hotkeys.rs` is a no-op stub (the real implementation is a macOS CGEventTap), so the binding the Settings UI writes was never read. Windows instead registered a hardcoded Ctrl+Alt+Space, which meant the hotkey UI was inert and the actual trigger was unchangeable. That combination is also a popular one — Claude desktop's default, among others — and Windows `RegisterHotKey` is first-come-first-served across processes. When another app already owns it, registration failed, and since the configured binding did nothing and there is no in-app record button, there was no way at all to start dictation. The failure surfaced only as a `tracing::warn!`, so the app looked healthy while doing nothing. - `trigger::sync_shortcuts` now rebuilds every global shortcut from the four configured bindings. It is idempotent (unregisters first), runs at pipeline start, and re-runs after every rebind, so Settings works on Windows. - Registration problems are returned as `HotkeyProblem { action, message }`, stored on `AppState`, and rendered by `HotkeyRebinder` next to the shortcut that caused them — a taken or unregistrable combination is now visible. - `RegisterHotKey` cannot express a bare modifier, so the shipped macOS default (Right Control alone) is reported as unsupported rather than silently ignored. Left/right modifier sides are folded, as Windows has no notion of them. - Add `tauri-plugin-single-instance`: a second launch previously started a second process that lost the hotkey race to the first and ended up equally dead. Focus the existing window instead. - Fix `log_dir()`, which returned the macOS `~/Library/Logs` path on every platform, to use the local data dir off macOS. Fixes desduvauchelle/echo-scribe-landing#2 Co-Authored-By: Claude Opus 5 (1M context) --- docs/WINDOWS.md | 26 ++- src-tauri/Cargo.toml | 2 + src-tauri/src/commands.rs | 112 ++++++--- src-tauri/src/input/trigger.rs | 363 +++++++++++++++++++++++++++--- src-tauri/src/lib.rs | 46 ++++ src/components/HotkeyRebinder.tsx | 31 ++- src/lib/api.ts | 8 + src/views/Settings.tsx | 3 + 8 files changed, 527 insertions(+), 64 deletions(-) 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.toml b/src-tauri/Cargo.toml index 51e00374..f7e9d11f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -46,6 +46,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 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/input/trigger.rs b/src-tauri/src/input/trigger.rs index 1b77e0e8..344892f6 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,343 @@ 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, + /// 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.") + } + } + } +} + +/// 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); + } + } + Ok((code, kinds)) +} + #[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}"))?; + } - tracing::info!(target: "trigger", "registered default dictation hotkey Ctrl+Alt+Space"); - Ok(()) + /// 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; + }; + + 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; + } + }; + 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." + ), + )); + } + } + } + + 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()); + } + + #[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/lib.rs b/src-tauri/src/lib.rs index e302bd3b..47de7c12 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 { @@ -230,6 +274,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 +723,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/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() {
From e1a78d8cab26780744b15200d6e32a3bb01507c7 Mon Sep 17 00:00:00 2001 From: kstorm12 Date: Sat, 25 Jul 2026 20:47:23 -0400 Subject: [PATCH 2/7] fix(windows): name our own shortcut on a duplicate-binding collision Two Echo Scribe actions bound to the same combination is legal on macOS (the CGEventTap just sees the same keys satisfy two bindings) but RegisterHotKey rejects the second registration. That surfaced as the generic "another application is probably already using that combination" message, sending the user hunting for a conflict that is actually between two of our own hotkeys. Observed on a real Windows run: voice-at-cursor and log-capture were both Shift+T, and the failure carried the SAME HotKey id as the successful one. Detect the collision before asking the OS and report which action holds the combination. Modifier order is now canonicalised in shortcut_parts so two bindings requiring the same modifiers compare equal. Co-Authored-By: Claude Opus 5 (1M context) --- src-tauri/src/input/trigger.rs | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src-tauri/src/input/trigger.rs b/src-tauri/src/input/trigger.rs index 344892f6..e32b6326 100644 --- a/src-tauri/src/input/trigger.rs +++ b/src-tauri/src/input/trigger.rs @@ -43,6 +43,11 @@ pub enum UnsupportedBinding { /// 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, } @@ -60,6 +65,10 @@ impl UnsupportedBinding { 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." + ), } } } @@ -85,9 +94,22 @@ pub fn shortcut_parts( 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 use platform::sync_shortcuts; @@ -244,6 +266,10 @@ mod platform { 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, @@ -253,6 +279,19 @@ mod platform { 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"); @@ -358,6 +397,39 @@ mod tests { 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"); From e64bfde9fdab90926054894834bb8f48d99475a0 Mon Sep 17 00:00:00 2001 From: kstorm12 Date: Sat, 25 Jul 2026 21:01:52 -0400 Subject: [PATCH 3/7] feat(windows): make edit-selection capture the selection via Ctrl+C MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `focus::capture_selection` returned None off macOS, so the edit-selection hotkey registered and then silently did nothing — the most confusing kind of half-working feature. macOS tries AXSelectedText first and falls back to a synthetic Cmd+C plus a clipboard-delta read. That fallback is entirely platform-neutral apart from the chord synthesis, so rather than duplicate it, `capture_selection_via_copy` is now compiled on every platform and only `synthesize_cmd_c` is cfg-split — mirroring how `synthesize_cmd_v` already had an enigo path for Windows. Windows therefore reports SelectionMethod::Copy, which means the apply path skips the AX write-back (unavailable anyway, `replace_selected_text` returns -1) and goes through paste_at_cursor's Ctrl+V, replacing the live selection. Co-Authored-By: Claude Opus 5 (1M context) --- src-tauri/src/input/focus.rs | 11 +++++++++++ src-tauri/src/input/paste.rs | 34 ++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/input/focus.rs b/src-tauri/src/input/focus.rs index 5156aadc..b8c4c995 100644 --- a/src-tauri/src/input/focus.rs +++ b/src-tauri/src/input/focus.rs @@ -260,8 +260,19 @@ pub fn capture_selection(element: Option<&FocusElement>) -> Option) -> Option { + if let Some(text) = crate::input::paste::capture_selection_via_copy() { + tracing::info!(target: "edit", chars = text.len(), "capture_selection: via Ctrl+C fallback"); + return Some(SelectionSnapshot { text, method: SelectionMethod::Copy }); + } + tracing::info!(target: "edit", "capture_selection: no selection found (clipboard unchanged)"); None } diff --git a/src-tauri/src/input/paste.rs b/src-tauri/src/input/paste.rs index da379eb1..0bd74bfe 100644 --- a/src-tauri/src/input/paste.rs +++ b/src-tauri/src/input/paste.rs @@ -139,10 +139,13 @@ 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")] +/// 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() { @@ -170,10 +173,6 @@ pub fn capture_selection_via_copy() -> Option { result } -#[cfg(not(target_os = "macos"))] -pub fn capture_selection_via_copy() -> Option { - None -} /// Synthesize Cmd+C via CoreGraphics (same deterministic approach as /// [`synthesize_cmd_v`], with the Command flag set directly on the keydown). @@ -200,6 +199,25 @@ 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::*; From 4e545a77345747fbf46cb755c6c8367b82f2a4d2 Mon Sep 17 00:00:00 2001 From: kstorm12 Date: Sat, 25 Jul 2026 21:08:59 -0400 Subject: [PATCH 4/7] fix(windows): make voice actions (launch app, email, URL) work off macOS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All three executable voice actions shelled out to `Command::new("open")`, which does not exist on Windows. Every one failed with: ERROR coordinator: Voice action execution failed; falling back to standard pipeline error=execution failed: program not found The LLM classified correctly and produced a valid ActionCommand — only the execution line was macOS-only. - draft_email and open_url now go through tauri-plugin-opener, which is cross-platform. Note this is not merely a substitute for `cmd /C start`: `start` treats the `&` between `?subject=` and `&body=` as a command separator, so the naive Windows port would silently truncate every email body. The opener plugin does not have that failure mode. (The JS half, @tauri-apps/plugin-opener, was already in package.json; only the Rust crate was missing.) - launch_app has no cross-platform primitive (the opener plugin opens paths and URLs, not app names), so it stays cfg-split: `open -a` on macOS, `cmd /C start "" ` on Windows, explicit error elsewhere. Also bundle tray_thinking.png / tray_thinking_dark.png. Both exist in resources/ but were absent from tauri.conf.json's resources array, so the "thinking" tray state failed to load on EVERY platform, not just Windows: WARN ui::tray: failed to load tray icon, falling back to solid resolved="...\resources\tray_thinking.png" Co-Authored-By: Claude Opus 5 (1M context) --- src-tauri/Cargo.toml | 4 ++ src-tauri/src/lib.rs | 1 + src-tauri/src/llm/action_launcher.rs | 96 +++++++++++++++++++--------- src-tauri/tauri.conf.json | 4 +- 4 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index f7e9d11f..9ac73c91 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" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 47de7c12..9910a03f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -250,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()) 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", From ef482c3aed609c0fd0f7e023901c89ed02faf18d Mon Sep 17 00:00:00 2001 From: kstorm12 Date: Sun, 26 Jul 2026 06:57:41 -0400 Subject: [PATCH 5/7] fix(edit): prove the synthetic copy happened with a sentinel probe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit capture_selection_via_copy inferred "something was copied" by comparing the clipboard before and after. That comparison is unsound when the pre-read fails, because `before == None` makes ANY leftover clipboard content compare as changed. Observed on Windows: edit-selection captured `https://github.com/` — a URL from an unrelated test minutes earlier — with chars=19 matching that string exactly. The LLM echoed it back and the output sanitizer rejected it, so nothing was pasted, but the selection editor had been handed text the user never selected. Windows makes this easy to hit: the clipboard is a single global resource that browsers and clipboard managers lock constantly, so a transient read failure is routine rather than exotic. Write a known sentinel before synthesizing the copy, then check whether it survived. Because we author the pre-state, "nothing was copied" becomes provable instead of inferred, and an unreadable clipboard can no longer be mistaken for a selection. Restore is now unconditional, including on the early-return paths, so a bail-out can't leave the probe marker in the clipboard. selection_from_clipboard_delta is kept (correct when the pre-read is known to have succeeded) with a test documenting the unsoundness that motivated this. Co-Authored-By: Claude Opus 5 (1M context) --- src-tauri/src/input/paste.rs | 110 ++++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/input/paste.rs b/src-tauri/src/input/paste.rs index 0bd74bfe..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,6 +145,28 @@ pub fn selection_from_clipboard_delta(before: Option<&str>, after: Option<&str>) } } +/// 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. @@ -156,23 +184,57 @@ 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 } +/// 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). @@ -222,6 +284,40 @@ fn synthesize_cmd_c() -> Result<(), PasteError> { 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()); From 9856d20d4c6dd40d234481196f821dcdbb6a6a24 Mon Sep 17 00:00:00 2001 From: kstorm12 Date: Sun, 26 Jul 2026 07:14:51 -0400 Subject: [PATCH 6/7] revert(windows): stop synthesizing Ctrl+C for edit-selection Enabling the copy fallback off macOS leaked keystrokes into the user's document. Reported and reproduced: holding Ctrl+Shift+S in Notepad played the recording-start sound and then typed a stream of "S" into the file. Cause: the coordinator captures the selection at hotkey-PRESS time, while the trigger is still held. enigo's chord ends by RELEASING Ctrl, so the OS now sees Ctrl as up; the still-held primary key keeps auto-repeating, no longer matches the RegisterHotKey combination, Windows stops claiming it, and it goes to the focused app. macOS never had this: the CGEventTap swallows the primary key, and CGEvent sets the Command flag on the event rather than pressing and releasing a real modifier, so global modifier state is untouched. No keystroke-based fix is safe. Re-pressing Ctrl still leaks whatever repeated in the gap, and "Ctrl is already down, just send c" breaks when the binding also holds Shift (Ctrl+Shift+C is not a copy). The correct implementation reads the selection without synthesizing input: UI Automation TextPattern (IUIAutomationTextPattern::GetSelection), the Win32 counterpart to AXSelectedText. capture_selection_via_copy itself is KEPT, including the sentinel-probe soundness fix, which is a genuine improvement to the macOS path. Only the non-macOS call site is withdrawn, with a warn! naming the real fix so this reads as deliberate rather than unimplemented. Co-Authored-By: Claude Opus 5 (1M context) --- src-tauri/src/input/focus.rs | 40 ++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src-tauri/src/input/focus.rs b/src-tauri/src/input/focus.rs index b8c4c995..2525b019 100644 --- a/src-tauri/src/input/focus.rs +++ b/src-tauri/src/input/focus.rs @@ -262,17 +262,39 @@ pub fn capture_selection(element: Option<&FocusElement>) -> Option) -> Option { - if let Some(text) = crate::input::paste::capture_selection_via_copy() { - tracing::info!(target: "edit", chars = text.len(), "capture_selection: via Ctrl+C fallback"); - return Some(SelectionSnapshot { text, method: SelectionMethod::Copy }); - } - tracing::info!(target: "edit", "capture_selection: no selection found (clipboard unchanged)"); + tracing::warn!( + target: "edit", + "capture_selection: not supported on this platform yet — a synthetic copy \ + at press time desyncs the held hotkey's modifiers and leaks keystrokes \ + into the focused app; needs UI Automation TextPattern" + ); None } From e9ee6e9279479b2e9cd4978684d9968f041c81e3 Mon Sep 17 00:00:00 2001 From: kstorm12 Date: Sun, 26 Jul 2026 08:01:12 -0400 Subject: [PATCH 7/7] feat(windows): read the selection via UI Automation TextPattern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Edit-selection has never worked on Windows. capture_selection returned None there, so every attempt aborted at the "select text first" hint: WARN edit: capture_selection: not supported on this platform yet INFO coordinator: edit selection: nothing selected; showing hint That stub was deliberate. 9856d20 withdrew the synthetic Ctrl+C because the coordinator captures at hotkey-PRESS time, while the trigger is still held: enigo's chord ends by releasing Ctrl, the still-held primary key stops matching the RegisterHotKey combination, and its auto-repeat lands in the user's document. That commit named UI Automation TextPattern as the correct fix and left it unimplemented. This implements it. input/uia.rs reads the focused control's selection out of band — GetFocusedElement -> GetCurrentPatternAs(UIA_TextPatternId) -> GetSelection(). No synthesized input, clipboard untouched, so none of the modifier-desync failure modes apply. GetCurrentPatternAs rather than GetCurrentPattern: it does the QueryInterface, so an element with no TextPattern surfaces as an Err instead of a silently-null IUnknown. The COM work runs on a throwaway thread behind a 600 ms deadline. The press handler is on the hotkey's critical path, and a provider pumping a modal loop must cost us the deadline and nothing more. Only the String crosses the thread boundary, which is what keeps it sound given COM interfaces are !Send. TextPattern is a per-control opt-in — Win32 edits, RichEdit, UWP, Office and Chromium expose it, some custom-drawn editors do not. Every failure step logs a distinct reason so an unsupported control reads as itself rather than as "no selection". Also instruments the trigger-word path, which is a separate reported failure with no evidence in the log. Plain voice-at-cursor never logged its transcript, so "the trigger word didn't match" and "the hotkey never fired" were indistinguishable. The DB's items table shows the word absent from every transcript ("Open Google.", "Make this more concise."), so the matcher had nothing to match. The trigger word is spoken FIRST, which makes it exactly the word microphone start-up latency clips, so log both halves: the compared prefix (head=) on a miss, and press_to_capture_ms once the recorder is live. Cargo.lock was already stale — tauri-plugin-opener, added to Cargo.toml in 4e545a7, was never recorded. Re-resolved; the diff is purely additive. Not compiled locally: this machine has no MSVC toolchain, so cargo cannot link. Every signature was verified against the vendored windows-0.58.0 source instead. CI is the compiler. Co-Authored-By: Claude Opus 5 --- src-tauri/Cargo.lock | 105 ++++++++++++++++++++++ src-tauri/Cargo.toml | 10 +++ src-tauri/src/coordinator.rs | 36 ++++++++ src-tauri/src/input/focus.rs | 58 ++++++------ src-tauri/src/input/mod.rs | 2 + src-tauri/src/input/uia.rs | 168 +++++++++++++++++++++++++++++++++++ 6 files changed, 348 insertions(+), 31 deletions(-) create mode 100644 src-tauri/src/input/uia.rs 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 9ac73c91..7db78a3e 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -113,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/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 2525b019..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,40 +262,34 @@ 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 yet — a synthetic copy \ - at press time desyncs the held hotkey's modifiers and leaks keystrokes \ - into the focused app; needs UI Automation TextPattern" + "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/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) +}