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
//! Flutter counterpart: `services/clipboard.dart`.
use inset_foundation::App;
/// Data stored on the system clipboard.
///
/// The system clipboard can contain data of various media types. This data
/// structure currently supports only plain text data, in the [`text`](Self::text) property.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClipboardData {
/// Plain text variant of this clipboard data.
///
/// This is nullable as other clipboard data variants, like images, may be
/// added in the future. Currently, plain text is the only supported variant
/// and this is guaranteed to be non-null.
pub text: Option<String>,
}
impl ClipboardData {
/// Creates data for the system clipboard.
pub fn new(text: impl Into<String>) -> ClipboardData {
ClipboardData {
text: Some(text.into()),
}
}
}
/// Utility methods for interacting with the system's clipboard.
pub struct Clipboard;
impl Clipboard {
/// Plain text data format string.
///
/// Used with [`get_data`](Self::get_data).
pub const K_TEXT_PLAIN: &'static str = "text/plain";
/// Stores the given clipboard data on the clipboard.
pub fn set_data(app: &App, data: ClipboardData) {
let Some(text) = data.text.as_deref() else {
return;
};
if let Some(clipboard) = app.platform().clipboard() {
clipboard.set_text(text);
}
}
/// Retrieves data from the clipboard that matches the given format.
///
/// The `format` argument specifies the media type, such as `text/plain`, of
/// the data to obtain.
///
/// Returns [`None`] if the data could not be obtained.
pub fn get_data(app: &App, format: &str) -> Option<ClipboardData> {
if format != Self::K_TEXT_PLAIN {
return None;
}
app.platform()
.clipboard()
.and_then(|clipboard| clipboard.text())
.map(ClipboardData::new)
}
/// Returns true if (and only if) the clipboard contains string data.
pub fn has_strings(app: &App) -> bool {
app.platform()
.clipboard()
.is_some_and(|clipboard| clipboard.has_strings())
}
}