Skip to content

Commit

Permalink
Add binding to GetPriorityClipboardFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Mar 5, 2024
1 parent 2f7e3a0 commit deb4454
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NonZeroU32> {
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.
///
Expand Down
1 change: 1 addition & 0 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/test_clip.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit deb4454

Please sign in to comment.