2013-09-09 07:35:57 +00:00
# menu
2013-08-14 22:43:35 +00:00
2013-08-29 14:37:51 +00:00
The `Menu` class is used to create native menus that can be used as
2014-10-25 23:03:27 +00:00
application menus and context menus. Each menu consists of multiple menu
2013-08-29 14:37:51 +00:00
items, and each menu item can have a submenu.
2013-08-14 22:43:35 +00:00
2014-10-25 23:03:27 +00:00
Below is an example of creating a menu dynamically in a web page by using
the [remote ](remote.md ) module, and showing it when the user right clicks
2013-08-29 14:37:51 +00:00
the page:
2013-08-14 22:43:35 +00:00
2014-06-10 05:29:09 +00:00
```html
<!-- index.html -->
< script >
var remote = require('remote');
var Menu = remote.require('menu');
var MenuItem = remote.require('menu-item');
2013-08-14 22:43:35 +00:00
var menu = new Menu();
2013-12-13 06:31:19 +00:00
menu.append(new MenuItem({ label: 'MenuItem1', click: function() { console.log('item 1 clicked'); } }));
2013-08-14 22:43:35 +00:00
menu.append(new MenuItem({ type: 'separator' }));
2014-05-24 02:41:54 +00:00
menu.append(new MenuItem({ label: 'MenuItem2', type: 'checkbox', checked: true }));
2013-08-14 22:43:35 +00:00
window.addEventListener('contextmenu', function (e) {
e.preventDefault();
2014-06-10 05:29:09 +00:00
menu.popup(remote.getCurrentWindow());
2013-08-14 22:43:35 +00:00
}, false);
2014-06-10 05:29:09 +00:00
< / script >
2013-08-14 22:43:35 +00:00
```
Another example of creating the application menu with the simple template API:
2015-06-09 21:09:23 +00:00
```html
2015-06-09 21:08:34 +00:00
<!-- index.html -->
2015-06-09 21:09:23 +00:00
< script >
2015-06-09 21:08:34 +00:00
var remote = require('remote');
var Menu = remote.require('menu');
2013-08-14 22:43:35 +00:00
var template = [
{
2015-04-08 17:16:10 +00:00
label: 'Electron',
2013-08-14 22:43:35 +00:00
submenu: [
{
2015-04-08 17:16:10 +00:00
label: 'About Electron',
2013-08-14 22:43:35 +00:00
selector: 'orderFrontStandardAboutPanel:'
},
{
type: 'separator'
},
2014-09-05 05:39:29 +00:00
{
label: 'Services',
submenu: []
},
{
type: 'separator'
},
2013-08-14 22:43:35 +00:00
{
2015-04-08 17:16:10 +00:00
label: 'Hide Electron',
2013-08-14 22:43:35 +00:00
accelerator: 'Command+H',
selector: 'hide:'
},
{
label: 'Hide Others',
accelerator: 'Command+Shift+H',
selector: 'hideOtherApplications:'
},
{
label: 'Show All',
selector: 'unhideAllApplications:'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
2015-06-09 21:08:34 +00:00
selector: 'terminate:'
2013-08-14 22:43:35 +00:00
},
]
},
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'Command+Z',
selector: 'undo:'
},
{
label: 'Redo',
accelerator: 'Shift+Command+Z',
selector: 'redo:'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'Command+X',
selector: 'cut:'
},
{
label: 'Copy',
accelerator: 'Command+C',
selector: 'copy:'
},
{
label: 'Paste',
accelerator: 'Command+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'Command+A',
selector: 'selectAll:'
2015-06-09 16:08:02 +00:00
}
2013-08-14 22:43:35 +00:00
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'Command+R',
2015-06-10 04:39:59 +00:00
click: function() { remote.getCurrentWindow().reload(); }
2013-08-14 22:43:35 +00:00
},
{
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
2015-06-09 21:08:34 +00:00
click: function() { remote.getCurrentWindow().toggleDevTools(); }
2013-08-14 22:43:35 +00:00
},
]
},
{
label: 'Window',
submenu: [
{
label: 'Minimize',
accelerator: 'Command+M',
selector: 'performMiniaturize:'
},
{
label: 'Close',
accelerator: 'Command+W',
selector: 'performClose:'
},
{
type: 'separator'
},
{
label: 'Bring All to Front',
selector: 'arrangeInFront:'
2015-06-09 16:08:02 +00:00
}
2013-08-14 22:43:35 +00:00
]
},
2014-09-05 05:39:29 +00:00
{
label: 'Help',
submenu: []
2015-06-09 16:08:02 +00:00
}
2013-08-14 22:43:35 +00:00
];
menu = Menu.buildFromTemplate(template);
2014-12-17 02:50:40 +00:00
2015-06-09 21:08:34 +00:00
Menu.setApplicationMenu(menu);
2015-06-09 21:09:23 +00:00
< / script >
2013-08-14 22:43:35 +00:00
```
## Class: Menu
### new Menu()
Creates a new menu.
### Class Method: Menu.setApplicationMenu(menu)
* `menu` Menu
2014-05-30 01:02:21 +00:00
Sets `menu` as the application menu on OS X. On Windows and Linux, the `menu`
will be set as each window's top menu.
2013-08-14 22:43:35 +00:00
### Class Method: Menu.sendActionToFirstResponder(action)
* `action` String
2013-08-29 14:37:51 +00:00
Sends the `action` to the first responder of application, this is used for
emulating default Cocoa menu behaviors, usually you would just use the
`selector` property of `MenuItem` .
2013-08-14 22:43:35 +00:00
**Note:** This method is OS X only.
### Class Method: Menu.buildFromTemplate(template)
* `template` Array
2013-08-29 14:37:51 +00:00
Generally, the `template` is just an array of `options` for constructing
2014-09-05 05:39:29 +00:00
[MenuItem ](menu-item.md ), the usage can be referenced above.
2013-08-14 22:43:35 +00:00
2013-08-29 14:37:51 +00:00
You can also attach other fields to element of the `template` , and they will
become properties of the constructed menu items.
2013-08-14 22:43:35 +00:00
2014-11-26 14:15:55 +00:00
### Menu.popup(browserWindow, [x, y])
2013-08-14 22:43:35 +00:00
* `browserWindow` BrowserWindow
2014-11-26 14:15:55 +00:00
* `x` Number
* `y` Number
2013-08-14 22:43:35 +00:00
2014-11-26 14:15:55 +00:00
Popups this menu as a context menu in the `browserWindow` . You can optionally
provide a `(x,y)` coordinate to place the menu at, otherwise it will be placed
at the current mouse cursor position.
2013-08-14 22:43:35 +00:00
### Menu.append(menuItem)
* `menuItem` MenuItem
Appends the `menuItem` to the menu.
### Menu.insert(pos, menuItem)
* `pos` Integer
* `menuItem` MenuItem
Inserts the `menuItem` to the `pos` position of the menu.
### Menu.items
2013-08-29 14:37:51 +00:00
Get the array containing the menu's items.
2014-09-05 05:39:29 +00:00
## Notes on OS X application menu
OS X has a completely different style of application menu from Windows and
Linux, and here are some notes on making your app's menu more native-like.
### Standard menus
On OS X there are many system defined standard menus, like the `Services` and
`Windows` menus. To make your menu a standard menu, you can just set your menu's
2015-04-16 03:31:12 +00:00
label to one of followings, and Electron will recognize them and make them
2014-09-05 05:39:29 +00:00
become standard menus:
* `Window`
* `Help`
* `Services`
### Standard menu item actions
OS X has provided standard actions for some menu items (which are called
`selector` s), like `About xxx` , `Hide xxx` , and `Hide Others` . To set the action
of a menu item to a standard action, you can set the `selector` attribute of the
menu item.
### Main menu's name
On OS X the label of application menu's first item is always your app's name,
no matter what label you set. To change it you have to change your app's name
by modifying your app bundle's `Info.plist` file. See
[About Information Property List Files ](https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html )
for more.
2015-04-13 03:22:33 +00:00
## Menu item position
You can make use of `position` and `id` to control how the item would be placed
when building a menu with `Menu.buildFromTemplate` .
The `position` attribute of `MenuItem` has the form `[placement]=[id]` where
placement is one of `before` , `after` , or `endof` and `id` is the unique ID of
an existing item in the menu:
* `before` - Inserts this item before the id referenced item. If the
referenced item doesn't exist the item will be inserted at the end of
the menu.
* `after` - Inserts this item after id referenced item. If the referenced
item doesn't exist the item will be inserted at the end of the menu.
* `endof` - Inserts this item at the end of the logical group containing
the id referenced item. (Groups are created by separator items). If
the referenced item doesn't exist a new separator group is created with
the given id and this item is inserted after that separator.
When an item is positioned following unpositioned items are inserted after
it, until a new item is positioned. So if you want to position a group of
menu items in the same location you only need to specify a position for
the first item.
### Examples
Template:
```javascript
[
2015-06-09 16:08:27 +00:00
{label: '4', id: '4'},
{label: '5', id: '5'},
{label: '1', id: '1', position: 'before=4'},
{label: '2', id: '2'},
2015-04-13 03:22:33 +00:00
{label: '3', id: '3'}
]
```
Menu:
```
- 1
- 2
- 3
- 4
- 5
```
Template:
```javascript
[
2015-06-09 16:08:27 +00:00
{label: 'a', position: 'endof=letters'},
{label: '1', position: 'endof=numbers'},
{label: 'b', position: 'endof=letters'},
{label: '2', position: 'endof=numbers'},
{label: 'c', position: 'endof=letters'},
2015-04-13 03:22:33 +00:00
{label: '3', position: 'endof=numbers'}
]
```
Menu:
```
- ---
- a
- b
- c
- ---
- 1
- 2
- 3
```