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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//!
//! Access to the system clipboard allowing to copy and paste images from
//! and to other applications.
//!
//! # Synopsis
//! ```
//! // get the system clipboard
//! let clipboard = nw::clipboard::get();
//!
//! //read available types of data in clipboard currently
//! let types = clip.get_available_types();
//! log_info!("clipboard data types: {:?}", types);
//!
//! // write text data to clipboard
//! clip.set("Hello");
//!
//! // read text data from clipboard
//! let text = clip.get();
//!
//! ```

use crate::options::OptionsTrait;
use crate::result::Result;
use js_sys::{Array, Object};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {

    ///
    /// Interface for interacting with the system clipboard. For usage example please refer to [nw_sys::clipboard](self)
    ///

    #[wasm_bindgen(js_namespace=nw, js_name = Clipboard)]
    #[derive(Debug, Clone)]
    pub type Clipboard;

    #[wasm_bindgen(js_namespace=["nw", "Clipboard"], js_name = get)]
    fn get_impl() -> Clipboard;

    #[wasm_bindgen(method, js_name = set)]
    /// Write `data` of type string to the clipboard.
    /// This method will clear the clipboard and replace with the given data.
    /// Hence another call to this method will overwrite with the new one.
    /// To write multiple types of data to clipboard simultaneously,
    /// you will need to use [clip.set(clipboardDataList)](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddatalist) below.
    ///
    /// - data: the data to write to the clipboard
    ///  
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
    ///
    pub fn set(this: &Clipboard, data: &str);

    #[wasm_bindgen(method, js_name = set)]
    /// Write `data` of `type` to the clipboard.
    /// This method will clear the clipboard and replace with the given data.
    /// Hence another call to this method will overwrite with the new one.
    /// To write multiple types of data to clipboard simultaneously,
    /// you will need to use [clip.set(clipboardDataList)](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddatalist) below.
    ///
    /// - data: the data to write to the clipboard
    /// - data_type: the type of the data. Support text, png, jpeg, html and rtf. By default, type is set to "text".
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
    ///
    pub fn set_with_data_type(this: &Clipboard, data: &str, data_type: &str);

    #[wasm_bindgen(method, js_name = set)]
    /// Write `data` of `type` to the clipboard.
    /// This method will clear the clipboard and replace with the given data.
    /// Hence another call to this method will overwrite with the new one.
    /// To write multiple types of data to clipboard simultaneously,
    /// you will need to use [clip.set(clipboardDataList)](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddatalist) below.
    ///
    /// - data: the data to write to the clipboard
    /// - data_type: the type of the data. Support text, png, jpeg, html and rtf. By default, type is set to "text".
    /// - raw: requiring raw image data. This option is only valid if type is png or jpeg. By default, raw is set to false.
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
    ///
    pub fn set_with_data_type_and_raw(this: &Clipboard, data: &str, data_type: &str, raw: bool);

    #[wasm_bindgen(method, js_name = set)]
    /// Write data of type to the clipboard.
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddata)
    ///
    pub fn set_data(this: &Clipboard, data: DataWrite);

    #[wasm_bindgen(method, js_name = set)]
    /// Write data of type to the clipboard.
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddata)
    ///
    fn set_data_array_impl(this: &Clipboard, data: Array);

    #[wasm_bindgen(method, js_name = get)]
    /// Get the data
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgettype-raw)
    ///
    pub fn get(this: &Clipboard) -> String;

    #[wasm_bindgen(method, js_name = get)]
    /// Get the data of type
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgettype-raw)
    ///
    pub fn get_with_data_type(this: &Clipboard, data_type: &str) -> String;

    #[wasm_bindgen(method, js_name = get)]
    /// Get the data of type and as raw
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgettype-raw)
    ///
    pub fn get_with_data_type_and_raw(this: &Clipboard, data_type: &str, raw: bool) -> String;

    #[wasm_bindgen(method, js_name = get)]
    /// Get the data
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgetclipboarddata)
    ///
    pub fn get_data(this: &Clipboard, data: DataRead) -> String;

    #[wasm_bindgen(method, js_name = get)]
    fn get_data_array_impl(this: &Clipboard, data: Array) -> Array;

    #[wasm_bindgen(method, js_name = readAvailableTypes)]
    fn get_available_types_impl(this: &Clipboard) -> Array;

    #[wasm_bindgen(method)]
    /// Clear the clipboard.
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipclear)
    ///
    pub fn clear(this: &Clipboard);

    #[wasm_bindgen(extends = Object)]
    #[derive(Debug, Clone, PartialEq, Eq)]
    /// A object containing
    /// [`data`](Self#method.data),
    /// [`type`](Self#method.data_type) and
    /// [`raw`](Self#method.raw) to be written to clipboard
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddata)
    ///
    pub type DataWrite;

    #[wasm_bindgen(extends = Object)]
    #[derive(Debug, Clone, PartialEq, Eq)]
    /// A object containing
    /// [`type`](Self#method.data_type) and
    /// [`raw`](Self#method.raw) for reading from clipboard
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgetclipboarddata)
    ///
    pub type DataRead;
}

