clipboard-rs 0.3.5

Cross-platform clipboard API (text | image | rich text | html | files | monitoring changes) | 跨平台剪贴板 API(文本|图片|富文本|html|文件|监听变化) Windows,MacOS,Linux
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use crate::{common::Result, Clipboard, ClipboardContent, ClipboardHandler, ContentFormat};

#[cfg(feature = "image")]
use crate::{common::RustImage, RustImageData};

use std::io::Read;
use std::sync::mpsc::{self, Receiver, Sender};
use std::time::Duration;
use wl_clipboard_rs::{
	copy::{self, MimeSource, MimeType, Options, Source},
	paste::{self, get_contents, get_mime_types, ClipboardType, Seat},
	utils::is_primary_selection_supported,
};

const MIME_HTML: &str = "text/html";
const MIME_RTF: &str = "text/rtf";
#[cfg(feature = "image")]
const MIME_PNG: &str = "image/png";
const MIME_URI_LIST: &str = "text/uri-list";
const FILE_PATH_PREFIX: &str = "file://";

fn read_wayland_clipboard(mime: paste::MimeType) -> Result<Vec<u8>> {
	let result = get_contents(ClipboardType::Regular, Seat::Unspecified, mime);
	match result {
		Ok((mut pipe, _)) => {
			let mut buffer = vec![];
			pipe.read_to_end(&mut buffer).map_err(
				|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() },
			)?;
			Ok(buffer)
		}
		Err(paste::Error::ClipboardEmpty) | Err(paste::Error::NoMimeType) => {
			Err("Clipboard is empty or content type not available".into())
		}
		Err(e) => Err(e.to_string().into()),
	}
}

fn write_wayland_clipboard(sources: Vec<MimeSource>) -> Result<()> {
	let mut opts = Options::new();
	opts.foreground(false);
	opts.clipboard(copy::ClipboardType::Regular);
	opts.copy_multi(sources)
		.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() })?;
	Ok(())
}

fn get_offered_mime_types() -> Result<std::collections::HashSet<String>> {
	get_mime_types(ClipboardType::Regular, Seat::Unspecified)
		.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() })
}

pub struct ClipboardContext;

impl ClipboardContext {
	pub fn new() -> Result<Self> {
		match is_primary_selection_supported() {
			Ok(_) => Ok(Self),
			Err(e) => Err(e.to_string().into()),
		}
	}
}

impl Clipboard for ClipboardContext {
	fn available_formats(&self) -> Result<Vec<String>> {
		let mime_types = get_offered_mime_types()?;
		Ok(mime_types.into_iter().collect())
	}

	fn has(&self, format: ContentFormat) -> bool {
		#[allow(unreachable_patterns)]
		let mime_to_check = match format {
			ContentFormat::Text => "text/plain",
			ContentFormat::Html => MIME_HTML,
			ContentFormat::Rtf => MIME_RTF,
			#[cfg(feature = "image")]
			ContentFormat::Image => MIME_PNG,
			ContentFormat::Files => MIME_URI_LIST,
			ContentFormat::Other(ref s) => s.as_str(),
			#[cfg(not(feature = "image"))]
			_ => return false,
		};
		if let Ok(mime_types) = get_offered_mime_types() {
			// For text, also check text/plain;charset=utf-8
			if mime_to_check == "text/plain" {
				mime_types.iter().any(|m| m.starts_with("text/plain"))
			} else {
				mime_types.contains(mime_to_check)
			}
		} else {
			false
		}
	}

