From adacc2bcf950d82739d9740bd3c3baad44161116 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 26 Jun 2013 17:22:24 +0800 Subject: [PATCH] Use applicationShouldTerminate to control whether application should quit. --- browser/atom_application_delegate_mac.h | 3 --- browser/atom_application_delegate_mac.mm | 19 +++++++++---------- browser/browser.cc | 17 ++++++++++++----- browser/browser.h | 6 ++++++ browser/browser_mac.mm | 7 ++++++- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/browser/atom_application_delegate_mac.h b/browser/atom_application_delegate_mac.h index ba8bab0..f687952 100644 --- a/browser/atom_application_delegate_mac.h +++ b/browser/atom_application_delegate_mac.h @@ -7,7 +7,4 @@ @interface AtomApplicationDelegate : NSObject { } -- (void)handleQuitEvent:(NSAppleEventDescriptor*)event - withReplyEvent:(NSAppleEventDescriptor*)replyEvent; - @end diff --git a/browser/atom_application_delegate_mac.mm b/browser/atom_application_delegate_mac.mm index 83fecfe..76dc16f 100644 --- a/browser/atom_application_delegate_mac.mm +++ b/browser/atom_application_delegate_mac.mm @@ -15,13 +15,6 @@ } - (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(); } @@ -31,9 +24,15 @@ 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 diff --git a/browser/browser.cc b/browser/browser.cc index 290d413..58c6ab6 100644 --- a/browser/browser.cc +++ b/browser/browser.cc @@ -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() { diff --git a/browser/browser.h b/browser/browser.h index 74a1c0b..a915837 100644 --- a/browser/browser.h +++ b/browser/browser.h @@ -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: diff --git a/browser/browser_mac.mm b/browser/browser_mac.mm index 5adf347..1a8253c 100644 --- a/browser/browser_mac.mm +++ b/browser/browser_mac.mm @@ -11,11 +11,12 @@ 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 -- 2.7.4