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> {
}
- (void)handleQuitEvent:(NSAppleEventDescriptor*)event
withReplyEvent:(NSAppleEventDescriptor*)replyEvent;
@end

View file

@ -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

View 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() {

View 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:

View file

@ -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