clipboard_rs/
lib.rs

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