SDL: cocoa: don't show the menu bar while a fullscreen Space app deactivates

From b74b85087a77a87c6f47af9e22cbcbc21ff8aec7 Mon Sep 17 00:00:00 2001
From: Alexis Rico <[EMAIL REDACTED]>
Date: Thu, 22 Oct 2026 16:43:16 +0200
Subject: [PATCH] cocoa: don't show the menu bar while a fullscreen Space app
 deactivates

Showing the menu bar from windowDidResignKey while the app was being
deactivated made macOS hand focus back to it, so clicking another app's
window on a different display was undone a moment later. Restore it once
deactivation is done, only when another of our windows took key and is not
itself in a fullscreen Space.
---
 src/video/cocoa/SDL_cocoawindow.m | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index 70fd238ee18e8..6393076cd52ae 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -1340,6 +1340,9 @@ - (void)windowDidBecomeKey:(NSNotification *)aNotification
 
     if (isFullscreenSpace && !window->fullscreen_exclusive) {
         Cocoa_ToggleFullscreenSpaceMenuVisibility(window);
+    } else if (!isFullscreenSpace && ![NSMenu menuBarVisible]) {
+        // windowDidResignKey skips restoring the menu bar if the app was deactivated.
+        [NSMenu setMenuBarVisible:YES];
     }
     {
         const unsigned int newflags = [NSEvent modifierFlags] & NSEventModifierFlagCapsLock;
@@ -1369,7 +1372,17 @@ - (void)windowDidResignKey:(NSNotification *)aNotification
     }
 
     if (isFullscreenSpace) {
-        [NSMenu setMenuBarVisible:YES];
+        /* Showing the menu bar while the app is being deactivated can make macOS
+         * reactivate it, so defer it and skip it if another app or a fullscreen
+         * window has taken key. */
+        NSWindow *nswindow = _data.nswindow;
+        dispatch_async(dispatch_get_main_queue(), ^{
+            NSWindow *key = [NSApp keyWindow];
+            if ([NSApp isActive] && ![nswindow isKeyWindow] &&
+                !(key && ([key styleMask] & NSWindowStyleMaskFullScreen))) {
+                [NSMenu setMenuBarVisible:YES];
+            }
+        });
     }
 }