SDL: video: Drop size and position requests for windows in a fixed size/position state

From eda0261c4e2fc845fadff397da1b04a2c5d2cd7f Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Fri, 13 Dec 2024 13:58:15 -0500
Subject: [PATCH] video: Drop size and position requests for windows in a fixed
 size/position state

It is not uncommon for clients to redundantly set the window size and position, either as a holdover from an SDL 1 port, when this was required, due to any window state change triggering a universal update function that sets all window state, even if unnecessary (e.g. always calling SDL_SetWindowSize(), even if the window is fullscreen), or due to the use of compatability layers. Historically, these clients expect that their behavior won't override the base window state, which is an assumption that the windowing changes in SDL 3 broke by caching size and position changes that can't be applied immediately.

This change drops size and position requests when the window is in the maximized and fullscreen states (fullscreen-desktop windows will be repositioned, but the non-fullscreen floating position will not be overwritten), which is behavior more in line with existing client assumptions, and should ease the porting process, as well as prevent annoying bugs when older software is run via sdl2-compat.

In the process of making these changes, pending window state has been moved to separate variables in the SDL_Window struct, as this fixes bugs regarding fullscreen display selection and centering windows immediately after resize on asynchronous platforms, which had issues due to pending state possibly being overwritten.
---
 include/SDL3/SDL_video.h                   |  24 ++--
 src/events/SDL_windowevents.c              |   3 +-
 src/video/SDL_sysvideo.h                   |   6 +-
 src/video/SDL_video.c                      |  37 ++---
 src/video/cocoa/SDL_cocoawindow.h          |   9 +-
 src/video/cocoa/SDL_cocoawindow.m          | 150 +++++++++------------
 src/video/dummy/SDL_nullvideo.c            |   4 +-
 src/video/emscripten/SDL_emscriptenvideo.c |   6 +-
 src/video/haiku/SDL_bwindow.cc             |   8 +-
 src/video/offscreen/SDL_offscreenwindow.c  |   2 +-
 src/video/openvr/SDL_openvrvideo.c         |   8 +-
 src/video/qnx/SDL_qnxvideo.c               |   4 +-
 src/video/wayland/SDL_waylandwindow.c      | 109 ++++++++-------
 src/video/wayland/SDL_waylandwindow.h      |   3 +-
 src/video/windows/SDL_windowsevents.c      |  26 ----
 src/video/windows/SDL_windowswindow.c      |  16 ++-
 src/video/windows/SDL_windowswindow.h      |   4 +-
 src/video/x11/SDL_x11events.c              |  55 +++++---
 src/video/x11/SDL_x11window.c              |  99 +++++++++-----
 src/video/x11/SDL_x11window.h              |   2 +
 test/testautomation_video.c                |  57 +++++++-
 21 files changed, 354 insertions(+), 278 deletions(-)

