Skip to content

Commit

Permalink
feat(clipboard): handle clipboard message
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Jul 3, 2024
1 parent 085230f commit cf357f8
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 10 deletions.
169 changes: 159 additions & 10 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ crossbeam-channel = "0.5"
getopts = "0.2.17"
surfman = { version = "0.9", features = ["chains", "sm-angle-default", "sm-raw-window-handle"] }
winit = { version = "0.29", features = ["rwh_05"] }
arboard = "3.4.0"

[target."cfg(any(target_os = \"ios\", target_os = \"macos\"))".dependencies]
block = "0.1"
Expand Down
38 changes: 38 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ use crate::{
Status,
};

use arboard::Clipboard;

/// A Verso window is a Winit window containing several web views.
pub struct Window {
/// Access to Winit window with webrender context.
Expand All @@ -45,7 +47,10 @@ pub struct Window {
webrender_gl: Rc<dyn gl::Gl>,
/// The mouse physical position in the web view.
mouse_position: Cell<PhysicalPosition<f64>>,
/// The current state of the keyboard modifiers.
modifiers_state: Cell<ModifiersState>,
/// The clipboard. `None` if the platform or desktop environment is not support.
clipboard: Option<Clipboard>,
}

impl Window {
Expand Down Expand Up @@ -81,6 +86,11 @@ impl Window {
webrender_gl,
mouse_position: Cell::new(PhysicalPosition::default()),
modifiers_state: Cell::new(ModifiersState::default()),
clipboard: Clipboard::new()
.map_err(|e| {
log::warn!("Failed to create clipboard: {}", e);
})
.ok(),
}
}

Expand Down Expand Up @@ -220,6 +230,34 @@ impl Window {
) -> bool {
let mut need_present = false;
servo.get_events().into_iter().for_each(|(w, m)| {

// Window-wise events
match &m {
EmbedderMsg::GetClipboardContents(sender) => {
if let Some(clipboard) = self.clipboard.as_mut() {
let contents = clipboard.get_text().unwrap_or_else(|e| {
log::warn!("Failed to get clipboard content: {}", e);
String::new()
});
if let Err(e) = sender.send(contents) {
log::warn!("Failed to send clipboard content: {}", e);
}
} else {
log::trace!("Clipboard is not supported on this platform.");
}
},
EmbedderMsg::SetClipboardContents(text) => {
if let Some(clipboard) = self.clipboard.as_mut() {
if let Err(e) = clipboard.set_text(text) {
log::warn!("Failed to set clipboard contents: {}", e);
}
} else {
log::trace!("Clipboard is not supported on this platform.");
}
},
_ => ()
}

match w {
// Handle message in Verso Panel
Some(p) if p == self.panel.id() => {
Expand Down

0 comments on commit cf357f8

Please sign in to comment.