mirror of
https://github.com/atom/atom.git
synced 2026-01-24 22:38:20 -05:00
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:
@@ -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')
|
||||
|
||||
@@ -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]()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user