diff options
Diffstat (limited to 'copypasta')
-rw-r--r-- | copypasta/Cargo.toml | 3 | ||||
-rw-r--r-- | copypasta/src/lib.rs | 26 | ||||
-rw-r--r-- | copypasta/src/windows.rs | 74 |
3 files changed, 96 insertions, 7 deletions
diff --git a/copypasta/Cargo.toml b/copypasta/Cargo.toml index 1b887072..869857ff 100644 --- a/copypasta/Cargo.toml +++ b/copypasta/Cargo.toml @@ -12,3 +12,6 @@ keywords = ["clipboard", "copy", "paste"] objc = "0.2" objc_id = "0.1" objc-foundation = "0.1" + +[target.'cfg(windows)'.dependencies] +clipboard = "0.4.2" diff --git a/copypasta/src/lib.rs b/copypasta/src/lib.rs index f0b3c53e..cb2ebef8 100644 --- a/copypasta/src/lib.rs +++ b/copypasta/src/lib.rs @@ -4,16 +4,20 @@ // This has to be here due to macro_use #[cfg(target_os = "macos")] -#[macro_use] extern crate objc; +#[macro_use] +extern crate objc; + +#[cfg(windows)] +extern crate clipboard; /// An enumeration describing available clipboard buffers pub enum Buffer { Primary, - Selection + Selection, } /// Types that can get the system clipboard contents -pub trait Load : Sized { +pub trait Load: Sized { /// Errors encountered when working with a clipboard. Each implementation is /// allowed to define its own error type, but it must conform to std error. type Err: ::std::error::Error + Send + Sync + 'static; @@ -45,18 +49,21 @@ pub trait Load : Sized { /// /// Note that some platforms require the clipboard context to stay active in /// order to load the contents from other applications. -pub trait Store : Load { +pub trait Store: Load { /// Sets the primary clipboard contents fn store_primary<S>(&mut self, contents: S) -> Result<(), Self::Err> - where S: Into<String>; + where + S: Into<String>; /// Sets the secondary clipboard contents fn store_selection<S>(&mut self, contents: S) -> Result<(), Self::Err> - where S: Into<String>; + where + S: Into<String>; /// Store into the specified `buffer`. fn store<S>(&mut self, contents: S, buffer: Buffer) -> Result<(), Self::Err> - where S: Into<String> + where + S: Into<String>, { match buffer { Buffer::Selection => self.store_selection(contents), @@ -74,3 +81,8 @@ pub use x11::{Clipboard, Error}; mod macos; #[cfg(target_os = "macos")] pub use macos::{Clipboard, Error}; + +#[cfg(windows)] +mod windows; +#[cfg(windows)] +pub use windows::{Clipboard, Error}; diff --git a/copypasta/src/windows.rs b/copypasta/src/windows.rs new file mode 100644 index 00000000..8ec783b2 --- /dev/null +++ b/copypasta/src/windows.rs @@ -0,0 +1,74 @@ +use clipboard::ClipboardContext; +use clipboard::ClipboardProvider; + +use super::{Load, Store}; + +pub struct Clipboard(ClipboardContext); + +#[derive(Debug)] +pub enum Error { + Clipboard(Box<::std::error::Error>), +} + +impl ::std::error::Error for Error { + fn description(&self) -> &str { + match *self { + Error::Clipboard(..) => "error opening clipboard", + } + } +} + +impl ::std::fmt::Display for Error { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + match *self { + Error::Clipboard(ref err) => err.fmt(f), + } + } +} + +unsafe impl Send for Error {} +unsafe impl Sync for Error {} + +impl Load for Clipboard { + type Err = Error; + + fn new() -> Result<Self, Error> { + ClipboardContext::new() + .map(Clipboard) + .map_err(Error::Clipboard) + } + + fn load_primary(&self) -> Result<String, Self::Err> { + let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap(); + ctx.get_contents().map_err(Error::Clipboard) + } + + fn load_selection(&self) -> Result<String, Self::Err> { + let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap(); + ctx.get_contents().map_err(Error::Clipboard) + } +} + +impl Store for Clipboard { + /// Sets the primary clipboard contents + #[inline] + fn store_primary<S>(&mut self, contents: S) -> Result<(), Self::Err> + where + S: Into<String>, + { + self.0 + .set_contents(contents.into()) + .map_err(Error::Clipboard) + } + + /// Sets the secondary clipboard contents + #[inline] + fn store_selection<S>(&mut self, contents: S) -> Result<(), Self::Err> + where + S: Into<String>, + { + self.0 + .set_contents(contents.into()) + .map_err(Error::Clipboard) + } +} |