1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

Replace copypasta with arboard (#1475)

* Replace copypasta with arboard

Closes https://github.com/emilk/egui/issues/1474

* Clean up deny.toml
This commit is contained in:
Emil Ernerfeldt
2022-04-10 16:41:07 +02:00
committed by GitHub
parent b036257729
commit 65d16695ae
4 changed files with 107 additions and 107 deletions

View File

@@ -22,7 +22,7 @@ default = ["clipboard", "dark-light", "links"]
# enable cut/copy/paste to OS clipboard.
# if disabled a clipboard will be simulated so you can still copy/paste within the egui app.
clipboard = ["copypasta"]
clipboard = ["arboard"]
# implement bytemuck on most types.
bytemuck = ["egui/bytemuck"]
@@ -51,8 +51,8 @@ winit = "0.26.1"
# Optional:
epi = { version = "0.17.0", path = "../epi", optional = true }
copypasta = { version = "0.7", optional = true }
dark-light = { version = "0.2.1", optional = true } # detect dark mode system preference
arboard = { version = "2.1", optional = true, default-features = false }
dark-light = { version = "0.2.1", optional = true } # detect dark mode system preference
glow = { version = "0.11", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
webbrowser = { version = "0.6", optional = true }

View File

@@ -1,21 +1,21 @@
/// Handles interfacing either with the OS clipboard.
/// If the "clipboard" feature is off it will instead simulate the clipboard locally.
pub struct Clipboard {
#[cfg(feature = "copypasta")]
copypasta: Option<copypasta::ClipboardContext>,
#[cfg(feature = "arboard")]
arboard: Option<arboard::Clipboard>,
/// Fallback manual clipboard.
#[cfg(not(feature = "copypasta"))]
#[cfg(not(feature = "arboard"))]
clipboard: String,
}
impl Default for Clipboard {
fn default() -> Self {
Self {
#[cfg(feature = "copypasta")]
copypasta: init_copypasta(),
#[cfg(feature = "arboard")]
arboard: init_arboard(),
#[cfg(not(feature = "copypasta"))]
#[cfg(not(feature = "arboard"))]
clipboard: String::default(),
}
}
@@ -23,11 +23,10 @@ impl Default for Clipboard {
impl Clipboard {
pub fn get(&mut self) -> Option<String> {
#[cfg(feature = "copypasta")]
if let Some(clipboard) = &mut self.copypasta {
use copypasta::ClipboardProvider as _;
match clipboard.get_contents() {
Ok(contents) => Some(contents),
#[cfg(feature = "arboard")]
if let Some(clipboard) = &mut self.arboard {
match clipboard.get_text() {
Ok(text) => Some(text),
Err(err) => {
tracing::error!("Paste error: {}", err);
None
@@ -37,29 +36,28 @@ impl Clipboard {
None
}
#[cfg(not(feature = "copypasta"))]
#[cfg(not(feature = "arboard"))]
Some(self.clipboard.clone())
}
pub fn set(&mut self, text: String) {
#[cfg(feature = "copypasta")]
if let Some(clipboard) = &mut self.copypasta {
use copypasta::ClipboardProvider as _;
if let Err(err) = clipboard.set_contents(text) {
#[cfg(feature = "arboard")]
if let Some(clipboard) = &mut self.arboard {
if let Err(err) = clipboard.set_text(text) {
tracing::error!("Copy/Cut error: {}", err);
}
}
#[cfg(not(feature = "copypasta"))]
#[cfg(not(feature = "arboard"))]
{
self.clipboard = text;
}
}
}
#[cfg(feature = "copypasta")]
fn init_copypasta() -> Option<copypasta::ClipboardContext> {
match copypasta::ClipboardContext::new() {
#[cfg(feature = "arboard")]
fn init_arboard() -> Option<arboard::Clipboard> {
match arboard::Clipboard::new() {
Ok(clipboard) => Some(clipboard),
Err(err) => {
tracing::error!("Failed to initialize clipboard: {}", err);