watch_change/
watch_change.rs

1use clipboard_rs::{
2	Clipboard, ClipboardContext, ClipboardHandler, ClipboardWatcher, ClipboardWatcherContext,
3};
4use std::{thread, time::Duration};
5
6struct Manager {
7	ctx: ClipboardContext,
8}
9
10impl Manager {
11	pub fn new() -> Self {
12		let ctx = ClipboardContext::new().unwrap();
13		Manager { ctx }
14	}
15}
16
17impl ClipboardHandler for Manager {
18	fn on_clipboard_change(&mut self) {
19		println!(
20			"on_clipboard_change, txt = {}",
21			self.ctx.get_text().unwrap_or("".to_string())
22		);
23	}
24}
25
26fn main() {
27	let manager = Manager::new();
28
29	let mut watcher = ClipboardWatcherContext::new().unwrap();
30
31	let watcher_shutdown: clipboard_rs::WatcherShutdown =
32		watcher.add_handler(manager).get_shutdown_channel();
33
34	thread::spawn(move || {
35		thread::sleep(Duration::from_secs(5));
36		println!("stop watch!");
37		watcher_shutdown.stop();
38	});
39
40	println!("start watch!");
41	watcher.start_watch();
42}