diff --git a/spec/atom/window-spec.coffee b/spec/atom/window-spec.coffee index 90f745c0f..a1eb4541d 100644 --- a/spec/atom/window-spec.coffee +++ b/spec/atom/window-spec.coffee @@ -1,4 +1,5 @@ require 'window' +$ = require 'jquery' describe "Window", -> describe "keybindings", -> @@ -56,14 +57,42 @@ describe "Window", -> expectNoMatch 'meta+1', 'alt+1' describe "bindMenuItem(path, action)", -> + it "causes the given menu item to be added to the menu when the window is focused and removed when it is blurred", -> + addedPaths = [] + spyOn(atom.native, 'addMenuItem').andCallFake (path) -> addedPaths.push(path) + + window.bindMenuItem 'Submenu 1 > Item 1' + window.bindMenuItem 'Submenu 1 > Item 2' + window.bindMenuItem 'Submenu 2 > Item 1' + + expect(atom.native.addMenuItem).not.toHaveBeenCalled() + + $(document).focus() + + expect(atom.native.addMenuItem).toHaveBeenCalled() + expect(addedPaths).toContain('Submenu 1 > Item 1') + expect(addedPaths).toContain('Submenu 1 > Item 2') + expect(addedPaths).toContain('Submenu 2 > Item 1') + + spyOn(atom.native, 'resetMainMenu') + + $(document).blur() + + expect(atom.native.resetMainMenu).toHaveBeenCalled() + it "causes the given action to be invoked when the menu item is selected", -> handler = jasmine.createSpy('menuItemHandler') window.bindMenuItem 'Submenu > Item', handler + $(document).focus() OSX.NSApp.mainMenu.itemWithTitle('Submenu').submenu.performActionForItemAtIndex(0) expect(handler).toHaveBeenCalled() + describe "menu items", -> + it "adds a Save item to the main menu after startup", -> + expect(OSX.NSApp.mainMenu.itemWithTitle('File').submenu.itemWithTitle('Save')).not.toBeNull() + describe 'meta+s', -> it 'saves the buffer', -> spyOn(window.editor, 'save') diff --git a/src/atom/window.coffee b/src/atom/window.coffee index 5bc26ff75..1fa6d10b3 100644 --- a/src/atom/window.coffee +++ b/src/atom/window.coffee @@ -19,19 +19,26 @@ windowAdditions = @menuItemActions = {} @layout = Layout.attach() @editor = new Editor $atomController.url?.toString() - @registerKeydownHandler() + @registerEventHandlers() @bindKeys() + @bindMenuItems() + $(document).focus() shutdown: -> @layout.remove() @editor.shutdown() + $(document).unbind('focus') + $(document).unbind('focus') + $(window).unbind('keydown') bindKeys: -> @bindKey 'meta+s', => @editor.save() + bindMenuItems: -> + @bindMenuItem "File > Save", => @editor.save() + bindMenuItem: (path, action) -> @menuItemActions[path] = action - atom.native.addMenuItem(path) bindKey: (pattern, action) -> @keyBindings[pattern] = action @@ -50,11 +57,18 @@ windowAdditions = patternModifiers.metaKey == event.metaKey and event.which == key.toUpperCase().charCodeAt 0 - registerKeydownHandler: -> + registerEventHandlers: -> $(document).bind 'keydown', (event) => for pattern, action of @keyBindings action() if @keyEventMatchesPattern(event, pattern) + $(document).focus => @registerMenuItems() + $(document).blur -> atom.native.resetMainMenu() + + registerMenuItems: -> + for path of @menuItemActions + atom.native.addMenuItem(path) + performActionForMenuItemPath: (path) -> @menuItemActions[path]()