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

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