diff --git a/include/SDL3/SDL_video.h b/include/SDL3/SDL_video.h
index fe68a582741c2..a565af703d675 100644
--- a/include/SDL3/SDL_video.h
+++ b/include/SDL3/SDL_video.h
@@ -1529,13 +1529,12 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetWindowIcon(SDL_Window *window, SDL_Surfa
 /**
  * Request that the window's position be set.
  *
- * If, at the time of this request, the window is in a fixed-size state such
- * as maximized, this request may be deferred until the window returns to a
- * resizable state.
+ * If the window is in an exclusive fullscreen or maximized state, this request
+ * has no effect.
  *
  * This can be used to reposition fullscreen-desktop windows onto a different
- * display, however, exclusive fullscreen windows are locked to a specific
- * display and can only be repositioned programmatically via
+ * display, however, as exclusive fullscreen windows are locked to a specific
+ * display, they can only be repositioned programmatically via
  * SDL_SetWindowFullscreenMode().
  *
  * On some windowing systems this request is asynchronous and the new
@@ -1596,12 +1595,11 @@ extern SDL_DECLSPEC bool SDLCALL SDL_GetWindowPosition(SDL_Window *window, int *
 /**
  * Request that the size of a window's client area be set.
  *
- * If, at the time of this request, the window in a fixed-size state, such as
- * maximized or fullscreen, the request will be deferred until the window
- * exits this state and becomes resizable again.
+ * If the window is in a fullscreen or maximized state, this request has no
+ * effect.
  *
- * To change the fullscreen mode of a window, use
- * SDL_SetWindowFullscreenMode()
+ * To change the exclusive fullscreen mode of a window, use
+ * SDL_SetWindowFullscreenMode().
  *
  * On some windowing systems, this request is asynchronous and the new window
  * size may not have have been applied immediately upon the return of this
@@ -2021,6 +2019,9 @@ extern SDL_DECLSPEC bool SDLCALL SDL_MaximizeWindow(SDL_Window *window);
 /**
  * Request that the window be minimized to an iconic representation.
  *
+ * If the window is in a fullscreen state, this request has no direct effect.
+ * It may alter the state the window is returned to when leaving fullscreen.
+ *
  * On some windowing systems this request is asynchronous and the new window
  * state may not have been applied immediately upon the return of this
  * function. If an immediate change is required, call SDL_SyncWindow() to
@@ -2048,6 +2049,9 @@ extern SDL_DECLSPEC bool SDLCALL SDL_MinimizeWindow(SDL_Window *window);
  * Request that the size and position of a minimized or maximized window be
  * restored.
  *
+ * If the window is in a fullscreen state, this request has no direct effect.
+ * It may alter the state the window is returned to when leaving fullscreen.
+ *
  * On some windowing systems this request is asynchronous and the new window
  * state may not have have been applied immediately upon the return of this
  * function. If an immediate change is required, call SDL_SyncWindow() to
diff --git a/src/events/SDL_windowevents.c b/src/events/SDL_windowevents.c
index b31f3edd0d7c8..5535ec5059f9b 100644
--- a/src/events/SDL_windowevents.c
+++ b/src/events/SDL_windowevents.c
@@ -69,7 +69,7 @@ bool SDL_SendWindowEvent(SDL_Window *window, SDL_EventType windowevent, int data
     case SDL_EVENT_WINDOW_MOVED:
         window->undefined_x = false;
         window->undefined_y = false;
-        window->use_pending_position_for_fullscreen = false;
+        window->last_position_pending = false;
         if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
             window->windowed.x = data1;
             window->windowed.y = data2;
@@ -86,6 +86,7 @@ bool SDL_SendWindowEvent(SDL_Window *window, SDL_EventType windowevent, int data
         window->y = data2;
         break;
     case SDL_EVENT_WINDOW_RESIZED:
+        window->last_size_pending = false;
         if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
             window->windowed.w = data1;
             window->windowed.h = data2;
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index cd63a18d2e90d..51d991f1dfdb0 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -78,6 +78,9 @@ struct SDL_Window
      */
     SDL_Rect floating;
 
+    // The last client requested size and position for the window.
+    SDL_Rect pending;
+
     /* Toggle for drivers to indicate that the current window state is tiled,
      * and sizes set non-programmatically shouldn't be cached.
      */
@@ -98,7 +101,8 @@ struct SDL_Window
 
     bool is_hiding;
     bool restore_on_show; // Child was hidden recursively by the parent, restore when shown.
-    bool use_pending_position_for_fullscreen;
+    bool last_position_pending; // This should NOT be cleared by the backend, as it is used for fullscreen positioning.
+    bool last_size_pending; // This should be cleared by the backend if the new size cannot be applied.
     bool is_destroying;
     bool is_dropping; // drag/drop in progress, expecting SDL_SendDropComplete().
 
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 9760a4426a1e6..8aebf769b1c3d 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -1664,14 +1664,13 @@ SDL_VideoDisplay *SDL_GetVideoDisplayForFullscreenWindow(SDL_Window *window)
      * the current position won't be updated at the time of the fullscreen call.
      */
     if (!displayID) {
-        if (window->use_pending_position_for_fullscreen) {
-            // The last coordinates were client requested; use the pending floating coordinates.
-            displayID = GetDisplayForRect(window->floating.x, window->floating.y, window->floating.w, window->floating.h);
-        }
-        else {
-            // The last coordinates were from the window manager; use the current position.
-            displayID = GetDisplayForRect(window->x, window->y, 1, 1);
-        }
+        // Use the pending position and dimensions, if available, otherwise, use the current.
+        const int x = window->last_position_pending ? window->pending.x : window->x;
+        const int y = window->last_position_pending ? window->pending.y : window->y;
+        const int w = window->last_size_pending ? window->pending.w : window->w;
+        const int h = window->last_size_pending ? window->pending.h : window->h;
+
+        displayID = GetDisplayForRect(x, y, w, h);
     }
     if (!displayID) {
         // Use the primary display for a window if we can't find it anywhere else
@@ -2763,6 +2762,9 @@ bool SDL_SetWindowPosition(SDL_Window *window, int x, int y)
 
     CHECK_WINDOW_MAGIC(window, false);
 
+    const int w = window->last_size_pending ? window->pending.w : window->windowed.w;
+    const int h = window->last_size_pending ? window->pending.h : window->windowed.h;
+
     original_displayID = SDL_GetDisplayForWindow(window);
 
     if (SDL_WINDOWPOS_ISUNDEFINED(x)) {
@@ -2785,26 +2787,24 @@ bool SDL_SetWindowPosition(SDL_Window *window, int x, int y)
         }
 
         SDL_zero(bounds);
-        if (!SDL_GetDisplayUsableBounds(displayID, &bounds) ||
-            window->windowed.w > bounds.w ||
-            window->windowed.h > bounds.h) {
+        if (!SDL_GetDisplayUsableBounds(displayID, &bounds) || w > bounds.w || h > bounds.h) {
             if (!SDL_GetDisplayBounds(displayID, &bounds)) {
                 return false;
             }
         }
         if (SDL_WINDOWPOS_ISCENTERED(x)) {
-            x = bounds.x + (bounds.w - window->windowed.w) / 2;
+            x = bounds.x + (bounds.w - w) / 2;
         }
         if (SDL_WINDOWPOS_ISCENTERED(y)) {
-            y = bounds.y + (bounds.h - window->windowed.h) / 2;
+            y = bounds.y + (bounds.h - h) / 2;
         }
     }
 
-    window->floating.x = x;
-    window->floating.y = y;
+    window->pending.x = x;
+    window->pending.y = y;
     window->undefined_x = false;
     window->undefined_y = false;
-    window->use_pending_position_for_fullscreen = true;
+    window->last_position_pending = true;
 
     if (_this->SetWindowPosition) {
         const bool result = _this->SetWindowPosition(_this, window);
@@ -2952,8 +2952,9 @@ bool SDL_SetWindowSize(SDL_Window *window, int w, int h)
         h = window->max_h;
     }
 
-    window->floating.w = w;
-    window->floating.h = h;
+    window->last_size_pending = true;
+    window->pending.w = w;
+    window->pending.h = h;
 
     if (_this->SetWindowSize) {
         _this->SetWindowSize(_this, window);
diff --git a/src/video/cocoa/SDL_cocoawindow.h b/src/video/cocoa/SDL_cocoawindow.h
index dd30c200a8134..9add0c0e5866d 100644
--- a/src/video/cocoa/SDL_cocoawindow.h
+++ b/src/video/cocoa/SDL_cocoawindow.h
@@ -38,7 +38,8 @@ typedef enum
     PENDING_OPERATION_NONE = 0x00,
     PENDING_OPERATION_ENTER_FULLSCREEN = 0x01,
     PENDING_OPERATION_LEAVE_FULLSCREEN = 0x02,
-    PENDING_OPERATION_MINIMIZE = 0x04
+    PENDING_OPERATION_MINIMIZE = 0x04,
+    PENDING_OPERATION_ZOOM = 0x08
 } PendingWindowOperation;
 
 @interface SDL3Cocoa_WindowListener : NSResponder <NSWindowDelegate>
@@ -144,10 +145,10 @@ typedef enum
 @property(nonatomic) SDL3Cocoa_WindowListener *listener;
 @property(nonatomic) NSModalSession modal_session;
 @property(nonatomic) SDL_CocoaVideoData *videodata;
-@property(nonatomic) bool send_floating_size;
-@property(nonatomic) bool send_floating_position;
+@property(nonatomic) bool pending_size;
+@property(nonatomic) bool pending_position;
 @property(nonatomic) bool border_toggled;
-@property(nonatomic) BOOL checking_zoom;
+
 #ifdef SDL_VIDEO_OPENGL_EGL
 @property(nonatomic) EGLSurface egl_surface;
 #endif
diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index f2238f015c713..0401a04532859 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -666,17 +666,6 @@ static void Cocoa_WaitForMiniaturizable(SDL_Window *window)
     }
 }
 
