Skip to content

Commit

Permalink
fix: move clipboard to app level
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Jul 4, 2024
1 parent 5d6d83c commit f207602
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
13 changes: 10 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use arboard::Clipboard;
use servo::{
compositing::{
windowing::{EmbedderEvent, EmbedderMethods},
Expand Down Expand Up @@ -32,6 +33,8 @@ pub struct Verso {
window: Window,
events: Vec<EmbedderEvent>,
status: Status,
/// The clipboard. `None` if the platform or desktop environment is not support.
clipboard: Option<Clipboard>,
}

impl Verso {
Expand Down Expand Up @@ -66,6 +69,7 @@ impl Verso {
window,
events: vec![],
status: Status::None,
clipboard: None,
}
}

Expand Down Expand Up @@ -105,9 +109,12 @@ impl Verso {
return;
};

let need_present =
self.window
.handle_servo_messages(servo, &mut self.events, &mut self.status);
let need_present = self.window.handle_servo_messages(
servo,
&mut self.events,
&mut self.status,
&mut self.clipboard,
);

log::trace!("Verso is handling embedder events: {:?}", self.events);
if servo.handle_events(self.events.drain(..)) {
Expand Down
88 changes: 49 additions & 39 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ pub struct Window {
mouse_position: Cell<PhysicalPosition<f64>>,
/// Modifiers state of the keyboard.
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 @@ -86,13 +84,6 @@ impl Window {
webrender_gl,
mouse_position: Cell::new(PhysicalPosition::default()),
modifiers_state: Cell::new(ModifiersState::default()),
clipboard: match Clipboard::new() {
Ok(clipboard) => Some(clipboard),
Err(e) => {
log::warn!("Failed to create clipboard: {}", e);
None
}
},
}
}

Expand Down Expand Up @@ -229,39 +220,10 @@ impl Window {
servo: &mut Servo<GLWindow>,
events: &mut Vec<EmbedderEvent>,
status: &mut Status,
clipboard: &mut Option<Clipboard>,
) -> bool {
let mut need_present = false;
servo.get_events().into_iter().for_each(|(w, m)| {

// Handle 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.");
}
return
},
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.");
}
return
},
_ => ()
}

match w {
// Handle message in Verso Panel
Some(p) if p == self.panel.id() => {
Expand Down Expand Up @@ -336,6 +298,30 @@ impl Window {
},
_ => log::warn!("Verso Panel isn't supporting this prompt yet")
},
EmbedderMsg::GetClipboardContents(sender) => {
let contents = match clipboard.as_mut() {
Some(clipboard) => clipboard.get_text().unwrap_or_else(|e| {
log::warn!("Failed to get clipboard content: {}", e);
String::new()
}),
None => {
log::trace!("Clipboard is not supported on this platform.");
String::new()
}
};
if let Err(e) = sender.send(contents) {
log::warn!("Failed to send clipboard content: {}", e);
}
},
EmbedderMsg::SetClipboardContents(text) => {
if let Some(clipboard) = 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.");
}
},
e => {
log::warn!(
"Verso Panel isn't supporting this message yet: {e:?}"
Expand Down Expand Up @@ -374,6 +360,30 @@ impl Window {
EmbedderMsg::WebViewFocused(w) => {
events.push(EmbedderEvent::ShowWebView(w, false));
}
EmbedderMsg::GetClipboardContents(sender) => {
let contents = match clipboard.as_mut() {
Some(clipboard) => clipboard.get_text().unwrap_or_else(|e| {
log::warn!("Failed to get clipboard content: {}", e);
String::new()
}),
None => {
log::trace!("Clipboard is not supported on this platform.");
String::new()
}
};
if let Err(e) = sender.send(contents) {
log::warn!("Failed to send clipboard content: {}", e);
}
},
EmbedderMsg::SetClipboardContents(text) => {
if let Some(clipboard) = 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.");
}
},
e => {
log::warn!(
"Verso WebView isn't supporting this message yet: {e:?}"
Expand Down

0 comments on commit f207602

Please sign in to comment.