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
//! A clipboard of the embedder's own: when the core has one
//! ([`crate::Core::set_clipboard`]), the app's clipboard calls (`nsh/base`'s
//! `Clipboard`, `setString`, `getString`, `hasString`) are served from it
//! rather than the system's, so that a test's clipboard is its own and the
//! machine's stays as it was (`nsh test --target`). Text inputs' own copy
//! and paste stay the system's.
use std::sync::Mutex;
use crate::element::ClipboardItem;
/// What the app's clipboard calls keep.
pub trait ClipboardBackend: Send + Sync {
/// The text it holds.
fn text(&self) -> Option<String>;
/// Holds `text` from now on, alone (`None`: nothing).
fn set_text(&self, text: Option<String>);
/// The item it holds (`Clipboard.write`), every format it keeps; a
/// backend that keeps text only holds an item of its text.
fn item(&self) -> Option<ClipboardItem> {
self.text().map(|text| ClipboardItem { text, html: None })
}
/// Holds `item` from now on (`None`: nothing, as after an image was
/// copied, which the backend does not keep). A backend that keeps text
/// only keeps the item's text.
fn set_item(&self, item: Option<ClipboardItem>) {
self.set_text(item.map(|item| item.text));
}
}
/// A clipboard in memory.
#[derive(Debug, Default)]
pub struct MemoryClipboard {
item: Mutex<Option<ClipboardItem>>,
}
impl MemoryClipboard {
/// A clipboard holding `text` at first.
pub fn new(text: Option<String>) -> MemoryClipboard {
MemoryClipboard {
item: Mutex::new(text.map(|text| ClipboardItem { text, html: None })),
}
}
}
impl ClipboardBackend for MemoryClipboard {
fn text(&self) -> Option<String> {
self.item().map(|item| item.text)
}
fn set_text(&self, text: Option<String>) {
self.set_item(text.map(|text| ClipboardItem { text, html: None }));
}
fn item(&self) -> Option<ClipboardItem> {
self.item.lock().unwrap_or_else(|e| e.into_inner()).clone()
}
fn set_item(&self, item: Option<ClipboardItem>) {
*self.item.lock().unwrap_or_else(|e| e.into_inner()) = item;
}
}