SDL: dialog: Cocoa backend should reactivate the app after the modal dialog. (de5cb)

From de5cb9db2317c194918909ec243507c7b39a4675 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 2 May 2025 01:48:13 -0400
Subject: [PATCH] dialog: Cocoa backend should reactivate the app after the
 modal dialog.

Otherwise the window won't have focus until you click on it again. Calling
makeKeyAndOrderFront isn't enough to fix it, either.

This trick comes from a similar problem we solve in our
applicationDidFinishLaunching implementation: activate (give app focus to) the
system Dock, as something that definitely exists that isn't us and is harmless
to activate, and then activate us right afterwards. This unconfuses whatever
is getting confused inside Cocoa.

Fixes #12684.

(cherry picked from commit 57346f2ba8d920856526dae234b135e1c27d3007)
---
 src/dialog/cocoa/SDL_cocoadialog.m | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/dialog/cocoa/SDL_cocoadialog.m b/src/dialog/cocoa/SDL_cocoadialog.m
index a6054a6ebae7f..64b2fdf978fb5 100644
--- a/src/dialog/cocoa/SDL_cocoadialog.m
+++ b/src/dialog/cocoa/SDL_cocoadialog.m
@@ -47,6 +47,15 @@ static void AddFileExtensionType(NSMutableArray *types, const char *pattern_ptr)
     }
 }
 
+static void ReactivateAfterDialog(void)
+{
+    for (NSRunningApplication *i in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) {
+        [i activateWithOptions:0];
+        break;
+    }
+    [NSApp activateIgnoringOtherApps:YES];
+}
+
 void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props)
 {
     SDL_Window* window = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, NULL);
@@ -176,6 +185,8 @@ void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFil
                 const char *files[1] = { NULL };
                 callback(userdata, files, -1);
             }
+
+            ReactivateAfterDialog();
         }];
     } else {
         if ([dialog runModal] == NSModalResponseOK) {
@@ -195,6 +206,7 @@ void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFil
             const char *files[1] = { NULL };
             callback(userdata, files, -1);
         }
+        ReactivateAfterDialog();
     }
 }