refactor: allow clipboard methods to take generic data
This commit is contained in:
parent
4bd0f74db5
commit
8e7827ebbe
9 changed files with 100 additions and 82 deletions
|
|
@ -6,51 +6,55 @@ use std::error::Error;
|
|||
pub use clipboard_wayland as wayland;
|
||||
pub use clipboard_x11 as x11;
|
||||
|
||||
pub enum Clipboard {
|
||||
Wayland(wayland::Clipboard),
|
||||
X11(x11::Clipboard),
|
||||
}
|
||||
|
||||
impl ClipboardProvider for Clipboard {
|
||||
fn read_text(&self) -> Result<String, Box<dyn Error>> {
|
||||
match self {
|
||||
Clipboard::Wayland(c) => c.read_text(),
|
||||
Clipboard::X11(c) => c.read().map_err(Box::from),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_text(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
|
||||
match self {
|
||||
Clipboard::Wayland(c) => c.write_text(contents),
|
||||
Clipboard::X11(c) => c.write(contents).map_err(Box::from),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_primary_text(&self) -> Option<Result<String, Box<dyn Error>>> {
|
||||
match self {
|
||||
Clipboard::Wayland(c) => Some(c.read_primary_text()),
|
||||
Clipboard::X11(c) => Some(c.read_primary().map_err(Box::from)),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_primary_text(
|
||||
&mut self,
|
||||
contents: String,
|
||||
) -> Option<Result<(), Box<dyn Error>>> {
|
||||
match self {
|
||||
Clipboard::Wayland(c) => Some(c.write_primary_text(contents)),
|
||||
Clipboard::X11(c) => {
|
||||
Some(c.write_primary(contents).map_err(Box::from))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn connect<W: HasDisplayHandle>(
|
||||
window: &W,
|
||||
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
|
||||
) -> Result<Clipboard, Box<dyn Error>> {
|
||||
let clipboard = match window.display_handle()?.as_raw() {
|
||||
RawDisplayHandle::Wayland(handle) => {
|
||||
Box::new(wayland::Clipboard::connect(handle.display.as_ptr())) as _
|
||||
}
|
||||
_ => Box::new(x11::Clipboard::connect()?) as _,
|
||||
RawDisplayHandle::Wayland(handle) => Clipboard::Wayland(
|
||||
wayland::Clipboard::connect(handle.display.as_ptr()),
|
||||
) as _,
|
||||
_ => Clipboard::X11(x11::Clipboard::connect()?) as _,
|
||||
};
|
||||
|
||||
Ok(clipboard)
|
||||
}
|
||||
|
||||
impl ClipboardProvider for wayland::Clipboard {
|
||||
fn read(&self) -> Result<String, Box<dyn Error>> {
|
||||
self.read()
|
||||
}
|
||||
|
||||
fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
|
||||
Some(self.read_primary())
|
||||
}
|
||||
|
||||
fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
|
||||
self.write(contents)
|
||||
}
|
||||
|
||||
fn write_primary(&mut self, contents: String) -> Option<Result<(), Box<dyn Error>>> {
|
||||
Some(self.write_primary(contents))
|
||||
}
|
||||
}
|
||||
|
||||
impl ClipboardProvider for x11::Clipboard {
|
||||
fn read(&self) -> Result<String, Box<dyn Error>> {
|
||||
self.read().map_err(Box::from)
|
||||
}
|
||||
|
||||
fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
|
||||
Some(self.read_primary().map_err(Box::from))
|
||||
}
|
||||
|
||||
fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
|
||||
self.write(contents).map_err(Box::from)
|
||||
}
|
||||
|
||||
fn write_primary(&mut self, contents: String) -> Option<Result<(), Box<dyn Error>>> {
|
||||
Some(self.write_primary(contents).map_err(Box::from))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue