Add Menu.buildFromTemplate API to greatly simplify building menu.

This commit is contained in:
Cheng Zhao 2013-05-16 22:43:58 +08:00
parent e58f115b43
commit eec8abf397
2 changed files with 84 additions and 46 deletions

View file

@ -44,4 +44,16 @@ Menu.setApplicationMenu = (menu) ->
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
Menu.buildFromTemplate = (template) ->
throw new TypeError('Invalid template for Menu') unless Array.isArray template
menu = new Menu
for item in template
throw new TypeError('Invalid template for MenuItem') unless typeof item is 'object'
item.submenu = Menu.buildFromTemplate item.submenu if item.submenu?
menu.append new MenuItem(item)
menu
module.exports = Menu

View file

@ -28,53 +28,79 @@ delegate.browserMainParts.preMainMessageLoopRun = function() {
mainWindow = null;
});
menu = new Menu;
var appleMenu = new Menu;
appleMenu.append(new MenuItem({
label: 'About Atom Shell',
selector: 'orderFrontStandardAboutPanel:'
}));
appleMenu.append(new MenuItem({ type: 'separator' }));
appleMenu.append(new MenuItem({
label: 'Hide Atom Shell',
accelerator: 'Command+H',
selector: 'hide:'
}));
appleMenu.append(new MenuItem({
label: 'Hide Others',
accelerator: 'Command+Shift+H',
selector: 'hideOtherApplications:'
}));
appleMenu.append(new MenuItem({ type: 'separator' }));
appleMenu.append(new MenuItem({
label: 'Quit',
accelerator: 'Command+Q',
click: function() {
app.quit();
}
}));
var windowMenu = new Menu;
windowMenu.append(new MenuItem({
label: 'Minimize',
accelerator: 'Command+M',
selector: 'performMiniaturize:'
}));
windowMenu.append(new MenuItem({
label: 'Close',
accelerator: 'Command+W',
selector: 'performClose:'
}));
windowMenu.append(new MenuItem({ type: 'separator' }));
windowMenu.append(new MenuItem({
label: 'Bring All to Front',
selector: 'arrangeInFront:'
}));
menu.append(new MenuItem({ submenu: appleMenu }));
menu.append(new MenuItem({ label: 'Window', submenu: windowMenu }));
var template = [
{
label: 'Atom Shell',
submenu: [
{
label: 'About Atom Shell',
selector: 'orderFrontStandardAboutPanel:'
},
{
type: 'separator'
},
{
label: 'Hide Atom Shell',
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',
click: function() { app.quit(); }
},
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'Command+R',
},
{
label: 'Show DevTools',
accelerator: 'Alt+Command+I',
},
]
},
{
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:'
},
]
},
];
menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
ipc.on('message', function(processId, routingId, type) {