diff --git a/Cargo.lock b/Cargo.lock index bd223b40a860..067de9769194 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4311,6 +4311,7 @@ source = "git+https://github.com/pcwalton/signpost.git#7ed712507f343c38646b9d1fe name = "simpleservo" version = "0.0.1" dependencies = [ + "clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "gl_generator 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.53 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ports/libsimpleservo/api/Cargo.toml b/ports/libsimpleservo/api/Cargo.toml index 0556947d7da6..32ae1accd7cd 100644 --- a/ports/libsimpleservo/api/Cargo.toml +++ b/ports/libsimpleservo/api/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" publish = false [dependencies] +clipboard = "0.5" libservo = { path = "../../../components/servo" } log = "0.4" diff --git a/ports/libsimpleservo/api/src/lib.rs b/ports/libsimpleservo/api/src/lib.rs index baafef8aa630..dd94167586cb 100644 --- a/ports/libsimpleservo/api/src/lib.rs +++ b/ports/libsimpleservo/api/src/lib.rs @@ -9,6 +9,7 @@ pub mod gl_glue; pub use servo::script_traits::MouseButton; +use clipboard::{ClipboardContext, ClipboardProvider}; use servo::compositing::windowing::{ AnimationState, EmbedderCoordinates, EmbedderMethods, MouseWindowEvent, WindowEvent, WindowMethods, @@ -25,6 +26,7 @@ use servo::servo_url::ServoUrl; use servo::webrender_api::{DevicePixel, FramebufferPixel, ScrollLocation}; use servo::webvr::{VRExternalShmemPtr, VRMainThreadHeartbeat, VRServiceManager}; use servo::{self, gl, BrowserId, Servo}; + use std::cell::RefCell; use std::mem; use std::os::raw::c_void; @@ -126,6 +128,7 @@ pub struct ServoGlue { browsers: Vec, events: Vec, current_url: Option, + clipboard_ctx: Option, } pub fn servo_version() -> String { @@ -192,6 +195,13 @@ pub fn init( browsers: vec![], events: vec![], current_url: Some(url.clone()), + clipboard_ctx: match ClipboardContext::new() { + Ok(c) => Some(c), + Err(e) => { + warn!("Error creating clipboard context ({})", e); + None + }, + }, }; let browser_id = BrowserId::new(); let _ = servo_glue.process_event(WindowEvent::NewBrowser(url, browser_id)); @@ -516,6 +526,28 @@ impl ServoGlue { } self.events.push(WindowEvent::SelectBrowser(new_browser_id)); }, + EmbedderMsg::GetClipboardContents(sender) => { + let contents = match self.clipboard_ctx { + Some(ref mut ctx) => match ctx.get_contents() { + Ok(c) => c, + Err(e) => { + warn!("Error getting clipboard contents ({}), defaulting to empty string", e); + "".to_owned() + }, + }, + None => "".to_owned(), + }; + if let Err(e) = sender.send(contents) { + warn!("Failed to send clipboard ({})", e); + } + }, + EmbedderMsg::SetClipboardContents(text) => { + if let Some(ref mut ctx) = self.clipboard_ctx { + if let Err(e) = ctx.set_contents(text) { + warn!("Error setting clipboard contents ({})", e); + } + } + }, EmbedderMsg::CloseBrowser => { // TODO: close the appropriate "tab". let _ = self.browsers.pop();