Move ipc handling from WindowEventHandler to ApplicationDelegate

Signed-off-by: Max Brunsfeld <maxbrunsfeld@github.com>
This commit is contained in:
Nathan Sobo
2015-10-13 16:57:02 -06:00
committed by Max Brunsfeld
parent 83a0cf28cd
commit f989ed65e1
4 changed files with 65 additions and 37 deletions

View File

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

View File

@@ -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")

View File

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

View File

@@ -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')