diff --git a/spec/menu-manager-spec.coffee b/spec/menu-manager-spec.coffee index d92c921c4..f86b6e1a0 100644 --- a/spec/menu-manager-spec.coffee +++ b/spec/menu-manager-spec.coffee @@ -1,3 +1,4 @@ +path = require 'path' MenuManager = require '../src/menu-manager' describe "MenuManager", -> @@ -46,3 +47,32 @@ describe "MenuManager", -> menu.add [{label: "A", submenu: [{label: "B", command: "b"}]}] menu.add [{label: "A", submenu: [{label: "B", command: "b"}]}] expect(menu.template[originalItemCount]).toEqual {label: "A", submenu: [{label: "B", command: "b"}]} + + describe "::update()", -> + it "sends the current menu template and associated key bindings to the browser process", -> + spyOn(menu, 'sendToBrowserProcess') + menu.add [{label: "A", submenu: [{label: "B", command: "b"}]}] + atom.keymap.add 'test', 'atom-workspace': 'ctrl-b': 'b' + menu.update() + + waits 1 + + runs -> expect(menu.sendToBrowserProcess.argsForCall[0][1]['b']).toEqual ['ctrl-b'] + + it "omits key bindings that are mapped to unset! in any context", -> + # it would be nice to be smarter about omitting, but that would require a much + # more dynamic interaction between the currently focused element and the menu + spyOn(menu, 'sendToBrowserProcess') + menu.add [{label: "A", submenu: [{label: "B", command: "b"}]}] + atom.keymap.add 'test', 'atom-workspace': 'ctrl-b': 'b' + atom.keymap.add 'test', 'atom-text-editor': 'ctrl-b': 'unset!' + + waits 1 + + runs -> expect(menu.sendToBrowserProcess.argsForCall[0][1]['b']).toBeUndefined() + + it "updates the application menu when a keymap is reloaded", -> + spyOn(menu, 'update') + keymapPath = path.join(__dirname, 'fixtures', 'packages', 'package-with-keymaps', 'keymaps', 'keymap-1.cson') + atom.keymaps.reloadKeymap(keymapPath) + expect(menu.update).toHaveBeenCalled() diff --git a/src/menu-manager.coffee b/src/menu-manager.coffee index b5c9a80e3..b138f2da2 100644 --- a/src/menu-manager.coffee +++ b/src/menu-manager.coffee @@ -63,6 +63,7 @@ class MenuManager @pendingUpdateOperation = null @template = [] atom.keymaps.onDidLoadBundledKeymaps => @loadPlatformItems() + atom.keymaps.onDidReloadKeymap => @update() atom.packages.onDidActivateInitialPackages => @sortPackagesMenu() # Public: Adds the given items to the application menu. @@ -139,10 +140,19 @@ class MenuManager update: -> clearImmediate(@pendingUpdateOperation) if @pendingUpdateOperation? @pendingUpdateOperation = setImmediate => - keystrokesByCommand = {} + includedBindings = [] + unsetKeystrokes = new Set + for binding in atom.keymaps.getKeyBindings() when @includeSelector(binding.selector) + includedBindings.push(binding) + if binding.command is 'unset!' + unsetKeystrokes.add(binding.keystrokes) + + keystrokesByCommand = {} + for binding in includedBindings when not unsetKeystrokes.has(binding.keystrokes) keystrokesByCommand[binding.command] ?= [] keystrokesByCommand[binding.command].unshift binding.keystrokes + @sendToBrowserProcess(@template, keystrokesByCommand) loadPlatformItems: ->