@interface AtomApplicationDelegate : NSObject<NSApplicationDelegate> {
}
-- (void)handleQuitEvent:(NSAppleEventDescriptor*)event
- withReplyEvent:(NSAppleEventDescriptor*)replyEvent;
-
@end
}
- (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
}
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();
}
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() {
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:
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() {
return base::SysNSStringToUTF8(version);
}
+void Browser::CancelQuit() {
+ [[AtomApplication sharedApplication] replyToApplicationShouldTerminate:NO];
+}
+
} // namespace atom