Skip to content

Commit

Permalink
Forward to embedder message for setting/getting clipboard contents fr…
Browse files Browse the repository at this point in the history
…om clipboard provider.

Create clipboard context in browser.rs and handle new messages.
  • Loading branch information
mmiecz committed Jun 13, 2019
1 parent 1e5103e commit f4d972a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions components/script/clipboard_provider.rs
Expand Up @@ -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;
Expand All @@ -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();
}
}

Expand Down
1 change: 1 addition & 0 deletions ports/glutin/Cargo.toml
Expand Up @@ -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"
Expand Down
35 changes: 35 additions & 0 deletions ports/glutin/browser.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}

Expand All @@ -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,
}
Expand Down Expand Up @@ -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);
},
Expand Down
1 change: 1 addition & 0 deletions ports/glutin/main2.rs
Expand Up @@ -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;
Expand Down

0 comments on commit f4d972a

Please sign in to comment.