Skip to content

armgit / Handy

Public Imported from GitHub
0

A free, open source, and extensible speech-to-text application that works completely offline.

fix(linux): dispatch paste methods before touching Enigo (#1756)

Kirill Filippenko07a54e29e6e2
diff --git a/src-tauri/src/clipboard.rs b/src-tauri/src/clipboard.rs
index cbf916db..f3ee8bea 100644
--- a/src-tauri/src/clipboard.rs
+++ b/src-tauri/src/clipboard.rs
@@ -14,9 +14,45 @@ use tauri_plugin_clipboard_manager::ClipboardExt;
#[cfg(target_os = "linux")]
use crate::utils::{is_kde_wayland, is_wayland};
+fn with_enigo<T>(
+ app_handle: &AppHandle,
+ f: impl FnOnce(&mut Enigo) -> Result<T, String>,
+) -> 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)
+}
+
+fn write_text_to_clipboard(app_handle: &AppHandle, text: &str) -> Result<(), String> {
+ #[cfg(target_os = "linux")]
+ if is_wayland() && is_wl_copy_available() {
+ info!("Using wl-copy for clipboard write on Wayland");
+ return write_clipboard_via_wl_copy(text);
+ }
+
+ app_handle
+ .clipboard()
+ .write_text(text)
+ .map_err(|e| format!("Failed to write to clipboard: {}", e))
+}
+
+fn finish_clipboard_paste(
+ paste_result: Result<(), String>,
+ paste_delay_after_ms: u64,
+ restore_clipboard: impl FnOnce(),
+) -> Result<(), String> {
+ std::thread::sleep(Duration::from_millis(paste_delay_after_ms));
+ restore_clipboard();
+ paste_result
+}
+
/// Pastes text using the clipboard: saves current content, writes text, sends paste keystroke, restores clipboard.
fn paste_via_clipboard(
- enigo: &mut Enigo,
text: &str,
app_handle: &AppHandle,
paste_method: &PasteMethod,
@@ -35,71 +71,50 @@ fn paste_via_clipboard(
};
// Write text to clipboard first
- // On Wayland, prefer wl-copy for better compatibility (especially with umlauts)
- #[cfg(target_os = "linux")]
- let write_result = if is_wayland() && is_wl_copy_available() {
- info!("Using wl-copy for clipboard write on Wayland");
- write_clipboard_via_wl_copy(text)
- } else {
- clipboard
- .write_text(text)
- .map_err(|e| format!("Failed to write to clipboard: {}", e))
- };
-
- #[cfg(not(target_os = "linux"))]
- let write_result = clipboard
- .write_text(text)
- .map_err(|e| format!("Failed to write to clipboard: {}", e));
-
- write_result?;
+ write_text_to_clipboard(app_handle, text)?;
std::thread::sleep(Duration::from_millis(paste_delay_ms));
- // Send paste key combo
- #[cfg(target_os = "linux")]
- let key_combo_sent = try_send_key_combo_linux(paste_method)?;
-
- #[cfg(not(target_os = "linux"))]
- let key_combo_sent = false;
-
- // Fall back to enigo if no native tool handled it
- if !key_combo_sent {
- 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)?,
- PasteMethod::CtrlShiftV => input::send_paste_ctrl_shift_v(enigo, 100)?,
- PasteMethod::ShiftInsert => input::send_paste_shift_insert(enigo, 100)?,
- _ => return Err("Invalid paste method for clipboard paste".into()),
- }
- }
-
- std::thread::sleep(Duration::from_millis(paste_delay_after_ms));
-
- // Restore original clipboard content.
- // Text takes priority so this path stays identical to the previous behavior;
- // an image is only restored when the clipboard held no text at all, which is
- // the case that used to silently wipe screenshots.
- if let Some(clipboard_content) = saved_text {
- // On Wayland, prefer wl-copy for better compatibility
+ // Capture key injection errors so the original clipboard is restored before
+ // propagating them to the caller.
+ let paste_result = (|| -> Result<(), String> {
+ // Send paste key combo
#[cfg(target_os = "linux")]
- if is_wayland() && is_wl_copy_available() {
- let _ = write_clipboard_via_wl_copy(&clipboard_content);
- } else {
- let _ = clipboard.write_text(&clipboard_content);
- }
+ let key_combo_sent = try_send_key_combo_linux(paste_method)?;
#[cfg(not(target_os = "linux"))]
- let _ = clipboard.write_text(&clipboard_content);
- } else if let Some(image) = saved_image {
- info!("Restoring image to clipboard");
- let _ = clipboard.write_image(&image);
- } else {
- // Nothing was there to begin with — don't leave the transcription behind.
- let _ = clipboard.clear();
- }
+ let key_combo_sent = false;
+
+ // Fall back to enigo if no native tool handled it
+ if !key_combo_sent {
+ with_enigo(app_handle, |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),
+ PasteMethod::CtrlShiftV => input::send_paste_ctrl_shift_v(enigo, 100),
+ PasteMethod::ShiftInsert => input::send_paste_shift_insert(enigo, 100),
+ _ => Err("Invalid paste method for clipboard paste".into()),
+ })?;
+ }
- Ok(())
+ Ok(())
+ })();
+
+ finish_clipboard_paste(paste_result, paste_delay_after_ms, || {
+ // Restore original clipboard content even when key injection failed.
+ // Text takes priority so this path stays identical to the previous behavior;
+ // an image is only restored when the clipboard held no text at all, which is
+ // the case that used to silently wipe screenshots.
+ if let Some(clipboard_content) = saved_text {
+ let _ = write_text_to_clipboard(app_handle, &clipboard_content);
+ } else if let Some(image) = saved_image {
+ info!("Restoring image to clipboard");
+ let _ = clipboard.write_image(&image);
+ } else {
+ // Nothing was there to begin with — don't leave the transcription behind.
+ let _ = clipboard.clear();
+ }
+ })
}
/// Attempts to send a key combination using Linux-native tools.
@@ -644,8 +659,8 @@ fn paste_via_external_script(text: &str, script_path: &str) -> Result<(), String
/// Types text directly by simulating individual key presses.
fn paste_direct(
- enigo: &mut Enigo,
text: &str,
+ app_handle: &AppHandle,
#[cfg(target_os = "linux")] typing_tool: TypingTool,
) -> Result<(), String> {
#[cfg(target_os = "linux")]
@@ -656,7 +671,7 @@ fn paste_direct(
info!("Falling back to enigo for direct text input");
}
- input::paste_text_direct(enigo, text)
+ with_enigo(app_handle, |enigo| input::paste_text_direct(enigo, text))
}
pub(crate) fn send_return_key(enigo: &mut Enigo, key_type: AutoSubmitKey) -> Result<(), String> {
@@ -724,15 +739,6 @@ pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {
paste_method, paste_delay_ms, paste_delay_after_ms
);
- // Get the managed Enigo instance
- 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))?;
-
// Perform the paste operation
match paste_method {
PasteMethod::None => {
@@ -740,8 +746,8 @@ pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {
}
PasteMethod::Direct => {
paste_direct(
- &mut enigo,
&text,
+ &app_handle,
#[cfg(target_os = "linux")]
settings.typing_tool,
)?;
@@ -754,15 +760,18 @@ 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 {
- match crate::paste_tx::try_reliable_paste(
- &text,
- &app_handle,
- &paste_method,
- &mut enigo,
- settings.auto_submit,
- settings.auto_submit_key,
- settings.clipboard_handling,
- ) {
+ let reliable_result = with_enigo(&app_handle, |enigo| {
+ crate::paste_tx::try_reliable_paste(
+ &text,
+ &app_handle,
+ &paste_method,
+ enigo,
+ settings.auto_submit,
+ settings.auto_submit_key,
+ settings.clipboard_handling,
+ )
+ });
+ match reliable_result {
Ok(()) => return Ok(()),
Err(e) => {
log::warn!("Reliable paste unavailable ({e}); falling back to legacy paste")
@@ -770,7 +779,6 @@ pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {
}
}
paste_via_clipboard(
- &mut enigo,
&text,
&app_handle,
&paste_method,
@@ -790,15 +798,16 @@ 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));
- send_return_key(&mut enigo, settings.auto_submit_key)?;
+ if let Err(error) = with_enigo(&app_handle, |enigo| {
+ send_return_key(enigo, settings.auto_submit_key)
+ }) {
+ log::warn!("Paste succeeded, but auto-submit failed: {error}");
+ }
}
// After pasting, optionally copy to clipboard based on settings
if settings.clipboard_handling == ClipboardHandling::CopyToClipboard {
- let clipboard = app_handle.clipboard();
- clipboard
- .write_text(&text)
- .map_err(|e| format!("Failed to copy to clipboard: {}", e))?;
+ write_text_to_clipboard(&app_handle, &text)?;
}
Ok(())
@@ -807,6 +816,7 @@ pub fn paste(text: String, app_handle: AppHandle) -> Result<(), String> {
#[cfg(test)]
mod tests {
use super::*;
+ use std::cell::Cell;
#[cfg(target_os = "linux")]
const YDOTOOL_0_1_8_HELP: &str = r#"
@@ -903,4 +913,15 @@ e.g. 28:1 28:0 means pressing on the Enter button on a standard US keyboard.
assert!(should_send_auto_submit(true, PasteMethod::CtrlShiftV));
assert!(should_send_auto_submit(true, PasteMethod::ShiftInsert));
}
+
+ #[test]
+ fn clipboard_is_restored_before_key_injection_error_is_returned() {
+ let restored = Cell::new(false);
+ let result = finish_clipboard_paste(Err("input failed".into()), 0, || {
+ restored.set(true);
+ });
+
+ assert_eq!(result.unwrap_err(), "input failed");
+ assert!(restored.get());
+ }
}