nw_sys/menu.rs
1//!
2//! Functionality for building application, popup and tray menus.
3//!
4//! # Synopsis
5//! ```rust
6//! // An example to create a context menu:
7//! // Create an empty context menu
8//! let menu = nw_sys::Menu::new();
9//!
10//! // Add some items
11//! menu.append(&nw_sys::MenuItem::new(&nw_sys::menu_item::Options::new().label("Item A")));
12//! menu.append(&nw_sys::MenuItem::new(&nw_sys::menu_item::Options::new().label("Item B")));
13//! menu.append(&nw_sys::MenuItem::new(&nw_sys::menu_item::Type::Separator.into()));
14//! menu.append(&nw_sys::MenuItem::new(&nw_sys::menu_item::Options::new().label("Item C")));
15//!
16//! // Remove one item
17//! menu.remove_at(1);
18//!
19//! // Popup as context menu
20//! menu.popup(10, 10);
21//!
22//! // Iterate menu's items
23//! for item in &menu.items(){
24//! log_trace!("{:?}", item);
25//! }
26//!
27//! // To create a menubar, usually you have to create a 2-level menu and assign it
28//! // to win.menu. Here is the example of creating a menubar:
29//! //
30//! // Create an empty menubar
31//! let menu = nw_sys::Menu::new_with_options(&nw_sys::menu::Type::Menubar.into());
32//!
33//! // Create a submenu as the 2nd level menu
34//! let submenu = nw_sys::Menu::new();
35//! submenu.append(&nw_sys::MenuItem::new(&nw_sys::menu_item::Options::new().label("Item A")));
36//! submenu.append(&nw_sys::MenuItem::new(&nw_sys::menu_item::Options::new().label("Item B")));
37//!
38//! // Create and append the 1st level menu to the menubar
39//! let options = nw_sys::menu_item::Options::new()
40//! .label("First Menu")
41//! .submenu(&submenu);
42//!
43//! //create builtin Edit and Window menus on Mac
44//! menu.create_mac_builtin("Example App");
45//!
46//! menu.append(&nw_sys::MenuItem::new(&options));
47//!
48//! // Assign it to `window.menu` to get the menu displayed
49//! nw_sys::window::get().set_menu(&menu);
50//!
51//! ```
52
53use crate::menu_item::MenuItem;
54use crate::options::OptionsTrait;
55use js_sys::Object;
56use wasm_bindgen::prelude::*;
57
58#[wasm_bindgen]
59extern "C" {
60
61 ///
62 /// Menu interface. For usage example please refer to [nw_sys::menu](self)
63 ///
64
65 #[wasm_bindgen(js_namespace=nw, js_name = Menu)]
66 #[derive(Debug, Clone)]
67 pub type Menu;
68
69 #[wasm_bindgen(constructor, js_namespace=["nw"])]
70 /// Create Menu
71 ///
72 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/)
73 ///
74 pub fn new() -> Menu;
75
76 #[wasm_bindgen(constructor, js_namespace=["nw"])]
77 /// Create Menu
78 ///
79 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#new-menuoption)
80 ///
81 pub fn new_with_options(options: &Options) -> Menu;
82
83 #[wasm_bindgen(method, getter, js_name = items)]
84 /// Get an array that contains all items of a menu.
85 /// Each item is an instance of MenuItem.
86 /// See MenuItem for detailed information.
87 ///
88 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menuitems)
89 pub fn items(this: &Menu) -> Vec<MenuItem>;
90
91 #[wasm_bindgen(method)]
92 /// Append item to the tail of the menu.
93 ///
94 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menuappenditem)
95 ///
96 pub fn append(this: &Menu, item: &MenuItem);
97
98 #[wasm_bindgen(method, js_name = insert)]
99 /// Insert the item at `index`th position of the menu. The index is started from 0.
100 ///
101 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menuinsertitem-i)
102 pub fn insert(this: &Menu, item: &MenuItem, index: u16);
103
104 #[wasm_bindgen(method, js_name = remove)]
105 /// Remove item from the menu. This method requires you to keep the MenuItem outside the Menu.
106 ///
107 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menuremoveitem)
108 pub fn remove(this: &Menu);
109
110 #[wasm_bindgen(method, js_name = removeAt)]
111 /// Remove the item form the menu by index.
112 ///
113 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menuremoveati)
114 pub fn remove_at(this: &Menu, index: u16);
115
116 #[wasm_bindgen(method, js_name = popup)]
117 /// Popup the context menu at the anchor in (x, y) in current window.
118 /// This method is only valid for contextmenu type.
119 ///
120 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menupopupx-y)
121 pub fn popup(this: &Menu, x: i32, y: i32);
122
123 #[wasm_bindgen(method, js_name=createMacBuiltin)]
124 /// (Mac) Creates the builtin menus (App, Edit and Window) within the menubar on Mac.
125 /// The items can be manipulated with the items property.
126 /// The argument appname is used for the title of App menu.
127 /// You can still use builtin menus with other menu items.
128 /// i.e. append or insert items to the menu is still valid.
129 ///
130 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menucreatemacbuiltinappname-options-mac)
131 ///
132 pub fn create_mac_builtin(this: &Menu, app_name: &str);
133
134 #[wasm_bindgen(method, js_name=createMacBuiltin)]
135 /// (Mac) Creates the builtin menus (App, Edit and Window) within the menubar on Mac.
136 /// The items can be manipulated with the items property.
137 /// The argument appname is used for the title of App menu.
138 /// You can still use builtin menus with other menu items.
139 /// i.e. append or insert items to the menu is still valid.
140 ///
141 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menucreatemacbuiltinappname-options-mac)
142 ///
143 pub fn create_mac_builtin_with_options(this: &Menu, app_name: &str, options: &MacOptions);
144
145 #[wasm_bindgen(extends = Object)]
146 #[derive(Debug, Clone, PartialEq, Eq)]
147 pub type Options;
148
149 #[wasm_bindgen(extends = Object)]
150 #[derive(Debug, Clone, PartialEq, Eq)]
151 pub type MacOptions;
152
153}
154
155impl OptionsTrait for Options {}
156impl OptionsTrait for MacOptions {}
157
158impl MacOptions {
159 /// do not populate the Edit menu
160 ///
161 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menucreatemacbuiltinappname-options-mac)
162 pub fn hide_edit(self, hide: bool) -> Self {
163 self.set("hideEdit", JsValue::from(hide))
164 }
165 /// do not populate the Window menu
166 ///
167 /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/Menu/#menucreatemacbuiltinappname-options-mac)
168 pub fn hide_window(self, hide: bool) -> Self {
169 self.set("hideWindow", JsValue::from(hide))
170 }
171}
172
173/// Menu Type
174pub enum Type {
175 Menubar,
176}
177
178impl From<Type> for Options {
179 fn from(t: Type) -> Self {
180 let options = Self::new();
181 match t {
182 Type::Menubar => options.set("type", JsValue::from("menubar")),
183 }
184 }
185}