Send window commands instead of invoking window APIs directly.

For some events generated on the browser side, like reloading, closing
window, we should not handle those events directly, instead we should
send the corresponding commands to the renderer and let the renderer
deal with it.

The window.reload() is also moved to atom.reload(), because I saw things
like toggleFullScreen(), focus(), toggleDevTools() are also there.
This commit is contained in:
Cheng Zhao
2013-05-20 14:19:12 +08:00
parent 5459f1e9db
commit 60fcb97a3e
3 changed files with 15 additions and 15 deletions

View File

@@ -209,6 +209,10 @@ window.atom =
toggleDevTools: ->
remote.getCurrentWindow().toggleDevTools()
reload: ->
# TODO Restart renderer process when needed
remote.getCurrentWindow().reloadIgnoringCache()
focus: ->
remote.getCurrentWindow().focus()

View File

@@ -111,13 +111,13 @@ window.unloadConfigWindow = ->
$(window).off('focus blur before')
window.handleEvents = ->
ipc.on 'close', -> $(window).trigger 'window:close'
ipc.on 'command', (command) -> console.log command; $(window).trigger command
$(window).command 'window:toggle-full-screen', => atom.toggleFullScreen()
$(window).on 'focus', -> $("body").removeClass('is-blurred')
$(window).on 'blur', -> $("body").addClass('is-blurred')
$(window).command 'window:close', => confirmClose()
$(window).command 'window:reload', => reload()
$(window).command 'window:reload', => atom.reload()
$(document).on 'click', 'a', (e) ->
location = $(e.target).attr('href')
@@ -224,16 +224,6 @@ window.applyStylesheet = (id, text, ttype = 'bundled') ->
window.closeWithoutConfirm = ->
ipc.sendChannel 'close-without-confirm'
window.reload = ->
timesReloaded = process.global.timesReloaded ? 0
++timesReloaded
if timesReloaded > 3
atom.restartRendererProcess()
else
process.global.timesReloaded = timesReloaded
$native.reload()
window.onerror = ->
atom.openDevTools()

View File

@@ -89,8 +89,8 @@ class AtomApplication
viewMenu =
label: 'View'
submenu:[
{ label: 'Reload', accelerator: 'Command+R', click: -> BrowserWindow.getFocusedWindow()?.reloadIgnoringCache() }
{ label: 'Toggle DevTools', accelerator: 'Alt+Command+I', click: -> BrowserWindow.getFocusedWindow()?.toggleDevTools() }
{ label: 'Reload', accelerator: 'Command+R', click: => @sendCommand 'window:reload' }
{ label: 'Toggle DevTools', accelerator: 'Alt+Command+I', click: => @sendCommand 'toggle-dev-tools' }
]
windowMenu =
@@ -129,6 +129,9 @@ class AtomApplication
ipc.on 'new-window', =>
@createAtomWindow()
sendCommand: (command) ->
atomWindow.sendCommand command for atomWindow in @windows when atomWindow.browserWindow.isFocused()
createAtomWindow: (path) ->
new AtomWindow
path: path
@@ -157,4 +160,7 @@ class AtomWindow
@browserWindow.on 'close', (event) =>
event.preventDefault()
ipc.sendChannel @browserWindow.getProcessId(), @browserWindow.getRoutingId(), 'close'
@sendCommand 'window:close'
sendCommand: (command) ->
ipc.sendChannel @browserWindow.getProcessId(), @browserWindow.getRoutingId(), 'command', command