SDL: Fix Mac child windows that are created hidden showing if their parent window is shown

From 787a15f76005158bf64517ccf24f3f4b9baf415c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 4 Jan 2024 12:44:57 -0800
Subject: [PATCH] Fix Mac child windows that are created hidden showing if
 their parent window is shown

- Child windows are added and removed dynamically from the window hierarchy when they're shown/hidden. Adding a hidden child window to a visible
  parent is fine, but adding a hidden child window to a hidden parent will cause the child to show when the parent window is shown as it's still a part of
  the window hierarchy.

- For some reason, not adding the child window to the parent entirely causes the child to not focus correctly the first time it's shown. Adding then immediately
  calling orderOut to remove the child window from the hierarchy does work correctly so we do this to work around the weird issue.
---
 src/video/cocoa/SDL_cocoawindow.m | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index d2745db04c25..5dbc04b46d42 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -1926,6 +1926,16 @@ static int SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, NSWindow
                     Cocoa_SetKeyboardFocus(window);
                 }
             }
+
+            /* FIXME: Should not need to call addChildWindow then orderOut.
+               Attaching a hidden child window to a hidden parent window will cause the child window
+               to show when the parent does. We therefore shouldn't attach the child window here as we're
+               going to do so when the child window is explicitly shown later but skipping the addChildWindow
+               entirely causes the child window to not get key focus correctly the first time it's shown. Adding
+               then immediately ordering out (removing) the window does work. */
+            if (window->flags & SDL_WINDOW_HIDDEN) {
+                [nswindow orderOut:nil];
+            }
         }
 
         if (nswindow.isOpaque) {