]> khamidou — GitHub mirrors - electron.git/commitdiff
Emit a new before-quit event at the start of Browser::Quit, which gives us a chance...
authorCharlie Hess <charlie@slack-corp.com>
Thu, 26 Feb 2015 03:33:42 +0000 (19:33 -0800)
committerCharlie Hess <charlie@slack-corp.com>
Thu, 26 Feb 2015 03:33:42 +0000 (19:33 -0800)
atom/browser/api/atom_api_app.cc
atom/browser/api/atom_api_app.h
atom/browser/browser.cc
atom/browser/browser.h
atom/browser/browser_observer.h
docs/api/app.md

index c7a2a542d1d8e0fb532f2b832a088268a5725da4..85d95f4c2d803f747099b499dafdb8d2fa119496 100644 (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");
 }
index 41a820b7a765751a92df4d0d5953bef0065345e8..062d2d083d2a5699960d6eff2bb61afe9a102ce2 100644 (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;
index b658de4102097500cd2a504b9df2cf48b16b3ffe..02a5a4697c0c4ed6ce8550330ebb9aa74cd36543 100644 (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,13 @@ 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
index d079d53a6dea4f51a3fbd56646fb3036a60dfccb..8648d8853384dfe7802e725108650ce6b78ebea1 100644 (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:
index 40069cbb4495e420e3714cf3064568a96c93cbf3..7dbd0039791df63bdd148700350ae829feab7ec7 100644 (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) {}
 
index 4ac377692622ac1454025438af8a2e5a7f68b3b6..4119dbf759048d2bc906e096b185450a00c93073 100644 (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
 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
@@ -78,9 +86,9 @@ click on the application's dock icon.
 
 ## 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