Only show menu items for a window/document when it is the key window.

Also add "File > Save" menu item. When document is focused, all menu items associated with its window are added to the main menu. When it is blurred, main menu is reset.
This commit is contained in:
Nathan Sobo
2011-12-26 14:49:51 -06:00
parent a010c8e3c6
commit 90188a871c
2 changed files with 46 additions and 3 deletions

View File

@@ -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')

View File

@@ -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]()