egui-winit: if clipboard fails to start, fall back to local clipboard
This commit is contained in:
parent
d364dfac66
commit
08b208586a
3 changed files with 17 additions and 22 deletions
|
@ -1,11 +1,12 @@
|
||||||
/// Handles interfacing either with the OS clipboard.
|
/// Handles interfacing with the OS clipboard.
|
||||||
/// If the "clipboard" feature is off it will instead simulate the clipboard locally.
|
///
|
||||||
|
/// 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 {
|
pub struct Clipboard {
|
||||||
#[cfg(feature = "arboard")]
|
#[cfg(feature = "arboard")]
|
||||||
arboard: Option<arboard::Clipboard>,
|
arboard: Option<arboard::Clipboard>,
|
||||||
|
|
||||||
/// Fallback manual clipboard.
|
/// Fallback manual clipboard.
|
||||||
#[cfg(not(feature = "arboard"))]
|
|
||||||
clipboard: String,
|
clipboard: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +16,6 @@ impl Default for Clipboard {
|
||||||
#[cfg(feature = "arboard")]
|
#[cfg(feature = "arboard")]
|
||||||
arboard: init_arboard(),
|
arboard: init_arboard(),
|
||||||
|
|
||||||
#[cfg(not(feature = "arboard"))]
|
|
||||||
clipboard: String::default(),
|
clipboard: String::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,18 +25,15 @@ impl Clipboard {
|
||||||
pub fn get(&mut self) -> Option<String> {
|
pub fn get(&mut self) -> Option<String> {
|
||||||
#[cfg(feature = "arboard")]
|
#[cfg(feature = "arboard")]
|
||||||
if let Some(clipboard) = &mut self.arboard {
|
if let Some(clipboard) = &mut self.arboard {
|
||||||
match clipboard.get_text() {
|
return match clipboard.get_text() {
|
||||||
Ok(text) => Some(text),
|
Ok(text) => Some(text),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
tracing::error!("Paste error: {}", err);
|
tracing::error!("Paste error: {}", err);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "arboard"))]
|
|
||||||
Some(self.clipboard.clone())
|
Some(self.clipboard.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,12 +43,10 @@ impl Clipboard {
|
||||||
if let Err(err) = clipboard.set_text(text) {
|
if let Err(err) = clipboard.set_text(text) {
|
||||||
tracing::error!("Copy/Cut error: {}", err);
|
tracing::error!("Copy/Cut error: {}", err);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "arboard"))]
|
self.clipboard = text;
|
||||||
{
|
|
||||||
self.clipboard = text;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -412,9 +412,10 @@ impl State {
|
||||||
self.egui_input.events.push(egui::Event::Copy);
|
self.egui_input.events.push(egui::Event::Copy);
|
||||||
} else if is_paste_command(self.egui_input.modifiers, keycode) {
|
} else if is_paste_command(self.egui_input.modifiers, keycode) {
|
||||||
if let Some(contents) = self.clipboard.get() {
|
if let Some(contents) = self.clipboard.get() {
|
||||||
self.egui_input
|
let contents = contents.replace("\r\n", "\n");
|
||||||
.events
|
if !contents.is_empty() {
|
||||||
.push(egui::Event::Paste(contents.replace("\r\n", "\n")));
|
self.egui_input.events.push(egui::Event::Paste(contents));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -464,12 +464,11 @@ fn install_document_events(runner_container: &AppRunnerContainer) -> Result<(),
|
||||||
|event: web_sys::ClipboardEvent, mut runner_lock| {
|
|event: web_sys::ClipboardEvent, mut runner_lock| {
|
||||||
if let Some(data) = event.clipboard_data() {
|
if let Some(data) = event.clipboard_data() {
|
||||||
if let Ok(text) = data.get_data("text") {
|
if let Ok(text) = data.get_data("text") {
|
||||||
runner_lock
|
let text = text.replace("\r\n", "\n");
|
||||||
.input
|
if !text.is_empty() {
|
||||||
.raw
|
runner_lock.input.raw.events.push(egui::Event::Paste(text));
|
||||||
.events
|
runner_lock.needs_repaint.set_true();
|
||||||
.push(egui::Event::Paste(text.replace("\r\n", "\n")));
|
}
|
||||||
runner_lock.needs_repaint.set_true();
|
|
||||||
event.stop_propagation();
|
event.stop_propagation();
|
||||||
event.prevent_default();
|
event.prevent_default();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue