nw_sys/shortcut.rs
1//!
2//! Register keyboard shurtcuts that will be received by your application.
3//!
4//! # Synopsis
5//! ```
6//! use workflow_wasm::prelude::*;
7//!
8//! //Create a shortcut with |option|.
9//! let shortcut = nw_sys::Shortcut::new(&nw_sys::shortcut::Options::new().key("Ctrl+Shift+A"));
10//!
11//! // If register |shortcut| successfully and user struck "Ctrl+Shift+A", |shortcut|
12//! // will get an "active" event.
13//!
14//! // You can also add callback to shortcut's active and failed event.
15//! let callback = callback!(||{
16//! log_info!("Global desktop keyboard shortcut: 'Ctrl+Shift+A' active.");
17//! });
18//! shortcut.on_active(callback.as_ref());
19//!
20//! // Register global desktop shortcut, which can work without focus.
21//! nw_sys::app::register_global_hot_key(&shortcut);
22//!
23//! // Unregister the global desktop shortcut.
24//! nw_sys::app::unregister_global_hot_key(&shortcut);
25//!
26//! //save callback
27//! app.push_callback(callback)?;
28//!
29//! ```
30//!
31
32use crate::options::OptionsTrait;
33use js_sys::{Function, Object};
34use wasm_bindgen::prelude::*;
35
36#[wasm_bindgen]
37extern "C" {
38 ///
39 /// Interface for registering keyboard shortcuts. For usage example please refer to [nw_sys::shortcut](self)
40 ///
41 /// Shortcut represents a global keyboard shortcut,
42 /// also known as system-wide hotkey. If registered successfully,
43 /// it works even if your app does not have focus.
44 ///
45 /// Shortcut inherited from EventEmitter.
46 /// Every time the user presses the registered shortcut,
47 /// your app will receive an active event of the shortcut object.
48 ///
49 #[wasm_bindgen(js_namespace=nw, js_name = Tray)]
50 #[derive(Debug, Clone)]
51 pub type Shortcut;
52
53 #[wasm_bindgen(constructor, js_namespace=["nw"])]
54 /// # Synopsis
55 ///
56 /// ```
57 /// //Create a Shortcut
58 /// let shortcut = nw_sys::shortcut::new(&nw_sys::shortcut::Options::new().key("Ctrl+Shift+A"));
59 /// ```
60 ///
61 /// Create new Shortcut
62 ///
63 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Shortcut/#new-shortcutoption)
64 ///
65 pub fn new(options: &Options) -> Shortcut;
66
67 #[wasm_bindgen(setter, method, js_namespace=["nw"], js_name=active)]
68 /// Set the active callback of a Shortcut.
69 /// It will be called when user presses the shortcut.
70 ///
71 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Shortcut/#shortcutactive)
72 ///
73 pub fn on_active(this: &Shortcut, callback: &Function);
74
75 #[wasm_bindgen(setter, method, js_namespace=["nw"], js_name=failed)]
76 /// Set the `failed` callback of a Shortcut.
77 /// It will be called when application passes an invalid key ,
78 /// or failed to register the key.
79 ///
80 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Shortcut/#shortcutactive)
81 ///
82 pub fn on_failed(this: &Shortcut, callback: &Function);
83
84 // shortcut.failed
85 // *Get or set the failed callback of a Shortcut. It will be called when application passes an invalid key , or failed to register the key.
86
87 /// Shortcut Options
88 #[wasm_bindgen(extends = Object)]
89 #[derive(Debug, Clone, PartialEq, Eq)]
90 pub type Options;
91}
92
93impl OptionsTrait for Options {}
94
95impl Options {
96 /// Set the `key` of a `Shortcut`.
97 /// It is a string to specify the shortcut key, like "Ctrl+Alt+A".
98 /// The key is consisted of zero or more modifiers and a key on your keyboard.
99 /// Only one key code is supported. Key code is case insensitive.
100 ///
101 /// ### List of supported modifiers:
102 ///
103 /// - Ctrl
104 /// - Alt
105 /// - Shift
106 /// - Command: Command modifier maps to Apple key (⌘) on Mac,
107 /// and maps to the Windows key on Windows and Linux.
108 ///
109 /// ### List of supported keys:
110 ///
111 /// - Alphabet: `A`-`Z`
112 /// - Digits: `0`-`9`
113 /// - Function Keys: `F1`-`F24`
114 /// - Home / End / PageUp / PageDown / Insert / Delete
115 /// - Up / Down / Left / Right
116 /// - MediaNextTrack / MediaPlayPause / MediaPrevTrack / MediaStop
117 /// - Comma or `,`
118 /// - Period or `.`
119 /// - Tab or `\t`
120 /// - Backquote or `` ` ``
121 /// - Enter or `\n`
122 /// - Minus or `-`
123 /// - Equal or `=`
124 /// - Backslash or `\`
125 /// - Semicolon or `;`
126 /// - Quote or `'`
127 /// - BracketLeft or `[`
128 /// - BracketRight or `]`
129 /// - Escape
130 ///
131 ///
132 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Shortcut/#shortcutkey)
133 pub fn key(self, key: &str) -> Self {
134 self.set("key", JsValue::from(key))
135 }
136
137 /// Set the active callback of a Shortcut.
138 /// It will be called when user presses the shortcut.
139 ///
140 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Shortcut/#shortcutactive)
141 pub fn active(self, callback: &Function) -> Self {
142 self.set("active", JsValue::from(callback))
143 }
144
145 /// Set the failed callback of a Shortcut.
146 /// It will be called when application passes an invalid key,
147 /// or failed to register the key.
148 ///
149 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Shortcut/#shortcutfailed)
150 pub fn failed(self, callback: &Function) -> Self {
151 self.set("failed", JsValue::from(callback))
152 }
153}