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);
}
void App::OnBeforeQuit(bool* prevent_default) {
*prevent_default = Emit("before-quit");
}
void App::OnWillQuit(bool* prevent_default) {
*prevent_default = Emit("will-quit");
}

View file

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

View file

@ -28,7 +28,9 @@ Browser* Browser::Get() {
}
void Browser::Quit() {
is_quiting_ = true;
is_quiting_ = HandleBeforeQuit();
if (!is_quiting_)
return;
atom::WindowList* window_list = atom::WindowList::GetInstance();
if (window_list->size() == 0)
@ -114,6 +116,15 @@ void Browser::NotifyAndShutdown() {
Shutdown();
}
bool Browser::HandleBeforeQuit() {
bool prevent_default = false;
FOR_EACH_OBSERVER(BrowserObserver,
observers_,
OnBeforeQuit(&prevent_default));
return !prevent_default;
}
void Browser::OnWindowCloseCancelled(NativeWindow* window) {
if (is_quiting_)
// 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.
void NotifyAndShutdown();
// Send the before-quit message and start closing windows.
bool HandleBeforeQuit();
bool is_quiting_;
private:

View file

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