]> khamidou — GitHub mirrors - electron.git/commitdiff
Use applicationShouldTerminate to control whether application should quit.
authorCheng Zhao <zcbenz@gmail.com>
Wed, 26 Jun 2013 09:22:24 +0000 (17:22 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 26 Jun 2013 09:48:59 +0000 (17:48 +0800)
browser/atom_application_delegate_mac.h
browser/atom_application_delegate_mac.mm
browser/browser.cc
browser/browser.h
browser/browser_mac.mm

index ba8bab07b7015608cbcc20a718fe59813f64a94d..f68795236c917e0c0810c13596a2f7a3cb2efcf7 100644 (file)
@@ -7,7 +7,4 @@
 @interface AtomApplicationDelegate : NSObject<NSApplicationDelegate> {
 }
 
-- (void)handleQuitEvent:(NSAppleEventDescriptor*)event
-         withReplyEvent:(NSAppleEventDescriptor*)replyEvent;
-
 @end
index 83fecfeb3c0329f3ac9c297671cd81aaf2c568c5..76dc16fff165e0cff458b0305aef23bfbc4d68ea 100644 (file)
 }
 
 - (void)applicationDidFinishLaunching:(NSNotification*)notify {
-  // Trap the quit message to handleQuitEvent.
-  NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager];
-  [em setEventHandler:self
-          andSelector:@selector(handleQuitEvent:withReplyEvent:)
-        forEventClass:kCoreEventClass
-           andEventID:kAEQuitApplication];
-
   atom::Browser::Get()->DidFinishLaunching();
 }
 
   return atom::Browser::Get()->OpenFile(filename_str) ? YES : NO;
 }
 
-- (void)handleQuitEvent:(NSAppleEventDescriptor*)event
-         withReplyEvent:(NSAppleEventDescriptor*)replyEvent {
-  [[AtomApplication sharedApplication] closeAllWindows:self];
+- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender {
+  atom::Browser* browser = atom::Browser::Get();
+  if (browser->is_quiting()) {
+    return NSTerminateNow;
+  } else {
+    // System started termination.
+    atom::Browser::Get()->Quit();
+    return NSTerminateLater;
+  }
 }
 
 @end
index 290d4133201c53b5bdb5fd3c65c1b379630a514c..58c6ab61feb615a43a12996f53f281b8c7f4f1d8 100644 (file)
@@ -24,11 +24,12 @@ Browser* Browser::Get() {
 }
 
 void Browser::Quit() {
+  is_quiting_ = true;
+
   atom::WindowList* window_list = atom::WindowList::GetInstance();
   if (window_list->size() == 0)
     NotifyAndTerminate();
 
-  is_quiting_ = true;
   window_list->CloseAllWindows();
 }
 
@@ -53,16 +54,22 @@ void Browser::NotifyAndTerminate() {
   bool prevent_default = false;
   FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillQuit(&prevent_default));
 
-  if (prevent_default)
+  if (prevent_default) {
+    is_quiting_ = false;
     return;
+  }
 
   Terminate();
 }
 
 void Browser::OnWindowCloseCancelled(NativeWindow* window) {
-  // Once a beforeunload handler has prevented the closing, we think the quit
-  // is cancelled too.
-  is_quiting_ = false;
+  if (is_quiting_) {
+    // Once a beforeunload handler has prevented the closing, we think the quit
+    // is cancelled too.
+    is_quiting_ = false;
+
+    CancelQuit();
+  }
 }
 
 void Browser::OnWindowAllClosed() {
index 74a1c0bb853dbdb57219b0e0990b5af8449c4069..a91583748d9972adad645a1fc4f46bb28af63ba3 100644 (file)
@@ -48,9 +48,15 @@ class Browser : public WindowListObserver {
     observers_.RemoveObserver(obs);
   }
 
+  bool is_quiting() const { return is_quiting_; }
+
  protected:
+  // Send the will-quit message and then terminate the application.
   void NotifyAndTerminate();
 
+  // Tell the system we have cancelled quiting.
+  void CancelQuit();
+
   bool is_quiting_;
 
  private:
index 5adf34764026b4d62dcc713f7781b6009094ba07..1a8253c011c6f7b5fb4ab59e0c50d21ff2e593eb 100644 (file)
 namespace atom {
 
 void Browser::Terminate() {
+  is_quiting_ = true;
   [[AtomApplication sharedApplication] terminate:nil];
 }
 
 void Browser::Focus() {
-  [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
+  [[AtomApplication sharedApplication] activateIgnoringOtherApps:YES];
 }
 
 std::string Browser::GetVersion() {
@@ -24,4 +25,8 @@ std::string Browser::GetVersion() {
   return base::SysNSStringToUTF8(version);
 }
 
+void Browser::CancelQuit() {
+  [[AtomApplication sharedApplication] replyToApplicationShouldTerminate:NO];
+}
+
 }  // namespace atom