diff --git a/core/tauri-runtime-wry/Cargo.toml b/core/tauri-runtime-wry/Cargo.toml index f5601e432b59..98095a700f96 100644 --- a/core/tauri-runtime-wry/Cargo.toml +++ b/core/tauri-runtime-wry/Cargo.toml @@ -20,6 +20,7 @@ uuid = { version = "1", features = [ "v4" ] } rand = "0.8" raw-window-handle = "0.5" tracing = { version = "0.1", optional = true } +arboard = { version = "3", optional = true } [target."cfg(windows)".dependencies] webview2-com = "0.19.1" @@ -47,6 +48,6 @@ macos-private-api = [ ] objc-exception = [ "wry/objc-exception" ] global-shortcut = [ "tauri-runtime/global-shortcut" ] -clipboard = [ "tauri-runtime/clipboard" ] +clipboard = [ "tauri-runtime/clipboard", "arboard" ] linux-headers = [ "wry/linux-headers", "webkit2gtk/v2_36" ] tracing = [ "dep:tracing", "wry/tracing" ] diff --git a/core/tauri-runtime-wry/src/clipboard.rs b/core/tauri-runtime-wry/src/clipboard.rs index 428f1abb3a1b..13f61c6f0c58 100644 --- a/core/tauri-runtime-wry/src/clipboard.rs +++ b/core/tauri-runtime-wry/src/clipboard.rs @@ -4,59 +4,36 @@ //! Clipboard implementation. -use crate::{getter, Context, Message}; - -use std::sync::{ - mpsc::{channel, Sender}, - Arc, Mutex, +use std::{ + fmt, + sync::{Arc, Mutex}, }; -use tauri_runtime::{ClipboardManager, Result, UserEvent}; -pub use wry::application::clipboard::Clipboard; +pub use arboard::Clipboard; +use tauri_runtime::{ClipboardManager, Result}; -#[derive(Debug, Clone)] -pub enum ClipboardMessage { - WriteText(String, Sender<()>), - ReadText(Sender>), +#[derive(Clone)] +pub struct ClipboardManagerWrapper { + pub clipboard: Arc>, } -#[derive(Debug, Clone)] -pub struct ClipboardManagerWrapper { - pub context: Context, +impl fmt::Debug for ClipboardManagerWrapper { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("ClipboardManagerWrapper").finish() + } } -// SAFETY: this is safe since the `Context` usage is guarded on `send_user_message`. -#[allow(clippy::non_send_fields_in_send_ty)] -unsafe impl Sync for ClipboardManagerWrapper {} - -impl ClipboardManager for ClipboardManagerWrapper { +impl ClipboardManager for ClipboardManagerWrapper { fn read_text(&self) -> Result> { - let (tx, rx) = channel(); - getter!(self, rx, Message::Clipboard(ClipboardMessage::ReadText(tx))) + Ok(self.clipboard.lock().unwrap().get_text().ok()) } fn write_text>(&mut self, text: V) -> Result<()> { - let (tx, rx) = channel(); - getter!( - self, - rx, - Message::Clipboard(ClipboardMessage::WriteText(text.into(), tx)) - )?; - Ok(()) - } -} - -pub fn handle_clipboard_message( - message: ClipboardMessage, - clipboard_manager: &Arc>, -) { - match message { - ClipboardMessage::WriteText(text, tx) => { - clipboard_manager.lock().unwrap().write_text(text); - tx.send(()).unwrap(); - } - ClipboardMessage::ReadText(tx) => tx - .send(clipboard_manager.lock().unwrap().read_text()) - .unwrap(), + self + .clipboard + .lock() + .unwrap() + .set_text(text.into()) + .map_err(|e| crate::Error::Clipboard(Box::new(e))) } } diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 27f5418387f5..e944917d459d 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -175,8 +175,6 @@ pub(crate) fn send_user_message( webview_id_map: context.webview_id_map.clone(), #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager: context.main_thread.global_shortcut_manager.clone(), - #[cfg(feature = "clipboard")] - clipboard_manager: context.main_thread.clipboard_manager.clone(), windows: context.main_thread.windows.clone(), #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager: context.main_thread.system_tray_manager.clone(), @@ -276,8 +274,6 @@ pub struct DispatcherMainThreadContext { pub web_context: WebContextStore, #[cfg(all(desktop, feature = "global-shortcut"))] pub global_shortcut_manager: Rc>, - #[cfg(feature = "clipboard")] - pub clipboard_manager: Arc>, pub windows: Rc>>, #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager: SystemTrayManager, @@ -1213,8 +1209,6 @@ pub enum Message { ), #[cfg(all(desktop, feature = "global-shortcut"))] GlobalShortcut(GlobalShortcutMessage), - #[cfg(feature = "clipboard")] - Clipboard(ClipboardMessage), UserEvent(T), } @@ -1226,8 +1220,6 @@ impl Clone for Message { Self::Tray(i, m) => Self::Tray(*i, m.clone()), #[cfg(all(desktop, feature = "global-shortcut"))] Self::GlobalShortcut(m) => Self::GlobalShortcut(m.clone()), - #[cfg(feature = "clipboard")] - Self::Clipboard(m) => Self::Clipboard(m.clone()), Self::UserEvent(t) => Self::UserEvent(t.clone()), _ => unimplemented!(), } @@ -1829,7 +1821,7 @@ pub struct Wry { global_shortcut_manager_handle: GlobalShortcutManagerHandle, #[cfg(feature = "clipboard")] - clipboard_manager_handle: ClipboardManagerWrapper, + clipboard_manager_handle: ClipboardManagerWrapper, event_loop: EventLoop>, } @@ -1860,11 +1852,7 @@ impl fmt::Debug for Wry { ); #[cfg(feature = "clipboard")] - d.field( - "clipboard_manager", - &self.context.main_thread.clipboard_manager, - ) - .field("clipboard_manager_handle", &self.clipboard_manager_handle); + d.field("clipboard_manager_handle", &self.clipboard_manager_handle); d.finish() } @@ -1985,9 +1973,6 @@ impl Wry { #[cfg(all(desktop, feature = "global-shortcut"))] let global_shortcut_manager = Rc::new(Mutex::new(WryShortcutManager::new(&event_loop))); - #[cfg(feature = "clipboard")] - let clipboard_manager = Arc::new(Mutex::new(Clipboard::new())); - let windows = Rc::new(RefCell::new(HashMap::default())); let webview_id_map = WebviewIdStore::default(); @@ -2003,8 +1988,6 @@ impl Wry { web_context, #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager, - #[cfg(feature = "clipboard")] - clipboard_manager, windows, #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager, @@ -2023,7 +2006,7 @@ impl Wry { #[cfg(feature = "clipboard")] #[allow(clippy::redundant_clone)] let clipboard_manager_handle = ClipboardManagerWrapper { - context: context.clone(), + clipboard: Arc::new(Mutex::new(Clipboard::new().unwrap())), }; Ok(Self { @@ -2056,7 +2039,7 @@ impl Runtime for Wry { type GlobalShortcutManager = GlobalShortcutManagerHandle; #[cfg(feature = "clipboard")] - type ClipboardManager = ClipboardManagerWrapper; + type ClipboardManager = ClipboardManagerWrapper; #[cfg(all(desktop, feature = "system-tray"))] type TrayHandler = SystemTrayHandle; @@ -2221,8 +2204,6 @@ impl Runtime for Wry { #[cfg(all(desktop, feature = "global-shortcut"))] let global_shortcut_manager_handle = self.global_shortcut_manager_handle.clone(); - #[cfg(feature = "clipboard")] - let clipboard_manager = self.context.main_thread.clipboard_manager.clone(); let mut iteration = RunIteration::default(); let proxy = self.event_loop.create_proxy(); @@ -2249,8 +2230,6 @@ impl Runtime for Wry { global_shortcut_manager: global_shortcut_manager.clone(), #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager_handle: &global_shortcut_manager_handle, - #[cfg(feature = "clipboard")] - clipboard_manager: clipboard_manager.clone(), #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager: system_tray_manager.clone(), #[cfg(feature = "tracing")] @@ -2275,8 +2254,6 @@ impl Runtime for Wry { global_shortcut_manager: global_shortcut_manager.clone(), #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager_handle: &global_shortcut_manager_handle, - #[cfg(feature = "clipboard")] - clipboard_manager: clipboard_manager.clone(), #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager: system_tray_manager.clone(), #[cfg(feature = "tracing")] @@ -2306,9 +2283,6 @@ impl Runtime for Wry { #[cfg(all(desktop, feature = "global-shortcut"))] let global_shortcut_manager_handle = self.global_shortcut_manager_handle.clone(); - #[cfg(feature = "clipboard")] - let clipboard_manager = self.context.main_thread.clipboard_manager.clone(); - let proxy = self.event_loop.create_proxy(); self.event_loop.run(move |event, event_loop, control_flow| { @@ -2326,8 +2300,6 @@ impl Runtime for Wry { global_shortcut_manager: global_shortcut_manager.clone(), #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager_handle: &global_shortcut_manager_handle, - #[cfg(feature = "clipboard")] - clipboard_manager: clipboard_manager.clone(), #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager: system_tray_manager.clone(), #[cfg(feature = "tracing")] @@ -2351,8 +2323,6 @@ impl Runtime for Wry { global_shortcut_manager: global_shortcut_manager.clone(), #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager_handle: &global_shortcut_manager_handle, - #[cfg(feature = "clipboard")] - clipboard_manager: clipboard_manager.clone(), #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager: system_tray_manager.clone(), #[cfg(feature = "tracing")] @@ -2372,8 +2342,6 @@ pub struct EventLoopIterationContext<'a, T: UserEvent> { pub global_shortcut_manager: Rc>, #[cfg(all(desktop, feature = "global-shortcut"))] pub global_shortcut_manager_handle: &'a GlobalShortcutManagerHandle, - #[cfg(feature = "clipboard")] - pub clipboard_manager: Arc>, #[cfg(all(desktop, feature = "system-tray"))] pub system_tray_manager: SystemTrayManager, #[cfg(feature = "tracing")] @@ -2385,8 +2353,6 @@ struct UserMessageContext { webview_id_map: WebviewIdStore, #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager: Rc>, - #[cfg(feature = "clipboard")] - clipboard_manager: Arc>, #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager: SystemTrayManager, } @@ -2401,8 +2367,6 @@ fn handle_user_message( webview_id_map, #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager, - #[cfg(feature = "clipboard")] - clipboard_manager, windows, #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager, @@ -2805,8 +2769,6 @@ fn handle_user_message( Message::GlobalShortcut(message) => { handle_global_shortcut_message(message, &global_shortcut_manager) } - #[cfg(feature = "clipboard")] - Message::Clipboard(message) => handle_clipboard_message(message, &clipboard_manager), Message::UserEvent(_) => (), } @@ -2831,8 +2793,6 @@ fn handle_event_loop( global_shortcut_manager, #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager_handle, - #[cfg(feature = "clipboard")] - clipboard_manager, #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager, #[cfg(feature = "tracing")] @@ -3079,8 +3039,6 @@ fn handle_event_loop( webview_id_map, #[cfg(all(desktop, feature = "global-shortcut"))] global_shortcut_manager, - #[cfg(feature = "clipboard")] - clipboard_manager, windows, #[cfg(all(desktop, feature = "system-tray"))] system_tray_manager, diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index 0671fa25378e..9e961e863c47 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -252,6 +252,9 @@ pub enum Error { #[cfg(all(desktop, feature = "global-shortcut"))] #[error(transparent)] GlobalShortcut(Box), + #[cfg(all(desktop, feature = "clipboard"))] + #[error(transparent)] + Clipboard(Box), #[error("Invalid header name: {0}")] InvalidHeaderName(#[from] InvalidHeaderName), #[error("Invalid header value: {0}")]