nw_sys/
menu_item.rs

1//!
2//! Menu item control used in conjunction with the [`Menu`](crate::menu::Menu) interface.
3//!
4//! For usage example please refer to [nw_sys::menu](crate::menu)
5//!
6
7use crate::menu::Menu;
8use crate::options::OptionsTrait;
9use js_sys::{Function, Object};
10use wasm_bindgen::prelude::*;
11
12#[wasm_bindgen]
13extern "C" {
14
15    ///
16    /// Interface for controlling menu items. For usage example please refer to [nw_sys::menu](crate::menu)
17    ///
18
19    #[wasm_bindgen(js_namespace=nw, js_name = MenuItem)]
20    #[derive(Debug, Clone)]
21    pub type MenuItem;
22
23    #[wasm_bindgen(constructor, js_namespace=["nw"])]
24    /// Create MenuItem
25    ///
26    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#menuitem)
27    ///
28    pub fn new(options: &Options) -> MenuItem;
29
30    #[wasm_bindgen(method, getter, js_name = label)]
31    /// Get the label of a MenuItem
32    ///
33    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemlabel)
34    pub fn label(this: &MenuItem) -> String;
35
36    #[wasm_bindgen(method, setter, js_name = label)]
37    /// Set the label of a MenuItem
38    ///
39    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemlabel)
40    pub fn set_label(this: &MenuItem) -> String;
41
42    #[wasm_bindgen(method, getter, js_name = type)]
43    /// Get the type of a MenuItem
44    ///
45    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemtype)
46    pub fn get_type(this: &MenuItem) -> String;
47
48    #[wasm_bindgen(method, getter, js_name = icon)]
49    /// Get the icon of a MenuItem
50    ///
51    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemicon)
52    pub fn icon(this: &MenuItem) -> String;
53
54    #[wasm_bindgen(method, setter, js_name = icon)]
55    /// Set the icon of a MenuItem, it must a path to your icon file.
56    /// It can be a relative path which points to an icon in your app,
57    /// or an absolute path pointing to a file in user’s system.
58    ///
59    /// It has no effect on setting icon of a separator item.
60    ///
61    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemicon)
62    pub fn set_icon(this: &MenuItem, icon: &str);
63
64    #[wasm_bindgen(method, getter, js_name = iconIsTemplate)]
65    /// (Mac) Get whether icon image is treated as "template".
66    ///
67    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemiconistemplate-mac)
68    pub fn icon_is_template(this: &MenuItem) -> bool;
69
70    #[wasm_bindgen(method, setter, js_name = iconIsTemplate)]
71    /// (Mac) Set whether icon image is treated as "template" (true by default).
72    /// When the property is set to true the image is treated as "template"
73    /// and the system automatically ensures proper styling according to the
74    /// various states of the status item (e.g. dark menu, light menu, etc.).
75    /// Template images should consist only of black and clear colours
76    /// and can use the alpha channel in the image to adjust the opacity of
77    /// black content. It has no effects on Linux and Windows.
78    ///
79    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemiconistemplate-mac)
80    pub fn set_icon_is_template(this: &MenuItem, value: bool);
81
82    #[wasm_bindgen(method, getter, js_name = tooltip)]
83    /// (Mac) Get the tooltip of a MenuItem
84    ///
85    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemtooltip-mac)
86    pub fn tooltip(this: &MenuItem) -> String;
87
88    #[wasm_bindgen(method, setter, js_name = tooltip)]
89    /// (Mac) Set the tooltip of a MenuItem, it can only be plain text.
90    /// A tooltip is short string that describes the menu item,
91    /// it will show when you hover your mouse on the item.
92    ///
93    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemtooltip-mac)
94    pub fn set_tooltip(this: &MenuItem, tooltip: &str);
95
96    #[wasm_bindgen(method, getter, js_name = checked)]
97    /// Get whether the MenuItem is checked.
98    ///
99    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemchecked)
100    pub fn checked(this: &MenuItem) -> bool;
101
102    #[wasm_bindgen(method, setter, js_name = checked)]
103    /// Set whether the MenuItem is checked.
104    /// Usually if a MenuItem is checked.
105    /// There will be a mark on the left side of it.
106    /// It’s used mostly to indicate whether an option is on.
107    ///
108    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemchecked)
109    pub fn set_checked(this: &MenuItem, checked: bool);
110
111    #[wasm_bindgen(method, getter, js_name = enabled)]
112    /// Get whether the MenuItem is enabled.
113    ///
114    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemenabled)
115    pub fn enabled(this: &MenuItem) -> bool;
116
117    #[wasm_bindgen(method, setter, js_name = enabled)]
118    /// Set whether the MenuItem is enabled.
119    /// An disabled MenuItem will be greyed out and you will not be able to
120    /// click on it.
121    ///
122    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemenabled)
123    pub fn set_enabled(this: &MenuItem, enabled: bool);
124
125    #[wasm_bindgen(method, getter, js_name = submenu)]
126    /// Get the submenu of a MenuItem.
127    ///
128    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemsubmenu)
129    pub fn submenu(this: &MenuItem) -> Option<Menu>;
130
131    #[wasm_bindgen(method, setter, js_name = submenu)]
132    /// Set the submenu of a MenuItem, the submenu must be a Menu object.
133    /// You should set submenu in the option when creating the MenuItem.
134    /// Changing it at runtime is slow on some platforms.
135    ///
136    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemsubmenu)
137    pub fn set_submenu(this: &MenuItem, submenu: &Menu);
138
139    /*
140    #[wasm_bindgen(method, getter, js_name = enabled)]
141    /// Get the submenu of a MenuItem.
142    ///
143    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemclick)
144    pub fn click(this:&MenuItem)->Option<Function>;
145    */
146
147    #[wasm_bindgen(method, setter, js_name = click)]
148    /// Set the submenu of a MenuItem, the submenu must be a Menu object.
149    /// You should set submenu in the option when creating the MenuItem.
150    /// Changing it at runtime is slow on some platforms.
151    ///
152    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemclick)
153    pub fn set_click(this: &MenuItem, callback: &Function);
154
155    #[wasm_bindgen(method, getter, js_name = key)]
156    /// A single character string to specify the shortcut key for the menu item.
157    ///
158    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemkey)
159    pub fn key(this: &MenuItem) -> String;
160
161    #[wasm_bindgen(method, setter, js_name = key)]
162    /// Set shortcut key
163    ///
164    /// Valid Keys for All Platforms
165    /// Alphabet: a-z
166    /// Digits: 0-9
167    /// Other keys on main area: [ , ] , ' , , , . , / , ` , - , = , \ , ' , ; , Tab
168    /// Esc
169    /// Down , Up , Left , Right
170    /// W3C DOM Level 3 KeyboardEvent Key Values: KeyA (same as A),
171    /// Escape (same as Esc), F1, ArrowDown (same as Down) etc.
172    /// Special Keys for Mac Only
173    ///
174    /// On Mac, you can also use special keys as shortcut key with
175    /// String.fromCharCode(specialKey).
176    ///
177    /// Here are some useful keys:
178    ///
179    /// 28: Left (←)
180    /// 29: Right (→)
181    /// 30: Up (↑)
182    /// 31: Down (↓)
183    /// 27: Escape (⎋)
184    /// 11: PageUp (⇞)
185    /// 12: PageDown (⇟)
186    /// For full list of special keys supported on Mac,
187    /// see [NSMenuItem.keyEquivalent](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenuItem_Class/#//apple_ref/occ/instp/NSMenuItem/keyEquivalent)
188    /// and [NSEvent: Function-Key Unicodes](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/index.html#//apple_ref/doc/constant_group/Function_Key_Unicodes).
189    ///
190    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemkey)
191    pub fn set_key(this: &MenuItem, key: &str);
192
193    #[wasm_bindgen(method, getter, js_name = modifiers)]
194    /// Get modifier keys
195    /// A string to specify the modifier keys for the shortcut of the menu item.
196    /// It should be the concatenation of the following strings:
197    /// cmd / command / super, shift, ctrl, alt. e.g. "cmd+shift+alt".
198    /// cmd represents different keys on
199    /// all platforms: Windows key (Windows) on Windows and Linux,
200    /// Apple key (⌘) on Mac. super and command are aliases of cmd.
201    ///
202    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemmodifiers)
203    pub fn modifiers(this: &MenuItem) -> String;
204
205    #[wasm_bindgen(method, setter, js_name = modifiers)]
206    /// Set modifier keys
207    ///
208    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#itemmodifiers)
209    pub fn set_modifiers(this: &MenuItem, key: &str);
210
211    #[wasm_bindgen(extends = Object)]
212    #[derive(Debug, Clone, PartialEq, Eq)]
213    pub type Options;
214
215}
216
217impl OptionsTrait for Options {}
218
219impl Options {
220    /// Type of MenuItem
221    ///
222    /// Three types are accepted: normal, checkbox, separator
223    ///
224    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
225    pub fn set_type(self, t: Type) -> Self {
226        self.set("type", t.into())
227    }
228
229    /// Label for normal item or checkbox
230    ///
231    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
232    pub fn label(self, label: &str) -> Self {
233        self.set("label", JsValue::from(label))
234    }
235
236    /// Icon for normal item or checkbox
237    ///
238    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
239    pub fn icon(self, icon: &str) -> Self {
240        self.set("icon", JsValue::from(icon))
241    }
242
243    /// Tooltip for normal item or checkbox
244    ///
245    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
246    pub fn tooltip(self, tooltip: &str) -> Self {
247        self.set("tooltip", JsValue::from(tooltip))
248    }
249
250    /// The callback function when item is triggered by mouse click or keyboard shortcut
251    ///
252    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
253    pub fn click(self, callback: &Function) -> Self {
254        self.set("click", JsValue::from(callback))
255    }
256
257    /// Whether the item is enabled or disabled. It’s set to true by default.
258    ///
259    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
260    pub fn enabled(self, enabled: bool) -> Self {
261        self.set("enabled", JsValue::from(enabled))
262    }
263
264    /// Whether the checkbox is checked or not. It’s set to false by default.
265    ///
266    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
267    pub fn checked(self, checked: bool) -> Self {
268        self.set("checked", JsValue::from(checked))
269    }
270
271    /// A submenu
272    ///
273    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
274    pub fn submenu(self, submenu: &Menu) -> Self {
275        self.set("submenu", JsValue::from(submenu))
276    }
277
278    /// The key of the shortcut
279    ///
280    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
281    pub fn key(self, key: &str) -> Self {
282        self.set("key", JsValue::from(key))
283    }
284
285    /// The modifiers of the shortcut
286    ///
287    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
288    pub fn modifiers(self, modifiers: &str) -> Self {
289        self.set("modifiers", JsValue::from(modifiers))
290    }
291}
292
293/// Type of menu item
294///
295/// Three types are accepted: Normal, Checkbox, Separator
296///
297/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/MenuItem/#new-menuitemoption)
298///
299pub enum Type {
300    Separator,
301    Normal,
302    Checkbox,
303}
304
305impl From<Type> for Options {
306    fn from(t: Type) -> Self {
307        match t {
308            Type::Separator => Options::new().set_type(Type::Separator),
309            Type::Normal => Options::new().set_type(Type::Normal),
310            Type::Checkbox => Options::new().set_type(Type::Checkbox),
311        }
312    }
313}
314
315impl std::fmt::Display for Type {
316    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
317        match self {
318            Type::Separator => write!(f, "Separator"),
319            Type::Normal => write!(f, "Normal"),
320            Type::Checkbox => write!(f, "Checkbox"),
321        }
322    }
323}
324
325impl From<Type> for JsValue {
326    fn from(t: Type) -> Self {
327        JsValue::from(t.to_string().to_lowercase())
328    }
329}