From 40631edb70abfe1803a8e912dfc0c27fcaed8ea4 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 11 May 2015 14:30:26 +0800 Subject: [PATCH] Use Chrome's navigation controller for in-page navigations --- atom/browser/api/atom_api_web_contents.cc | 10 +++++++ atom/browser/api/atom_api_web_contents.h | 3 ++ .../api/lib/navigation-controller.coffee | 28 ++++++++++++------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 2fedd10cf4a..558b26b9d11 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -443,6 +443,14 @@ void WebContents::ReloadIgnoringCache() { web_contents()->GetController().ReloadIgnoringCache(false); } +void WebContents::GoBack() { + web_contents()->GetController().GoBack(); +} + +void WebContents::GoForward() { + web_contents()->GetController().GoForward(); +} + int WebContents::GetRoutingID() const { return web_contents()->GetRoutingID(); } @@ -611,6 +619,8 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse) .SetMethod("_stop", &WebContents::Stop) .SetMethod("_reloadIgnoringCache", &WebContents::ReloadIgnoringCache) + .SetMethod("_goBack", &WebContents::GoBack) + .SetMethod("_goForward", &WebContents::GoForward) .SetMethod("getRoutingId", &WebContents::GetRoutingID) .SetMethod("getProcessId", &WebContents::GetProcessID) .SetMethod("isCrashed", &WebContents::IsCrashed) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index cc2fd30c832..aa0ee8d6f68 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -53,6 +53,9 @@ class WebContents : public mate::EventEmitter, bool IsWaitingForResponse() const; void Stop(); void ReloadIgnoringCache(); + void GoBack(); + void GoForward(); + void GoToIndex(); int GetRoutingID() const; int GetProcessID() const; bool IsCrashed() const; diff --git a/atom/browser/api/lib/navigation-controller.coffee b/atom/browser/api/lib/navigation-controller.coffee index 28404b1214f..675f34e85f8 100644 --- a/atom/browser/api/lib/navigation-controller.coffee +++ b/atom/browser/api/lib/navigation-controller.coffee @@ -10,20 +10,20 @@ class NavigationController @pendingIndex = -1 @webContents.on 'navigation-entry-commited', (event, url, inPage, replaceEntry) => - if replaceEntry - @history[@currentIndex] = {url, inPage} - return + console.log 'navigation-entry-commited', url, inPage, replaceEntry - if @pendingIndex is -1 # Normal navigation. + if @pendingIndex >= 0 # Go to index. + @currentIndex = @pendingIndex + @pendingIndex = -1 + @history[@currentIndex] = {url, inPage} + else if replaceEntry # Non-user initialized navigation. + @history[@currentIndex] = {url, inPage} + else # Normal navigation. @history = @history.slice 0, @currentIndex + 1 # Clear history. currentEntry = @history[@currentIndex] if currentEntry?.url isnt url or currentEntry?.inPage isnt inPage @currentIndex++ @history.push {url, inPage} - else # Go to index. - @currentIndex = @pendingIndex - @pendingIndex = -1 - @history[@currentIndex] = {url, inPage} loadUrl: (url, options={}) -> @pendingIndex = -1 @@ -62,12 +62,20 @@ class NavigationController goBack: -> return unless @canGoBack() @pendingIndex = @getActiveIndex() - 1 - @webContents._loadUrl @history[@pendingIndex].url, {} + pendingEntry = @history[@pendingIndex] + if pendingEntry.inPage + @webContents._goBack() + else + @webContents._loadUrl pendingEntry.url, {} goForward: -> return unless @canGoForward() @pendingIndex = @getActiveIndex() + 1 - @webContents._loadUrl @history[@pendingIndex].url, {} + pendingEntry = @history[@pendingIndex] + if pendingEntry.inPage + @webContents._goForward() + else + @webContents._loadUrl pendingEntry.url, {} goToIndex: (index) -> return unless @canGoToIndex index