/// Returns the `Clipboard` object
///
/// **Note:**
/// The Selection Clipboard in X11 is not supported.
///
/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipboardget)
///
pub fn get() -> Clipboard {
    Clipboard::get_impl()
}

impl Clipboard {
    /// Write data of type to the clipboard.
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetclipboarddata)
    ///
    pub fn set_data_array(&self, list: Vec<DataWrite>) {
        let data_array = Array::new();
        for d in list {
            data_array.push(&JsValue::from(d));
        }
        self.set_data_array_impl(data_array);
    }

    /// Get the data as `Vector<Option<String>`
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipgetclipboarddatalist)
    ///
    pub fn get_data_array(&self, list: Vec<DataRead>) -> Result<Vec<Option<String>>> {
        let data_array = Array::new();
        for d in list {
            data_array.push(&JsValue::from(d));
        }
        let array = self.get_data_array_impl(data_array);
        let mut result = Vec::new();
        for index in 0..array.length() {
            let data = array.get(index);
            let data = js_sys::Reflect::get(&data, &JsValue::from("data"))?;
            result.push(data.as_string());
        }

        Ok(result)
    }

    /// Returns list of available types of data in clipboard currently.
    ///
    /// ### Each item is one of following types:
    /// - text: plain text. Can be read by [clip.get_with_data_type("text")](self::Clipboard#method.get_with_data_type).
    /// - html: HTML text. Can be read by [clip.get_with_data_type("html")](self::Clipboard#method.get_with_data_type).
    /// - rtf: RTF (Rich Text Format). Can be read by [clip.get_with_data_type("rtf")](self::Clipboard#method.get_with_data_type).
    /// - png: PNG image. Can be read by [clip.get_with_data_type("png")](self::Clipboard#method.get_with_data_type).
    /// - jpeg: JPEG image. Can be read by [clip.get_with_data_type("jpeg")](self::Clipboard#method.get_with_data_type).
    ///
    /// You can use the returned list as a suggestion to get the right data from clipboard.
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipreadavailabletypes)
    ///
    pub fn get_available_types(&self) -> Vec<String> {
        let array = self.get_available_types_impl();
        let mut result = Vec::new();
        for index in 0..array.length() {
            if let Some(v) = array.get(index).as_string() {
                result.push(v);
            }
        }

        result
    }
}

impl OptionsTrait for DataWrite {
    fn initialize(self) -> Self {
        self.data_type("text").raw(false)
    }
}
impl OptionsTrait for DataRead {}

impl DataWrite {
    /// The data to write to the clipboard
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
    pub fn data(self, data: &str) -> Self {
        self.set("data", JsValue::from(data))
    }

    /// The type of the data.
    /// Support text, png, jpeg, html and rtf.
    ///
    /// By default, type is set to "text".
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
    pub fn data_type(self, data_type: &str) -> Self {
        self.set("type", JsValue::from(data_type))
    }

    /// Requiring raw image data.
    /// This option is only valid if type is png or jpeg.
    ///
    /// By default, raw is set to false.
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
    pub fn raw(self, raw: bool) -> Self {
        self.set("raw", JsValue::from(raw))
    }
}

impl DataRead {
    /// The type of the data.
    /// Support text, png, jpeg, html and rtf.
    ///
    /// By default, type is set to "text".
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
    pub fn data_type(self, data_type: &str) -> Self {
        self.set("type", JsValue::from(data_type))
    }

    /// Requiring raw image data.
    /// This option is only valid if type is png or jpeg.
    ///
    /// By default, raw is set to false.
    ///
    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Clipboard/#clipsetdata-type-raw)
    pub fn raw(self, raw: bool) -> Self {
        self.set("raw", JsValue::from(raw))
    }
}

impl From<(String, Option<bool>)> for DataRead {
    fn from(info: (String, Option<bool>)) -> Self {
        let option = Self::new().data_type(&info.0);
        if let Some(raw) = info.1 {
            option.raw(raw)
        } else {
            option
        }
    }
}