SDL: Fixed the size and position of minimized windows on Windows

From a1a4948fdae38b8e9dc1e8091d5e56de4466e994 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 26 Jan 2024 22:36:32 -0800
Subject: [PATCH] Fixed the size and position of minimized windows on Windows

---
 src/video/windows/SDL_windowswindow.c | 82 ++++++++++++++-------------
 1 file changed, 44 insertions(+), 38 deletions(-)

diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index 7b30ca7d7da6..789411bcba13 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -387,10 +387,44 @@ static int SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, HWND hwnd
     }
 #endif
 
-    /* Fill in the SDL window with the window data */
+    /* Fill in the SDL window with the window state */
     {
+        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
+        if (style & WS_VISIBLE) {
+            window->flags &= ~SDL_WINDOW_HIDDEN;
+        } else {
+            window->flags |= SDL_WINDOW_HIDDEN;
+        }
+        if (style & WS_POPUP) {
+            window->flags |= SDL_WINDOW_BORDERLESS;
+        } else {
+            window->flags &= ~SDL_WINDOW_BORDERLESS;
+        }
+        if (style & WS_THICKFRAME) {
+            window->flags |= SDL_WINDOW_RESIZABLE;
+        } else {
+            window->flags &= ~SDL_WINDOW_RESIZABLE;
+        }
+#ifdef WS_MAXIMIZE
+        if (style & WS_MAXIMIZE) {
+            window->flags |= SDL_WINDOW_MAXIMIZED;
+        } else
+#endif
+        {
+            window->flags &= ~SDL_WINDOW_MAXIMIZED;
+        }
+#ifdef WS_MINIMIZE
+        if (style & WS_MINIMIZE) {
+            window->flags |= SDL_WINDOW_MINIMIZED;
+        } else
+#endif
+        {
+            window->flags &= ~SDL_WINDOW_MINIMIZED;
+        }
+    }
+    if (!(window->flags & SDL_WINDOW_MINIMIZED)) {
         RECT rect;
-        if (GetClientRect(hwnd, &rect)) {
+        if (GetClientRect(hwnd, &rect) && !IsRectEmpty(&rect)) {
             int w = rect.right;
             int h = rect.bottom;
 
@@ -412,7 +446,7 @@ static int SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, HWND hwnd
         }
     }
 #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
-    {
+    if (!(window->flags & SDL_WINDOW_MINIMIZED)) {
         POINT point;
         point.x = 0;
         point.y = 0;
@@ -425,42 +459,10 @@ static int SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, HWND hwnd
             window->y = point.y;
         }
     }
+
     WIN_UpdateWindowICCProfile(window, SDL_FALSE);
 #endif
-    {
-        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
-        if (style & WS_VISIBLE) {
-            window->flags &= ~SDL_WINDOW_HIDDEN;
-        } else {
-            window->flags |= SDL_WINDOW_HIDDEN;
-        }
-        if (style & WS_POPUP) {
-            window->flags |= SDL_WINDOW_BORDERLESS;
-        } else {
-            window->flags &= ~SDL_WINDOW_BORDERLESS;
-        }
-        if (style & WS_THICKFRAME) {
-            window->flags |= SDL_WINDOW_RESIZABLE;
-        } else {
-            window->flags &= ~SDL_WINDOW_RESIZABLE;
-        }
-#ifdef WS_MAXIMIZE
-        if (style & WS_MAXIMIZE) {
-            window->flags |= SDL_WINDOW_MAXIMIZED;
-        } else
-#endif
-        {
-            window->flags &= ~SDL_WINDOW_MAXIMIZED;
-        }
-#ifdef WS_MINIMIZE
-        if (style & WS_MINIMIZE) {
-            window->flags |= SDL_WINDOW_MINIMIZED;
-        } else
-#endif
-        {
-            window->flags &= ~SDL_WINDOW_MINIMIZED;
-        }
-    }
+
 #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
     window->flags |= SDL_WINDOW_INPUT_FOCUS;
 #else
@@ -930,9 +932,13 @@ void WIN_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *
     if (GetClientRect(hwnd, &rect) && !WIN_IsRectEmpty(&rect)) {
         *w = rect.right;
         *h = rect.bottom;
-    } else {
+    } else if (window->last_pixel_w && window->last_pixel_h) {
         *w = window->last_pixel_w;
         *h = window->last_pixel_h;
+    } else {
+        /* Probably created minimized, use the restored size */
+        *w = window->floating.w;
+        *h = window->floating.h;
     }
 }