From 60fcb97a3ee5e443d2bdd020d25ca076d8f36f70 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 20 May 2013 14:19:12 +0800 Subject: [PATCH] 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. --- src/app/atom.coffee | 4 ++++ src/app/window.coffee | 14 ++------------ src/main.coffee | 12 +++++++++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/app/atom.coffee b/src/app/atom.coffee index d0bfa05da..c09dfe909 100644 --- a/src/app/atom.coffee +++ b/src/app/atom.coffee @@ -209,6 +209,10 @@ window.atom = toggleDevTools: -> remote.getCurrentWindow().toggleDevTools() + reload: -> + # TODO Restart renderer process when needed + remote.getCurrentWindow().reloadIgnoringCache() + focus: -> remote.getCurrentWindow().focus() diff --git a/src/app/window.coffee b/src/app/window.coffee index 8d9b68f09..6cf64ac75 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -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() diff --git a/src/main.coffee b/src/main.coffee index 28f2bf1b3..7531caf7d 100644 --- a/src/main.coffee +++ b/src/main.coffee @@ -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