From 8e7827ebbe8cfd3d0badd1c4a62e28b51c7fd7a1 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 29 Feb 2024 14:30:53 -0500 Subject: [PATCH] refactor: allow clipboard methods to take generic data --- src/lib.rs | 40 +++++++++++-------- src/platform/android.rs | 4 +- src/platform/dummy.rs | 8 ++-- src/platform/ios.rs | 4 +- src/platform/linux.rs | 86 +++++++++++++++++++++-------------------- src/platform/macos.rs | 7 ++-- src/platform/windows.rs | 4 +- wayland/Cargo.toml | 2 +- wayland/src/lib.rs | 27 +++++++------ 9 files changed, 100 insertions(+), 82 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a9857e3..d3f0c61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +pub mod mime; + #[cfg(all( unix, not(any( @@ -49,49 +51,55 @@ mod platform; use raw_window_handle::HasDisplayHandle; use std::error::Error; -pub struct Clipboard { - raw: Box, +pub struct Clipboard { + raw: C, } -impl Clipboard { +impl Clipboard { /// Safety: the display handle must be valid for the lifetime of `Clipboard` pub unsafe fn connect( window: &W, ) -> Result> { - let raw = platform::connect(window)?; - - Ok(Clipboard { raw }) + Ok(Clipboard { + raw: platform::connect(window)?, + }) } pub fn read(&self) -> Result> { - self.raw.read() + self.raw.read_text() } pub fn write(&mut self, contents: String) -> Result<(), Box> { - self.raw.write(contents) + self.raw.write_text(contents) } } -impl Clipboard { +impl Clipboard { pub fn read_primary(&self) -> Option>> { - self.raw.read_primary() + self.raw.read_primary_text() } - pub fn write_primary(&mut self, contents: String) -> Option>> { - self.raw.write_primary(contents) + pub fn write_primary( + &mut self, + contents: String, + ) -> Option>> { + self.raw.write_primary_text(contents) } } pub trait ClipboardProvider { - fn read(&self) -> Result>; + fn read_text(&self) -> Result>; - fn write(&mut self, contents: String) -> Result<(), Box>; + fn write_text(&mut self, contents: String) -> Result<(), Box>; - fn read_primary(&self) -> Option>> { + fn read_primary_text(&self) -> Option>> { None } - fn write_primary(&mut self, _contents: String) -> Option>> { + fn write_primary_text( + &mut self, + _contents: String, + ) -> Option>> { None } } diff --git a/src/platform/android.rs b/src/platform/android.rs index b0ac8b6..c2d460a 100644 --- a/src/platform/android.rs +++ b/src/platform/android.rs @@ -5,8 +5,8 @@ use std::error::Error; pub fn connect( _window: &W, -) -> Result, Box> { - Ok(Box::new(Clipboard::new()?)) +) -> Result> { + Ok(Clipboard::new()) } pub struct Clipboard; diff --git a/src/platform/dummy.rs b/src/platform/dummy.rs index 8cf7418..096f68c 100644 --- a/src/platform/dummy.rs +++ b/src/platform/dummy.rs @@ -2,15 +2,15 @@ use crate::ClipboardProvider; use raw_window_handle::HasDisplayHandle; -struct Dummy; +struct Clipboard; pub fn connect( _window: &W, -) -> Result, Box> { - Ok(Box::new(Dummy)) +) -> Result> { + Ok(Clipboard) } -impl ClipboardProvider for Dummy { +impl ClipboardProvider for Clipboard { fn read(&self) -> Result> { Err(Box::new(Error::Unimplemented)) } diff --git a/src/platform/ios.rs b/src/platform/ios.rs index 84ae06a..1ab6e84 100644 --- a/src/platform/ios.rs +++ b/src/platform/ios.rs @@ -5,8 +5,8 @@ use std::error::Error; pub fn connect( _window: &W, -) -> Result, Box> { - Ok(Box::new(Clipboard::new()?)) +) -> Result> { + Clipboard::new() } pub struct Clipboard; diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 12d8c67..8fef7e9 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -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> { + 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> { + 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>> { + 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>> { + 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( window: &W, -) -> Result, Box> { +) -> Result> { 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> { - self.read() - } - - fn read_primary(&self) -> Option>> { - Some(self.read_primary()) - } - - fn write(&mut self, contents: String) -> Result<(), Box> { - self.write(contents) - } - - fn write_primary(&mut self, contents: String) -> Option>> { - Some(self.write_primary(contents)) - } -} - -impl ClipboardProvider for x11::Clipboard { - fn read(&self) -> Result> { - self.read().map_err(Box::from) - } - - fn read_primary(&self) -> Option>> { - Some(self.read_primary().map_err(Box::from)) - } - - fn write(&mut self, contents: String) -> Result<(), Box> { - self.write(contents).map_err(Box::from) - } - - fn write_primary(&mut self, contents: String) -> Option>> { - Some(self.write_primary(contents).map_err(Box::from)) - } -} diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 5b399e0..084ace8 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -1,15 +1,16 @@ use crate::ClipboardProvider; +pub(crate) use clipboard_macos::Clipboard; use raw_window_handle::HasDisplayHandle; use std::error::Error; pub fn connect( _window: &W, -) -> Result, Box> { - Ok(Box::new(clipboard_macos::Clipboard::new()?)) +) -> Result> { + Clipboard::new() } -impl ClipboardProvider for clipboard_macos::Clipboard { +impl ClipboardProvider for Clipboard { fn read(&self) -> Result> { self.read() } diff --git a/src/platform/windows.rs b/src/platform/windows.rs index a0c774b..f317b10 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -7,8 +7,8 @@ use std::error::Error; pub fn connect( _window: &W, -) -> Result, Box> { - Ok(Box::new(Clipboard)) +) -> Result> { + Ok(Clipboard) } pub struct Clipboard; diff --git a/wayland/Cargo.toml b/wayland/Cargo.toml index 6853c9d..d5919d9 100644 --- a/wayland/Cargo.toml +++ b/wayland/Cargo.toml @@ -10,4 +10,4 @@ documentation = "https://docs.rs/clipboard_wayland" keywords = ["clipboard", "wayland"] [dependencies] -smithay-clipboard = "0.7" +smithay-clipboard = { git = "https://github.com/wash2/smithay-clipboard", branch = "mime-types" } diff --git a/wayland/src/lib.rs b/wayland/src/lib.rs index fdf62c0..2558177 100644 --- a/wayland/src/lib.rs +++ b/wayland/src/lib.rs @@ -12,9 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::error::Error; -use std::ffi::c_void; -use std::sync::{Arc, Mutex}; +use std::{ + error::Error, + ffi::c_void, + sync::{Arc, Mutex}, +}; pub struct Clipboard { context: Arc>, @@ -29,22 +31,25 @@ impl Clipboard { Clipboard { context } } - pub fn read(&self) -> Result> { - Ok(self.context.lock().unwrap().load()?) + pub fn read_text(&self) -> Result> { + Ok(self.context.lock().unwrap().load_text()?) } - pub fn read_primary(&self) -> Result> { - Ok(self.context.lock().unwrap().load_primary()?) + pub fn read_primary_text(&self) -> Result> { + Ok(self.context.lock().unwrap().load_primary_text()?) } - pub fn write(&mut self, data: String) -> Result<(), Box> { - self.context.lock().unwrap().store(data); + pub fn write_text(&mut self, data: String) -> Result<(), Box> { + self.context.lock().unwrap().store_text(data); Ok(()) } - pub fn write_primary(&mut self, data: String) -> Result<(), Box> { - self.context.lock().unwrap().store_primary(data); + pub fn write_primary_text( + &mut self, + data: String, + ) -> Result<(), Box> { + self.context.lock().unwrap().store_primary_text(data); Ok(()) }