Skip to content

Commit

Permalink
egui-winit: if clipboard fails to start, fall back to local clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Apr 12, 2022
1 parent d364dfa commit 08b2085
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
21 changes: 8 additions & 13 deletions egui-winit/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/// Handles interfacing either with the OS clipboard.
/// If the "clipboard" feature is off it will instead simulate the clipboard locally.
/// Handles interfacing with the OS clipboard.
///
/// If the "clipboard" feature is off, or we cannot connect to the OS clipboard,
/// then a fallback clipboard that just works works within the same app is used instead.
pub struct Clipboard {
#[cfg(feature = "arboard")]
arboard: Option<arboard::Clipboard>,

/// Fallback manual clipboard.
#[cfg(not(feature = "arboard"))]
clipboard: String,
}

Expand All @@ -15,7 +16,6 @@ impl Default for Clipboard {
#[cfg(feature = "arboard")]
arboard: init_arboard(),

#[cfg(not(feature = "arboard"))]
clipboard: String::default(),
}
}
Expand All @@ -25,18 +25,15 @@ impl Clipboard {
pub fn get(&mut self) -> Option<String> {
#[cfg(feature = "arboard")]
if let Some(clipboard) = &mut self.arboard {
match clipboard.get_text() {
return match clipboard.get_text() {
Ok(text) => Some(text),
Err(err) => {
tracing::error!("Paste error: {}", err);
None
}
}
} else {
None
};
}

#[cfg(not(feature = "arboard"))]
Some(self.clipboard.clone())
}

Expand All @@ -46,12 +43,10 @@ impl Clipboard {
if let Err(err) = clipboard.set_text(text) {
tracing::error!("Copy/Cut error: {}", err);
}
return;
}

#[cfg(not(feature = "arboard"))]
{
self.clipboard = text;
}
self.clipboard = text;
}
}

Expand Down
7 changes: 4 additions & 3 deletions egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,10 @@ impl State {
self.egui_input.events.push(egui::Event::Copy);
} else if is_paste_command(self.egui_input.modifiers, keycode) {
if let Some(contents) = self.clipboard.get() {
self.egui_input
.events
.push(egui::Event::Paste(contents.replace("\r\n", "\n")));
let contents = contents.replace("\r\n", "\n");
if !contents.is_empty() {
self.egui_input.events.push(egui::Event::Paste(contents));
}
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions egui_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,11 @@ fn install_document_events(runner_container: &AppRunnerContainer) -> Result<(),
|event: web_sys::ClipboardEvent, mut runner_lock| {
if let Some(data) = event.clipboard_data() {
if let Ok(text) = data.get_data("text") {
runner_lock
.input
.raw
.events
.push(egui::Event::Paste(text.replace("\r\n", "\n")));
runner_lock.needs_repaint.set_true();
let text = text.replace("\r\n", "\n");
if !text.is_empty() {
runner_lock.input.raw.events.push(egui::Event::Paste(text));
runner_lock.needs_repaint.set_true();
}
event.stop_propagation();
event.prevent_default();
}
Expand Down

0 comments on commit 08b2085

Please sign in to comment.