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), NotWayland(RefCell), } impl Clipboard { pub fn new( window: &W, ) -> Result> { 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> { // 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, ) -> Result<(), Box> { 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()) } } } }