From 9286c8b9c23a613f96636b8e2d1391d89cedd199 Mon Sep 17 00:00:00 2001
From: Zhao Yuhang <2546789017@qq.com>
Date: 周六, 07 12月 2024 21:11:05 +0800
Subject: [PATCH] Win32 code cleanup & improve

---
 src/core/contexts/win32windowcontext.cpp |   42 ++++++++++++++++++++----------------------
 1 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 9e4155f..3c7ba80 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -343,7 +343,6 @@
             case HTBORDER:
                 return Win32WindowContext::FixedBorder;
             default:
-                Q_UNREACHABLE();
                 break;
         }
         return Win32WindowContext::Outside;
@@ -828,6 +827,10 @@
             return isSystemBorderEnabled() && !isWin11OrGreater();
         }
 
+        if (key == QStringLiteral("windows-system-border-enabled")) {
+            return isSystemBorderEnabled();
+        }
+
         if (key == QStringLiteral("border-thickness")) {
             return m_windowId
                        ? int(getWindowFrameBorderThickness(reinterpret_cast<HWND>(m_windowId)))
@@ -835,9 +838,7 @@
         }
 
         if (key == QStringLiteral("title-bar-height")) {
-            return m_windowId
-                       ? int(getTitleBarHeight(reinterpret_cast<HWND>(m_windowId)))
-                       : 0;
+            return m_windowId ? int(getTitleBarHeight(reinterpret_cast<HWND>(m_windowId))) : 0;
         }
         return AbstractWindowContext::windowAttribute(key);
     }
@@ -848,16 +849,10 @@
         lastHitTestResult = WindowPart::Outside;
         lastHitTestResultRaw = HTNOWHERE;
 
-        if (!isSystemBorderEnabled()) {
-            m_delegate->setWindowFlags(m_host, m_delegate->getWindowFlags(m_host) |
-                                                   Qt::FramelessWindowHint);
-        }
-
         // If the original window id is valid, remove all resources related
         if (oldWinId) {
             removeManagedWindow(reinterpret_cast<HWND>(oldWinId));
         }
-
         if (!winId) {
             return;
         }
@@ -953,7 +948,7 @@
 
         if (key == QStringLiteral("extra-margins")) {
             auto margins = qmargins2margins(attribute.value<QMargins>());
-            return apis.pDwmExtendFrameIntoClientArea(hwnd, &margins) == S_OK;
+            return SUCCEEDED(apis.pDwmExtendFrameIntoClientArea(hwnd, &margins));
         }
 
         if (key == QStringLiteral("dark-mode")) {
@@ -967,12 +962,8 @@
             } else {
                 apis.pAllowDarkModeForApp(enable);
             }
-            for (const auto attr : {
-                     _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1,
-                     _DWMWA_USE_IMMERSIVE_DARK_MODE,
-                 }) {
-                apis.pDwmSetWindowAttribute(hwnd, attr, &enable, sizeof(enable));
-            }
+            const auto attr = isWin1020H1OrGreater() ? _DWMWA_USE_IMMERSIVE_DARK_MODE : _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
+            apis.pDwmSetWindowAttribute(hwnd, attr, &enable, sizeof(enable));
 
             apis.pFlushMenuThemes();
             return true;
@@ -1696,7 +1687,8 @@
                         // outside the window, that is, the three transparent window resize area.
                         // Returning HTCLIENT will confuse Windows, we can't put our controls there
                         // anyway.
-                        *result = HTNOWHERE; // Make sure we can know we don't set any value explicitly later.
+                        *result = HTNOWHERE; // Make sure we can know we don't set any value
+                                             // explicitly later.
                         if (originalHitTestResult == HTCAPTION) {
                         } else if (isFixedSize || dontOverrideCursor) {
                             *result = HTBORDER;
@@ -1725,11 +1717,13 @@
                                 } else {
                                     *result = HTLEFT;
                                 }
-                            } else if (originalHitTestResult == HTLEFT || originalHitTestResult == HTRIGHT) {
+                            } else if (originalHitTestResult == HTLEFT ||
+                                       originalHitTestResult == HTRIGHT) {
                                 if (isFixedWidth) {
                                     *result = HTBORDER;
                                 }
-                            } else if (originalHitTestResult == HTTOP || originalHitTestResult == HTBOTTOM) {
+                            } else if (originalHitTestResult == HTTOP ||
+                                       originalHitTestResult == HTBOTTOM) {
                                 if (isFixedHeight) {
                                     *result = HTBORDER;
                                 }
@@ -1758,7 +1752,8 @@
                         // inside our homemade title bar now, return HTCLIENT to let our
                         // title bar can still capture mouse events.
                         *result = [&]() {
-                            if (isFixedSize || isFixedHeight || dontOverrideCursor || (isFixedWidth && (isInLeftBorder || isInRightBorder))) {
+                            if (isFixedSize || isFixedHeight || dontOverrideCursor ||
+                                (isFixedWidth && (isInLeftBorder || isInRightBorder))) {
                                 if (isInTitleBar) {
                                     return HTCAPTION;
                                 } else {
@@ -2174,7 +2169,10 @@
         // of the upper-left non-client area. It's confirmed that this issue exists
         // from Windows 7 to Windows 10. Not tested on Windows 11 yet. Don't know
         // whether it exists on Windows XP to Windows Vista or not.
-        *result = wParam ? WVR_REDRAW : FALSE;
+
+        // https://github.com/chromium/chromium/blob/5d297da3cf2a642e9ace2b23fed097370bc70814/ui/views/win/hwnd_message_handler.cc#L2330
+        // Do not return WVR_REDRAW otherwise child HWNDs will be mispositioned.
+        *result = FALSE;
         return true;
     }
 

--
Gitblit v1.9.1