Skip to content

Commit

Permalink
Replace copypasta with arboard
Browse files Browse the repository at this point in the history
Closes #1474
  • Loading branch information
emilk committed Apr 10, 2022
1 parent 9b37c82 commit a25d957
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 81 deletions.
123 changes: 65 additions & 58 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions egui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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 }
Expand Down
38 changes: 18 additions & 20 deletions egui-winit/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
/// 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(),
}
}
}

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
Expand All @@ -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);
Expand Down

0 comments on commit a25d957

Please sign in to comment.