Use applicationShouldTerminate to control whether application should quit.

This commit is contained in:
Cheng Zhao 2013-06-26 17:22:24 +08:00
parent 6362e60a7b
commit adacc2bcf9
5 changed files with 33 additions and 19 deletions

View file

@ -7,7 +7,4 @@
@interface AtomApplicationDelegate : NSObject<NSApplicationDelegate> { @interface AtomApplicationDelegate : NSObject<NSApplicationDelegate> {
} }
- (void)handleQuitEvent:(NSAppleEventDescriptor*)event
withReplyEvent:(NSAppleEventDescriptor*)replyEvent;
@end @end

View file

@ -15,13 +15,6 @@
} }
- (void)applicationDidFinishLaunching:(NSNotification*)notify { - (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(); atom::Browser::Get()->DidFinishLaunching();
} }
@ -31,9 +24,15 @@
return atom::Browser::Get()->OpenFile(filename_str) ? YES : NO; return atom::Browser::Get()->OpenFile(filename_str) ? YES : NO;
} }
- (void)handleQuitEvent:(NSAppleEventDescriptor*)event - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender {
withReplyEvent:(NSAppleEventDescriptor*)replyEvent { atom::Browser* browser = atom::Browser::Get();
[[AtomApplication sharedApplication] closeAllWindows:self]; if (browser->is_quiting()) {
return NSTerminateNow;
} else {
// System started termination.
atom::Browser::Get()->Quit();
return NSTerminateLater;
}
} }
@end @end

View file

@ -24,11 +24,12 @@ Browser* Browser::Get() {
} }
void Browser::Quit() { void Browser::Quit() {
is_quiting_ = true;
atom::WindowList* window_list = atom::WindowList::GetInstance(); atom::WindowList* window_list = atom::WindowList::GetInstance();
if (window_list->size() == 0) if (window_list->size() == 0)
NotifyAndTerminate(); NotifyAndTerminate();
is_quiting_ = true;
window_list->CloseAllWindows(); window_list->CloseAllWindows();
} }
@ -53,16 +54,22 @@ void Browser::NotifyAndTerminate() {
bool prevent_default = false; bool prevent_default = false;
FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillQuit(&prevent_default)); FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillQuit(&prevent_default));
if (prevent_default) if (prevent_default) {
is_quiting_ = false;
return; return;
}
Terminate(); Terminate();
} }
void Browser::OnWindowCloseCancelled(NativeWindow* window) { void Browser::OnWindowCloseCancelled(NativeWindow* window) {
// Once a beforeunload handler has prevented the closing, we think the quit if (is_quiting_) {
// is cancelled too. // Once a beforeunload handler has prevented the closing, we think the quit
is_quiting_ = false; // is cancelled too.
is_quiting_ = false;
CancelQuit();
}
} }
void Browser::OnWindowAllClosed() { void Browser::OnWindowAllClosed() {

View file

@ -48,9 +48,15 @@ class Browser : public WindowListObserver {
observers_.RemoveObserver(obs); observers_.RemoveObserver(obs);
} }
bool is_quiting() const { return is_quiting_; }
protected: protected:
// Send the will-quit message and then terminate the application.
void NotifyAndTerminate(); void NotifyAndTerminate();
// Tell the system we have cancelled quiting.
void CancelQuit();
bool is_quiting_; bool is_quiting_;
private: private:

View file

@ -11,11 +11,12 @@
namespace atom { namespace atom {
void Browser::Terminate() { void Browser::Terminate() {
is_quiting_ = true;
[[AtomApplication sharedApplication] terminate:nil]; [[AtomApplication sharedApplication] terminate:nil];
} }
void Browser::Focus() { void Browser::Focus() {
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; [[AtomApplication sharedApplication] activateIgnoringOtherApps:YES];
} }
std::string Browser::GetVersion() { std::string Browser::GetVersion() {
@ -24,4 +25,8 @@ std::string Browser::GetVersion() {
return base::SysNSStringToUTF8(version); return base::SysNSStringToUTF8(version);
} }
void Browser::CancelQuit() {
[[AtomApplication sharedApplication] replyToApplicationShouldTerminate:NO];
}
} // namespace atom } // namespace atom