mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Send context menu commands without focused window
The context menu always knows what window it is bound to so start supporting running context menus on non-focused windows. Previously these would either fail silently or log an error if focusedWindow() returned null and the command wasn't guarding against it.
This commit is contained in:
@@ -140,7 +140,9 @@ class AtomApplication
|
||||
@on 'application:open-file', -> @promptForPath(type: 'file')
|
||||
@on 'application:open-folder', -> @promptForPath(type: 'folder')
|
||||
@on 'application:open-dev', -> @promptForPath(devMode: true)
|
||||
@on 'application:inspect', ({x,y}) -> @focusedWindow().browserWindow.inspectElement(x, y)
|
||||
@on 'application:inspect', ({x,y}, atomWindow=@focusedWindow()) ->
|
||||
atomWindow?.browserWindow?.inspectElement(x, y)
|
||||
|
||||
@on 'application:open-documentation', -> shell.openExternal('https://atom.io/docs/latest/?app')
|
||||
@on 'application:install-update', -> @autoUpdateManager.install()
|
||||
@on 'application:check-for-update', => @autoUpdateManager.check()
|
||||
@@ -228,6 +230,18 @@ class AtomApplication
|
||||
else
|
||||
@sendCommandToFirstResponder(command)
|
||||
|
||||
# Public: Executes the given command on the given window.
|
||||
#
|
||||
# command - The string representing the command.
|
||||
# atomWindow - The {AtomWindow} to send the command to.
|
||||
# args - The optional arguments to pass along.
|
||||
sendCommandToWindow: (command, atomWindow, args...) ->
|
||||
unless @emit(command, args..., atomWindow)
|
||||
if atomWindow?
|
||||
atomWindow.sendCommand(command, args...)
|
||||
else
|
||||
@sendCommandToFirstResponder(command)
|
||||
|
||||
# Translates the command into OS X action and sends it to application's first
|
||||
# responder.
|
||||
sendCommandToFirstResponder: (command) ->
|
||||
|
||||
@@ -107,7 +107,7 @@ class AtomWindow
|
||||
when 1 then @browserWindow.restart()
|
||||
|
||||
@browserWindow.on 'context-menu', (menuTemplate) =>
|
||||
new ContextMenu(menuTemplate, @browserWindow)
|
||||
new ContextMenu(menuTemplate, this)
|
||||
|
||||
if @isSpec
|
||||
# Spec window's web view should always have focus
|
||||
|
||||
@@ -2,10 +2,10 @@ Menu = require 'menu'
|
||||
|
||||
module.exports =
|
||||
class ContextMenu
|
||||
constructor: (template, browserWindow) ->
|
||||
constructor: (template, @atomWindow) ->
|
||||
template = @createClickHandlers(template)
|
||||
menu = Menu.buildFromTemplate(template)
|
||||
menu.popup(browserWindow)
|
||||
menu.popup(@atomWindow.browserWindow)
|
||||
|
||||
# It's necessary to build the event handlers in this process, otherwise
|
||||
# closures are drug across processes and failed to be garbage collected
|
||||
@@ -14,6 +14,7 @@ class ContextMenu
|
||||
for item in template
|
||||
if item.command
|
||||
(item.commandOptions ?= {}).contextCommand = true
|
||||
item.click = do (item) ->
|
||||
=> global.atomApplication.sendCommand(item.command, item.commandOptions)
|
||||
do (item) =>
|
||||
item.click = =>
|
||||
global.atomApplication.sendCommandToWindow(item.command, @atomWindow, item.commandOptions)
|
||||
item
|
||||
|
||||
Reference in New Issue
Block a user