nw_sys/
window.rs

1//!
2//! Creation and management of application windows.  Provides control such as maximizing,
3//! minimizing and restoring application windows, changing the title, size and position,
4//! window transparency, control over kiosk and full-screen modes, window image capture,
5//! iframe control and print options control including print header, footer and other options.
6//!
7//! # Synopsis
8//!
9//! ```rust
10//! use workflow_wasm::prelude::*;
11//!
12//! // Get the current window
13//! let win = nw_sys::window::get();
14//!
15//! // Listen to the minimize event
16//! let minimize_callback = callback!(|| {
17//!     log_info!("Window is minimized");
18//! });
19//!
20//! win.on("minimize", minimize_callback.as_ref());
21//!
22//! // Minimize the window
23//! win.minimize();
24//!
25//! // Unlisten the minimize event
26//! win.remove_all_listeners_with_name("minimize");
27//!
28//! // Create a new window and get it
29//! let options = nw_sys::window::Options::new()
30//!     .title("Test window");
31//!
32//! let open_callback = callback!(|new_win:nw_sys::Window| {
33//!     // And listen to new window's focus event
34//!     let focus_callabck = callback!(||{
35//!         log_info!("New window is focused");
36//!     });
37//!     new_win.on("focus", focus_callabck.as_ref());
38//! });
39//!
40//! nw_sys::window::open_with_options_and_callback(
41//!     "https://github.com",
42//!     &options,
43//!     open_callback.as_ref()
44//! );
45//!
46//! // save these `open_callback`, `focus_callabck`
47//! // and `minimize_callback` somewhere
48//! app.push_callback(open_callback);
49//! app.push_callback(minimize_callback);
50//!
51//! ```
52
53use crate::menu::Menu;
54use crate::options::OptionsTrait;
55use cfg_if::cfg_if;
56use js_sys::{ArrayBuffer, Function, Object, Promise};
57use wasm_bindgen::prelude::*;
58use web_sys::HtmlIFrameElement;
59
60#[wasm_bindgen]
61extern "C" {
62    // TODO: win.cookies.*
63    ///
64    /// Interface for managing application windows. For usage example please refer to [nw_sys::window](self)
65    ///
66    #[wasm_bindgen(js_namespace=nw, js_name = Window)]
67    #[derive(Debug, Clone)]
68    pub type Window;
69
70    #[wasm_bindgen(method, getter, js_name = window)]
71    /// Get the corresponding DOM window object of the native window.
72    ///
73    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winwindow)
74    ///
75    pub fn window(this: &Window) -> web_sys::Window;
76
77    #[wasm_bindgen(method, getter, js_name = x)]
78    /// Get left offset from window frame to screen.
79    ///
80    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winx)
81    ///
82    pub fn x(this: &Window) -> i32;
83
84    #[wasm_bindgen(method, setter, js_name = x)]
85    /// Set left offset from window frame to screen.
86    ///
87    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winx)
88    ///
89    pub fn set_x(this: &Window, x: i32);
90
91    #[wasm_bindgen(method, getter, js_name = y)]
92    /// Get top offset from window frame to screen.
93    ///
94    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winy)
95    ///
96    pub fn y(this: &Window) -> i32;
97
98    #[wasm_bindgen(method, setter, js_name = y)]
99    /// Set top offset from window frame to screen.
100    ///
101    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winy)
102    ///
103    pub fn set_y(this: &Window, y: i32);
104
105    #[wasm_bindgen(method, getter, js_name = width)]
106    /// Get window’s size, including the window’s frame.
107    ///
108    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winwidth)
109    ///
110    pub fn width(this: &Window) -> u32;
111
112    #[wasm_bindgen(method, setter, js_name = width)]
113    /// Set window’s size, including the window’s frame.
114    ///
115    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winwidth)
116    ///
117    pub fn set_width(this: &Window, width: u32);
118
119    #[wasm_bindgen(method, getter, js_name = height)]
120    /// Get window’s size, including the window’s frame.
121    ///
122    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winheight)
123    ///
124    pub fn height(this: &Window) -> u32;
125
126    #[wasm_bindgen(method, setter, js_name = height)]
127    /// Set window’s size, including the window’s frame.
128    ///
129    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winheight)
130    ///
131    pub fn set_height(this: &Window, height: u32);
132
133    #[wasm_bindgen(method, getter, js_name = title)]
134    /// Get window’s title.
135    ///
136    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wintitle)
137    ///
138    pub fn title(this: &Window) -> String;
139
140    #[wasm_bindgen(method, setter, js_name = title)]
141    /// Set window’s title.
142    ///
143    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wintitle)
144    ///
145    pub fn set_title(this: &Window, title: &str);
146
147    #[wasm_bindgen(method, getter, js_name = menu)]
148    /// Get window’s menubar.
149    ///
150    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winmenu)
151    ///
152    pub fn menu(this: &Window) -> Menu;
153
154    #[wasm_bindgen(method, setter, js_name = menu)]
155    /// Set window’s menubar.
156    ///
157    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winmenu)
158    ///
159    pub fn set_menu(this: &Window, menu: &Menu);
160
161    #[wasm_bindgen(method, setter, js_name = menu)]
162    /// Set window’s menubar = null.
163    ///
164    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winmenu)
165    ///
166    pub fn remove_menu_impl(this: &Window, menu: JsValue);
167
168    #[wasm_bindgen(method, getter, js_name = isAlwaysOnTop)]
169    /// Get whether the window is always on top of other windows.
170    ///
171    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winisalwaysontop)
172    ///
173    pub fn is_always_on_top(this: &Window) -> bool;
174
175    #[wasm_bindgen(method, getter, js_name = isFullscreen)]
176    /// Get whether we’re in fullscreen mode.
177    ///
178    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winisfullscreen)
179    ///
180    pub fn is_fullscreen(this: &Window) -> bool;
181
182    #[wasm_bindgen(method, getter, js_name = isTransparent)]
183    /// Get whether transparency is turned on
184    ///
185    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winistransparent)
186    ///
187    pub fn is_transparent(this: &Window) -> bool;
188
189    #[wasm_bindgen(method, getter, js_name = isKioskMode)]
190    /// Get whether we’re in kiosk mode.
191    ///
192    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winiskioskmode)
193    ///
194    pub fn is_kiosk_mode(this: &Window) -> bool;
195
196    #[wasm_bindgen(method, getter, js_name = zoomLevel)]
197    /// Get the page zoom. 0 for normal size; positive value for zooming in; negative value for zooming out.
198    ///
199    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winzoomlevel)
200    ///
201    pub fn zoom_level(this: &Window) -> i16;
202
203    #[wasm_bindgen(method, setter, js_name = zoomLevel)]
204    /// Set the page zoom. 0 for normal size; positive value for zooming in; negative value for zooming out.
205    ///
206    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winzoomlevel)
207    ///
208    pub fn set_zoom_level(this: &Window, zoom: i16);
209
210    //TODO: Cookies
211
212    #[wasm_bindgen(method, js_name = moveTo)]
213    /// Moves a window’s left and top edge to the specified coordinates.
214    ///
215    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winmovetox-y)
216    ///
217    pub fn move_to(this: &Window, x: u32, y: u32);
218
219    #[wasm_bindgen(method, js_name = moveBy)]
220    /// Moves a window a specified number of pixels relative to its current coordinates.
221    ///
222    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winmovebyx-y)
223    ///
224    pub fn move_by(this: &Window, x: u32, y: u32);
225
226    #[wasm_bindgen(method, js_name = resizeTo)]
227    /// Resizes a window to the specified width and height.
228    ///
229    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winresizetowidth-height)
230    ///
231    pub fn resize_to(this: &Window, width: u32, height: u32);
232
233    #[wasm_bindgen(method, js_name = setInnerWidth)]
234    /// Set the inner width of the window
235    ///
236    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetinnerwidthwidth)
237    ///
238    pub fn set_inner_width(this: &Window, width: u32);
239
240    #[wasm_bindgen(method, js_name = setInnerHeight)]
241    /// Set the inner height of the window
242    ///
243    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetinnerheightheight)
244    ///
245    pub fn set_inner_height(this: &Window, height: u32);
246
247    #[wasm_bindgen(method, js_name = resizeBy)]
248    /// Resizes a window by the specified amount.
249    ///
250    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winresizebywidth-height)
251    ///
252    pub fn resize_by(this: &Window, width: u32, height: u32);
253
254    #[wasm_bindgen(method, js_name = focus)]
255    /// Focus on the window.
256    ///
257    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winfocus)
258    ///
259    pub fn focus(this: &Window);
260
261    #[wasm_bindgen(method, js_name = blur)]
262    /// Move focus away. Usually it will move focus to other windows of your app,
263    /// since on some platforms there is no concept of blur.
264    ///
265    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winblur)
266    ///
267    pub fn blur(this: &Window);
268
269    #[wasm_bindgen(method, js_name = show)]
270    /// Show the window if it’s not shown.
271    ///
272    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winshowis_show)
273    ///
274    pub fn show(this: &Window);
275
276    #[wasm_bindgen(method, js_name = show)]
277    /// Show/Hide the window
278    ///
279    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winshowis_show)
280    ///
281    pub fn set_show(this: &Window, is_show: bool);
282
283    #[wasm_bindgen(method, js_name = hide)]
284    /// Hide the window. User will not be able to find the window once it’s hidden.
285    ///
286    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winhide)
287    ///
288    pub fn hide(this: &Window);
289
290    #[wasm_bindgen(method, js_name = close)]
291    /// Closes the current window.
292    /// You can prevent the closing by listening to the close event.
293    ///
294    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincloseforce)
295    ///
296    pub fn close(this: &Window);
297
298    #[wasm_bindgen(method, js_name = close)]
299    /// Closes the current window.
300    /// You can prevent the closing by listening to the close event.
301    /// if force is equals true, then the close event will be ignored.
302    ///
303    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincloseforce)
304    ///
305    pub fn close_impl(this: &Window, force: bool);
306
307    #[wasm_bindgen(method)]
308    /// Reloads the current window.
309    ///
310    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winreload)
311    ///
312    pub fn reload(this: &Window);
313
314    #[wasm_bindgen(method, js_name=reloadDev)]
315    /// Reloads the current page by starting a new renderer process from scratch.
316    ///
317    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winreloaddev)
318    ///
319    pub fn reload_dev(this: &Window);
320
321    #[wasm_bindgen(method, js_name=reloadIgnoringCache)]
322    /// Like reload(), but don’t use caches (aka “shift-reload”).
323    ///
324    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winreloadignoringcache)
325    ///
326    pub fn reload_ignoring_cache(this: &Window);
327
328    #[wasm_bindgen(method)]
329    /// Maximize the window on GTK and Windows, and zoom the window on Mac OS X.
330    ///
331    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winmaximize)
332    ///
333    pub fn maximize(this: &Window);
334
335    #[wasm_bindgen(method)]
336    /// Unmaximize the window, i.e. the reverse of maximize().
337    ///
338    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winunmaximize)
339    ///
340    pub fn unmaximize(this: &Window);
341
342    #[wasm_bindgen(method)]
343    /// Minimize the window to task bar on Windows, iconify the window on GTK,
344    /// and miniaturize the window on Mac OS X.
345    ///
346    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winminimize)
347    ///
348    pub fn minimize(this: &Window);
349
350    #[wasm_bindgen(method)]
351    /// Restore window to previous state after the window is minimized,
352    /// i.e. the reverse of minimize().
353    /// It’s not named unminimize since restore is used commonly.
354    ///
355    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winrestore)
356    ///
357    pub fn restore(this: &Window);
358
359    #[wasm_bindgen(method, js_name=enterFullscreen)]
360    /// Make the window fullscreen.
361    ///
362    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winenterfullscreen)
363    ///
364    pub fn enter_fullscreen(this: &Window);
365
366    #[wasm_bindgen(method, js_name=leaveFullscreen)]
367    /// Leave the fullscreen mode.
368    ///
369    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winleavefullscreen)
370    ///
371    pub fn leave_fullscreen(this: &Window);
372
373    #[wasm_bindgen(method, js_name=toggleFullscreen)]
374    /// Toggle the fullscreen mode.
375    ///
376    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wintogglefullscreen)
377    ///
378    pub fn toggle_fullscreen(this: &Window);
379
380    #[wasm_bindgen(method, js_name=enterKioskMode)]
381    /// Enter the Kiosk mode.
382    /// In Kiosk mode, the app will be fullscreen and try to prevent users from
383    /// leaving the app, so you should remember to provide a way in app to
384    /// leave Kiosk mode. This mode is mainly used for presentation on public
385    /// displays.
386    ///
387    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winenterkioskmode)
388    ///
389    pub fn enter_kiosk_mode(this: &Window);
390
391    #[wasm_bindgen(method, js_name=leaveKioskMode)]
392    /// Leave the Kiosk mode.
393    ///
394    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winleavekioskmode)
395    ///
396    pub fn leave_kiosk_mode(this: &Window);
397
398    #[wasm_bindgen(method, js_name=toggleKioskMode)]
399    /// Toggle the kiosk mode.
400    ///
401    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wintogglekioskmode)
402    ///
403    pub fn toggle_kiosk_mode(this: &Window);
404
405    #[wasm_bindgen(method, js_name=setTransparent)]
406    /// Turn on/off the transparency support.
407    ///
408    /// See more info on [Transparent Window](https://docs.nwjs.io/en/latest/For%20Users/Advanced/Transparent%20Window/).
409    ///
410    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wintogglekioskmode)
411    ///
412    pub fn set_transparent(this: &Window, transparent: bool);
413
414    #[wasm_bindgen(method, js_name=setShadow)]
415    /// (Mac) Turn the window’s native shadow on/off.
416    /// Useful for frameless, transparent windows.
417    ///
418    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetshadowshadow-mac)
419    ///
420    pub fn set_shadow(this: &Window, shadow: bool);
421
422    #[wasm_bindgen(method, js_name=showDevTools)]
423    /// Open the devtools to inspect the window.
424    ///
425    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winshowdevtoolsiframe-callback)
426    ///
427    pub fn show_dev_tools(this: &Window);
428
429    #[wasm_bindgen(method, js_name=showDevTools)]
430    /// Open the devtools to inspect the window.
431    ///
432    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winshowdevtoolsiframe-callback)
433    ///
434    pub fn show_dev_tools_with_id(this: &Window, iframe_id: &str);
435
436    #[wasm_bindgen(method, js_name=showDevTools)]
437    /// Open the devtools to inspect the window.
438    ///
439    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winshowdevtoolsiframe-callback)
440    ///
441    pub fn show_dev_tools_with_id_and_callback(this: &Window, iframe_id: &str, callback: &Function);
442
443    #[wasm_bindgen(method, js_name=showDevTools)]
444    /// Open the devtools to inspect the window.
445    ///
446    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winshowdevtoolsiframe-callback)
447    ///
448    pub fn show_dev_tools_with_iframe(this: &Window, iframe_element: &HtmlIFrameElement);
449
450    #[wasm_bindgen(method, js_name=showDevTools)]
451    /// Open the devtools to inspect the window.
452    ///
453    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winshowdevtoolsiframe-callback)
454    ///
455    pub fn show_dev_tools_with_iframe_and_callback(
456        this: &Window,
457        iframe_element: &HtmlIFrameElement,
458        callback: &Function,
459    );
460
461    #[wasm_bindgen(method, js_name=closeDevTools)]
462    /// Close the devtools window.
463    ///
464    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winclosedevtools)
465    ///
466    pub fn close_dev_tools(this: &Window);
467
468    #[wasm_bindgen(method, js_name=getPrinters)]
469    /// Enumerate the printers in the system.
470    /// The callback function will receive an array of JSON objects for
471    /// the printer information. The device name of the JSON object can
472    /// be used as parameter in `win.print()`
473    ///
474    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wingetprinterscallback)
475    ///
476    pub fn get_printers(this: &Window, callback: &Function);
477
478    #[wasm_bindgen(method, js_name=isDevToolsOpen)]
479    /// Query the status of devtools window.
480    ///
481    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winisdevtoolsopen)
482    ///
483    pub fn is_dev_tools_open(this: &Window) -> bool;
484
485    #[wasm_bindgen(method, js_name=print)]
486    /// Print the web contents in the window with or without the need for
487    /// user’s interaction.
488    ///
489    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
490    ///
491    pub fn print(this: &Window, options: &PrintOptions);
492
493    #[wasm_bindgen(method, js_name=setMaximumSize)]
494    /// Set window’s maximum size.
495    ///
496    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetmaximumsizewidth-height)
497    ///
498    pub fn set_maximum_size(this: &Window, width: u32, height: u32);
499
500    #[wasm_bindgen(method, js_name=setMinimumSize)]
501    /// Set window’s minimum size.
502    ///
503    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetminimumsizewidth-height)
504    ///
505    pub fn set_minimum_size(this: &Window, width: u32, height: u32);
506
507    #[wasm_bindgen(method, js_name=setResizable)]
508    /// Set whether window is resizable.
509    ///
510    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetresizableresizable)
511    ///
512    pub fn set_resizable(this: &Window, resizable: bool);
513
514    #[wasm_bindgen(method, js_name=setAlwaysOnTop)]
515    /// Sets the widget to be on top of all other windows in the window system.
516    ///
517    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetalwaysontoptop)
518    ///
519    pub fn set_always_on_top(this: &Window, top: bool);
520
521    #[wasm_bindgen(method, js_name=setVisibleOnAllWorkspaces)]
522    /// (Mac and Linux)
523    /// Sets the widget to be on top of all other windows in the window system.
524    ///
525    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetvisibleonallworkspacesvisible-mac-and-linux)
526    ///
527    pub fn set_visible_on_all_workspaces(this: &Window, top: bool);
528
529    #[wasm_bindgen(method, js_name=canSetVisibleOnAllWorkspaces)]
530    /// (Mac and Linux)
531    /// Returns a boolean indicating if the platform (currently Mac OS X and Linux)
532    /// support Window API method `win.set_visible_on_all_workspaces(true/false)`.
533    ///
534    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincansetvisibleonallworkspaces-mac-and-linux)
535    ///
536    pub fn can_set_visible_on_all_workspaces(this: &Window) -> bool;
537
538    #[wasm_bindgen(method, js_name=setPosition)]
539    /// Move window to specified position.
540    /// Currently only center is supported on all platforms,
541    /// which will put window in the middle of the screen.
542    ///
543    /// There are three valid positions: null or center or mouse
544    ///
545    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetpositionposition)
546    ///
547    pub fn set_position_impl(this: &Window, position: JsValue);
548
549    #[wasm_bindgen(method, js_name=setShowInTaskbar)]
550    /// Control whether to show window in taskbar or dock.
551    ///
552    /// See also `show_in_taskbar` in [Manifest-format](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#show_in_taskbar).
553    ///
554    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetshowintaskbarshow)
555    ///
556    pub fn set_show_in_taskbar(this: &Window, show: bool);
557
558    #[wasm_bindgen(method, js_name=requestAttention)]
559    /// Request the user’s attension by making the window flashes in the task bar.
560    ///
561    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winrequestattentionattension)
562    ///
563    pub fn request_attention(this: &Window, attension: bool);
564
565    #[wasm_bindgen(method, js_name=requestAttention)]
566    /// Request the user’s attension by making the window flashes in the task bar.
567    ///
568    /// On Mac, value < 0 will trigger NSInformationalRequest, while value > 0 will trigger NSCriticalRequest.
569    ///
570    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winrequestattentionattension)
571    ///
572    pub fn request_attention_with_number(this: &Window, attension: i16);
573
574    #[wasm_bindgen(method, js_name=capturePage)]
575    /// Captures the visible area of the window.
576    ///
577    /// To capture the full page,
578    /// see [win.captureScreenshot](https://docs.nwjs.io/en/latest/References/Window/#wincapturescreenshotoptions-callback).
579    ///
580    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturepagecallback-config)
581    ///
582    pub fn capture_page(this: &Window, callback: &Function);
583
584    #[wasm_bindgen(method, js_name=capturePage)]
585    /// Captures the visible area of the window.
586    ///
587    /// To capture the full page,
588    /// see [win.captureScreenshot](https://docs.nwjs.io/en/latest/References/Window/#wincapturescreenshotoptions-callback).
589    ///
590    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturepagecallback-config)
591    ///
592    pub fn capture_page_with_config(this: &Window, callback: &Function, config: &CaptureConfig);
593
594    #[wasm_bindgen(method, js_name=captureScreenshot)]
595    /// Captures the the window.
596    /// It can be used to capture the full page beyond the visible area.
597    ///
598    /// Note: This API is experimental and subject to change in the future.
599    ///
600    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturepagecallback-config)
601    ///
602    pub fn capture_screenshot(this: &Window, config: &ScreenshotConfig) -> Promise;
603
604    #[wasm_bindgen(method, js_name=captureScreenshot)]
605    /// Captures the the window.
606    /// It can be used to capture the full page beyond the visible area.
607    ///
608    /// Note: This API is experimental and subject to change in the future.
609    ///
610    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturepagecallback-config)
611    ///
612    pub fn capture_screenshot_with_callback(
613        this: &Window,
614        config: &ScreenshotConfig,
615        callback: &Function,
616    );
617
618    #[wasm_bindgen(method, js_name=setProgressBar)]
619    /// Set progress bar
620    ///
621    /// Note: Only Ubuntu is supported,
622    /// and you’ll need to specify the application `.desktop` file through
623    /// `NW_DESKTOP` env.
624    ///
625    /// If `NW_DESKTOP` env variable is not found, it uses `nw.desktop` by default.
626    ///
627    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetprogressbarprogress)
628    ///
629    pub fn set_progress_bar(this: &Window, progress: f32);
630
631    #[wasm_bindgen(method, js_name=setBadgeLabel)]
632    /// Set the badge label on the window icon in taskbar or dock.
633    ///
634    /// Note: This API is only supported on Ubuntu and the label is restricted
635    /// to a string number only. You’ll also need to specify the `.desktop`
636    /// file for your application (see the note on `set_progress_bar`)
637    ///
638    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetbadgelabellabel)
639    ///
640    pub fn set_badge_label(this: &Window, label: &str);
641
642    #[wasm_bindgen(method, js_name=eval)]
643    /// Execute a piece of JavaScript in the frame.
644    ///
645    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalframe-script)
646    ///
647    pub fn eval_impl(this: &Window, iframe: JsValue, script: &str);
648
649    #[wasm_bindgen(method, js_name=eval)]
650    /// Execute a piece of JavaScript in the frame.
651    ///
652    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalframe-script)
653    ///
654    pub fn eval_with_iframe(this: &Window, iframe: &HtmlIFrameElement, script: &str);
655
656    #[wasm_bindgen(method, js_name=evalNWBin)]
657    /// Load and execute the compiled binary in the frame.
658    ///
659    /// See [Protect JavaScript Source Code](https://docs.nwjs.io/en/latest/For%20Users/Advanced/Protect%20JavaScript%20Source%20Code/).
660    ///
661    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalnwbinframe-path)
662    ///
663    pub fn eval_nw_bin_impl(this: &Window, iframe: JsValue, script: JsValue);
664
665    #[wasm_bindgen(method, js_name=evalNWBin)]
666    /// Load and execute the compiled binary in the frame.
667    ///
668    /// See [Protect JavaScript Source Code](https://docs.nwjs.io/en/latest/For%20Users/Advanced/Protect%20JavaScript%20Source%20Code/).
669    ///
670    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalnwbinframe-path)
671    ///
672    pub fn eval_nw_bin_with_iframe_impl(this: &Window, iframe: &HtmlIFrameElement, script: JsValue);
673
674    #[wasm_bindgen(method, js_name=evalNWBinModule)]
675    /// Load and execute the compiled binary for Modules in the frame.
676    ///
677    /// The binary should be compiled with nwjc --nw-module.
678    /// The following code will load `lib.bin` as module and other modules
679    /// can refer to it with something like `import * from "./lib.js"`
680    ///
681    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalnwbinmoduleframe-path-module_path)
682    ///
683    pub fn eval_nw_bin_module_impl(
684        this: &Window,
685        iframe: JsValue,
686        script: JsValue,
687        module_path: &str,
688    );
689
690    #[wasm_bindgen(method, js_name=evalNWBinModule)]
691    /// Load and execute the compiled binary for Modules in the frame.
692    ///
693    /// The binary should be compiled with nwjc --nw-module.
694    /// The following code will load `lib.bin` as module and other modules
695    /// can refer to it with something like `import * from "./lib.js"`
696    ///
697    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalnwbinmoduleframe-path-module_path)
698    ///
699    pub fn eval_nw_bin_module_with_iframe(
700        this: &Window,
701        iframe: &HtmlIFrameElement,
702        script: JsValue,
703        module_path: &str,
704    );
705
706    #[wasm_bindgen(method, js_name=removeAllListeners)]
707    /// Removes all listeners
708    ///
709    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winremovealllistenerseventname)
710    ///
711    pub fn remove_all_listeners(this: &Window);
712
713    #[wasm_bindgen(method, js_name=removeAllListeners)]
714    /// Removes all listeners of the specified `event_name`
715    ///
716    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winremovealllistenerseventname)
717    ///
718    pub fn remove_all_listeners_with_name(this: &Window, event_name: &str);
719
720    #[wasm_bindgen(method)]
721    /// Add event listener to the specified `event_name`
722    ///
723    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#event-close)
724    ///
725    pub fn on(this: &Window, event_name: &str, callback: &Function);
726
727}
728
729#[wasm_bindgen]
730extern "C" {
731
732    #[wasm_bindgen(js_namespace=["nw", "Window"], js_name = get)]
733    /// Get current window.
734    ///
735    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowgetwindow_object)
736    ///
737    pub fn get() -> Window;
738
739    #[wasm_bindgen(js_namespace=["nw", "Window"], js_name = getAll)]
740    /// Get all windows with a callback function whose parameter is an array of nw::Window object.
741    ///
742    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowgetallcallback)
743    ///
744    pub fn get_all(callback: &Function);
745
746    #[wasm_bindgen(js_namespace=["nw", "Window"], js_name = open)]
747    /// Open new window
748    ///
749    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
750    ///
751    pub fn open(url: &str);
752
753    /// Window open options
754    ///
755    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
756    #[wasm_bindgen(extends = Object)]
757    #[derive(Debug, Clone, PartialEq, Eq)]
758    pub type Options;
759
760    #[wasm_bindgen(js_namespace=["nw", "Window"], js_name = open)]
761    /// Open window with options
762    ///
763    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
764    pub fn open_with_options(url: &str, option: &Options);
765
766    #[wasm_bindgen(js_namespace=["nw", "Window"], js_name = open)]
767    /// Open window with options and callback.
768    ///
769    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
770    pub fn open_with_options_and_callback(url: &str, option: &Options, callback: &Function);
771
772    /// Window Print options
773    ///
774    #[wasm_bindgen(extends = Object)]
775    #[derive(Debug, Clone, PartialEq, Eq)]
776    pub type PrintOptions;
777
778    /// Window Capture Config
779    ///
780    #[wasm_bindgen(extends = Object)]
781    #[derive(Debug, Clone, PartialEq, Eq)]
782    pub type CaptureConfig;
783
784    /// Screenshot Config
785    ///
786    #[wasm_bindgen(extends = Object)]
787    #[derive(Debug, Clone, PartialEq, Eq)]
788    pub type ScreenshotConfig;
789
790}
791
792/// Window position
793///
794pub enum WindowPosition {
795    Null,
796    Center,
797    Mouse,
798}
799
800/// NW Binary data
801///
802pub enum NWBinary {
803    Path(String),
804    ArrayBuffer(ArrayBuffer),
805    //Buffer(Buffer)
806}
807
808impl Window {
809    /// Set window’s menubar = null.
810    ///
811    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winmenu)
812    ///
813    pub fn remove_menu(&self) {
814        self.remove_menu_impl(JsValue::null());
815    }
816
817    /// Closes the current window without triggering `close` event.
818    ///
819    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincloseforce)
820    ///
821    pub fn close_with_force(&self) {
822        self.close_impl(true);
823    }
824
825    /// Move window to specified position.
826    /// Currently only center is supported on all platforms,
827    /// which will put window in the middle of the screen.
828    ///
829    /// There are three valid positions: null or center or mouse
830    ///
831    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetpositionposition)
832    ///
833    pub fn set_position(&self, position: WindowPosition) {
834        let position = match position {
835            WindowPosition::Null => JsValue::null(),
836            WindowPosition::Center => JsValue::from("center"),
837            WindowPosition::Mouse => JsValue::from("mouse"),
838        };
839
840        self.set_position_impl(position);
841    }
842
843    /// Execute a piece of JavaScript in the frame.
844    ///
845    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalframe-script)
846    ///
847    pub fn eval(&self, iframe: Option<HtmlIFrameElement>, script: &str) {
848        if let Some(iframe) = iframe {
849            self.eval_with_iframe(&iframe, script);
850        } else {
851            self.eval_impl(JsValue::null(), script);
852        }
853    }
854
855    /// Load and execute the compiled binary in the frame.
856    ///
857    /// See [Protect JavaScript Source Code](https://docs.nwjs.io/en/latest/For%20Users/Advanced/Protect%20JavaScript%20Source%20Code/).
858    ///
859    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalnwbinframe-path)
860    ///
861    pub fn eval_nw_bin(&self, iframe: Option<HtmlIFrameElement>, script: NWBinary) {
862        let script = match script {
863            NWBinary::Path(path) => JsValue::from(path),
864            NWBinary::ArrayBuffer(buffer) => JsValue::from(buffer),
865        };
866        if let Some(iframe) = iframe {
867            self.eval_nw_bin_with_iframe_impl(&iframe, script);
868        } else {
869            self.eval_nw_bin_impl(JsValue::null(), script);
870        }
871    }
872
873    /// Load and execute the compiled binary for Modules in the frame.
874    ///
875    /// The binary should be compiled with nwjc --nw-module.
876    /// The following code will load `lib.bin` as module and other modules
877    /// can refer to it with something like `import * from "./lib.js"`
878    ///
879    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winevalnwbinmoduleframe-path-module_path)
880    ///
881    pub fn eval_nw_bin_module(
882        &self,
883        iframe: Option<HtmlIFrameElement>,
884        script: NWBinary,
885        module_path: &str,
886    ) {
887        let script = match script {
888            NWBinary::Path(path) => JsValue::from(path),
889            NWBinary::ArrayBuffer(buffer) => JsValue::from(buffer),
890        };
891
892        if let Some(iframe) = iframe {
893            self.eval_nw_bin_module_with_iframe(&iframe, script, module_path);
894        } else {
895            self.eval_nw_bin_module_impl(JsValue::null(), script, module_path);
896        }
897    }
898}
899
900cfg_if! {
901    if #[cfg(feature = "markers")] {
902        unsafe impl Send for Window {}
903        unsafe impl Sync for Window {}
904
905        unsafe impl Send for Options {}
906        unsafe impl Sync for Options {}
907    }
908}
909
910impl OptionsTrait for Options {}
911
912impl Options {
913    /// the id used to identify the window.
914    /// This will be used to remember the size and position of the window
915    /// and restore that geometry when a window with the same id is later opened.
916    /// [See also the Chrome App documentation](https://developer.chrome.com/docs/extensions/reference/app_window/#property-CreateWindowOptions-id)
917    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
918    pub fn id(self, id: &str) -> Self {
919        self.set("id", JsValue::from(id))
920    }
921
922    /// The default title of window created by NW.js, .
923    /// it's very useful if you want to show your own title when the app is starting
924    ///
925    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#webkit-subfields)
926    pub fn title(self, title: &str) -> Self {
927        self.set("title", JsValue::from(title))
928    }
929
930    /// the initial inner width of the window.
931    ///
932    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#webkit-subfields)
933    pub fn width(self, width: u32) -> Self {
934        self.set("width", JsValue::from(width))
935    }
936
937    /// the initial inner height of the window.
938    ///
939    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#webkit-subfields)
940    pub fn height(self, height: u32) -> Self {
941        self.set("height", JsValue::from(height))
942    }
943
944    /// path to window’s icon.
945    ///
946    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#icon)
947    pub fn icon(self, icon: &String) -> Self {
948        self.set("icon", JsValue::from(icon))
949    }
950
951    /// Move window to specified position.
952    /// Currently only center is supported on all platforms,
953    /// which will put window in the middle of the screen.
954    ///
955    /// There are three valid positions: null or center or mouse
956    ///
957    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winsetpositionposition)
958    ///
959    pub fn position(self, position: WindowPosition) -> Self {
960        let position = match position {
961            WindowPosition::Null => JsValue::null(),
962            WindowPosition::Center => JsValue::from("center"),
963            WindowPosition::Mouse => JsValue::from("mouse"),
964        };
965
966        self.set("position", position)
967    }
968
969    /// minimum inner width of window
970    ///
971    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#min_width)
972    pub fn min_width(self, min_width: u32) -> Self {
973        self.set("min_width", JsValue::from(min_width))
974    }
975
976    /// minimum inner height of window
977    ///
978    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#min_height)
979    pub fn min_height(self, min_height: u32) -> Self {
980        self.set("min_height", JsValue::from(min_height))
981    }
982
983    /// maximum inner width of window
984    ///
985    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#max_width)
986    pub fn max_width(self, max_width: u32) -> Self {
987        self.set("max_width", JsValue::from(max_width))
988    }
989
990    /// maximum inner height of window
991    ///
992    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#max_height)
993    pub fn max_height(self, max_height: u32) -> Self {
994        self.set("max_height", JsValue::from(max_height))
995    }
996
997    /// (Linux) show as desktop background window under X11 environment
998    ///
999    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#as_desktop-linux)
1000    pub fn as_desktop(self, as_desktop: bool) -> Self {
1001        self.set("as_desktop", JsValue::from(as_desktop))
1002    }
1003
1004    /// whether window is resizable
1005    ///
1006    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#resizable)
1007    pub fn resizable(self, resizable: bool) -> Self {
1008        self.set("resizable", JsValue::from(resizable))
1009    }
1010
1011    /// whether the window should always stay on top of other windows.
1012    ///
1013    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#always_on_top)
1014    pub fn always_on_top(self, always_on_top: bool) -> Self {
1015        self.set("always_on_top", JsValue::from(always_on_top))
1016    }
1017
1018    /// whether the window should be visible on all workspaces
1019    /// simultaneously (on platforms that support multiple workspaces,
1020    /// currently Mac OS X and Linux).
1021    ///
1022    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#visible_on_all_workspaces-mac-linux)
1023    pub fn visible_on_all_workspaces(self, visible_on_all_workspaces: bool) -> Self {
1024        self.set(
1025            "visible_on_all_workspaces",
1026            JsValue::from(visible_on_all_workspaces),
1027        )
1028    }
1029
1030    /// whether window is fullscreen
1031    ///
1032    /// Beware, if frame is also set to false in fullscreen it will prevent
1033    /// the mouse from being captured on the very edges of the screen.
1034    /// You should avoid activate it if fullscreen is also set to true.
1035    ///
1036    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#fullscreen)
1037    pub fn fullscreen(self, fullscreen: bool) -> Self {
1038        self.set("fullscreen", JsValue::from(fullscreen))
1039    }
1040
1041    /// whether the window is shown in taskbar or dock. The default is `true`.
1042    ///
1043    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#show_in_taskbar)
1044    pub fn show_in_taskbar(self, show_in_taskbar: bool) -> Self {
1045        self.set("show_in_taskbar", JsValue::from(show_in_taskbar))
1046    }
1047
1048    /// specify it to `false` to make the window frameless
1049    ///
1050    /// Beware, if frame is set to false in fullscreen it will prevent the
1051    /// mouse from being captured on the very edges of the screen.
1052    /// You should avoid activating it if fullscreen is also set to true.
1053    ///
1054    /// Frameless apps do not have a title bar for the user to click and
1055    /// drag the window. You can use CSS to designate DOM elements as
1056    /// draggable regions.
1057    ///
1058    /// ```css
1059    /// .drag-enable {
1060    ///   -webkit-app-region: drag;
1061    /// }
1062    /// .drag-disable {
1063    ///   -webkit-app-region: no-drag;
1064    /// }
1065    /// ```
1066
1067    ///
1068    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#frame)
1069    pub fn frame(self, frame: bool) -> Self {
1070        self.set("frame", JsValue::from(frame))
1071    }
1072
1073    /// specify it to `false` if you want your app to be hidden on startup
1074    ///
1075    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#show)
1076    pub fn show(self, show: bool) -> Self {
1077        self.set("show", JsValue::from(show))
1078    }
1079
1080    /// whether to use `Kiosk` mode. In `Kiosk` mode, the app will be fullscreen
1081    /// and try to prevent users from leaving the app, so you should
1082    /// remember to provide a way in app to leave Kiosk mode.
1083    /// This mode is mainly used for presentation on public displays
1084    ///
1085    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#kiosk)
1086    pub fn kiosk(self, kiosk: bool) -> Self {
1087        self.set("kiosk", JsValue::from(kiosk))
1088    }
1089
1090    /// whether to turn on transparent window mode.
1091    /// The default is `false`.
1092    /// Control the transparency with rgba background value in CSS.
1093    /// Use command line option `--disable-transparency` to disable this
1094    /// feature completely.
1095    ///
1096    /// There is experimental support for "click-through" on the
1097    /// transparent region: add `--disable-gpu` option to the command line.
1098    ///
1099    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#transparent)
1100    pub fn transparent(self, transparent: bool) -> Self {
1101        self.set("transparent", JsValue::from(transparent))
1102    }
1103
1104    /// the initial left of the window.
1105    ///
1106    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#webkit-subfields)
1107    pub fn left(self, left: u32) -> Self {
1108        self.set("x", JsValue::from(left))
1109    }
1110
1111    /// the initial top of the window.
1112    ///
1113    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Manifest%20Format/#webkit-subfields)
1114    pub fn top(self, top: u32) -> Self {
1115        self.set("y", JsValue::from(top))
1116    }
1117
1118    /// whether to open a new window in a separate render process.
1119    ///
1120    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
1121    pub fn new_instance(self, value: bool) -> Self {
1122        self.set("new_instance", JsValue::from(value))
1123    }
1124
1125    /// If true, the Node context and DOM context are merged in the new window’s process.
1126    /// Use only when new_instance is true.
1127    ///
1128    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
1129    pub fn mixed_context(self, value: bool) -> Self {
1130        self.set("mixed_context", JsValue::from(value))
1131    }
1132
1133    /// the script to be injected before any DOM is constructed and any script is run.
1134    ///
1135    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
1136    pub fn inject_js_start(self, js: &str) -> Self {
1137        self.set("inject_js_start", JsValue::from(js))
1138    }
1139
1140    /// the script to be injected after the document object is loaded, before onload event is fired.
1141    ///
1142    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#windowopenurl-options-callback)
1143    pub fn inject_js_end(self, js: &str) -> Self {
1144        self.set("inject_js_end", JsValue::from(js))
1145    }
1146}
1147
1148impl std::fmt::Display for Options {
1149    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1150        write!(f, "{:?}", self)?;
1151        Ok(())
1152    }
1153}
1154
1155impl OptionsTrait for CaptureConfig {
1156    fn initialize(self) -> Self {
1157        self.datatype("datauri")
1158    }
1159}
1160
1161impl CaptureConfig {
1162    /// The image format used to generate the image.
1163    /// It supports two formats: "png" and "jpeg".
1164    ///
1165    /// If ignored, it’s "jpeg" by default.
1166    ///
1167    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturepagecallback-config)
1168    pub fn format(self, format: &str) -> Self {
1169        self.set("format", JsValue::from(format))
1170    }
1171
1172    /// It supports three types: "raw", "buffer" and "datauri".
1173    ///
1174    /// If ignored, it’s "datauri" by default.
1175    ///
1176    /// The `raw` only contains the Base64 encoded image.
1177    /// But `datauri` contains the mime type headers as well,
1178    /// and it can be directly assigned to src of Image to load the image.
1179    ///
1180    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturepagecallback-config)
1181    ///
1182    pub fn datatype(self, datatype: &str) -> Self {
1183        self.set("datatype", JsValue::from(datatype))
1184    }
1185}
1186
1187impl std::fmt::Display for CaptureConfig {
1188    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1189        write!(f, "{:?}", self)?;
1190        Ok(())
1191    }
1192}
1193
1194impl OptionsTrait for ScreenshotConfig {}
1195
1196impl ScreenshotConfig {
1197    /// Capture the whole page beyond the visible area.
1198    /// Currently the height of captured image is capped at 16384 pixels by Chromium.
1199    ///
1200    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturescreenshotoptions-callback)
1201    ///
1202    pub fn fullsize(self, fullsize: bool) -> Self {
1203        self.set("fullsize", JsValue::from(fullsize))
1204    }
1205
1206    /// The image format used to generate the image.
1207    ///
1208    /// It supports two formats: "png" and "jpeg".
1209    ///
1210    /// "png" is the default.
1211    ///
1212    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturescreenshotoptions-callback)
1213    ///
1214    pub fn format(self, format: &str) -> Self {
1215        self.set("format", JsValue::from(format))
1216    }
1217
1218    /// Compression quality from range [0..100] (jpeg only).
1219    ///
1220    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturescreenshotoptions-callback)
1221    ///
1222    pub fn quality(self, quality: u8) -> Self {
1223        self.set("quality", JsValue::from(quality))
1224    }
1225
1226    /// Capture the screenshot of a given region only.
1227    ///
1228    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#wincapturescreenshotoptions-callback)
1229    ///
1230    pub fn clip(self, x: i32, y: i32, width: u32, height: u32, scale: u32) -> Self {
1231        let clip_region = Object::new();
1232        let items = vec![
1233            ("x", JsValue::from(x)),
1234            ("y", JsValue::from(y)),
1235            ("width", JsValue::from(width)),
1236            ("height", JsValue::from(height)),
1237            ("scale", JsValue::from(scale)),
1238        ];
1239
1240        for (key, value) in items {
1241            let _ = js_sys::Reflect::set(&clip_region, &JsValue::from(key), &value);
1242        }
1243
1244        self.set("clip", JsValue::from(clip_region))
1245    }
1246}
1247
1248impl std::fmt::Display for ScreenshotConfig {
1249    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1250        write!(f, "{:?}", self)?;
1251        Ok(())
1252    }
1253}
1254
1255/// Window print margin
1256pub enum PrintMargin {
1257    Default,
1258    NoMargins,
1259    Minimum,
1260
1261    ///Custom margin: left, top, right, bottom
1262    Custom(Option<u16>, Option<u16>, Option<u16>, Option<u16>),
1263}
1264
1265impl OptionsTrait for PrintOptions {}
1266
1267impl PrintOptions {
1268    /// Whether to print without the need for user’s interaction; optional,
1269    /// true by default
1270    ///
1271    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1272    ///
1273    pub fn autoprint(self, autoprint: bool) -> Self {
1274        self.set("autoprint", JsValue::from(autoprint))
1275    }
1276
1277    /// Hide the flashing print preview dialog; optional, false by default
1278    ///
1279    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1280    ///
1281    pub fn silent(self, silent: bool) -> Self {
1282        self.set("silent", JsValue::from(silent))
1283    }
1284
1285    /// The device name of the printer returned by `nw::Window::get_printers();`
1286    ///
1287    /// No need to set this when printing to PDF
1288    ///
1289    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1290    ///
1291    pub fn printer(self, printer: &str) -> Self {
1292        self.set("printer", JsValue::from(printer))
1293    }
1294
1295    /// The path of the output PDF when printing to PDF
1296    ///
1297    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1298    ///
1299    pub fn pdf_path(self, pdf_path: &str) -> Self {
1300        self.set("pdf_path", JsValue::from(pdf_path))
1301    }
1302
1303    /// Whether to enable header and footer
1304    ///
1305    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1306    ///
1307    pub fn header_footer_enabled(self, header_footer_enabled: bool) -> Self {
1308        self.set("headerFooterEnabled", JsValue::from(header_footer_enabled))
1309    }
1310
1311    /// Whether to use landscape or portrait
1312    ///
1313    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1314    ///
1315    pub fn landscape(self, landscape: bool) -> Self {
1316        self.set("landscape", JsValue::from(landscape))
1317    }
1318
1319    /// The paper size spec
1320    ///
1321    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1322    ///
1323    pub fn media_size(self, media_size: Object) -> Self {
1324        self.set("mediaSize", JsValue::from(media_size))
1325    }
1326
1327    /// Whether to print CSS backgrounds
1328    ///
1329    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1330    ///
1331    pub fn should_print_backgrounds(self, should_print_backgrounds: bool) -> Self {
1332        self.set(
1333            "shouldPrintBackgrounds",
1334            JsValue::from(should_print_backgrounds),
1335        )
1336    }
1337
1338    /// MarginsType
1339    ///
1340    /// see margins_custom.
1341    ///
1342    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1343    ///
1344    pub fn margin(mut self, margin: PrintMargin) -> Self {
1345        let margin_type = match margin {
1346            PrintMargin::Default => 0,
1347            PrintMargin::NoMargins => 1,
1348            PrintMargin::Minimum => 2,
1349            PrintMargin::Custom(l, t, r, b) => {
1350                let margins_custom = Object::new();
1351                let items = vec![
1352                    ("marginLeft", l),
1353                    ("marginTop", t),
1354                    ("marginRight", r),
1355                    ("marginBottom", b),
1356                ];
1357
1358                for (key, value) in items {
1359                    let v = value.unwrap_or(0);
1360                    let _ = js_sys::Reflect::set(
1361                        &margins_custom,
1362                        &JsValue::from(key),
1363                        &JsValue::from(v),
1364                    );
1365                }
1366
1367                self = self.set("marginsCustom", JsValue::from(margins_custom));
1368
1369                3
1370            }
1371        };
1372        self.set("marginsType", JsValue::from(margin_type))
1373    }
1374
1375    /// The number of copies to print.
1376    ///
1377    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1378    ///
1379    pub fn copies(self, copies: u8) -> Self {
1380        self.set("copies", JsValue::from(copies))
1381    }
1382
1383    /// The scale factor; 100 is the default.
1384    ///
1385    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1386    ///
1387    pub fn scale_factor(self, scale_factor: u8) -> Self {
1388        self.set("scaleFactor", JsValue::from(scale_factor))
1389    }
1390
1391    /// String to replace the URL in the header.
1392    ///
1393    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1394    ///
1395    pub fn header_string(self, header_string: &str) -> Self {
1396        self.set("headerString", JsValue::from(header_string))
1397    }
1398
1399    /// String to replace the URL in the footer.
1400    ///
1401    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Window/#winprintoptions)
1402    ///
1403    pub fn footer_string(self, footer_string: &str) -> Self {
1404        self.set("footerString", JsValue::from(footer_string))
1405    }
1406}
1407
1408impl std::fmt::Display for PrintOptions {
1409    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1410        write!(f, "{:?}", self)?;
1411        Ok(())
1412    }
1413}