From be3a6bc6d018c777de77c06d16ae7f326bf012ca Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 16 Apr 2014 17:03:38 -0700 Subject: [PATCH 1/4] 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. --- src/browser/atom-application.coffee | 16 +++++++++++++++- src/browser/atom-window.coffee | 2 +- src/browser/context-menu.coffee | 9 +++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/browser/atom-application.coffee b/src/browser/atom-application.coffee index 20d5e1e5b..62855ebca 100644 --- a/src/browser/atom-application.coffee +++ b/src/browser/atom-application.coffee @@ -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) -> diff --git a/src/browser/atom-window.coffee b/src/browser/atom-window.coffee index 4d4f71a93..744ae9a50 100644 --- a/src/browser/atom-window.coffee +++ b/src/browser/atom-window.coffee @@ -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 diff --git a/src/browser/context-menu.coffee b/src/browser/context-menu.coffee index e3044b30d..c8ab57de5 100644 --- a/src/browser/context-menu.coffee +++ b/src/browser/context-menu.coffee @@ -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 From 020c5e795aa82cfb38f7c9bd65bb8ae1e44a081c Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 16 Apr 2014 17:07:46 -0700 Subject: [PATCH 2/4] Remove unneeded . --- src/context-menu-manager.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/context-menu-manager.coffee b/src/context-menu-manager.coffee index 40228156b..c310c9c48 100644 --- a/src/context-menu-manager.coffee +++ b/src/context-menu-manager.coffee @@ -18,7 +18,7 @@ class ContextMenuManager label: 'Inspect Element' command: 'application:inspect' executeAtBuild: (e) -> - @.commandOptions = x: e.pageX, y: e.pageY + @commandOptions = x: e.pageX, y: e.pageY ] # Public: Creates menu definitions from the object specified by the menu From 9128041c202e89b57a8014f4a18b6276019087c2 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 17 Apr 2014 11:36:19 -0700 Subject: [PATCH 3/4] Include AtomWindow in command options This ensures that all application commands can use it instead of relying on the focused window. --- src/browser/atom-application.coffee | 5 +++-- src/browser/context-menu.coffee | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/browser/atom-application.coffee b/src/browser/atom-application.coffee index 62855ebca..23dc09686 100644 --- a/src/browser/atom-application.coffee +++ b/src/browser/atom-application.coffee @@ -140,7 +140,8 @@ 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}, atomWindow=@focusedWindow()) -> + @on 'application:inspect', ({x,y, atomWindow}) -> + atomWindow ?= @focusedWindow() atomWindow?.browserWindow?.inspectElement(x, y) @on 'application:open-documentation', -> shell.openExternal('https://atom.io/docs/latest/?app') @@ -236,7 +237,7 @@ class AtomApplication # atomWindow - The {AtomWindow} to send the command to. # args - The optional arguments to pass along. sendCommandToWindow: (command, atomWindow, args...) -> - unless @emit(command, args..., atomWindow) + unless @emit(command, args...) if atomWindow? atomWindow.sendCommand(command, args...) else diff --git a/src/browser/context-menu.coffee b/src/browser/context-menu.coffee index c8ab57de5..1f8377637 100644 --- a/src/browser/context-menu.coffee +++ b/src/browser/context-menu.coffee @@ -13,7 +13,9 @@ class ContextMenu createClickHandlers: (template) -> for item in template if item.command - (item.commandOptions ?= {}).contextCommand = true + item.commandOptions ?= {} + item.commandOptions.contextCommand = true + item.commandOptions.atomWindow = @atomWindow do (item) => item.click = => global.atomApplication.sendCommandToWindow(item.command, @atomWindow, item.commandOptions) From e37fc316fbaca21182bdd8ee44e7f4c833e3094b Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 17 Apr 2014 11:42:57 -0700 Subject: [PATCH 4/4] Remove unneeded ? AtomWindow always has a non-null browserWindow property --- src/browser/atom-application.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/browser/atom-application.coffee b/src/browser/atom-application.coffee index 23dc09686..01ca799b7 100644 --- a/src/browser/atom-application.coffee +++ b/src/browser/atom-application.coffee @@ -142,7 +142,7 @@ class AtomApplication @on 'application:open-dev', -> @promptForPath(devMode: true) @on 'application:inspect', ({x,y, atomWindow}) -> atomWindow ?= @focusedWindow() - atomWindow?.browserWindow?.inspectElement(x, y) + atomWindow?.browserWindow.inspectElement(x, y) @on 'application:open-documentation', -> shell.openExternal('https://atom.io/docs/latest/?app') @on 'application:install-update', -> @autoUpdateManager.install()