Start with an initial draft

This commit is contained in:
Héctor Ramón Jiménez 2019-12-18 05:55:32 +01:00
commit 6f71824857
7 changed files with 160 additions and 0 deletions

85
src/lib.rs Normal file
View file

@ -0,0 +1,85 @@
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use std::cell::RefCell;
use std::error::Error;
pub struct Clipboard {
raw: Raw,
}
enum Raw {
#[cfg(all(
unix,
not(any(
target_os = "macos",
target_os = "android",
target_os = "emscripten"
))
))]
Wayland(RefCell<smithay_clipboard::WaylandClipboard>),
NotWayland(RefCell<clipboard::ClipboardContext>),
}
impl Clipboard {
pub fn new<W: HasRawWindowHandle>(
window: &W,
) -> Result<Self, Box<dyn Error>> {
let raw = match window.raw_window_handle() {
#[cfg(all(
unix,
not(any(
target_os = "macos",
target_os = "android",
target_os = "emscripten"
))
))]
RawWindowHandle::Wayland(handle) => {
assert!(!handle.display.is_null());
Raw::Wayland(RefCell::new(unsafe {
smithay_clipboard::WaylandClipboard::new_from_external(
handle.display as *mut _,
)
}))
}
_ => {
use clipboard::ClipboardProvider as _;
Raw::NotWayland(RefCell::new(
clipboard::ClipboardContext::new()?
))
}
};
Ok(Clipboard { raw })
}
pub fn read(&self) -> Result<String, Box<dyn Error>> {
// TODO: Think about use of `RefCell`
// Maybe we should make `read` mutable (?)
use clipboard::ClipboardProvider as _;
match &self.raw {
Raw::Wayland(clipboard) => Ok(clipboard.borrow_mut().load(None)),
Raw::NotWayland(clipboard) => clipboard.borrow_mut().get_contents(),
}
}
pub fn write(
&mut self,
contents: impl Into<String>,
) -> Result<(), Box<dyn Error>> {
use clipboard::ClipboardProvider as _;
match &self.raw {
Raw::Wayland(clipboard) => {
clipboard.borrow_mut().store(None, contents);
Ok(())
}
Raw::NotWayland(clipboard) => {
clipboard.borrow_mut().set_contents(contents.into())
}
}
}
}