A free, open source, and extensible speech-to-text application that works completely offline.
fix: stop blocking the main thread during paste (#2302)
CJ Pais5a9451e70aed
diff --git a/src-tauri/src/actions.rs b/src-tauri/src/actions.rsindex a159841e..921db1b3 100644--- a/src-tauri/src/actions.rs+++ b/src-tauri/src/actions.rs@@ -19,6 +19,7 @@ use log::{debug, error, warn};use once_cell::sync::Lazy;use std::collections::HashMap;use std::future::Future;+use std::sync::atomic::{AtomicU64, Ordering};use std::sync::Arc;use std::time::{Duration, Instant};use tauri::Manager;@@ -26,6 +27,11 @@ use tauri::{AppHandle, Emitter};const CANCELLATION_POLL_INTERVAL: Duration = Duration::from_millis(25);+/// Bumped each time a recording session takes over the tray and overlay. A+/// paste still finishing for an earlier session compares against it so its+/// cleanup doesn't hide the new session's overlay or reset its tray icon.+static RECORDING_SESSION: AtomicU64 = AtomicU64::new(0);+#[derive(Clone, serde::Serialize)]struct RecordingErrorEvent {error_type: String,@@ -419,6 +425,7 @@ impl ShortcutAction for TranscribeAction {}let binding_id = binding_id.to_string();+ RECORDING_SESSION.fetch_add(1, Ordering::SeqCst);let tray_started = Instant::now();set_tray_state(app, TrayIconState::Recording);let tray_elapsed = tray_started.elapsed();@@ -738,32 +745,47 @@ impl ShortcutAction for TranscribeAction {let paste_time = Instant::now();let final_text = processed.final_text;let rm_for_paste = Arc::clone(&rm);- ah.run_on_main_thread(move || {- if rm_for_paste.was_cancelled_since(cancel_generation) {- debug!("Transcription operation cancelled before paste");- utils::hide_recording_overlay(&ah_clone);- set_tray_state(&ah_clone, TrayIconState::Idle);- return;- }-- match utils::paste(final_text, ah_clone.clone()) {- Ok(()) => debug!(- "Text pasted successfully in {:?}",- paste_time.elapsed()- ),- Err(e) => {- error!("Failed to paste transcription: {}", e);- let _ = ah_clone.emit("paste-error", ());+ // Taken before the paste thread exists, and so+ // before the next session can start.+ let session = RECORDING_SESSION.load(Ordering::SeqCst);+ // Paste on its own thread, not the main one: it+ // sleeps between steps, and a paste into Handy's+ // own windows needs the main thread free to land.+ std::thread::Builder::new()+ .name("paste".into())+ .spawn(move || {+ if rm_for_paste.was_cancelled_since(cancel_generation) {+ debug!(+ "Transcription operation cancelled before paste"+ );+ utils::hide_recording_overlay(&ah_clone);+ set_tray_state(&ah_clone, TrayIconState::Idle);+ return;+ }++ match utils::paste(final_text, ah_clone.clone()) {+ Ok(()) => debug!(+ "Text pasted successfully in {:?}",+ paste_time.elapsed()+ ),+ Err(e) => {+ error!("Failed to paste transcription: {}", e);+ let _ = ah_clone.emit("paste-error", ());+ }+ }+ // A new session may have started while+ // this paste ran; the UI is now its.+ if RECORDING_SESSION.load(Ordering::SeqCst) == session {+ utils::hide_recording_overlay(&ah_clone);+ set_tray_state(&ah_clone, TrayIconState::Idle);}- }- utils::hide_recording_overlay(&ah_clone);- set_tray_state(&ah_clone, TrayIconState::Idle);- })- .unwrap_or_else(|e| {- error!("Failed to run paste on main thread: {:?}", e);- utils::hide_recording_overlay(&ah);- set_tray_state(&ah, TrayIconState::Idle);- });+ })+ .map(|_| ())+ .unwrap_or_else(|e| {+ error!("Failed to start the paste thread: {:?}", e);+ utils::hide_recording_overlay(&ah);+ set_tray_state(&ah, TrayIconState::Idle);+ });}}Err(err) => {diff --git a/src-tauri/src/clipboard.rs b/src-tauri/src/clipboard.rsindex 6beba078..7ba2085d 100644--- a/src-tauri/src/clipboard.rs+++ b/src-tauri/src/clipboard.rs@@ -7,6 +7,7 @@ use log::info;use std::process::Command;#[cfg(target_os = "linux")]use std::sync::OnceLock;+use std::sync::{mpsc, Mutex, PoisonError};use std::time::Duration;use tauri::{AppHandle, Manager};use tauri_plugin_clipboard_manager::ClipboardExt;@@ -14,18 +15,42 @@ use tauri_plugin_clipboard_manager::ClipboardExt;#[cfg(target_os = "linux")]use crate::utils::{is_gnome_wayland, is_kde_wayland, is_wayland};-fn with_enigo<T>(+/// Serializes pastes. Each one snapshots and restores the clipboard, so two+/// overlapping pastes would save each other's transcript as "the user's+/// clipboard". Only taken by `paste`, which never runs on the main thread.+static PASTE_LOCK: Mutex<()> = Mutex::new(());++/// Runs `f` with the shared Enigo on the main thread and waits for its result.+///+/// Paste runs on a worker thread so the main thread stays free while it+/// sleeps, which lets Handy's own windows handle a paste aimed at them. Input+/// injection hops back here because macOS's TIS keyboard layout APIs must run+/// on the main thread. Tauri runs the closure inline when already on the main+/// thread, so nested calls can't deadlock.+fn with_enigo<T: Send + 'static>(app_handle: &AppHandle,- f: impl FnOnce(&mut Enigo) -> Result<T, String>,+ f: impl FnOnce(&mut Enigo) -> Result<T, String> + Send + 'static,) -> Result<T, String> {- let enigo_state = app_handle- .try_state::<EnigoState>()- .ok_or("Enigo state not initialized")?;- let mut enigo = enigo_state- .0- .lock()- .map_err(|e| format!("Failed to lock Enigo: {}", e))?;- f(&mut enigo)+ let (tx, rx) = mpsc::sync_channel(1);+ let app = app_handle.clone();+ app_handle+ .run_on_main_thread(move || {+ let result = (|| {+ let enigo_state = app+ .try_state::<EnigoState>()+ .ok_or("Enigo state not initialized")?;+ let mut enigo = enigo_state+ .0+ .lock()+ .map_err(|e| format!("Failed to lock Enigo: {}", e))?;+ f(&mut enigo)+ })();+ let _ = tx.send(result);+ })+ .map_err(|e| format!("Failed to run input on the main thread: {}", e))?;+ // A dropped closure (event loop shutting down) closes the channel.+ rx.recv()+ .map_err(|_| "Main thread dropped the input request".to_string())?}fn write_text_to_clipboard(app_handle: &AppHandle, text: &str) -> Result<(), String> {@@ -87,7 +112,8 @@ fn paste_via_clipboard(// Fall back to enigo if no native tool handled itif !key_combo_sent {- with_enigo(app_handle, |enigo| match paste_method {+ let paste_method = *paste_method;+ with_enigo(app_handle, move |enigo| match paste_method {// The legacy path cannot detect a mistimed chord, so it keeps the// conservative 100ms modifier hold.PasteMethod::CtrlV => input::send_paste_ctrl_v(enigo, 100),@@ -721,7 +747,10 @@ fn paste_direct(info!("Falling back to enigo for direct text input");}- with_enigo(app_handle, |enigo| input::paste_text_direct(enigo, text))+ let text = text.to_string();+ with_enigo(app_handle, move |enigo| {+ input::paste_text_direct(enigo, &text)+ })}pub(crate) fn send_return_key(enigo: &mut Enigo, key_type: AutoSubmitKey) -> Result<(), String> {@@ -771,7 +800,11 @@ fn should_send_auto_submit(auto_submit: bool, paste_method: PasteMethod) -> boolauto_submit && paste_method != PasteMethod::None}+/// Pastes `text` into the focused application. Call it from a worker thread,+/// never the main thread: it sleeps between steps, and only the input+/// injection hops to the main thread (see `with_enigo`).pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {+ let _paste = PASTE_LOCK.lock().unwrap_or_else(PoisonError::into_inner);let settings = get_settings(&app_handle);let paste_method = settings.paste_method;let paste_delay_ms = settings.paste_delay_ms;@@ -810,15 +843,22 @@ pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {// the legacy path untouched.#[cfg(any(target_os = "macos", target_os = "windows"))]if settings.reliable_paste {- let reliable_result = with_enigo(&app_handle, |enigo| {+ // Runs entirely on the main thread, as it requires.+ let (text, app) = (text.clone(), app_handle.clone());+ let (auto_submit, auto_submit_key, clipboard_handling) = (+ settings.auto_submit,+ settings.auto_submit_key,+ settings.clipboard_handling,+ );+ let reliable_result = with_enigo(&app_handle, move |enigo| {crate::paste_tx::try_reliable_paste(&text,- &app_handle,+ &app,&paste_method,enigo,- settings.auto_submit,- settings.auto_submit_key,- settings.clipboard_handling,+ auto_submit,+ auto_submit_key,+ clipboard_handling,)});match reliable_result {@@ -848,8 +888,9 @@ pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {if should_send_auto_submit(settings.auto_submit, paste_method) {std::thread::sleep(Duration::from_millis(50));- if let Err(error) = with_enigo(&app_handle, |enigo| {- send_return_key(enigo, settings.auto_submit_key)+ let auto_submit_key = settings.auto_submit_key;+ if let Err(error) = with_enigo(&app_handle, move |enigo| {+ send_return_key(enigo, auto_submit_key)}) {log::warn!("Paste succeeded, but auto-submit failed: {error}");}diff --git a/src-tauri/src/input.rs b/src-tauri/src/input.rsindex f479f8b0..1f02d581 100644--- a/src-tauri/src/input.rs+++ b/src-tauri/src/input.rs@@ -72,8 +72,9 @@ mod macos {/// held. Including Command is important: non-Latin layouts commonly map/// Cmd shortcuts to their ANSI equivalents, while standard Dvorak does not.///- /// TIS APIs must run on the main thread. Handy's paste path already enters- /// through `AppHandle::run_on_main_thread` before reaching this function.+ /// TIS APIs must run on the main thread. Paste itself runs on a worker,+ /// but its Enigo calls go through `clipboard::with_enigo`, which hops to+ /// the main thread before reaching this function.fn resolve_command_v_keycode() -> Result<u16, String> {// SAFETY: This function is called on the macOS main thread. The returned// source follows the Create Rule and is released by InputSource::drop.