From f4d972adb2407275e30c97b175ab4885f18de546 Mon Sep 17 00:00:00 2001 From: Michal Mieczkowski Date: Thu, 13 Jun 2019 20:17:49 +0200 Subject: [PATCH] Forward to embedder message for setting/getting clipboard contents from clipboard provider. Create clipboard context in browser.rs and handle new messages. --- Cargo.lock | 1 + components/script/clipboard_provider.rs | 11 ++++++-- ports/glutin/Cargo.toml | 1 + ports/glutin/browser.rs | 35 +++++++++++++++++++++++++ ports/glutin/main2.rs | 1 + 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 179e7109d5d8..bd223b40a860 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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)", diff --git a/components/script/clipboard_provider.rs b/components/script/clipboard_provider.rs index 72afd5727d29..3d67d69bcefe 100644 --- a/components/script/clipboard_provider.rs +++ b/components/script/clipboard_provider.rs @@ -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(); } } diff --git a/ports/glutin/Cargo.toml b/ports/glutin/Cargo.toml index 41fbf47d7342..d47b8bce8a91 100644 --- a/ports/glutin/Cargo.toml +++ b/ports/glutin/Cargo.toml @@ -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" diff --git a/ports/glutin/browser.rs b/ports/glutin/browser.rs index e2f7d3979195..bde2b6b80f44 100644 --- a/ports/glutin/browser.rs +++ b/ports/glutin/browser.rs @@ -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 { loading_state: Option, window: Rc, event_queue: Vec, + clipboard_ctx: Option, 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); }, diff --git a/ports/glutin/main2.rs b/ports/glutin/main2.rs index 496c0515e19f..0e208fabb3cb 100644 --- a/ports/glutin/main2.rs +++ b/ports/glutin/main2.rs @@ -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;