]> khamidou — GitHub mirrors - electron.git/commitdiff
Add Menu.buildFromTemplate API to greatly simplify building menu.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 16 May 2013 14:43:58 +0000 (22:43 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 16 May 2013 14:43:58 +0000 (22:43 +0800)
browser/api/lib/menu.coffee
browser/default_app/main.js

index 013354bbbff1151680fd9f0d23f399783814480b..35dffa025e7b08804c1158306d77a2f94980236d 100644 (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
index 8e3a76be53491fa4f3e3301aed1d023bee2ec672..37030eab46c915f1d79fbcf83e694c68832a9f5f 100644 (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) {