clipboard_rs/
lib.rs

1pub mod common;
2mod platform;
3pub use common::{ClipboardContent, ClipboardHandler, ContentFormat, Result, RustImageData};
4pub use image::imageops::FilterType;
5#[cfg(target_os = "linux")]
6pub use platform::ClipboardContextX11Options;
7pub use platform::{ClipboardContext, ClipboardWatcherContext, WatcherShutdown};
8
9pub trait Clipboard: Send {
10	/// zh: 获得剪切板当前内容的所有格式
11	/// en: Get all formats of the current content in the clipboard
12	fn available_formats(&self) -> Result<Vec<String>>;
13
14	fn has(&self, format: ContentFormat) -> bool;
15
16	/// zh: 清空剪切板
17	/// en: clear clipboard
18	fn clear(&self) -> Result<()>;
19
20	/// zh: 获得指定格式的数据,以字节数组形式返回
21	/// en: Get the data in the specified format in the clipboard as a byte array
22	fn get_buffer(&self, format: &str) -> Result<Vec<u8>>;
23
24	/// zh: 仅获得无格式纯文本,以字符串形式返回
25	/// en: Get plain text content in the clipboard as string
26	fn get_text(&self) -> Result<String>;
27
28	/// zh: 获得剪贴板中的富文本内容,以字符串形式返回
29	/// en: Get the rich text content in the clipboard as string
30	fn get_rich_text(&self) -> Result<String>;
31
32	/// zh: 获得剪贴板中的html内容,以字符串形式返回
33	/// en: Get the html format content in the clipboard as string
34	fn get_html(&self) -> Result<String>;
35
36	fn get_image(&self) -> Result<RustImageData>;
37
38	fn get_files(&self) -> Result<Vec<String>>;
39
40	fn get(&self, formats: &[ContentFormat]) -> Result<Vec<ClipboardContent>>;
41
42	fn set_buffer(&self, format: &str, buffer: Vec<u8>) -> Result<()>;
43
44	fn set_text(&self, text: String) -> Result<()>;
45
46	fn set_rich_text(&self, text: String) -> Result<()>;
47
48	fn set_html(&self, html: String) -> Result<()>;
49
50	fn set_image(&self, image: RustImageData) -> Result<()>;
51
52	fn set_files(&self, files: Vec<String>) -> Result<()>;
53
54	/// set image will clear clipboard
55	fn set(&self, contents: Vec<ClipboardContent>) -> Result<()>;
56}
57
58pub trait ClipboardWatcher<T: ClipboardHandler>: Send {
59	/// zh: 添加一个剪切板变化处理器,可以添加多个处理器,处理器需要实现 [`ClipboardHandler`] 这个trait
60	/// en: Add a clipboard change handler, you can add multiple handlers, the handler needs to implement the trait [`ClipboardHandler`]
61	fn add_handler(&mut self, handler: T) -> &mut Self;
62
63	/// zh: 开始监视剪切板变化,这是一个阻塞方法,直到监视结束,或者调用了stop方法,所以建议在单独的线程中调用
64	/// en: Start monitoring clipboard changes, this is a blocking method, until the monitoring ends, or the stop method is called, so it is recommended to call it in a separate thread
65	fn start_watch(&mut self);
66
67	/// zh: 获得停止监视的通道,可以通过这个通道停止监视
68	/// en: Get the channel to stop monitoring, you can stop monitoring through this channel
69	fn get_shutdown_channel(&self) -> WatcherShutdown;
70}
71
72impl WatcherShutdown {
73	/// zh: 停止监视
74	/// en: stop watching
75	pub fn stop(self) {
76		drop(self);
77	}
78}