nw_sys/clipboard.rs
1//!
2//! Access to the system clipboard allowing to copy and paste images from
3//! and to other applications.
4//!
5//! # Synopsis
6//! ```
7//! // get the system clipboard
8//! let clipboard = nw::clipboard::get();
9//!
10//! //read available types of data in clipboard currently
11//! let types = clip.get_available_types();
12//! log_info!("clipboard data types: {:?}", types);
13//!
14//! // write text data to clipboard
15//! clip.set("Hello");
16//!
17//! // read text data from clipboard
18//! let text = clip.get();
19//!
20//! ```
21
22use crate::options::OptionsTrait;
23use crate::result::Result;
24use js_sys::{Array, Object};
25use wasm_bindgen::prelude::*;
26
27#[wasm_bindgen]
28extern "C" {
29
30 ///
31 /// Interface for interacting with the system clipboard. For usage example please refer to [nw_sys::clipboard](self)
32 ///
33
34 #[wasm_bindgen(js_namespace=nw, js_name = Clipboard)]
35 #[derive(Debug, Clone)]
36 pub type Clipboard;
37
38 #[wasm_bindgen(js_namespace=["nw", "Clipboard"], js_name = get)]
39 fn get_impl() -> Clipboard;
40
41 #[wasm_bindgen(method, js_name = set)]
42 /// Write `data` of type string to the clipboard.
43 /// This method will clear the clipboard and replace with the given data.
44 /// Hence another call to this method will overwrite with the new one.
45 /// To write multiple types of data to clipboard simultaneously,
46 /// you will need to use [clip.set(clipboardDataList)](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddatalist) below.
47 ///
48 /// - data: the data to write to the clipboard
49 ///
50 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
51 ///
52 pub fn set(this: &Clipboard, data: &str);
53
54 #[wasm_bindgen(method, js_name = set)]
55 /// Write `data` of `type` to the clipboard.
56 /// This method will clear the clipboard and replace with the given data.
57 /// Hence another call to this method will overwrite with the new one.
58 /// To write multiple types of data to clipboard simultaneously,
59 /// you will need to use [clip.set(clipboardDataList)](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddatalist) below.
60 ///
61 /// - data: the data to write to the clipboard
62 /// - data_type: the type of the data. Support text, png, jpeg, html and rtf. By default, type is set to "text".
63 ///
64 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
65 ///
66 pub fn set_with_data_type(this: &Clipboard, data: &str, data_type: &str);
67
68 #[wasm_bindgen(method, js_name = set)]
69 /// Write `data` of `type` to the clipboard.
70 /// This method will clear the clipboard and replace with the given data.
71 /// Hence another call to this method will overwrite with the new one.
72 /// To write multiple types of data to clipboard simultaneously,
73 /// you will need to use [clip.set(clipboardDataList)](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddatalist) below.
74 ///
75 /// - data: the data to write to the clipboard
76 /// - data_type: the type of the data. Support text, png, jpeg, html and rtf. By default, type is set to "text".
77 /// - raw: requiring raw image data. This option is only valid if type is png or jpeg. By default, raw is set to false.
78 ///
79 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
80 ///
81 pub fn set_with_data_type_and_raw(this: &Clipboard, data: &str, data_type: &str, raw: bool);
82
83 #[wasm_bindgen(method, js_name = set)]
84 /// Write data of type to the clipboard.
85 ///
86 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddata)
87 ///
88 pub fn set_data(this: &Clipboard, data: DataWrite);
89
90 #[wasm_bindgen(method, js_name = set)]
91 /// Write data of type to the clipboard.
92 ///
93 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddata)
94 ///
95 fn set_data_array_impl(this: &Clipboard, data: Array);
96
97 #[wasm_bindgen(method, js_name = get)]
98 /// Get the data
99 ///
100 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgettype-raw)
101 ///
102 pub fn get(this: &Clipboard) -> String;
103
104 #[wasm_bindgen(method, js_name = get)]
105 /// Get the data of type
106 ///
107 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgettype-raw)
108 ///
109 pub fn get_with_data_type(this: &Clipboard, data_type: &str) -> String;
110
111 #[wasm_bindgen(method, js_name = get)]
112 /// Get the data of type and as raw
113 ///
114 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgettype-raw)
115 ///
116 pub fn get_with_data_type_and_raw(this: &Clipboard, data_type: &str, raw: bool) -> String;
117
118 #[wasm_bindgen(method, js_name = get)]
119 /// Get the data
120 ///
121 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgetclipboarddata)
122 ///
123 pub fn get_data(this: &Clipboard, data: DataRead) -> String;
124
125 #[wasm_bindgen(method, js_name = get)]
126 fn get_data_array_impl(this: &Clipboard, data: Array) -> Array;
127
128 #[wasm_bindgen(method, js_name = readAvailableTypes)]
129 fn get_available_types_impl(this: &Clipboard) -> Array;
130
131 #[wasm_bindgen(method)]
132 /// Clear the clipboard.
133 ///
134 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipclear)
135 ///
136 pub fn clear(this: &Clipboard);
137
138 #[wasm_bindgen(extends = Object)]
139 #[derive(Debug, Clone, PartialEq, Eq)]
140 /// A object containing
141 /// [`data`](Self#method.data),
142 /// [`type`](Self#method.data_type) and
143 /// [`raw`](Self#method.raw) to be written to clipboard
144 ///
145 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddata)
146 ///
147 pub type DataWrite;
148
149 #[wasm_bindgen(extends = Object)]
150 #[derive(Debug, Clone, PartialEq, Eq)]
151 /// A object containing
152 /// [`type`](Self#method.data_type) and
153 /// [`raw`](Self#method.raw) for reading from clipboard
154 ///
155 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgetclipboarddata)
156 ///
157 pub type DataRead;
158}
159
160/// Returns the `Clipboard` object
161///
162/// **Note:**
163/// The Selection Clipboard in X11 is not supported.
164///
165/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipboardget)
166///
167pub fn get() -> Clipboard {
168 Clipboard::get_impl()
169}
170
171impl Clipboard {
172 /// Write data of type to the clipboard.
173 ///
174 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddata)
175 ///
176 pub fn set_data_array(&self, list: Vec<DataWrite>) {
177 let data_array = Array::new();
178 for d in list {
179 data_array.push(&JsValue::from(d));
180 }
181 self.set_data_array_impl(data_array);
182 }
183
184 /// Get the data as `Vector<Option<String>`
185 ///
186 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgetclipboarddatalist)
187 ///
188 pub fn get_data_array(&self, list: Vec<DataRead>) -> Result<Vec<Option<String>>> {
189 let data_array = Array::new();
190 for d in list {
191 data_array.push(&JsValue::from(d));
192 }
193 let array = self.get_data_array_impl(data_array);
194 let mut result = Vec::new();
195 for index in 0..array.length() {
196 let data = array.get(index);
197 let data = js_sys::Reflect::get(&data, &JsValue::from("data"))?;
198 result.push(data.as_string());
199 }
200
201 Ok(result)
202 }
203
204 /// Returns list of available types of data in clipboard currently.
205 ///
206 /// ### Each item is one of following types:
207 /// - text: plain text. Can be read by [clip.get_with_data_type("text")](self::Clipboard#method.get_with_data_type).
208 /// - html: HTML text. Can be read by [clip.get_with_data_type("html")](self::Clipboard#method.get_with_data_type).
209 /// - rtf: RTF (Rich Text Format). Can be read by [clip.get_with_data_type("rtf")](self::Clipboard#method.get_with_data_type).
210 /// - png: PNG image. Can be read by [clip.get_with_data_type("png")](self::Clipboard#method.get_with_data_type).
211 /// - jpeg: JPEG image. Can be read by [clip.get_with_data_type("jpeg")](self::Clipboard#method.get_with_data_type).
212 ///
213 /// You can use the returned list as a suggestion to get the right data from clipboard.
214 ///
215 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipreadavailabletypes)
216 ///
217 pub fn get_available_types(&self) -> Vec<String> {
218 let array = self.get_available_types_impl();
219 let mut result = Vec::new();
220 for index in 0..array.length() {
221 if let Some(v) = array.get(index).as_string() {
222 result.push(v);
223 }
224 }
225
226 result
227 }
228}
229
230impl OptionsTrait for DataWrite {
231 fn initialize(self) -> Self {
232 self.data_type("text").raw(false)
233 }
234}
235impl OptionsTrait for DataRead {}
236
237impl DataWrite {
238 /// The data to write to the clipboard
239 ///
240 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
241 pub fn data(self, data: &str) -> Self {
242 self.set("data", JsValue::from(data))
243 }
244
245 /// The type of the data.
246 /// Support text, png, jpeg, html and rtf.
247 ///
248 /// By default, type is set to "text".
249 ///
250 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
251 pub fn data_type(self, data_type: &str) -> Self {
252 self.set("type", JsValue::from(data_type))
253 }
254
255 /// Requiring raw image data.
256 /// This option is only valid if type is png or jpeg.
257 ///
258 /// By default, raw is set to false.
259 ///
260 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
261 pub fn raw(self, raw: bool) -> Self {
262 self.set("raw", JsValue::from(raw))
263 }
264}
265
266impl DataRead {
267 /// The type of the data.
268 /// Support text, png, jpeg, html and rtf.
269 ///
270 /// By default, type is set to "text".
271 ///
272 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
273 pub fn data_type(self, data_type: &str) -> Self {
274 self.set("type", JsValue::from(data_type))
275 }
276
277 /// Requiring raw image data.
278 /// This option is only valid if type is png or jpeg.
279 ///
280 /// By default, raw is set to false.
281 ///
282 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
283 pub fn raw(self, raw: bool) -> Self {
284 self.set("raw", JsValue::from(raw))
285 }
286}
287
288impl From<(String, Option<bool>)> for DataRead {
289 fn from(info: (String, Option<bool>)) -> Self {
290 let option = Self::new().data_type(&info.0);
291 if let Some(raw) = info.1 {
292 option.raw(raw)
293 } else {
294 option
295 }
296 }
297}