2019-12-19 05:47:36 +01:00
|
|
|
use crate::ClipboardProvider;
|
|
|
|
|
|
2024-01-16 21:28:25 -08:00
|
|
|
use raw_window_handle::{HasDisplayHandle, 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
|
|
|
|
2024-01-16 21:28:25 -08:00
|
|
|
pub unsafe fn connect<W: HasDisplayHandle>(
|
2019-12-19 05:47:36 +01:00
|
|
|
window: &W,
|
|
|
|
|
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
|
2024-01-16 21:28:25 -08:00
|
|
|
let clipboard = match window.display_handle()?.as_raw() {
|
|
|
|
|
RawDisplayHandle::Wayland(handle) => {
|
|
|
|
|
Box::new(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
|
|
|
}
|
|
|
|
|
}
|