Skip to content

Commit

Permalink
Implement pasting HTML on Windows (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucieleblanc authored May 22, 2024
2 parents ed3b752 + 9febf30 commit 2138b8a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ windows-sys = { version = "0.48.0", optional = true, features = [
"Win32_System_Memory",
"Win32_System_Ole",
]}
clipboard-win = "5.0.0"
clipboard-win = "5.3.1"
log = "0.4"

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/get_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ fn main() {
let html_data = ctx.get_html().unwrap();

println!("HTML data is:\n{:?}\nAlt text is:\n{:?}", html_data.html, html_data.alt_text);
}
}
2 changes: 1 addition & 1 deletion examples/hello_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ fn main() {
"But now the clipboard text should be this HTML: \"{}\" with this alternate text: \"{}\"",
html, alt_text
);
}
}
1 change: 1 addition & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl<F: FnOnce()> Drop for ScopeGuard<F> {
}

/// Common trait for sealing platform extension traits.
#[cfg(not(target_os = "macos"))]
pub(crate) mod private {
pub trait Sealed {}

Expand Down
7 changes: 6 additions & 1 deletion src/platform/osx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ the Apache 2.0 or the MIT license at the licensee's choice. The terms
and conditions of the chosen license apply to this file.
*/

use crate::common::Error;
#[cfg(feature = "image-data")]
use crate::common::ImageData;
use crate::common::{Error, HTMLData};
#[cfg(feature = "image-data")]
use core_graphics::{
base::{kCGBitmapByteOrderDefault, kCGImageAlphaLast, kCGRenderingIntentDefault, CGFloat},
Expand Down Expand Up @@ -210,6 +210,11 @@ impl<'clipboard> Get<'clipboard> {
})
}

/// TODO: Implement get_html for MacOS.
pub(crate) fn html(self) -> Result<HTMLData, Error> {
self.text().map(HTMLData::from_alt_text)
}

#[cfg(feature = "image-data")]
pub(crate) fn image(self) -> Result<ImageData<'static>, Error> {
use objc_foundation::NSData;
Expand Down
25 changes: 24 additions & 1 deletion src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ the Apache 2.0 or the MIT license at the licensee's choice. The terms
and conditions of the chosen license apply to this file.
*/

use clipboard_win::{formats, Getter};

#[cfg(feature = "image-data")]
use crate::common::ImageData;
use crate::common::{private, Error};
use crate::common::{private, Error, HTMLData};
use std::{borrow::Cow, marker::PhantomData, thread, time::Duration};

#[cfg(feature = "image-data")]
Expand Down Expand Up @@ -522,6 +524,27 @@ impl<'clipboard> Get<'clipboard> {
String::from_utf16(&out[..bytes_read]).map_err(|_| Error::ConversionFailure)
}

pub(crate) fn html(self) -> Result<HTMLData, Error> {
if let Some(format_code) = clipboard_win::register_format("HTML Format") {
let _clipboard_assertion = self.clipboard?;

if !clipboard_win::is_format_avail(format_code.get()) {
return Err(Error::ContentNotAvailable);
}

let html_format = formats::Html::new().unwrap();
let mut html_out = String::new();
html_format
.read_clipboard(&mut html_out)
.map(|_| HTMLData::from_html(html_out))
.map_err(|e| {
Error::unknown(format!("failed to read clipboard HTML data, code {:?}", e))
})
} else {
Err(Error::ContentNotAvailable)
}
}

#[cfg(feature = "image-data")]
pub(crate) fn image(self) -> Result<ImageData<'static>, Error> {
const FORMAT: u32 = clipboard_win::formats::CF_DIBV5;
Expand Down

0 comments on commit 2138b8a

Please sign in to comment.