2019-12-18 05:55:32 +01:00
|
|
|
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
|
|
pub struct Clipboard {
|
2019-12-18 06:38:55 +01:00
|
|
|
raw: RefCell<Box<dyn copypasta::ClipboardProvider>>,
|
2019-12-18 05:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
2019-12-18 06:38:55 +01:00
|
|
|
Box::new(unsafe {
|
|
|
|
|
let (_, raw) = copypasta::wayland_clipboard::create_clipboards_from_external(
|
2019-12-18 05:55:32 +01:00
|
|
|
handle.display as *mut _,
|
2019-12-18 06:38:55 +01:00
|
|
|
);
|
2019-12-18 05:55:32 +01:00
|
|
|
|
2019-12-18 06:38:55 +01:00
|
|
|
raw
|
|
|
|
|
}) as _
|
2019-12-18 05:55:32 +01:00
|
|
|
}
|
2019-12-18 06:38:55 +01:00
|
|
|
_ => Box::new(copypasta::ClipboardContext::new()?) as _,
|
2019-12-18 05:55:32 +01:00
|
|
|
};
|
|
|
|
|
|
2019-12-18 06:38:55 +01:00
|
|
|
Ok(Clipboard {
|
|
|
|
|
raw: RefCell::new(raw),
|
|
|
|
|
})
|
2019-12-18 05:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn read(&self) -> Result<String, Box<dyn Error>> {
|
|
|
|
|
// TODO: Think about use of `RefCell`
|
|
|
|
|
// Maybe we should make `read` mutable (?)
|
2019-12-18 06:38:55 +01:00
|
|
|
self.raw.borrow_mut().get_contents()
|
2019-12-18 05:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn write(
|
|
|
|
|
&mut self,
|
|
|
|
|
contents: impl Into<String>,
|
|
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2019-12-18 06:38:55 +01:00
|
|
|
self.raw.borrow_mut().set_contents(contents.into())
|
2019-12-18 05:55:32 +01:00
|
|
|
}
|
|
|
|
|
}
|