gravel_core/
clipboard.rs

1use std::sync::Mutex;
2
3pub struct Clipboard {
4	inner: Option<Mutex<arboard::Clipboard>>,
5}
6
7impl Clipboard {
8	#[expect(clippy::new_without_default)]
9	pub fn new() -> Self {
10		Self {
11			inner: create_clipboard().map(Mutex::new),
12		}
13	}
14
15	pub fn set_text(&self, content: &str) {
16		log::trace!("setting clipboard to {content}");
17
18		let Some(mutex) = &self.inner else {
19			log::trace!("clipboard not initialized, canceling operation");
20			return;
21		};
22
23		let Ok(mut clipboard) = mutex.lock() else {
24			log::error!("clipboard mutex poisened, canceling operation");
25			return;
26		};
27
28		clipboard
29			.set_text(content)
30			.inspect_err(|e| log::error!("unable to set clipboard: {e}"))
31			.ok();
32	}
33}
34
35fn create_clipboard() -> Option<arboard::Clipboard> {
36	log::trace!("spawning clipboard instance");
37
38	arboard::Clipboard::new()
39		.inspect_err(|e| log::error!("unable to initialize clipboard: {e}"))
40		.ok()
41}