From 86bd43bf73eb045b0c7e67a78f74438b6e02c3bc Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Oct 2014 15:21:06 -0700 Subject: [PATCH] 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})