Skip to content

Commit

Permalink
Clipboard handling in libsimpleservo
Browse files Browse the repository at this point in the history
  • Loading branch information
mmiecz committed Jun 15, 2019
1 parent 2726fc1 commit 588cec0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 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.

1 change: 1 addition & 0 deletions ports/libsimpleservo/api/Cargo.toml
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
publish = false

[dependencies]
clipboard = "0.5"
libservo = { path = "../../../components/servo" }
log = "0.4"

Expand Down
32 changes: 32 additions & 0 deletions ports/libsimpleservo/api/src/lib.rs
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 588cec0

Please sign in to comment.