diff --git a/src/clipboard.rs b/src/clipboard.rs index 609c770..a9bb87a 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -1,23 +1,27 @@ -#[cfg(any(test, not(any(feature = "x11", target_os = "macos", windows))))] +#[cfg(any(test, not(any(target_os = "linux", target_os = "macos", windows))))] use copypasta::nop_clipboard::NopClipboardContext; -#[cfg(any(target_os = "macos", windows))] +#[cfg(target_os = "linux")] +use copypasta::x11_clipboard::{Primary as X11SelectionClipboard, X11ClipboardContext}; +#[cfg(any(target_os = "linux", target_os = "macos", windows))] use copypasta::ClipboardContext; use copypasta::ClipboardProvider; pub struct Clipboard { clipboard: Box, + selection: Option>, } impl Clipboard { - #[cfg(any(target_os = "macos", windows))] + #[cfg(any(target_os = "linux", target_os = "macos", windows))] pub fn new() -> Self { Self::default() } - #[cfg(any(test, not(any(target_os = "macos", windows))))] + #[cfg(any(test, not(any(target_os = "linux", target_os = "macos", windows))))] pub fn new_nop() -> Self { Self { clipboard: Box::new(NopClipboardContext::new().unwrap()), + selection: None, } } } @@ -27,24 +31,41 @@ impl Default for Clipboard { #[cfg(any(target_os = "macos", windows))] return Self { clipboard: Box::new(ClipboardContext::new().unwrap()), + selection: None, }; - #[cfg(not(any(target_os = "macos", windows)))] + #[cfg(target_os = "linux")] + return Self { + clipboard: Box::new(ClipboardContext::new().unwrap()), + selection: Some(Box::new( + X11ClipboardContext::::new().unwrap(), + )), + }; + + #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))] return Self::new_nop(); } } impl Clipboard { pub fn store(&mut self, text: impl Into) { - self.clipboard - .set_contents(text.into()) - .unwrap_or_else(|err| { - panic!("Unable to store text in clipboard: {}", err); - }); + let clipboard = match &mut self.selection { + Some(provider) => provider, + None => &mut self.clipboard, + }; + + clipboard.set_contents(text.into()).unwrap_or_else(|err| { + panic!("Unable to store text in clipboard: {}", err); + }); } pub fn _load(&mut self) -> String { - match self.clipboard.get_contents() { + let clipboard = match &mut self.selection { + Some(provider) => provider, + None => &mut self.clipboard, + }; + + match clipboard.get_contents() { Err(err) => { panic!("Unable to load text from clipboard: {}", err); }