mac: Support external quit requests (#1099)

By no longer always returning NSTerminateCancel in
applicationShouldTerminate.

And implement applicationWillTerminate to ensure the cleanup code in
SolveSpaceUI::Exit() is always called.
This commit is contained in:
tomsci 2021-08-26 13:03:28 +01:00 committed by GitHub
parent 7e823df94a
commit 31a709e2c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1427,9 +1427,22 @@ void OpenInBrowser(const std::string &url) {
- (IBAction)preferences:(id)sender; - (IBAction)preferences:(id)sender;
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename; - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename;
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
@property BOOL exiting;
@end @end
@implementation SSApplicationDelegate @implementation SSApplicationDelegate
@synthesize exiting;
- (id)init {
if (self = [super init]) {
self.exiting = false;
}
return self;
}
- (IBAction)preferences:(id)sender { - (IBAction)preferences:(id)sender {
if (!SS.GW.showTextWindow) { if (!SS.GW.showTextWindow) {
SolveSpace::SS.GW.MenuView(SolveSpace::Command::SHOW_TEXT_WND); SolveSpace::SS.GW.MenuView(SolveSpace::Command::SHOW_TEXT_WND);
@ -1444,12 +1457,27 @@ void OpenInBrowser(const std::string &url) {
} }
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
[[[NSApp mainWindow] delegate] windowShouldClose:[NSApp mainWindow]]; if(!SS.unsaved) {
return NSTerminateCancel; return NSTerminateNow;
} else {
[self performSelectorOnMainThread:@selector(applicationTerminatePrompt) withObject:nil
waitUntilDone:NO modes:@[NSDefaultRunLoopMode, NSModalPanelRunLoopMode]];
return NSTerminateLater;
}
}
- (void)applicationWillTerminate:(NSNotification *)notification {
if(!exiting) {
// Prevent the Platform::ExitGui() call from SolveSpaceUI::Exit()
// triggering another terminate
exiting = true;
// Now let SS save settings etc
SS.Exit();
}
} }
- (void)applicationTerminatePrompt { - (void)applicationTerminatePrompt {
SolveSpace::SS.MenuFile(SolveSpace::Command::EXIT); [NSApp replyToApplicationShouldTerminate:SS.OkayToStartNewFile()];
} }
@end @end
@ -1488,8 +1516,10 @@ void RunGui() {
} }
void ExitGui() { void ExitGui() {
[NSApp setDelegate:nil]; if(!ssDelegate.exiting) {
[NSApp terminate:nil]; ssDelegate.exiting = true;
[NSApp terminate:nil];
}
} }
void ClearGui() {} void ClearGui() {}