#![cfg(target_os = "windows")]
use clipboard_win::{raw, register_format, Clipboard, Setter, SysResult, Unicode};
const EXCLUDE_FORMAT: &str = "ExcludeClipboardContentFromMonitorProcessing";
const ATTEMPTS: usize = 5;
const WAIT: core::time::Duration = core::time::Duration::from_millis(5);
pub fn text<S: AsRef<str>>(data: S) -> SysResult<()> {
let mut attempts = ATTEMPTS;
let _clipboard = loop {
match Clipboard::new() {
Ok(v) => break v,
Err(e) => match attempts {
0 => return Err(e),
_ => {
std::thread::sleep(WAIT);
attempts -= 1;
}
},
}
};
Unicode.write_clipboard(&data)?;
if let Some(format) = register_format(EXCLUDE_FORMAT) {
raw::set_without_clear(format.get(), b"1")?;
}
Ok(())
}