From 3ae0313975e9e15bac3a7454a1ff8de7b121dc81 Mon Sep 17 00:00:00 2001 From: PandaDEV <70103896+0PandaDEV@users.noreply.github.com> Date: Fri, 22 Nov 2024 00:15:38 +1000 Subject: [PATCH] fix: pasting not working on mac --- src-tauri/src/api/clipboard.rs | 43 ++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src-tauri/src/api/clipboard.rs b/src-tauri/src/api/clipboard.rs index c587cb4..3150c58 100644 --- a/src-tauri/src/api/clipboard.rs +++ b/src-tauri/src/api/clipboard.rs @@ -62,7 +62,28 @@ pub async fn write_and_paste( IS_PROGRAMMATIC_PASTE.store(true, Ordering::SeqCst); - simulate_paste(); + thread::spawn(|| { + thread::sleep(Duration::from_millis(100)); + + #[cfg(target_os = "macos")] + let modifier_key = Key::MetaLeft; + #[cfg(not(target_os = "macos"))] + let modifier_key = Key::ControlLeft; + + let events = vec![ + EventType::KeyPress(modifier_key), + EventType::KeyPress(Key::KeyV), + EventType::KeyRelease(Key::KeyV), + EventType::KeyRelease(modifier_key), + ]; + + for event in events { + if let Err(e) = simulate(&event) { + println!("Simulation error: {:?}", e); + } + thread::sleep(Duration::from_millis(20)); + } + }); tokio::spawn(async { tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; @@ -72,22 +93,6 @@ pub async fn write_and_paste( Ok(()) } -fn simulate_paste() { - let mut events = vec![ - EventType::KeyPress(Key::ControlLeft), - EventType::KeyPress(Key::KeyV), - EventType::KeyRelease(Key::KeyV), - EventType::KeyRelease(Key::ControlLeft), - ]; - - thread::sleep(Duration::from_millis(100)); - - for event in events.drain(..) { - simulate(&event).unwrap(); - thread::sleep(Duration::from_millis(20)); - } -} - #[tauri::command] pub fn get_image_path(app_handle: tauri::AppHandle, filename: String) -> String { let app_data_dir = app_handle @@ -246,6 +251,8 @@ async fn insert_content_if_not_exists( .bind(favicon_base64) .execute(&pool) .await; + + let _ = app_handle.emit("clipboard-content-updated", ()); } } @@ -302,4 +309,4 @@ pub fn start_monitor(app_handle: AppHandle) -> Result<(), String> { .emit("plugin:clipboard://clipboard-monitor/status", true) .map_err(|e| e.to_string())?; Ok(()) -} \ No newline at end of file +}