-static bool Cocoa_IsZoomed(SDL_Window *window)
-{
-    SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
-
-    data.checking_zoom = YES;
-    const bool ret = [data.nswindow isZoomed];
-    data.checking_zoom = NO;
-
-    return ret;
-}
-
 static NSCursor *Cocoa_GetDesiredCursor(void)
 {
     SDL_Mouse *mouse = SDL_GetMouse();
@@ -1043,27 +1032,6 @@ - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
 {
     SDL_Window *window = _data.window;
 
-    /* XXX: Calling [isZoomed] calls this function, and calling [isZoomed]
-     *      from within this function will recurse until the stack overflows,
-     *      so a recursion guard is required.
-     */
-    if (!_data.checking_zoom) {
-        _data.checking_zoom = YES;
-        if ([_data.nswindow isZoomed] && !_data.was_zoomed && _data.send_floating_size) {
-            NSRect rect;
-
-            _data.send_floating_size = NO;
-            rect.origin.x = window->floating.x;
-            rect.origin.y = window->floating.y;
-            rect.size.width = window->floating.w;
-            rect.size.height = window->floating.h;
-            ConvertNSRect(&rect);
-
-            frameSize = rect.size;
-        }
-        _data.checking_zoom = NO;
-    }
-
     if (window->min_aspect > 0.0f || window->max_aspect > 0.0f) {
         NSWindow *nswindow = _data.nswindow;
         NSRect newContentRect = [nswindow contentRectForFrameRect:NSMakeRect(0, 0, frameSize.width, frameSize.height)];
@@ -1119,7 +1087,7 @@ - (void)windowDidResize:(NSNotification *)aNotification
     /* isZoomed always returns true if the window is not resizable
      * and fullscreen windows are considered zoomed.
      */
-    if ((window->flags & SDL_WINDOW_RESIZABLE) && Cocoa_IsZoomed(window) &&
+    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed] &&
         !(window->flags & SDL_WINDOW_FULLSCREEN) && ![self isInFullscreenSpace]) {
         zoomed = YES;
     } else {
@@ -1164,7 +1132,7 @@ - (void)windowDidDeminiaturize:(NSNotification *)aNotification
     SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
 
     // isZoomed always returns true if the window is not resizable.
-    if ((_data.window->flags & SDL_WINDOW_RESIZABLE) && Cocoa_IsZoomed(_data.window)) {
+    if ((_data.window->flags & SDL_WINDOW_RESIZABLE) && [_data.nswindow isZoomed]) {
         SDL_SendWindowEvent(_data.window, SDL_EVENT_WINDOW_MAXIMIZED, 0, 0);
     }
 
@@ -1324,6 +1292,9 @@ - (void)windowDidEnterFullScreen:(NSNotification *)aNotification
         }
         SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_ENTER_FULLSCREEN, 0, 0);
 
+        _data.pending_position = NO;
+        _data.pending_size = NO;
+
         /* Force the size change event in case it was delivered earlier
            while the window was still animating into place.
          */
@@ -1362,6 +1333,11 @@ - (void)windowDidFailToExitFullScreen:(NSNotification *)aNotification
         return;
     }
 
+    _data.pending_position = NO;
+    _data.pending_size = NO;
+    window->last_position_pending = false;
+    window->last_size_pending = false;
+
     SetWindowStyle(window, flags);
 
     isFullscreenSpace = YES;
@@ -1425,18 +1401,32 @@ when returning to windowed mode from a space (instead of using a pending
         }
         [NSMenu setMenuBarVisible:YES];
 
-        // Restore windowed size and position in case it changed while fullscreen
-        NSRect rect;
-        rect.origin.x = _data.was_zoomed ? window->windowed.x : window->floating.x;
-        rect.origin.y = _data.was_zoomed ? window->windowed.y : window->floating.y;
-        rect.size.width = _data.was_zoomed ? window->windowed.w : window->floating.w;
-        rect.size.height = _data.was_zoomed ? window->windowed.h : window->floating.h;
-        ConvertNSRect(&rect);
-
-        _data.send_floating_position = NO;
-        _data.send_floating_size = NO;
-        [nswindow setContentSize:rect.size];
-        [nswindow setFrameOrigin:rect.origin];
+        // Toggle zoom, if changed while fullscreen.
+        if ([self windowOperationIsPending:PENDING_OPERATION_ZOOM]) {
+            [self clearPendingWindowOperation:PENDING_OPERATION_ZOOM];
+            [nswindow zoom:nil];
+        }
+
+        if (![nswindow isZoomed]) {
+            // Apply a pending window size, if not zoomed.
+            NSRect rect;
+            rect.origin.x = _data.pending_position ? window->pending.x : window->floating.x;
+            rect.origin.y = _data.pending_position ? window->pending.y : window->floating.y;
+            rect.size.width = _data.pending_size ? window->pending.w : window->floating.w;
+            rect.size.height = _data.pending_size ? window->pending.h : window->floating.h;
+            ConvertNSRect(&rect);
+
+            if (_data.pending_size) {
+                [nswindow setContentSize:rect.size];
+            }
+            if (_data.pending_position) {
+                [nswindow setFrameOrigin:rect.origin];
+            }
+        }
+
+        _data.pending_size = NO;
+        _data.pending_position = NO;
+        _data.was_zoomed = NO;
 
         /* Force the size change event in case it was delivered earlier
          * while the window was still animating into place.
@@ -1446,8 +1436,6 @@ when returning to windowed mode from a space (instead of using a pending
         [self windowDidMove:aNotification];
         [self windowDidResize:aNotification];
 
-        _data.was_zoomed = false;
-
         // FIXME: Why does the window get hidden?
         if (!(window->flags & SDL_WINDOW_HIDDEN)) {
             Cocoa_ShowWindow(SDL_GetVideoDevice(), window);
@@ -2083,7 +2071,7 @@ static bool SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, NSWindow
         }
 
         // isZoomed always returns true if the window is not resizable
-        if ((window->flags & SDL_WINDOW_RESIZABLE) && Cocoa_IsZoomed(window)) {
+        if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
             window->flags |= SDL_WINDOW_MAXIMIZED;
         } else {
             window->flags &= ~SDL_WINDOW_MAXIMIZED;
@@ -2352,9 +2340,9 @@ bool Cocoa_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
         BOOL fullscreen = (window->flags & SDL_WINDOW_FULLSCREEN) ? YES : NO;
         int x, y;
 
-        if ([windata.listener windowOperationIsPending:(PENDING_OPERATION_ENTER_FULLSCREEN | PENDING_OPERATION_LEAVE_FULLSCREEN)] ||
-            [windata.listener isInFullscreenSpaceTransition]) {
-            Cocoa_SyncWindow(_this, window);
+        if ([windata.listener isInFullscreenSpaceTransition]) {
+            windata.pending_position = YES;
+            return true;
         }
 
         if (!(window->flags & SDL_WINDOW_MAXIMIZED)) {
@@ -2366,7 +2354,7 @@ bool Cocoa_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
                 rect.origin.x = r.x;
                 rect.origin.y = r.y;
             } else {
-                SDL_RelativeToGlobalForWindow(window, window->floating.x, window->floating.y, &x, &y);
+                SDL_RelativeToGlobalForWindow(window, window->pending.x, window->pending.y, &x, &y);
                 rect.origin.x = x;
                 rect.origin.y = y;
             }
@@ -2389,8 +2377,6 @@ bool Cocoa_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
             [nswindow setFrameOrigin:rect.origin];
 
             ScheduleContextUpdates(windata);
-        } else {
-            windata.send_floating_position = true;
         }
     }
     return true;
@@ -2402,13 +2388,13 @@ void Cocoa_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
         SDL_CocoaWindowData *windata = (__bridge SDL_CocoaWindowData *)window->internal;
         NSWindow *nswindow = windata.nswindow;
 
-        if ([windata.listener windowOperationIsPending:(PENDING_OPERATION_ENTER_FULLSCREEN | PENDING_OPERATION_LEAVE_FULLSCREEN)] ||
-            [windata.listener isInFullscreenSpaceTransition]) {
-            Cocoa_SyncWindow(_this, window);
+        if ([windata.listener isInFullscreenSpaceTransition]) {
+            windata.pending_size = YES;
+            return;
         }
 
         // isZoomed always returns true if the window is not resizable
-        if (!(window->flags & SDL_WINDOW_RESIZABLE) || !Cocoa_IsZoomed(window)) {
+        if (!(window->flags & SDL_WINDOW_RESIZABLE) || ![nswindow isZoomed]) {
             if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
                 int x, y;
                 NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
@@ -2420,17 +2406,19 @@ void Cocoa_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
                 SDL_RelativeToGlobalForWindow(window, window->floating.x, window->floating.y, &x, &y);
                 rect.origin.x = x;
                 rect.origin.y = y;
-                rect.size.width = window->floating.w;
-                rect.size.height = window->floating.h;
+                rect.size.width = window->pending.w;
+                rect.size.height = window->pending.h;
                 ConvertNSRect(&rect);
 
                 [nswindow setFrame:[nswindow frameRectForContentRect:rect] display:YES];
                 ScheduleContextUpdates(windata);
-            } else if (windata.was_zoomed) {
-                windata.send_floating_size = true;
+            } else {
+                // Can't set the window size.
+                window->last_size_pending = false;
             }
-        } else {
-            windata.send_floating_size = true;
+        }  else {
+            // Can't set the window size.
+            window->last_size_pending = false;
         }
     }
 }
@@ -2614,6 +2602,10 @@ void Cocoa_MaximizeWindow(SDL_VideoDevice *_this, SDL_Window *window)
             ![windata.listener isInFullscreenSpace]) {
             [nswindow zoom:nil];
             ScheduleContextUpdates(windata);
+        } else if (!windata.was_zoomed) {
+            [windata.listener addPendingWindowOperation:PENDING_OPERATION_ZOOM];
+        } else {
+            [windata.listener clearPendingWindowOperation:PENDING_OPERATION_ZOOM];
         }
     }
 }
@@ -2654,27 +2646,13 @@ void Cocoa_RestoreWindow(SDL_VideoDevice *_this, SDL_Window *window)
             ![data.listener isInFullscreenSpace]) {
             if ([nswindow isMiniaturized]) {
                 [nswindow deminiaturize:nil];
-            } else if ((window->flags & SDL_WINDOW_RESIZABLE) && Cocoa_IsZoomed(window)) {
-                NSRect rect;
-
-                // Update the floating coordinates
-                rect.origin.x = window->floating.x;
-                rect.origin.y = window->floating.y;
-
-                // The floating size will be set in windowWillResize
+            } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [data.nswindow isZoomed]) {
                 [nswindow zoom:nil];
-
-                rect.size.width = window->floating.w;
-                rect.size.height = window->floating.h;
-
-                ConvertNSRect(&rect);
-
-                if (data.send_floating_position) {
-                    data.send_floating_position = false;
-                    [nswindow setFrameOrigin:rect.origin];
-                    ScheduleContextUpdates(data);
-                }
             }
+        } else if (data.was_zoomed) {
+            [data.listener addPendingWindowOperation:PENDING_OPERATION_ZOOM];
+        } else {
+            [data.listener clearPendingWindowOperation:PENDING_OPERATION_ZOOM];
         }
     }
 }
@@ -2818,6 +2796,10 @@ SDL_FullscreenResult Cocoa_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Windo
         if (!fullscreen) {
             Cocoa_SetWindowTitle(_this, window);
             data.was_zoomed = NO;
+            if ([data.listener windowOperationIsPending:PENDING_OPERATION_ZOOM]) {
+                [data.listener clearPendingWindowOperation:PENDING_OPERATION_ZOOM];
+                [nswindow zoom:nil];
+            }
         }
 
         if (SDL_ShouldAllowTopmost() && fullscreen) {
diff --git a/src/video/dummy/SDL_nullvideo.c b/src/video/dummy/SDL_nullvideo.c
index 98b485bedfe98..295176c6af25b 100644
--- a/src/video/dummy/SDL_nullvideo.c
+++ b/src/video/dummy/SDL_nullvideo.c
@@ -57,13 +57,13 @@ static void DUMMY_VideoQuit(SDL_VideoDevice *_this);
 
 static bool DUMMY_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
 {
-    SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, window->floating.x, window->floating.y);
+    SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, window->pending.x, window->pending.y);
     return true;
 }
 
 static void DUMMY_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
 {
-    SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->floating.w, window->floating.h);
+    SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->pending.w, window->pending.h);
 }
 
 // DUMMY driver bootstrap functions
diff --git a/src/video/emscripten/SDL_emscriptenvideo.c b/src/video/emscripten/SDL_emscriptenvideo.c
index d7515bb2f08f2..45a6e9a1de47c 100644
--- a/src/video/emscripten/SDL_emscriptenvideo.c
+++ b/src/video/emscripten/SDL_emscriptenvideo.c
@@ -327,14 +327,14 @@ static void Emscripten_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
         if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
             data->pixel_ratio = emscripten_get_device_pixel_ratio();
         }
-        emscripten_set_canvas_element_size(data->canvas_id, SDL_lroundf(window->floating.w * data->pixel_ratio), SDL_lroundf(window->floating.h * data->pixel_ratio));
+        emscripten_set_canvas_element_size(data->canvas_id, SDL_lroundf(window->pending.w * data->pixel_ratio), SDL_lroundf(window->pending.h * data->pixel_ratio));
 
         // scale canvas down
         if (!data->external_size && data->pixel_ratio != 1.0f) {
-            emscripten_set_element_css_size(data->canvas_id, window->floating.w, window->floating.h);
+            emscripten_set_element_css_size(data->canvas_id, window->pending.w, window->pending.h);
         }
 
-        SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->floating.w, window->floating.h);
+        SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->pending.w, window->pending.h);
     }
 }
 
diff --git a/src/video/haiku/SDL_bwindow.cc b/src/video/haiku/SDL_bwindow.cc
index de9bc2484d696..902676a53a6c1 100644
--- a/src/video/haiku/SDL_bwindow.cc
+++ b/src/video/haiku/SDL_bwindow.cc
@@ -100,8 +100,8 @@ void HAIKU_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window * window)
 bool HAIKU_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window * window)
 {
     BMessage msg(BWIN_MOVE_WINDOW);
-    msg.AddInt32("window-x", window->floating.x);
-    msg.AddInt32("window-y", window->floating.y);
+    msg.AddInt32("window-x", window->pending.x);
+    msg.AddInt32("window-y", window->pending.y);
     _ToBeWin(window)->PostMessage(&msg);
     return true;
 }
@@ -109,8 +109,8 @@ bool HAIKU_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window * window)
 void HAIKU_SetWindowSize(SDL_VideoDevice *_this, SDL_Window * window)
 {
     BMessage msg(BWIN_RESIZE_WINDOW);
-    msg.AddInt32("window-w", window->floating.w - 1);
-    msg.AddInt32("window-h", window->floating.h - 1);
+    msg.AddInt32("window-w", window->pending.w - 1);
+    msg.AddInt32("window-h", window->pending.h - 1);
     _ToBeWin(window)->PostMessage(&msg);
 }
 
diff --git a/src/video/offscreen/SDL_offscreenwindow.c b/src/video/offscreen/SDL_offscreenwindow.c
index c76028e45df0b..9abc85cff909d 100644
--- a/src/video/offscreen/SDL_offscreenwindow.c
+++ b/src/video/offscreen/SDL_offscreenwindow.c
@@ -85,6 +85,6 @@ void OFFSCREEN_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
 
 void OFFSCREEN_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
 {
-    SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->floating.w, window->floating.h);
+    SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->pending.w, window->pending.h);
 }
 #endif // SDL_VIDEO_DRIVER_OFFSCREEN
diff --git a/src/video/openvr/SDL_openvrvideo.c b/src/video/openvr/SDL_openvrvideo.c
index eafd71ac6d40d..51c10dfbafbea 100644
--- a/src/video/openvr/SDL_openvrvideo.c
+++ b/src/video/openvr/SDL_openvrvideo.c
@@ -1138,12 +1138,12 @@ static void OPENVR_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
 {
     SDL_VideoData *data = (SDL_VideoData *)_this->internal;
 
-    if (window->floating.w != window->w) {
-        window->w = window->floating.w;
+    if (window->pending.w != window->w) {
+        window->w = window->pending.w;
     }
 
-    if (window->floating.h != window->h) {
-        window->h = window->floating.h;
+    if (window->pending.h != window->h) {
+        window->h = window->pending.h;
     }
 
     if (data->targh != window->h || data->targw != window->w) {
diff --git a/src/video/qnx/SDL_qnxvideo.c b/src/video/qnx/SDL_qnxvideo.c
index 6e7d5dc80de69..db231439289b2 100644
--- a/src/video/qnx/SDL_qnxvideo.c
+++ b/src/video/qnx/SDL_qnxvideo.c
@@ -246,8 +246,8 @@ static void setWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
     window_impl_t   *impl = (window_impl_t *)window->internal;
     int             size[2];
 
-    size[0] = window->floating.w;
-    size[1] = window->floating.h;
+    size[0] = window->pending.w;
+    size[1] = window->pending.h;
 
     screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_SIZE, size);
     screen_set_window_property_iv(impl->window, SCREEN_PROPERTY_SOURCE_SIZE, size);
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index 0fa54dcae82e8..31aa805cc6e0e 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -264,8 +264,8 @@ static void RepositionPopup(SDL_Window *window, bool use_current_position)
     if (wind->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_POPUP &&
         wind->shell_surface.xdg.popup.xdg_positioner &&
         xdg_popup_get_version(wind->shell_surface.xdg.popup.xdg_popup) >= XDG_POPUP_REPOSITION_SINCE_VERSION) {
-        int x = use_current_position ? window->x : window->floating.x;
-        int y = use_current_position ? window->y : window->floating.y;
+        int x = use_current_position ? window->x : window->pending.x;
+        int y = use_current_position ? window->y : window->pending.y;
 
         EnsurePopupPositionIsValid(window, &x, &y);
         if (wind->scale_to_display) {
@@ -499,28 +499,35 @@ static struct wl_callback_listener fullscreen_deadline_listener = {
     fullscreen_deadline_handler
 };
 
-static void maximized_deadline_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
+static void maximized_restored_deadline_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
 {
     // Get the window from the ID as it may have been destroyed
     SDL_WindowID windowID = (SDL_WindowID)((uintptr_t)data);
     

(Patch may be truncated, please check the link at the top of this post.)