From 1b9ac9ea6998ac8a4e51413e06aeed859e784b26 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周一, 11 12月 2023 20:09:19 +0800
Subject: [PATCH] minor tweaks

---
 src/core/contexts/win32windowcontext.cpp |  130 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 111 insertions(+), 19 deletions(-)

diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 9cf1ecc..ccc318d 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -5,7 +5,11 @@
 #include <QtCore/QHash>
 #include <QtCore/QScopeGuard>
 #include <QtGui/QGuiApplication>
+#include <QtGui/QPainter>
+#include <QtGui/QPalette>
+#include <QtGui/QStyleHints>
 
+#include <QtCore/private/qwinregistry_p.h>
 #include <QtCore/private/qsystemlibrary_p.h>
 #include <QtGui/private/qhighdpiscaling_p.h>
 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
@@ -21,7 +25,6 @@
 #include <shellscalingapi.h>
 #include <dwmapi.h>
 #include <timeapi.h>
-#include <versionhelpers.h>
 
 #include "nativeeventfilter.h"
 
@@ -33,6 +36,14 @@
 
     // The thickness of an auto-hide taskbar in pixels.
     static constexpr const auto kAutoHideTaskBarThickness = quint8{2};
+
+    static inline constexpr const auto kFrameBorderActiveColorLight =
+        QColor{110, 110, 110};                                                           // #6E6E6E
+    static inline constexpr const auto kFrameBorderActiveColorDark = QColor{51, 51, 51}; // #333333
+    static inline constexpr const auto kFrameBorderInactiveColorLight =
+        QColor{167, 167, 167};                                                           // #A7A7A7
+    static inline constexpr const auto kFrameBorderInactiveColorDark =
+        QColor{61, 61, 62};                                                              // #3D3D3E
 
     // hWnd -> context
     using WndProcHash = QHash<HWND, Win32WindowContext *>;
@@ -194,23 +205,22 @@
     }
 
     static inline bool isWin8OrGreater() {
-        static const bool result = ::IsWindows8OrGreater();
+        static const bool result = IsWindows8OrGreater_Real();
         return result;
     }
 
     static inline bool isWin8Point1OrGreater() {
-        static const bool result = ::IsWindows8Point1OrGreater();
+        static const bool result = IsWindows8Point1OrGreater_Real();
         return result;
     }
 
     static inline bool isWin10OrGreater() {
-        static const bool result = ::IsWindows10OrGreater();
+        static const bool result = IsWindows10OrGreater_Real();
         return result;
     }
 
     static inline bool isWin11OrGreater() {
-        static const bool result = ::IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN10),
-                                                               LOBYTE(_WIN32_WINNT_WIN10), 22000);
+        static const bool result = IsWindows11OrGreater_Real();
         return result;
     }
 
@@ -224,6 +234,57 @@
         }
         BOOL enabled = FALSE;
         return SUCCEEDED(apis.pDwmIsCompositionEnabled(&enabled)) && enabled;
+    }
+
+    static inline bool isWindowFrameBorderColorized() {
+        const QWinRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)");
+        if (!registry.isValid()) {
+            return false;
+        }
+        const auto value = registry.dwordValue(L"ColorPrevalence");
+        if (!value.second) {
+            return false;
+        }
+        return value.first;
+    }
+
+    static inline bool isDarkThemeActive() {
+#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
+        return QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark;
+#else
+        const QWinRegistryKey registry(
+            HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)");
+        if (!registry.isValid()) {
+            return false;
+        }
+        const auto value = registry.dwordValue(L"AppsUseLightTheme");
+        if (!value.second) {
+            return false;
+        }
+        return !value.first;
+#endif
+    }
+
+    static inline QColor getAccentColor() {
+#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
+        return QGuiApplication::palette().color(QPalette::Accent);
+#else
+        const QWinRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)");
+        if (!registry.isValid()) {
+            return {};
+        }
+        const auto value = registry.dwordValue(L"AccentColor");
+        if (!value.second) {
+            return {};
+        }
+        // The retrieved value is in the #AABBGGRR format, we need to
+        // convert it to the #AARRGGBB format which Qt expects.
+        const QColor abgr = QColor::fromRgba(value.first);
+        if (!abgr.isValid()) {
+            return {};
+        }
+        return QColor::fromRgb(abgr.blue(), abgr.green(), abgr.red(), abgr.alpha());
+#endif
     }
 
     static inline void triggerFrameChange(HWND hwnd) {
@@ -477,7 +538,8 @@
             case HTBORDER:
                 return Win32WindowContext::FixedBorder;
             default:
-                break; // unreachable
+                // unreachable
+                break;
         }
         return Win32WindowContext::Outside;
     }
