clipboard-rs 0.3.5

Cross-platform clipboard API (text | image | rich text | html | files | monitoring changes) | 跨平台剪贴板 API(文本|图片|富文本|html|文件|监听变化) Windows,MacOS,Linux
Documentation
#[cfg(target_os = "linux")]
use clipboard_rs::ClipboardContextX11Options;
use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};

#[cfg(target_os = "macos")]
const TMP_PATH: &str = "/tmp/";
#[cfg(target_os = "windows")]
const TMP_PATH: &str = "C:\\Windows\\Temp\\";
#[cfg(all(
	unix,
	not(any(
		target_os = "macos",
		target_os = "ios",
		target_os = "android",
		target_os = "emscripten"
	))
))]
const TMP_PATH: &str = "/tmp/";
// ios
#[cfg(any(target_os = "ios", target_os = "android"))]
const TMP_PATH: &str = "/tmp/";

#[cfg(target_os = "linux")]
fn setup_clipboard() -> ClipboardContext {
	ClipboardContext::new_with_options(ClipboardContextX11Options { read_timeout: None }).unwrap()
}

#[cfg(not(target_os = "linux"))]
fn setup_clipboard() -> ClipboardContext {
	ClipboardContext::new().unwrap()
}

fn main() {
	let ctx = setup_clipboard();

	let types = ctx.available_formats().unwrap();
	println!("{:?}", types);

	let img = ctx.get_image();

	match img {
		Ok(img) => {
			let _ = img
				.save_to_path(format!("{}test.png", TMP_PATH).as_str())
				.map_err(|e| println!("save test.png err={}", e));

			let resize_img = img
				.thumbnail(300, 300)
				.map_err(|e| println!("thumbnail err={}", e))
				.unwrap();

			let _ = resize_img
				.save_to_path(format!("{}test_thumbnail.png", TMP_PATH).as_str())
				.map_err(|e| println!("save test_thumbnail.png err={}", e));
		}
		Err(err) => {
			println!("err={}", err);
		}
	}
}