From deb4454c3565a8d1d3115f21ebe40339e216b7dd Mon Sep 17 00:00:00 2001 From: DoumanAsh Date: Tue, 5 Mar 2024 22:12:53 +0900 Subject: [PATCH] Add binding to GetPriorityClipboardFormat --- src/raw.rs | 16 ++++++++++++++++ src/sys.rs | 1 + tests/test_clip.rs | 7 +++++++ 3 files changed, 24 insertions(+) diff --git a/src/raw.rs b/src/raw.rs index 101e802..0cfa65e 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -207,6 +207,22 @@ pub fn is_format_avail(format: c_uint) -> bool { unsafe { IsClipboardFormatAvailable(format) != 0 } } +#[inline(always)] +///Returns the first available format in the specified list +/// +///Returns `None` if no format is available or clipboard is empty +pub fn which_format_avail(formats: &[c_uint]) -> Option { + let result = unsafe { + GetPriorityClipboardFormat(formats.as_ptr(), formats.len() as _) + }; + if result < 0 { + None + } else { + NonZeroU32::new(result as _) + } +} + + #[inline] ///Retrieves number of currently available formats on clipboard. /// diff --git a/src/sys.rs b/src/sys.rs index ed7f0a1..37ec106 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -23,6 +23,7 @@ extern "system" { pub fn EmptyClipboard() -> BOOL; pub fn GetClipboardSequenceNumber() -> DWORD; pub fn IsClipboardFormatAvailable(format: c_uint) -> BOOL; + pub fn GetPriorityClipboardFormat(formats: *const c_uint, size: c_int) -> BOOL; pub fn CountClipboardFormats() -> c_int; pub fn EnumClipboardFormats(format: c_uint) -> c_uint; pub fn GetClipboardFormatNameW(format: c_uint, lpszFormatName: *mut u16, cchMaxCount: c_int) -> c_int; diff --git a/tests/test_clip.rs b/tests/test_clip.rs index 5d76347..e610e86 100644 --- a/tests/test_clip.rs +++ b/tests/test_clip.rs @@ -1,4 +1,5 @@ use clipboard_win::{Getter, Setter, Clipboard, is_format_avail, types}; +use clipboard_win::raw::which_format_avail; use clipboard_win::formats::{Html, RawData, Unicode, Bitmap, CF_TEXT, CF_UNICODETEXT, CF_BITMAP, FileList, CF_HDROP}; fn should_set_file_list() { @@ -38,6 +39,10 @@ fn should_work_with_string() { Unicode.write_clipboard(&text).expect("Write text"); + let first_format = which_format_avail(&[CF_TEXT, CF_UNICODETEXT]).unwrap(); + assert!(is_format_avail(CF_UNICODETEXT)); + assert_eq!(CF_UNICODETEXT, first_format.get()); + let mut output = String::new(); assert_eq!(Unicode.read_clipboard(&mut output).expect("Read text"), text.len()); @@ -127,10 +132,12 @@ fn should_set_get_html() { assert_eq!(html1.code(), html2.code()); assert!(!is_format_avail(html1.code())); + assert!(which_format_avail(&[html1.code()]).is_none()); let _clip = Clipboard::new_attempts(10).expect("Open clipboard"); html1.write_clipboard(&HTML).expect("write clipboard"); assert!(is_format_avail(html1.code())); + assert_eq!(which_format_avail(&[CF_TEXT, html1.code()]).unwrap().get(), html1.code()); let mut out = String::new(); html1.read_clipboard(&mut out).expect("read clipboard");