From 6016d4189c7ee938b69ef454f10b72a86dfd08eb Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Thu, 11 Jul 2024 14:04:55 +0200 Subject: [PATCH] Add Get::html on macOS (unimplemented on others) --- src/common.rs | 2 +- src/lib.rs | 5 +++++ src/platform/linux/mod.rs | 4 ++++ src/platform/osx.rs | 22 ++++++++++++++++++++++ src/platform/windows.rs | 4 ++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/common.rs b/src/common.rs index 221a883..adda67c 100644 --- a/src/common.rs +++ b/src/common.rs @@ -176,7 +176,7 @@ impl Drop for ScopeGuard { pub(crate) mod private { // This is currently unused on macOS, so silence the warning which appears // since there's no extension traits making use of this trait sealing structure. - #[cfg_attr(target_vendor = "apple", allow(unreachable_pub))] + #[cfg_attr(target_vendor = "apple", allow(unreachable_pub, dead_code))] pub trait Sealed {} impl Sealed for crate::Get<'_> {} diff --git a/src/lib.rs b/src/lib.rs index 57256d5..92f1f8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -179,6 +179,11 @@ impl Get<'_> { self.platform.text() } + /// Completes the "get" operation by fetching HTML from the clipboard. + pub fn html(self) -> Result { + self.platform.html() + } + /// Completes the "get" operation by fetching image data from the clipboard and returning the /// decoded pixels. /// diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index d8207e3..10e7a3f 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -114,6 +114,10 @@ impl<'clipboard> Get<'clipboard> { } } + pub(crate) fn html(self) -> Result { + unimplemented!() + } + #[cfg(feature = "image-data")] pub(crate) fn image(self) -> Result, Error> { match self.clipboard { diff --git a/src/platform/osx.rs b/src/platform/osx.rs index ac4dc79..369dff1 100644 --- a/src/platform/osx.rs +++ b/src/platform/osx.rs @@ -205,6 +205,28 @@ impl<'clipboard> Get<'clipboard> { }) } + pub(crate) fn html(self) -> Result { + autoreleasepool(|_| { + // XXX: We explicitly use `pasteboardItems` and not `stringForType` since the latter will concat + // multiple strings, if present, into one and return it instead of reading just the first which is `arboard`'s + // historical behavior. + let contents = + unsafe { self.clipboard.pasteboard.pasteboardItems() }.ok_or_else(|| { + Error::Unknown { + description: String::from("NSPasteboard#pasteboardItems errored"), + } + })?; + + for item in contents { + if let Some(string) = unsafe { item.stringForType(NSPasteboardTypeHTML) } { + return Ok(string.to_string()); + } + } + + Err(Error::ContentNotAvailable) + }) + } + #[cfg(feature = "image-data")] pub(crate) fn image(self) -> Result, Error> { use objc2_app_kit::NSPasteboardTypeTIFF; diff --git a/src/platform/windows.rs b/src/platform/windows.rs index 6544b92..6b2f03f 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -567,6 +567,10 @@ impl<'clipboard> Get<'clipboard> { String::from_utf16(&out[..bytes_read]).map_err(|_| Error::ConversionFailure) } + pub(crate) fn html(self) -> Result { + unimplemented!() + } + #[cfg(feature = "image-data")] pub(crate) fn image(self) -> Result, Error> { const FORMAT: u32 = clipboard_win::formats::CF_DIBV5;