From 11d7437ecc080fcbb6e7ec0739b3539258ddce23 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Oct 2014 14:39:43 -0700 Subject: [PATCH 1/5] Add setActiveTemplate helper --- src/browser/application-menu.coffee | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/browser/application-menu.coffee b/src/browser/application-menu.coffee index a43410db2..13a5453db 100644 --- a/src/browser/application-menu.coffee +++ b/src/browser/application-menu.coffee @@ -10,8 +10,7 @@ _ = require 'underscore-plus' module.exports = class ApplicationMenu constructor: (@version) -> - @menu = Menu.buildFromTemplate @getDefaultTemplate() - Menu.setApplicationMenu @menu + @setActiveTemplate(@getDefaultTemplate()) global.atomApplication.autoUpdateManager.on 'state-changed', (state) => @showUpdateMenuItem(state) @@ -23,11 +22,14 @@ class ApplicationMenu update: (template, keystrokesByCommand) -> @translateTemplate(template, keystrokesByCommand) @substituteVersion(template) - @menu = Menu.buildFromTemplate(template) - Menu.setApplicationMenu(@menu) + @setActiveTemplate(template) @showUpdateMenuItem(global.atomApplication.autoUpdateManager.getState()) + setActiveTemplate: (template) -> + @menu = Menu.buildFromTemplate(template) + Menu.setApplicationMenu(@menu) + # Flattens the given menu and submenu items into an single Array. # # menu - A complete menu configuration object for atom-shell's menu API. From 3075b74b6478ed560912db11ee15bc21e5e300f9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Oct 2014 15:20:30 -0700 Subject: [PATCH 2/5] Close the template Menu.buildFromTemplate modifies it so copy before hand so it can be reused --- src/browser/application-menu.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/browser/application-menu.coffee b/src/browser/application-menu.coffee index 13a5453db..e965ae0ea 100644 --- a/src/browser/application-menu.coffee +++ b/src/browser/application-menu.coffee @@ -27,7 +27,7 @@ class ApplicationMenu @showUpdateMenuItem(global.atomApplication.autoUpdateManager.getState()) setActiveTemplate: (template) -> - @menu = Menu.buildFromTemplate(template) + @menu = Menu.buildFromTemplate(_.deepClone(template)) Menu.setApplicationMenu(@menu) # Flattens the given menu and submenu items into an single Array. From 86bd43bf73eb045b0c7e67a78f74438b6e02c3bc Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Oct 2014 15:21:06 -0700 Subject: [PATCH 3/5] Store each window's menu template Restore it when the window gains focus --- src/browser/application-menu.coffee | 23 ++++++++++++++++++++++- src/browser/atom-application.coffee | 5 +++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/browser/application-menu.coffee b/src/browser/application-menu.coffee index e965ae0ea..0f411cf5e 100644 --- a/src/browser/application-menu.coffee +++ b/src/browser/application-menu.coffee @@ -10,18 +10,23 @@ _ = require 'underscore-plus' module.exports = class ApplicationMenu constructor: (@version) -> + @windowTemplates = new WeakMap() @setActiveTemplate(@getDefaultTemplate()) global.atomApplication.autoUpdateManager.on 'state-changed', (state) => @showUpdateMenuItem(state) # Public: Updates the entire menu with the given keybindings. # + # window - The BrowserWindow this menu is associated with. # template - The Object which describes the menu to display. # keystrokesByCommand - An Object where the keys are commands and the values # are Arrays containing the keystroke. - update: (template, keystrokesByCommand) -> + update: (window, template, keystrokesByCommand) -> + return unless window is @lastFocusedWindow + @translateTemplate(template, keystrokesByCommand) @substituteVersion(template) + @windowTemplates.set(window, template) @setActiveTemplate(template) @showUpdateMenuItem(global.atomApplication.autoUpdateManager.getState()) @@ -30,6 +35,22 @@ class ApplicationMenu @menu = Menu.buildFromTemplate(_.deepClone(template)) Menu.setApplicationMenu(@menu) + # Register a BrowserWindow with this application menu. + addWindow: (window) -> + @lastFocusedWindow ?= window + + focusHandler = => + @lastFocusedWindow = window + if template = @windowTemplates.get(window) + @setActiveTemplate(template) + + window.on 'focus', focusHandler + window.once 'closed', => + @windowTemplates.delete(window) + window.removeListener 'focus', focusHandler + + @enableWindowSpecificItems(true) + # Flattens the given menu and submenu items into an single Array. # # menu - A complete menu configuration object for atom-shell's menu API. diff --git a/src/browser/atom-application.coffee b/src/browser/atom-application.coffee index 74b06e9ef..4eb6dc401 100644 --- a/src/browser/atom-application.coffee +++ b/src/browser/atom-application.coffee @@ -97,7 +97,7 @@ class AtomApplication # Public: Adds the {AtomWindow} to the global window list. addWindow: (window) -> @windows.push window - @applicationMenu?.enableWindowSpecificItems(true) + @applicationMenu?.addWindow(window.browserWindow) window.once 'window:loaded', => @autoUpdateManager.emitUpdateAvailableEvent(window) @@ -215,7 +215,8 @@ class AtomApplication @promptForPath({window}) ipc.on 'update-application-menu', (event, template, keystrokesByCommand) => - @applicationMenu.update(template, keystrokesByCommand) + win = BrowserWindow.fromWebContents(event.sender) + @applicationMenu.update(win, template, keystrokesByCommand) ipc.on 'run-package-specs', (event, specDirectory) => @runSpecs({resourcePath: global.devResourcePath, specDirectory: specDirectory, exitWhenDone: false}) From d5508b338bb05db5dd3b1de9ea8b5cc049d3e305 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Oct 2014 15:26:45 -0700 Subject: [PATCH 4/5] Compare templates before swapping them out --- src/browser/application-menu.coffee | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/browser/application-menu.coffee b/src/browser/application-menu.coffee index 0f411cf5e..5270b59d6 100644 --- a/src/browser/application-menu.coffee +++ b/src/browser/application-menu.coffee @@ -32,8 +32,10 @@ class ApplicationMenu @showUpdateMenuItem(global.atomApplication.autoUpdateManager.getState()) setActiveTemplate: (template) -> - @menu = Menu.buildFromTemplate(_.deepClone(template)) - Menu.setApplicationMenu(@menu) + unless _.isEqual(template, @activeTemplate) + @activeTemplate = template + @menu = Menu.buildFromTemplate(_.deepClone(template)) + Menu.setApplicationMenu(@menu) # Register a BrowserWindow with this application menu. addWindow: (window) -> From 2a09d25eb189fe2f4f2fb6975ce1111b887c98a5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Oct 2014 15:31:44 -0700 Subject: [PATCH 5/5] :memo: Add template --- src/browser/application-menu.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/browser/application-menu.coffee b/src/browser/application-menu.coffee index 5270b59d6..3432d86b2 100644 --- a/src/browser/application-menu.coffee +++ b/src/browser/application-menu.coffee @@ -17,7 +17,7 @@ class ApplicationMenu # Public: Updates the entire menu with the given keybindings. # - # window - The BrowserWindow this menu is associated with. + # window - The BrowserWindow this menu template is associated with. # template - The Object which describes the menu to display. # keystrokesByCommand - An Object where the keys are commands and the values # are Arrays containing the keystroke.