-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Menu
Menu API requires node-webkit >= 0.3.0
Menu
represents a native menu, it can be used as window menu or context menu.
// Load native UI library
var gui = require('nw.gui');
// Create an empty menu
var menu = new gui.Menu();
// Add some items
menu.append(new gui.MenuItem({ label: 'Item A' }));
menu.append(new gui.MenuItem({ label: 'Item B' }));
menu.append(new gui.MenuItem({ type: 'separator' }));
menu.append(new gui.MenuItem({ label: 'Item C' }));
// Remove one item
menu.removeAt(1);
// Popup as context menu
menu.popup(10, 10);
// Iterate menu's items
for (var i = 0; i < menu.items.length; ++i) {
console.log(menu.items[i]);
}
Create a new Menu
.
If no option
is specified, a normal context menu will be created. To create a menu to be used as window's menu, you need to explicitly specify 'type': 'menubar'
in the option
, e.g. new Menu({ 'type': 'menubar' })
.
Get an array that contains all items of a menu.
Get how many items this Menu
has.
Get the i
th menu item of the Menu
. It will return a MenuItem
object.
Append item
of MenuItem
type to the tail of the Menu
.
Insert item
of MenuItem
type to the i
th position of the Menu
, Menu
is 0-indexed.
Remove item
from Menu
. This method requires you to keep the MenuItem
outside the Menu
.
Remove the i
th item form Menu
Reference: x is the index of the menu object.
menu.items[x].click = function() {
console.log('clicked');
};
Popup the Menu
at position (x
, y
) in current window. Usually you would listen to contextmenu
event of DOM elements and manually popup the menu:
document.body.addEventListener('contextmenu', function(ev) {
ev.preventDefault();
menu.popup(ev.x, ev.y);
return false;
});
In this way, you can precisely choose which menu to show for different elements, and you can update menu elements just before popuping it.
since v0.10.0-rc1
OSX only. Creates the default menus (App, Edit and Window). The items can be manipulated with the items
property. The argument appname
is used for the title of App menu.
Example:
var nw = require('nw.gui');
var win = nw.Window.get();
var nativeMenuBar = new nw.Menu({ type: "menubar" });
nativeMenuBar.createMacBuiltin("My App");
win.menu = nativeMenuBar;
Since v0.10.1, if desired, you can also hide the "Edit" and "Window" menus when invoking createMacBuiltin
like so:
nativeMenuBar.createMacBuiltin("My App", {
hideEdit: true,
hideWindow: true
});