2019-12-19 05:47:36 +01:00
|
|
|
use crate::ClipboardProvider;
|
|
|
|
|
|
2022-07-27 15:02:43 +02:00
|
|
|
use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle};
|
2019-12-19 05:47:36 +01:00
|
|
|
use std::error::Error;
|
|
|
|
|
|
2019-12-19 06:32:29 +01:00
|
|
|
pub use clipboard_wayland as wayland;
|
|
|
|
|
pub use clipboard_x11 as x11;
|
2019-12-19 05:47:36 +01:00
|
|
|
|
2022-07-27 15:02:43 +02:00
|
|
|
pub fn connect<W: HasRawDisplayHandle>(
|
2019-12-19 05:47:36 +01:00
|
|
|
window: &W,
|
|
|
|
|
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
|
2022-07-27 15:02:43 +02:00
|
|
|
let clipboard = match window.raw_display_handle() {
|
2024-01-16 21:12:14 -08:00
|
|
|
Ok(RawDisplayHandle::Wayland(handle)) => Box::new(unsafe {
|
|
|
|
|
wayland::Clipboard::connect(handle.display.as_ptr())
|
|
|
|
|
}) as _,
|
2021-03-07 03:25:51 +01:00
|
|
|
_ => Box::new(x11::Clipboard::connect()?) as _,
|
2019-12-19 05:47:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(clipboard)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ClipboardProvider for wayland::Clipboard {
|
|
|
|
|
fn read(&self) -> Result<String, Box<dyn Error>> {
|
|
|
|
|
self.read()
|
|
|
|
|
}
|
2021-03-06 04:34:02 +01:00
|
|
|
|
|
|
|
|
fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
|
|
|
|
|
self.write(contents)
|
|
|
|
|
}
|
2019-12-19 05:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ClipboardProvider for x11::Clipboard {
|
|
|
|
|
fn read(&self) -> Result<String, Box<dyn Error>> {
|
2021-03-06 04:34:02 +01:00
|
|
|
self.read().map_err(Box::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
|
|
|
|
|
self.write(contents).map_err(Box::from)
|
2019-12-19 05:47:36 +01:00
|
|
|
}
|
|
|
|
|
}
|