From ae572a980883cc96d5594b8f679aa5471a01843d Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Date: Mon, 25 Jan 2016 15:06:32 +0100
Subject: [PATCH] Windows QPA: Clear maximized state before setting the normal
 geometry.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

A sequence of state changes fullscreen, maximized and back can leave the
window in a maximized state after setting the top level style. It needs to
be cleared before applying the normal geometry, otherwise, the window ends
up with a maximized button and normal geometry. Amends change
e3288f246b44ba2b6d90b90eb99ab61f496d8d57.

Task-number: QTBUG-49709
Change-Id: I0bb4ac1d60693e25d5ee74e763d293405636bb13
Reviewed-by: Błażej Szczygieł <spaz16@wp.pl>
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
---
 .../platforms/windows/qwindowswindow.cpp      | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp
index cac8ec5ecca..7f45b4817ac 100644
--- a/src/plugins/platforms/windows/qwindowswindow.cpp
+++ b/src/plugins/platforms/windows/qwindowswindow.cpp
@@ -224,6 +224,30 @@ static inline QRect frameGeometry(HWND hwnd, bool topLevel)
     return qrectFromRECT(rect);
 }
 
+// Return the visibility of the Window (except full screen since it is not a window state).
+static QWindow::Visibility windowVisibility_sys(HWND hwnd)
+{
+    if (!IsWindowVisible(hwnd))
+        return QWindow::Hidden;
+#ifndef Q_OS_WINCE
+    WINDOWPLACEMENT windowPlacement;
+    windowPlacement.length = sizeof(WINDOWPLACEMENT);
+    if (GetWindowPlacement(hwnd, &windowPlacement)) {
+        switch (windowPlacement.showCmd) {
+        case SW_SHOWMINIMIZED:
+        case SW_MINIMIZE:
+        case SW_FORCEMINIMIZE:
+            return QWindow::Minimized;
+        case SW_SHOWMAXIMIZED:
+            return QWindow::Maximized;
+        default:
+            break;
+        }
+    }
+#endif // !Q_OS_WINCE
+    return QWindow::Windowed;
+}
+
 static inline QSize clientSize(HWND hwnd)
 {
     RECT rect = { 0, 0, 0, 0 };
@@ -1764,6 +1788,10 @@ void QWindowsWindow::setWindowState_sys(Qt::WindowState newState)
                 swpf |= SWP_NOSIZE | SWP_NOMOVE;
             const bool wasSync = testFlag(SynchronousGeometryChangeEvent);
             setFlag(SynchronousGeometryChangeEvent);
+            // After maximized/fullscreen; the window can be in a maximized state. Clear
+            // it before applying the normal geometry.
+            if (windowVisibility_sys(m_data.hwnd) == QWindow::Maximized)
+                ShowWindow(m_data.hwnd, SW_SHOWNOACTIVATE);
             SetWindowPos(m_data.hwnd, 0, m_savedFrameGeometry.x(), m_savedFrameGeometry.y(),
                          m_savedFrameGeometry.width(), m_savedFrameGeometry.height(), swpf);
             if (!wasSync)
-- 
GitLab