	fn clear(&self) -> Result<()> {
		copy::clear(copy::ClipboardType::Regular, copy::Seat::All)
			.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() })?;
		Ok(())
	}

	fn get_buffer(&self, format: &str) -> Result<Vec<u8>> {
		read_wayland_clipboard(paste::MimeType::Specific(format))
	}

	fn get_text(&self) -> Result<String> {
		let bytes = read_wayland_clipboard(paste::MimeType::Text)?;
		String::from_utf8(bytes)
			.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() })
	}

	fn get_rich_text(&self) -> Result<String> {
		let bytes = read_wayland_clipboard(paste::MimeType::Specific(MIME_RTF))?;
		String::from_utf8(bytes)
			.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() })
	}

	fn get_html(&self) -> Result<String> {
		let bytes = read_wayland_clipboard(paste::MimeType::Specific(MIME_HTML))?;
		String::from_utf8(bytes)
			.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() })
	}

	#[cfg(feature = "image")]
	fn get_image(&self) -> Result<RustImageData> {
		let bytes = read_wayland_clipboard(paste::MimeType::Specific(MIME_PNG))?;
		RustImageData::from_bytes(&bytes)
	}

	fn get_files(&self) -> Result<Vec<String>> {
		let bytes = read_wayland_clipboard(paste::MimeType::Specific(MIME_URI_LIST))?;
		let text = String::from_utf8(bytes)
			.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() })?;
		Ok(text
			.lines()
			.map(|line| line.trim_end_matches('\r'))
			.filter(|line| {
				!line.starts_with('#') && !line.is_empty() && line.starts_with(FILE_PATH_PREFIX)
			})
			.map(|line| line.to_string())
			.collect())
	}

	fn get(&self, formats: &[ContentFormat]) -> Result<Vec<ClipboardContent>> {
		let mut contents = Vec::new();
		for format in formats {
			#[allow(unreachable_patterns)]
			match format {
				ContentFormat::Text => {
					if let Ok(text) = self.get_text() {
						contents.push(ClipboardContent::Text(text));
					}
				}
				ContentFormat::Html => {
					if let Ok(html) = self.get_html() {
						contents.push(ClipboardContent::Html(html));
					}
				}
				ContentFormat::Rtf => {
					if let Ok(rtf) = self.get_rich_text() {
						contents.push(ClipboardContent::Rtf(rtf));
					}
				}
				#[cfg(feature = "image")]
				ContentFormat::Image => {
					if let Ok(image) = self.get_image() {
						contents.push(ClipboardContent::Image(image));
					}
				}
				ContentFormat::Files => {
					if let Ok(files) = self.get_files() {
						contents.push(ClipboardContent::Files(files));
					}
				}
				ContentFormat::Other(mime) => {
					if let Ok(data) = self.get_buffer(mime) {
						contents.push(ClipboardContent::Other(mime.clone(), data));
					}
				}
				#[cfg(not(feature = "image"))]
				_ => {}
			}
		}
		Ok(contents)
	}

	fn set_buffer(&self, format: &str, buffer: Vec<u8>) -> Result<()> {
		write_wayland_clipboard(vec![MimeSource {
			source: Source::Bytes(buffer.into_boxed_slice()),
			mime_type: MimeType::Specific(format.to_string()),
		}])
	}

	fn set_text(&self, text: String) -> Result<()> {
		write_wayland_clipboard(vec![MimeSource {
			source: Source::Bytes(text.into_bytes().into_boxed_slice()),
			mime_type: MimeType::Text,
		}])
	}

	fn set_rich_text(&self, text: String) -> Result<()> {
		write_wayland_clipboard(vec![MimeSource {
			source: Source::Bytes(text.into_bytes().into_boxed_slice()),
			mime_type: MimeType::Specific(MIME_RTF.to_string()),
		}])
	}

	fn set_html(&self, html: String) -> Result<()> {
		write_wayland_clipboard(vec![MimeSource {
			source: Source::Bytes(html.into_bytes().into_boxed_slice()),
			mime_type: MimeType::Specific(MIME_HTML.to_string()),
		}])
	}

	#[cfg(feature = "image")]
	fn set_image(&self, image: RustImageData) -> Result<()> {
		let bytes = image.to_png()?;
		write_wayland_clipboard(vec![MimeSource {
			source: Source::Bytes(bytes.get_bytes().into()),
			mime_type: MimeType::Specific(MIME_PNG.to_string()),
		}])
	}

	fn set_files(&self, files: Vec<String>) -> Result<()> {
		// Normalize paths to file:// URIs and use CRLF for text/uri-list
		let uri_list: Vec<String> = files
			.into_iter()
			.map(|f| {
				if f.starts_with(FILE_PATH_PREFIX) {
					f
				} else {
					format!("{FILE_PATH_PREFIX}{f}")
				}
			})
			.collect();
		let data = uri_list.join("\r\n");
		write_wayland_clipboard(vec![MimeSource {
			source: Source::Bytes(data.into_bytes().into_boxed_slice()),
			mime_type: MimeType::Specific(MIME_URI_LIST.to_string()),
		}])
	}

	fn set(&self, contents: Vec<ClipboardContent>) -> Result<()> {
		let mut sources = Vec::new();
		for content in contents {
			#[allow(unreachable_patterns)]
			match content {
				ClipboardContent::Text(text) => {
					sources.push(MimeSource {
						source: Source::Bytes(text.into_bytes().into_boxed_slice()),
						mime_type: MimeType::Text,
					});
				}
				ClipboardContent::Html(html) => {
					sources.push(MimeSource {
						source: Source::Bytes(html.into_bytes().into_boxed_slice()),
						mime_type: MimeType::Specific(MIME_HTML.to_string()),
					});
				}
				ClipboardContent::Rtf(rtf) => {
					sources.push(MimeSource {
						source: Source::Bytes(rtf.into_bytes().into_boxed_slice()),
						mime_type: MimeType::Specific(MIME_RTF.to_string()),
					});
				}
				#[cfg(feature = "image")]
				ClipboardContent::Image(image) => {
					let bytes = image.to_png()?;
					sources.push(MimeSource {
						source: Source::Bytes(bytes.get_bytes().into()),
						mime_type: MimeType::Specific(MIME_PNG.to_string()),
					});
				}
				ClipboardContent::Files(files) => {
					let uri_list: Vec<String> = files
						.into_iter()
						.map(|f| {
							if f.starts_with(FILE_PATH_PREFIX) {
								f
							} else {
								format!("{FILE_PATH_PREFIX}{f}")
							}
						})
						.collect();
					let data = uri_list.join("\r\n");
					sources.push(MimeSource {
						source: Source::Bytes(data.into_bytes().into_boxed_slice()),
						mime_type: MimeType::Specific(MIME_URI_LIST.to_string()),
					});
				}
				ClipboardContent::Other(mime, data) => {
					sources.push(MimeSource {
						source: Source::Bytes(data.into_boxed_slice()),
						mime_type: MimeType::Specific(mime),
					});
				}
				#[cfg(not(feature = "image"))]
				_ => {}
			}
		}
		write_wayland_clipboard(sources)
	}
}

