From 4979a8ba3f8560abbf4b490865c16aa1fb16f218 Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 7 Dec 2022 17:48:33 +0100 Subject: [PATCH] cocoadisplay: More graceful application termination behavior See #1321 --- panda/src/cocoadisplay/cocoaPandaAppDelegate.h | 1 + panda/src/cocoadisplay/cocoaPandaAppDelegate.mm | 16 ++++++++++++++++ .../src/cocoadisplay/cocoaPandaWindowDelegate.h | 1 + .../src/cocoadisplay/cocoaPandaWindowDelegate.mm | 16 ++++++++++++---- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/panda/src/cocoadisplay/cocoaPandaAppDelegate.h b/panda/src/cocoadisplay/cocoaPandaAppDelegate.h index 6dd56b68e5..4fdb5483e9 100644 --- a/panda/src/cocoadisplay/cocoaPandaAppDelegate.h +++ b/panda/src/cocoadisplay/cocoaPandaAppDelegate.h @@ -24,6 +24,7 @@ class GraphicsEngine; - (id) initWithEngine:(GraphicsEngine *)engine; - (void)applicationDidFinishLaunching:(NSNotification *)notification; +- (BOOL)applicationShouldTerminate:(NSApplication *)app; - (void)applicationWillTerminate:(NSNotification *)notification; @end diff --git a/panda/src/cocoadisplay/cocoaPandaAppDelegate.mm b/panda/src/cocoadisplay/cocoaPandaAppDelegate.mm index 1906e15d44..5855c3025b 100644 --- a/panda/src/cocoadisplay/cocoaPandaAppDelegate.mm +++ b/panda/src/cocoadisplay/cocoaPandaAppDelegate.mm @@ -30,9 +30,25 @@ [NSApp activateIgnoringOtherApps:YES]; } +- (BOOL)applicationShouldTerminate:(NSApplication *)app { + if (cocoadisplay_cat.is_debug()) { + cocoadisplay_cat.debug() + << "Received applicationShouldTerminate, closing all Cocoa windows\n"; + } + // Call performClose on all the windows. This should make ShowBase shut down. + for (NSWindow *window in [app windows]) { + [window performClose:nil]; + } + return FALSE; +} + - (void)applicationWillTerminate:(NSNotification *)notification { // The application is about to be closed, tell the graphics engine to close // all the windows. + if (cocoadisplay_cat.is_debug()) { + cocoadisplay_cat.debug() + << "Received applicationWillTerminate, removing all windows\n"; + } _engine->remove_all_windows(); } diff --git a/panda/src/cocoadisplay/cocoaPandaWindowDelegate.h b/panda/src/cocoadisplay/cocoaPandaWindowDelegate.h index 93d5b7b35b..c71b1d509d 100644 --- a/panda/src/cocoadisplay/cocoaPandaWindowDelegate.h +++ b/panda/src/cocoadisplay/cocoaPandaWindowDelegate.h @@ -34,6 +34,7 @@ class CocoaGraphicsWindow; - (void)windowDidBecomeKey:(NSNotification *)notification; - (void)windowDidResignKey:(NSNotification *)notification; - (BOOL)windowShouldClose:(id)sender; +- (void)windowWillClose:(id)sender; // TODO: handle fullscreen on Lion. diff --git a/panda/src/cocoadisplay/cocoaPandaWindowDelegate.mm b/panda/src/cocoadisplay/cocoaPandaWindowDelegate.mm index 17fae241f3..dbbbe8e5b2 100644 --- a/panda/src/cocoadisplay/cocoaPandaWindowDelegate.mm +++ b/panda/src/cocoadisplay/cocoaPandaWindowDelegate.mm @@ -51,11 +51,19 @@ } - (BOOL) windowShouldClose:(id)sender { - bool should_close = _graphicsWindow->handle_close_request(); - if (should_close) { - _graphicsWindow->handle_close_event(); + if (cocoadisplay_cat.is_debug()) { + cocoadisplay_cat.debug() + << "Received windowShouldClose for window " << _graphicsWindow << "\n"; } - return should_close; + return _graphicsWindow->handle_close_request(); +} + +- (void) windowWillClose:(id)sender { + if (cocoadisplay_cat.is_debug()) { + cocoadisplay_cat.debug() + << "Received windowWillClose for window " << _graphicsWindow << "\n"; + } + _graphicsWindow->handle_close_event(); } @end