Browse Source

cocoadisplay: More graceful application termination behavior

See #1321
rdb 3 years ago
parent
commit
4979a8ba3f

+ 1 - 0
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

+ 16 - 0
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();
 }
 

+ 1 - 0
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.
 

+ 12 - 4
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