Merge pull request #1176 from CharlieHess/before-quit-event

App Before-Quit Event
This commit is contained in:
Cheng Zhao 2015-03-01 02:20:03 -08:00
commit 38a871aa4b
6 changed files with 34 additions and 4 deletions

View file

@ -131,6 +131,10 @@ App::~App() {
Browser::Get()->RemoveObserver(this); Browser::Get()->RemoveObserver(this);
} }
void App::OnBeforeQuit(bool* prevent_default) {
*prevent_default = Emit("before-quit");
}
void App::OnWillQuit(bool* prevent_default) { void App::OnWillQuit(bool* prevent_default) {
*prevent_default = Emit("will-quit"); *prevent_default = Emit("will-quit");
} }

View file

@ -38,6 +38,7 @@ class App : public mate::EventEmitter,
virtual ~App(); virtual ~App();
// BrowserObserver: // BrowserObserver:
void OnBeforeQuit(bool* prevent_default) override;
void OnWillQuit(bool* prevent_default) override; void OnWillQuit(bool* prevent_default) override;
void OnWindowAllClosed() override; void OnWindowAllClosed() override;
void OnQuit() override; void OnQuit() override;

View file

@ -28,7 +28,9 @@ Browser* Browser::Get() {
} }
void Browser::Quit() { void Browser::Quit() {
is_quiting_ = true; is_quiting_ = HandleBeforeQuit();
if (!is_quiting_)
return;
atom::WindowList* window_list = atom::WindowList::GetInstance(); atom::WindowList* window_list = atom::WindowList::GetInstance();
if (window_list->size() == 0) if (window_list->size() == 0)
@ -114,6 +116,15 @@ void Browser::NotifyAndShutdown() {
Shutdown(); Shutdown();
} }
bool Browser::HandleBeforeQuit() {
bool prevent_default = false;
FOR_EACH_OBSERVER(BrowserObserver,
observers_,
OnBeforeQuit(&prevent_default));
return !prevent_default;
}
void Browser::OnWindowCloseCancelled(NativeWindow* window) { void Browser::OnWindowCloseCancelled(NativeWindow* window) {
if (is_quiting_) if (is_quiting_)
// Once a beforeunload handler has prevented the closing, we think the quit // Once a beforeunload handler has prevented the closing, we think the quit

View file

@ -136,6 +136,9 @@ class Browser : public WindowListObserver {
// Send the will-quit message and then shutdown the application. // Send the will-quit message and then shutdown the application.
void NotifyAndShutdown(); void NotifyAndShutdown();
// Send the before-quit message and start closing windows.
bool HandleBeforeQuit();
bool is_quiting_; bool is_quiting_;
private: private:

View file

@ -11,6 +11,9 @@ namespace atom {
class BrowserObserver { class BrowserObserver {
public: public:
// The browser is about to close all windows.
virtual void OnBeforeQuit(bool* prevent_default) {}
// The browser has closed all windows and will quit. // The browser has closed all windows and will quit.
virtual void OnWillQuit(bool* prevent_default) {} virtual void OnWillQuit(bool* prevent_default) {}

View file

@ -34,6 +34,14 @@ user pressed `Cmd + Q`, or the developer called `app.quit()`, atom-shell would
first try to close all windows and then emit the `will-quit` event, and in first try to close all windows and then emit the `will-quit` event, and in
this case the `window-all-closed` would not be emitted. this case the `window-all-closed` would not be emitted.
## Event: before-quit
* `event` Event
Emitted before the application starts closing its windows.
Calling `event.preventDefault()` will prevent the default behaviour, which is
terminating the application.
## Event: will-quit ## Event: will-quit
* `event` Event * `event` Event
@ -78,9 +86,9 @@ click on the application's dock icon.
## app.quit() ## app.quit()
Try to close all windows. If all windows are successfully closed, the Try to close all windows. The `before-quit` event will first be emitted. If all
`will-quit` event will be emitted and by default the application would be windows are successfully closed, the `will-quit` event will be emitted and by
terminated. default the application would be terminated.
This method guarantees all `beforeunload` and `unload` handlers are correctly This method guarantees all `beforeunload` and `unload` handlers are correctly
executed. It is possible that a window cancels the quitting by returning executed. It is possible that a window cancels the quitting by returning