From f989ed65e1ffd684b338867d964c5de62e83eab3 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 13 Oct 2015 16:57:02 -0600 Subject: [PATCH] Move ipc handling from WindowEventHandler to ApplicationDelegate Signed-off-by: Max Brunsfeld --- src/application-delegate.coffee | 32 +++++++++++++++++++++- src/atom-environment.coffee | 32 ++++++++++++++++++++++ src/initialize-application-window.coffee | 3 +- src/window-event-handler.coffee | 35 ------------------------ 4 files changed, 65 insertions(+), 37 deletions(-) diff --git a/src/application-delegate.coffee b/src/application-delegate.coffee index cc6e8bda7..7cc76c50c 100644 --- a/src/application-delegate.coffee +++ b/src/application-delegate.coffee @@ -1,12 +1,14 @@ +ipc = require 'ipc' remote = require 'remote' shell = require 'shell' +{Disposable} = require 'event-kit' module.exports = class ApplicationDelegate open: (params) -> ipc.send('open', params) - pickFolder: -> + pickFolder: (callback) -> responseChannel = "atom-pick-folder-response" ipc.on responseChannel, (path) -> ipc.removeAllListeners(responseChannel) @@ -95,3 +97,31 @@ class ApplicationDelegate playBeepSound: -> shell.beep() + + onDidOpenLocations: (callback) -> + outerCallback = (message, detail) -> + if message is 'open-locations' + callback(detail) + + ipc.on('message', outerCallback) + new Disposable -> + ipc.removeEventListener('message', outerCallback) + + onUpdateAvailable: (callback) -> + outerCallback = (message, detail) -> + if message is 'update-available' + callback(detail) + + ipc.on('message', outerCallback) + new Disposable -> + ipc.removeEventListener('message', outerCallback) + + onApplicationMenuCommand: (callback) -> + ipc.on('command', callback) + new Disposable -> + ipc.removeEventListener('command', callback) + + onContextMenuCommand: (callback) -> + ipc.on('context-command', callback) + new Disposable -> + ipc.removeEventListener('context-command', callback) diff --git a/src/atom-environment.coffee b/src/atom-environment.coffee index bf798d451..4cb256a03 100644 --- a/src/atom-environment.coffee +++ b/src/atom-environment.coffee @@ -581,6 +581,11 @@ class AtomEnvironment extends Model @commandInstaller.installApmCommand false, (error) -> console.warn error.message if error? + @disposables.add(@applicationDelegate.onDidOpenLocations(@openLocations.bind(this))) + @disposables.add(@applicationDelegate.onUpdateAvailable(@updateAvailable.bind(this))) + @disposables.add(@applicationDelegate.onApplicationMenuCommand(@dispatchApplicationMenuCommand.bind(this))) + @disposables.add(@applicationDelegate.onContextMenuCommand(@dispatchContextMenuCommand.bind(this))) + @config.load() @themes.loadBaseStylesheets() @setBodyPlatformClass() @@ -863,6 +868,33 @@ class AtomEnvironment extends Model @applicationDelegate.setAutoHideWindowMenuBar(autoHide) @applicationDelegate.setWindowMenuBarVisibility(not autoHide) + dispatchApplicationMenuCommand: (command, arg) -> + activeElement = document.activeElement + # Use the workspace element if body has focus + if activeElement is document.body and workspaceElement = @views.getView(@workspace) + activeElement = workspaceElement + @commands.dispatch(activeElement, command, arg) + + dispatchContextMenuCommand: (command, args...) -> + @commands.dispatch(@contextMenu.activeElement, command, args) + + openLocations: (locations) -> + needsProjectPaths = @project?.getPaths().length is 0 + + for {pathToOpen, initialLine, initialColumn} in locations + if pathToOpen? and needsProjectPaths + if fs.existsSync(pathToOpen) + @project.addPath(pathToOpen) + else if fs.existsSync(path.dirname(pathToOpen)) + @project.addPath(path.dirname(pathToOpen)) + else + @project.addPath(pathToOpen) + + unless fs.isDirectorySync(pathToOpen) + @workspace?.open(pathToOpen, {initialLine, initialColumn}) + + return + # Preserve this deprecation until 2.0. Sorry. Should have removed Q sooner. Promise.prototype.done = (callback) -> deprecate("Atom now uses ES6 Promises instead of Q. Call promise.then instead of promise.done") diff --git a/src/initialize-application-window.coffee b/src/initialize-application-window.coffee index b10208bb1..242201b32 100644 --- a/src/initialize-application-window.coffee +++ b/src/initialize-application-window.coffee @@ -15,7 +15,8 @@ process.env.NODE_PATH = exportsPath process.env.NODE_ENV ?= 'production' unless devMode AtomEnvironment = require './atom-environment' -window.atom = new AtomEnvironment +ApplicationDelegate = require './application-delegate' +window.atom = new AtomEnvironment({applicationDelegate: new ApplicationDelegate}) atom.displayWindow() atom.loadStateSync() diff --git a/src/window-event-handler.coffee b/src/window-event-handler.coffee index 94fdf55c5..5b25ff18f 100644 --- a/src/window-event-handler.coffee +++ b/src/window-event-handler.coffee @@ -12,10 +12,6 @@ class WindowEventHandler @reloadRequested = false @subscriptions = new CompositeDisposable - @on(ipc, 'message', @handleIPCMessage) - @on(ipc, 'command', @handleIPCCommand) - @on(ipc, 'context-command', @handleIPCContextCommand) - @addEventListener(window, 'focus', @handleWindowFocus) @addEventListener(window, 'blur', @handleWindowBlur) @addEventListener(window, 'beforeunload', @handleWindowBeforeunload) @@ -134,37 +130,6 @@ class WindowEventHandler else if highestElement? highestElement.focus() - handleIPCMessage: (message, detail) => - switch message - when 'open-locations' - needsProjectPaths = @atomEnvironment.project?.getPaths().length is 0 - - for {pathToOpen, initialLine, initialColumn} in detail - if pathToOpen? and needsProjectPaths - if fs.existsSync(pathToOpen) - @atomEnvironment.project.addPath(pathToOpen) - else if fs.existsSync(path.dirname(pathToOpen)) - @atomEnvironment.project.addPath(path.dirname(pathToOpen)) - else - @atomEnvironment.project.addPath(pathToOpen) - - unless fs.isDirectorySync(pathToOpen) - @atomEnvironment.workspace?.open(pathToOpen, {initialLine, initialColumn}) - return - when 'update-available' - @atomEnvironment.updateAvailable(detail) - - handleIPCCommand: (command, args...) => - activeElement = document.activeElement - # Use the workspace element view if body has focus - if activeElement is document.body and workspaceElement = @atomEnvironment.views.getView(@atomEnvironment.workspace) - activeElement = workspaceElement - - @atomEnvironment.commands.dispatch(activeElement, command, args[0]) - - handleIPCContextCommand: (command, args...) => - @atomEnvironment.commands.dispatch(@atomEnvironment.contextMenu.activeElement, command, args) - handleWindowFocus: -> document.body.classList.remove('is-blurred')