Clipboard handling in libsimpleservo

This commit is contained in:
Michal Mieczkowski 2019-06-15 13:38:38 +02:00
parent 2726fc1dea
commit 588cec0422
3 changed files with 34 additions and 0 deletions

1
Cargo.lock generated
View file

@ -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)",

View file

@ -7,6 +7,7 @@ edition = "2018"
publish = false
[dependencies]
clipboard = "0.5"
libservo = { path = "../../../components/servo" }
log = "0.4"

View file

@ -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<BrowserId>,
events: Vec<WindowEvent>,
current_url: Option<ServoUrl>,
clipboard_ctx: Option<ClipboardContext>,
}
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();