diff --git a/src/app.rs b/src/app.rs index ac00f3ee..dab9ddf6 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,3 +1,4 @@ +use arboard::Clipboard; use servo::{ compositing::{ windowing::{EmbedderEvent, EmbedderMethods}, @@ -32,6 +33,8 @@ pub struct Verso { window: Window, events: Vec, status: Status, + /// The clipboard. `None` if the platform or desktop environment is not support. + clipboard: Option, } impl Verso { @@ -66,6 +69,7 @@ impl Verso { window, events: vec![], status: Status::None, + clipboard: None, } } @@ -105,9 +109,12 @@ impl Verso { return; }; - let need_present = - self.window - .handle_servo_messages(servo, &mut self.events, &mut self.status); + let need_present = self.window.handle_servo_messages( + servo, + &mut self.events, + &mut self.status, + &mut self.clipboard, + ); log::trace!("Verso is handling embedder events: {:?}", self.events); if servo.handle_events(self.events.drain(..)) { diff --git a/src/window.rs b/src/window.rs index 8ceaab85..37b7f058 100644 --- a/src/window.rs +++ b/src/window.rs @@ -49,8 +49,6 @@ pub struct Window { mouse_position: Cell>, /// Modifiers state of the keyboard. modifiers_state: Cell, - /// The clipboard. `None` if the platform or desktop environment is not support. - clipboard: Option, } impl Window { @@ -86,13 +84,6 @@ impl Window { webrender_gl, mouse_position: Cell::new(PhysicalPosition::default()), modifiers_state: Cell::new(ModifiersState::default()), - clipboard: match Clipboard::new() { - Ok(clipboard) => Some(clipboard), - Err(e) => { - log::warn!("Failed to create clipboard: {}", e); - None - } - }, } } @@ -229,39 +220,10 @@ impl Window { servo: &mut Servo, events: &mut Vec, status: &mut Status, + clipboard: &mut Option, ) -> bool { let mut need_present = false; servo.get_events().into_iter().for_each(|(w, m)| { - - // Handle Window-wise Events - match &m { - EmbedderMsg::GetClipboardContents(sender) => { - if let Some(clipboard) = self.clipboard.as_mut() { - let contents = clipboard.get_text().unwrap_or_else(|e| { - log::warn!("Failed to get clipboard content: {}", e); - String::new() - }); - if let Err(e) = sender.send(contents) { - log::warn!("Failed to send clipboard content: {}", e); - } - } else { - log::trace!("Clipboard is not supported on this platform."); - } - return - }, - EmbedderMsg::SetClipboardContents(text) => { - if let Some(clipboard) = self.clipboard.as_mut() { - if let Err(e) = clipboard.set_text(text) { - log::warn!("Failed to set clipboard contents: {}", e); - } - } else { - log::trace!("Clipboard is not supported on this platform."); - } - return - }, - _ => () - } - match w { // Handle message in Verso Panel Some(p) if p == self.panel.id() => { @@ -336,6 +298,30 @@ impl Window { }, _ => log::warn!("Verso Panel isn't supporting this prompt yet") }, + EmbedderMsg::GetClipboardContents(sender) => { + let contents = match clipboard.as_mut() { + Some(clipboard) => clipboard.get_text().unwrap_or_else(|e| { + log::warn!("Failed to get clipboard content: {}", e); + String::new() + }), + None => { + log::trace!("Clipboard is not supported on this platform."); + String::new() + } + }; + if let Err(e) = sender.send(contents) { + log::warn!("Failed to send clipboard content: {}", e); + } + }, + EmbedderMsg::SetClipboardContents(text) => { + if let Some(clipboard) = clipboard.as_mut() { + if let Err(e) = clipboard.set_text(text) { + log::warn!("Failed to set clipboard contents: {}", e); + } + } else { + log::trace!("Clipboard is not supported on this platform."); + } + }, e => { log::warn!( "Verso Panel isn't supporting this message yet: {e:?}" @@ -374,6 +360,30 @@ impl Window { EmbedderMsg::WebViewFocused(w) => { events.push(EmbedderEvent::ShowWebView(w, false)); } + EmbedderMsg::GetClipboardContents(sender) => { + let contents = match clipboard.as_mut() { + Some(clipboard) => clipboard.get_text().unwrap_or_else(|e| { + log::warn!("Failed to get clipboard content: {}", e); + String::new() + }), + None => { + log::trace!("Clipboard is not supported on this platform."); + String::new() + } + }; + if let Err(e) = sender.send(contents) { + log::warn!("Failed to send clipboard content: {}", e); + } + }, + EmbedderMsg::SetClipboardContents(text) => { + if let Some(clipboard) = clipboard.as_mut() { + if let Err(e) = clipboard.set_text(text) { + log::warn!("Failed to set clipboard contents: {}", e); + } + } else { + log::trace!("Clipboard is not supported on this platform."); + } + }, e => { log::warn!( "Verso WebView isn't supporting this message yet: {e:?}"