@@ -669,10 +731,39 @@
         }
     }
 
-    void Win32WindowContext::showSystemMenu(const QPoint &pos) {
-        auto winId = m_windowHandle->winId();
-        auto hWnd = reinterpret_cast<HWND>(winId);
-        showSystemMenu2(hWnd, {pos.x(), pos.y()}, false, m_delegate->isHostSizeFixed(m_host));
+    QString Win32WindowContext::key() const {
+        return "win32";
+    }
+
+    void Win32WindowContext::virtual_hook(int id, void *data) {
+        switch (id) {
+            case ShowSystemMenuHook: {
+                const auto &pos = *reinterpret_cast<const QPoint *>(data);
+                auto winId = m_windowHandle->winId();
+                auto hWnd = reinterpret_cast<HWND>(winId);
+                showSystemMenu2(hWnd, qpoint2point(pos), false,
+                                m_delegate->isHostSizeFixed(m_host));
+                return;
+            }
+            case NeedsDrawBordersHook: {
+                auto &result = *reinterpret_cast<bool *>(data);
+                result = isWin10OrGreater() && !isWin11OrGreater();
+                return;
+            }
+            case DrawBordersHook: {
+                auto args = reinterpret_cast<void **>(data);
+                auto &painter = *reinterpret_cast<QPainter *>(args[0]);
+                auto &rect = *reinterpret_cast<const QRect *>(args[1]);
+                auto &region = *reinterpret_cast<const QRegion *>(args[2]);
+                // ### TODO
+                return;
+            }
+            default: {
+                // unreachable
+                break;
+            }
+        }
+        AbstractWindowContext::virtual_hook(id, data);
     }
 
     bool Win32WindowContext::setupHost() {
@@ -742,11 +833,10 @@
         return false; // Not handled
     }
 
-    static constexpr const auto kMessageTag = WPARAM(0x97CCEA99);
-
-    static inline constexpr bool isTaggedMessage(WPARAM wParam) {
-        return (wParam == kMessageTag);
-    }
+    static constexpr const struct {
+        const WPARAM wParam = 0xF1C9ADD4;
+        const LPARAM lParam = 0xAFB6F4C6;
+    } kMessageTag;
 
     static inline quint64 getKeyState() {
         quint64 result = 0;
@@ -786,7 +876,7 @@
                 // wParam is always ignored in mouse leave messages, but here we
                 // give them a special tag to be able to distinguish which messages
                 // are sent by ourselves.
-                return kMessageTag;
+                return kMessageTag.wParam;
             }
             const quint64 keyState = getKeyState();
             if ((myMsg >= WM_NCXBUTTONDOWN) && (myMsg <= WM_NCXBUTTONDBLCLK)) {
@@ -864,6 +954,7 @@
                 SEND_MESSAGE(hWnd, WM_MOUSELEAVE, wParamNew, lParamNew);
                 break;
             default:
+                // unreachable
                 break;
         }
 
@@ -886,7 +977,7 @@
                                                LPARAM lParam, LRESULT *result) {
         switch (message) {
             case WM_MOUSELEAVE: {
-                if (!isTaggedMessage(wParam)) {
+                if (wParam == kMessageTag.wParam) {
                     // Qt will call TrackMouseEvent() to get the WM_MOUSELEAVE message when it
                     // receives WM_MOUSEMOVE messages, and since we are converting every
                     // WM_NCMOUSEMOVE message to WM_MOUSEMOVE message and send it back to the window
@@ -1198,7 +1289,8 @@
                                 *result = HTCLOSE;
                                 break;
                             default:
-                                break; // unreachable
+                                // unreachable
+                                break;
                         }
                     }
                     if (*result == HTNOWHERE) {

--
Gitblit v1.9.1