Forward to embedder message for setting/getting clipboard contents from clipboard provider.

Create clipboard context in browser.rs and handle new messages.
This commit is contained in:
Michal Mieczkowski 2019-06-13 20:17:49 +02:00
parent 1e5103e675
commit f4d972adb2
5 changed files with 47 additions and 2 deletions

View file

@ -54,6 +54,7 @@ libc = "0.2"
log = "0.4"
rust-webvr = { version = "0.11", features = ["glwindow"] }
tinyfiledialogs = "3.0"
clipboard = "0.5"
[target.'cfg(any(target_os = "linux", target_os = "windows"))'.dependencies]
image = "0.21"

View file

@ -16,6 +16,7 @@ use servo::servo_config::opts;
use servo::servo_config::pref;
use servo::servo_url::ServoUrl;
use servo::webrender_api::ScrollLocation;
use clipboard::{ClipboardContext, ClipboardProvider};
use std::env;
use std::fs::File;
use std::io::Write;
@ -43,6 +44,7 @@ pub struct Browser<Window: WindowPortsMethods + ?Sized> {
loading_state: Option<LoadingState>,
window: Rc<Window>,
event_queue: Vec<WindowEvent>,
clipboard_ctx: Option<ClipboardContext>,
shutdown_requested: bool,
}
@ -66,6 +68,13 @@ where
favicon: None,
loading_state: None,
window: window,
clipboard_ctx: match ClipboardContext::new() {
Ok(c) => Some(c),
Err(e) => {
warn!("Error creating clipboard context ({})", e);
None
},
},
event_queue: Vec::new(),
shutdown_requested: false,
}
@ -344,6 +353,32 @@ where
EmbedderMsg::Keyboard(key_event) => {
self.handle_key_from_servo(browser_id, key_event);
},
EmbedderMsg::GetClipboardContents(sender) => {
println!("glutin/browser.rs GetClipboardContents {:?} ", browser_id);
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) => {
println!("browser.rs SetClipBoardContents {} {:?}", text, browser_id);
if let Some( ref mut ctx ) = self.clipboard_ctx {
if let Err(e) = ctx.set_contents(text) {
warn!("Error setting clipboard contents ({})", e);
}
}
}
EmbedderMsg::SetCursor(cursor) => {
self.window.set_cursor(cursor);
},

View file

@ -9,6 +9,7 @@ extern crate log;
#[cfg(all(feature = "unstable", any(target_os = "macos", target_os = "linux")))]
#[macro_use]
extern crate sig;
extern crate clipboard;
mod app;
mod browser;