2016-10-08 18:42:33 -07:00
|
|
|
//! A cross-platform clipboard library
|
|
|
|
|
2018-07-03 00:11:24 +02:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", deny(clippy, if_not_else, enum_glob_use, wrong_pub_self_convention))]
|
|
|
|
|
2016-10-08 20:57:30 -07:00
|
|
|
// This has to be here due to macro_use
|
|
|
|
#[cfg(target_os = "macos")]
|
2018-10-17 06:02:52 +13:00
|
|
|
#[macro_use]
|
|
|
|
extern crate objc;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
extern crate clipboard;
|
2016-10-08 20:57:30 -07:00
|
|
|
|
2017-02-01 22:13:08 -08:00
|
|
|
/// An enumeration describing available clipboard buffers
|
|
|
|
pub enum Buffer {
|
|
|
|
Primary,
|
2018-10-17 06:02:52 +13:00
|
|
|
Selection,
|
2017-02-01 22:13:08 -08:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:42:33 -07:00
|
|
|
/// Types that can get the system clipboard contents
|
2018-10-17 06:02:52 +13:00
|
|
|
pub trait Load: Sized {
|
2016-10-08 18:42:33 -07:00
|
|
|
/// 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;
|
|
|
|
|
|
|
|
/// Create a clipboard
|
|
|
|
fn new() -> Result<Self, Self::Err>;
|
|
|
|
|
|
|
|
/// Get the primary clipboard contents.
|
|
|
|
fn load_primary(&self) -> Result<String, Self::Err>;
|
|
|
|
|
|
|
|
/// Get the clipboard selection contents.
|
|
|
|
///
|
|
|
|
/// On most platforms, this doesn't mean anything. A default implementation
|
|
|
|
/// is provided which uses the primary clipboard.
|
|
|
|
#[inline]
|
|
|
|
fn load_selection(&self) -> Result<String, Self::Err> {
|
|
|
|
self.load_primary()
|
|
|
|
}
|
2017-02-01 22:13:08 -08:00
|
|
|
|
|
|
|
fn load(&self, buffer: Buffer) -> Result<String, Self::Err> {
|
|
|
|
match buffer {
|
|
|
|
Buffer::Selection => self.load_selection(),
|
|
|
|
Buffer::Primary => self.load_primary(),
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Types that can set the system clipboard contents
|
|
|
|
///
|
|
|
|
/// Note that some platforms require the clipboard context to stay active in
|
|
|
|
/// order to load the contents from other applications.
|
2018-10-17 06:02:52 +13:00
|
|
|
pub trait Store: Load {
|
2016-10-08 18:42:33 -07:00
|
|
|
/// Sets the primary clipboard contents
|
2016-12-24 18:08:46 -05:00
|
|
|
fn store_primary<S>(&mut self, contents: S) -> Result<(), Self::Err>
|
2018-10-17 06:02:52 +13:00
|
|
|
where
|
|
|
|
S: Into<String>;
|
2016-10-08 18:42:33 -07:00
|
|
|
|
|
|
|
/// Sets the secondary clipboard contents
|
2016-12-24 18:08:46 -05:00
|
|
|
fn store_selection<S>(&mut self, contents: S) -> Result<(), Self::Err>
|
2018-10-17 06:02:52 +13:00
|
|
|
where
|
|
|
|
S: Into<String>;
|
2017-02-01 22:13:08 -08:00
|
|
|
|
|
|
|
/// Store into the specified `buffer`.
|
|
|
|
fn store<S>(&mut self, contents: S, buffer: Buffer) -> Result<(), Self::Err>
|
2018-10-17 06:02:52 +13:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
2017-02-01 22:13:08 -08:00
|
|
|
{
|
|
|
|
match buffer {
|
|
|
|
Buffer::Selection => self.store_selection(contents),
|
|
|
|
Buffer::Primary => self.store_primary(contents),
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:42:33 -07:00
|
|
|
}
|
|
|
|
|
2018-04-03 08:52:41 +02:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
2016-10-08 18:42:33 -07:00
|
|
|
mod x11;
|
2018-04-03 08:52:41 +02:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
|
2016-10-08 18:42:33 -07:00
|
|
|
pub use x11::{Clipboard, Error};
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
mod macos;
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
pub use macos::{Clipboard, Error};
|
2018-10-17 06:02:52 +13:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
mod windows;
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub use windows::{Clipboard, Error};
|