SDL: cocoa: Set the titled flag on fullscreen space windows

From ed62d6e7deb7684c8a59e5d77908fc762fdacccf Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Wed, 3 Jan 2024 19:56:57 -0500
Subject: [PATCH] cocoa: Set the titled flag on fullscreen space windows

For some reason, fullscreen space windows won't get any mouse button events unless the NSWindowStyleMaskTitled flag is set when entering fullscreen, even though they successfully become key and receive mouse motion events. Make sure the flag is always set when entering fullscreen space state.
---
 src/video/cocoa/SDL_cocoawindow.h |  1 +
 src/video/cocoa/SDL_cocoawindow.m | 37 +++++++++++++++++--------------
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/src/video/cocoa/SDL_cocoawindow.h b/src/video/cocoa/SDL_cocoawindow.h
index 46a27b6829a9..96fec957abb5 100644
--- a/src/video/cocoa/SDL_cocoawindow.h
+++ b/src/video/cocoa/SDL_cocoawindow.h
@@ -140,6 +140,7 @@ typedef enum
 @property(nonatomic) SDL_CocoaVideoData *videodata;
 @property(nonatomic) SDL_bool send_floating_size;
 @property(nonatomic) SDL_bool send_floating_position;
+@property(nonatomic) SDL_bool border_toggled;
 @property(nonatomic) BOOL checking_zoom;
 #ifdef SDL_VIDEO_OPENGL_EGL
 @property(nonatomic) EGLSurface egl_surface;
diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index 0b0c8113c8f7..d2745db04c25 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -1140,18 +1140,12 @@ - (void)windowDidChangeScreen:(NSNotification *)aNotification
 - (void)windowWillEnterFullScreen:(NSNotification *)aNotification
 {
     SDL_Window *window = _data.window;
-    NSUInteger flags = NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
+    const NSUInteger flags = NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable | NSWindowStyleMaskTitled;
 
-    /* Don't set the titled flag on a fullscreen window if the windowed-mode window
-     * is borderless, or the window can wind up in a weird, pseudo-decorated state
-     * when leaving fullscreen if the border flag was toggled on.
+    /* For some reason, the fullscreen window won't get any mouse button events
+     * without the NSWindowStyleMaskTitled flag being set when entering fullscreen,
+     * so it's needed even if the window is borderless.
      */
-    if (!(window->flags & SDL_WINDOW_BORDERLESS)) {
-        flags |= NSWindowStyleMaskTitled;
-    } else {
-        flags |= NSWindowStyleMaskBorderless;
-    }
-
     SetWindowStyle(window, flags);
 
     _data.was_zoomed = !!(window->flags & SDL_WINDOW_MAXIMIZED);
@@ -1212,6 +1206,19 @@ - (void)windowDidEnterFullScreen:(NSNotification *)aNotification
 
 - (void)windowWillExitFullScreen:(NSNotification *)aNotification
 {
+    SDL_Window *window = _data.window;
+
+    /* If the windowed mode borders were toggled on while in a fullscreen space,
+     * NSWindowStyleMaskTitled has to be cleared here, or the window can end up
+     * in a weird, semi-decorated state upon returning to windowed mode.
+     */
+    if (_data.border_toggled && !(window->flags & SDL_WINDOW_BORDERLESS)) {
+        const NSUInteger flags = NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
+
+        SetWindowStyle(window, flags);
+        _data.border_toggled = SDL_FALSE;
+    }
+
     isFullscreenSpace = NO;
     inFullscreenTransition = YES;
 }
@@ -1219,17 +1226,11 @@ - (void)windowWillExitFullScreen:(NSNotification *)aNotification
 - (void)windowDidFailToExitFullScreen:(NSNotification *)aNotification
 {
     SDL_Window *window = _data.window;
-    NSUInteger flags = NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
+    const NSUInteger flags = NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
 
     if (window->is_destroying) {
         return;
     }
-
-    if (!(window->flags & SDL_WINDOW_BORDERLESS)) {
-        flags |= NSWindowStyleMaskTitled;
-    } else {
-        flags |= NSWindowStyleMaskBorderless;
-    }
     
     SetWindowStyle(window, flags);
 
@@ -2455,6 +2456,8 @@ void Cocoa_SetWindowBordered(SDL_VideoDevice *_this, SDL_Window *window, SDL_boo
                     Cocoa_SetWindowTitle(_this, window); /* this got blanked out. */
                 }
             }
+        } else {
+            data.border_toggled = SDL_TRUE;
         }
     }
 }