From 5c471560256776f17f1e3d5c377c7d3e9fdcc0f1 Mon Sep 17 00:00:00 2001
From: Yuhang Zhao <zhaoyuhang@rankyee.com>
Date: 周一, 11 12月 2023 18:09:55 +0800
Subject: [PATCH] WIP: prepare border painter

---
 src/core/contexts/win32windowcontext.cpp |   77 ++++++++++++++++++++++++++++++++------
 1 files changed, 65 insertions(+), 12 deletions(-)

diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 07ab1d2..2a2dffb 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -6,7 +6,10 @@
 #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)
@@ -33,6 +36,11 @@
 
     // 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 *>;
@@ -223,6 +231,56 @@
         }
         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 QVariant value = registry.value(L"ColorPrevalence");
+        if (!value.isValid()) {
+            return false;
+        }
+        return qvariant_cast<DWORD>(value);
+    }
+
+    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 QVariant value = registry.value(L"AppsUseLightTheme");
+        if (!value.isValid()) {
+            return false;
+        }
+        return !qvariant_cast<DWORD>(value);
+#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 QVariant value = registry.value(L"AccentColor");
+        if (!value.isValid()) {
+            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(qvariant_cast<DWORD>(value));
+        if (!abgr.isValid()) {
+            return {};
+        }
+        return QColor::fromRgb(abgr.blue(), abgr.green(), abgr.red(), abgr.alpha());
+#endif
     }
 
     static inline void triggerFrameChange(HWND hwnd) {
@@ -678,7 +736,7 @@
                 const auto &pos = *reinterpret_cast<const QPoint *>(data);
                 auto winId = m_windowHandle->winId();
                 auto hWnd = reinterpret_cast<HWND>(winId);
-                showSystemMenu2(hWnd, {pos.x(), pos.y()}, false,
+                showSystemMenu2(hWnd, qpoint2point(pos), false,
                                 m_delegate->isHostSizeFixed(m_host));
                 return;
             }
@@ -688,17 +746,12 @@
                 return;
             }
             case DrawBordersHook: {
-                auto a = reinterpret_cast<void **>(data);
-                auto &painter = *reinterpret_cast<QPainter *>(a[0]);
-                auto &rect = *reinterpret_cast<const QRect *>(a[1]);
-                auto &region = *reinterpret_cast<const QRegion *>(a[2]);
-
-                qDebug() << "paint" << &painter << rect << region;
-
-                // TODO: Draw border
-                // ...
-
-                break;
+                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:
                 break;

--
Gitblit v1.9.1