SDL: Fixed always on top windows on Windows

From 08d5c1386e8eb468f0c66b5511c2d334e6ae0b22 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 17 Mar 2023 10:36:43 -0700
Subject: [PATCH] Fixed always on top windows on Windows

Also cleaned up some SetWindowPos() calls and made it explicit when we change the topmost flag
---
 src/video/windows/SDL_windowswindow.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index 98a36d72bd44..8de7ab80cc9d 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -391,7 +391,7 @@ static int SetupWindowData(_THIS, SDL_Window *window, HWND hwnd, HWND parent, SD
                 /* Figure out what the window area will be */
                 WIN_AdjustWindowRect(window, &x, &y, &w, &h, SDL_FALSE);
                 data->expected_resize = SDL_TRUE;
-                SetWindowPos(hwnd, HWND_NOTOPMOST, x, y, w, h, data->copybits_flag | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
+                SetWindowPos(hwnd, NULL, x, y, w, h, data->copybits_flag | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
                 data->expected_resize = SDL_FALSE;
             } else {
                 window->w = w;
@@ -458,6 +458,12 @@ static int SetupWindowData(_THIS, SDL_Window *window, HWND hwnd, HWND parent, SD
     }
 #endif
 
+    if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
+        WIN_SetWindowAlwaysOnTop(_this, window, SDL_TRUE);
+    } else {
+        WIN_SetWindowAlwaysOnTop(_this, window, SDL_FALSE);
+    }
+
 #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
     /* Enable multi-touch */
     if (videodata->RegisterTouchWindow) {
@@ -891,7 +897,7 @@ void WIN_ShowWindow(_THIS, SDL_Window *window)
 
     if (window->parent) {
         /* Update our position in case our parent moved while we were hidden */
-        WIN_SetWindowPositionInternal(window, window->driverdata->copybits_flag | SWP_NOACTIVATE);
+        WIN_SetWindowPosition(_this, window);
     }
 
     hwnd = window->driverdata->hwnd;
@@ -954,7 +960,9 @@ void WIN_RaiseWindow(_THIS, SDL_Window *window)
         ShowWindow(hwnd, SW_RESTORE);
         AttachThreadInput(dwCurID, dwMyID, TRUE);
         SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
-        SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
+        if (!SDL_ShouldAllowTopmost() || !(window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
+            SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
+        }
     }
     SetForegroundWindow(hwnd);
     if (bForce) {
@@ -1011,13 +1019,7 @@ void WIN_SetWindowResizable(_THIS, SDL_Window *window, SDL_bool resizable)
 
 void WIN_SetWindowAlwaysOnTop(_THIS, SDL_Window *window, SDL_bool on_top)
 {
-    SDL_WindowData *data = window->driverdata;
-    HWND hwnd = data->hwnd;
-    if (on_top) {
-        SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
-    } else {
-        SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
-    }
+    WIN_SetWindowPositionInternal(window, SWP_NOMOVE | SWP_NOSIZE);
 }
 
 void WIN_RestoreWindow(_THIS, SDL_Window *window)