unsafe impl Send for ClipboardContext {}

// Polling-based clipboard watcher for Wayland
pub struct ClipboardWatcherContext<T: ClipboardHandler> {
	pub(crate) handlers: Vec<T>,
	pub(crate) stop_signal: Sender<()>,
	stop_receiver: Receiver<()>,
	interval: Duration,
}

unsafe impl<T: ClipboardHandler + Send> Send for ClipboardWatcherContext<T> {}

/// Default polling interval. The Wayland backend has no change-notification
/// protocol, so it polls the clipboard; this is the wait between polls.
const DEFAULT_WATCH_INTERVAL: Duration = Duration::from_millis(500);

impl<T: ClipboardHandler> ClipboardWatcherContext<T> {
	pub fn new() -> Result<Self> {
		Self::new_with_interval(DEFAULT_WATCH_INTERVAL)
	}

	/// Creates a watcher that polls the clipboard every `interval`.
	///
	/// Wayland exposes no clipboard-change notification, so changes are detected
	/// by polling. A smaller `interval` lowers detection latency at the cost of
	/// more frequent wake-ups; `new` uses [`DEFAULT_WATCH_INTERVAL`].
	pub fn new_with_interval(interval: Duration) -> Result<Self> {
		// Verify data-control protocol is available (same check as ClipboardContext)
		is_primary_selection_supported()
			.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.to_string().into() })?;
		let (tx, rx) = mpsc::channel();
		Ok(Self {
			handlers: Vec::new(),
			stop_signal: tx,
			stop_receiver: rx,
			interval,
		})
	}
}

impl<T: ClipboardHandler> ClipboardWatcherContext<T> {
	pub(crate) fn get_shutdown_channel(&self) -> WatcherShutdown {
		WatcherShutdown {
			sender: self.stop_signal.clone(),
		}
	}

	pub(crate) fn start_watch_inner(&mut self) {
		let mut last_mime_types: Vec<String> = Vec::new();
		let mut last_text = String::new();

		// Get initial clipboard state
		if let Ok(types) = get_offered_mime_types() {
			last_mime_types = {
				let mut v: Vec<String> = types.into_iter().collect();
				v.sort();
				v
			};
		}
		if let Ok(text) = read_wayland_clipboard(paste::MimeType::Text) {
			if let Ok(s) = String::from_utf8(text) {
				last_text = s;
			}
		}

		loop {
			if self.stop_receiver.recv_timeout(self.interval).is_ok() {
				break;
			}

			let changed = if let Ok(types) = get_offered_mime_types() {
				let mut current_types: Vec<String> = types.into_iter().collect();
				current_types.sort();

				if current_types != last_mime_types {
					// MIME types changed, clipboard definitely changed
					last_mime_types = current_types;
					// Update text cache too
					if let Ok(bytes) = read_wayland_clipboard(paste::MimeType::Text) {
						if let Ok(s) = String::from_utf8(bytes) {
							last_text = s;
						}
					} else {
						last_text.clear();
					}
					true
				} else if let Ok(bytes) = read_wayland_clipboard(paste::MimeType::Text) {
					// Same MIME types, check if text content changed
					if let Ok(current) = String::from_utf8(bytes) {
						if current != last_text {
							last_text = current;
							true
						} else {
							false
						}
					} else {
						false
					}
				} else if !last_text.is_empty() {
					last_text.clear();
					true
				} else {
					false
				}
			} else {
				// No clipboard content available
				if !last_mime_types.is_empty() {
					last_mime_types.clear();
					last_text.clear();
					true
				} else {
					false
				}
			};

			if changed {
				self.handlers
					.iter_mut()
					.for_each(|handler| handler.on_clipboard_change());
			}
		}
	}
}

pub struct WatcherShutdown {
	pub(crate) sender: Sender<()>,
}

impl Drop for WatcherShutdown {
	fn drop(&mut self) {
		let _ = self.sender.send(());
	}
}