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

1
Cargo.lock generated
View file

@ -3935,6 +3935,7 @@ dependencies = [
"backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
"clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"euclid 0.19.7 (registry+https://github.com/rust-lang/crates.io-index)",
"gleam 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use embedder_traits::EmbedderMsg;
use ipc_channel::ipc::channel;
use script_traits::{ScriptMsg, ScriptToConstellationChan};
use std::borrow::ToOwned;
@ -16,11 +17,17 @@ pub trait ClipboardProvider {
impl ClipboardProvider for ScriptToConstellationChan {
fn clipboard_contents(&mut self) -> String {
let (tx, rx) = channel().unwrap();
self.send(ScriptMsg::GetClipboardContents(tx)).unwrap();
self.send(ScriptMsg::ForwardToEmbedder(
EmbedderMsg::GetClipboardContents(tx),
))
.unwrap();
rx.recv().unwrap()
}
fn set_clipboard_contents(&mut self, s: String) {
self.send(ScriptMsg::SetClipboardContents(s)).unwrap();
self.send(ScriptMsg::ForwardToEmbedder(
EmbedderMsg::SetClipboardContents(s),
))
.unwrap();
}
}

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;