diff --git a/atom/browser/api/atom_api_browser_window.cc b/atom/browser/api/atom_api_browser_window.cc index 1d4c0de1cb..626bb632f0 100644 --- a/atom/browser/api/atom_api_browser_window.cc +++ b/atom/browser/api/atom_api_browser_window.cc @@ -243,6 +243,14 @@ void BrowserWindow::WillDestroyNativeObject() { } } +void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) { + // When user tries to close the window by clicking the close button, we do + // not close the window immediately, instead we try to close the web page + // first, and when the web page is closed the window will also be closed. + *prevent_default = true; + window_->RequestToClosePage(); +} + void BrowserWindow::OnWindowClosed() { api_web_contents_->DestroyWebContents(true /* async */); diff --git a/atom/browser/api/atom_api_browser_window.h b/atom/browser/api/atom_api_browser_window.h index bf9a4021bf..4f75546e82 100644 --- a/atom/browser/api/atom_api_browser_window.h +++ b/atom/browser/api/atom_api_browser_window.h @@ -71,6 +71,7 @@ class BrowserWindow : public mate::TrackableObject, // NativeWindowObserver: void WillCloseWindow(bool* prevent_default) override; void WillDestroyNativeObject() override; + void OnCloseButtonClicked(bool* prevent_default) override; void OnWindowClosed() override; void OnWindowEndSession() override; void OnWindowBlur() override; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 5407fd4f53..8997161815 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -427,14 +427,6 @@ void NativeWindow::CloseFilePreview() { } void NativeWindow::RequestToClosePage() { - bool prevent_default = false; - for (NativeWindowObserver& observer : observers_) - observer.WillCloseWindow(&prevent_default); - if (prevent_default) { - WindowList::WindowCloseCancelled(this); - return; - } - // Assume the window is not responding if it doesn't cancel the close and is // not closed in 5s, in this way we can quickly show the unresponsive // dialog when the window is busy executing some script withouth waiting for @@ -480,6 +472,25 @@ void NativeWindow::RendererResponsive(content::WebContents* source) { observer.OnWindowResponsive(); } +void NativeWindow::NotifyWindowCloseButtonClicked() { + // First ask the observers whether we want to close. + bool prevent_default = false; + for (NativeWindowObserver& observer : observers_) + observer.WillCloseWindow(&prevent_default); + if (prevent_default) { + WindowList::WindowCloseCancelled(this); + return; + } + + // Then ask the observers how should we close the window. + for (NativeWindowObserver& observer : observers_) + observer.OnCloseButtonClicked(&prevent_default); + if (prevent_default) + return; + + CloseImmediately(); +} + void NativeWindow::NotifyWindowClosed() { if (is_closed_) return; diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 9ec55440aa..8f66764744 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -249,6 +249,7 @@ class NativeWindow : public base::SupportsUserData, // Public API used by platform-dependent delegates and observers to send UI // related notifications. + void NotifyWindowCloseButtonClicked(); void NotifyWindowClosed(); void NotifyWindowEndSession(); void NotifyWindowBlur(); diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index bcafcbefe0..ed51d7219e 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -415,10 +415,7 @@ bool ScopedDisableResize::disable_resize_ = false; } - (BOOL)windowShouldClose:(id)window { - // When user tries to close the window by clicking the close button, we do - // not close the window immediately, instead we try to close the web page - // first, and when the web page is closed the window will also be closed. - shell_->RequestToClosePage(); + shell_->NotifyWindowCloseButtonClicked(); return NO; } diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index 43eef69ed8..c57c819b86 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -37,6 +37,9 @@ class NativeWindowObserver { // Called before the native window object is going to be destroyed. virtual void WillDestroyNativeObject() {} + // Called when closed button is clicked. + virtual void OnCloseButtonClicked(bool* prevent_default) {} + // Called when the window is closed. virtual void OnWindowClosed() {} diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index 11c5efc757..895f1b07f7 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -124,7 +124,8 @@ class NativeWindowClientView : public views::ClientView { virtual ~NativeWindowClientView() {} bool CanClose() override { - static_cast(contents_view())->RequestToClosePage(); + static_cast(contents_view())-> + NotifyWindowCloseButtonClicked(); return false; }