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");
}
virtual ~App();
// BrowserObserver:
+ void OnBeforeQuit(bool* prevent_default) override;
void OnWillQuit(bool* prevent_default) override;
void OnWindowAllClosed() override;
void OnQuit() override;
}
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)
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
// 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:
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) {}
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.
+## 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` Event
## app.quit()
-Try to close all windows. If all windows are successfully closed, the
-`will-quit` event will be emitted and by default the application would be
-terminated.
+Try to close all windows. The `before-quit` event will first be emitted. If all
+windows are successfully closed, the `will-quit` event will be emitted and by
+default the application would be terminated.
This method guarantees all `beforeunload` and `unload` handlers are correctly
executed. It is possible that a window cancels the quitting by returning