From 1524ced816668f3b5cafc4ca2e03a6b3d3a2c1bc Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 5 Oct 2013 14:31:30 +0800 Subject: [PATCH] Set application menu would set menu for all windows on Windows. --- browser/api/lib/menu-item.coffee | 4 +++- browser/api/lib/menu.coffee | 10 ++++++++-- spec/main.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/browser/api/lib/menu-item.coffee b/browser/api/lib/menu-item.coffee index 02f8638..ec38963 100644 --- a/browser/api/lib/menu-item.coffee +++ b/browser/api/lib/menu-item.coffee @@ -1,3 +1,5 @@ +BrowserWindow = require 'browser-window' + nextCommandId = 0 class MenuItem @@ -26,7 +28,7 @@ class MenuItem @commandId = ++nextCommandId @click = => if typeof click is 'function' - click.apply this, arguments + click this, BrowserWindow.getFocusedWindow() else if typeof @selector is 'string' Menu.sendActionToFirstResponder @selector diff --git a/browser/api/lib/menu.coffee b/browser/api/lib/menu.coffee index 0a1bb07..5d00504 100644 --- a/browser/api/lib/menu.coffee +++ b/browser/api/lib/menu.coffee @@ -3,6 +3,7 @@ EventEmitter = require('events').EventEmitter IDWeakMap = require 'id-weak-map' MenuItem = require 'menu-item' +app = require 'app' bindings = process.atomBinding 'menu' Menu = bindings.Menu @@ -39,7 +40,7 @@ Menu::insert = (pos, item) -> getAcceleratorForCommandId: (commandId) => @commandsMap[commandId]?.accelerator executeCommand: (commandId) => activeItem = @commandsMap[commandId] - activeItem.click(activeItem) if activeItem? + activeItem.click() if activeItem? @items.splice pos, 0, item @commandsMap[item.commandId] = item @@ -47,7 +48,12 @@ applicationMenu = null Menu.setApplicationMenu = (menu) -> throw new TypeError('Invalid menu') unless menu?.constructor is Menu applicationMenu = menu # Keep a reference. - bindings.setApplicationMenu menu + + if process.platform is 'darwin' + bindings.setApplicationMenu menu + else + windows = app.getBrowserWindows() + w.setMenu menu for w in windows Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder diff --git a/spec/main.js b/spec/main.js index cf2453c..0c2f64d 100644 --- a/spec/main.js +++ b/spec/main.js @@ -1,6 +1,7 @@ var app = require('app'); var ipc = require('ipc'); var BrowserWindow = require('browser-window'); +var Menu = require('menu'); var window = null; @@ -39,6 +40,42 @@ app.on('window-all-closed', function() { }); app.on('finish-launching', function() { + var template = [ + { + label: 'File', + submenu: [ + { + label: 'Open', + accelerator: 'Command+O', + }, + { + label: 'Close', + accelerator: 'Command+W', + click: function(item, window) { window.close(); } + }, + ] + }, + { + label: 'View', + submenu: [ + { + label: 'Reload', + accelerator: 'Command+R', + click: function(item, window) { window.restart(); } + }, + { + label: 'Enter Fullscreen', + click: function(item, window) { window.setFullScreen(true); } + }, + { + label: 'Toggle DevTools', + accelerator: 'Alt+Command+I', + click: function(item, window) { window.toggleDevTools(); } + }, + ] + }, + ]; + // Test if using protocol module would crash. require('protocol').registerProtocol('test-if-crashes', function() {}); @@ -49,4 +86,7 @@ app.on('finish-launching', function() { height: 600 }); window.loadUrl('file://' + __dirname + '/index.html'); + + var menu = Menu.buildFromTemplate(template); + app.setApplicationMenu(menu); }); -- 2.7.4