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

---
 examples/qml/main.cpp                       |   13 ++--
 src/core/contexts/abstractwindowcontext.cpp |   49 +++++++++++++++
 examples/mainwindow/main.cpp                |    7 +
 src/core/qwkglobal_p.h                      |    5 +
 src/core/contexts/win32windowcontext.cpp    |   54 ++++++++++-------
 5 files changed, 95 insertions(+), 33 deletions(-)

diff --git a/examples/mainwindow/main.cpp b/examples/mainwindow/main.cpp
index 0e29f11..66f6bee 100644
--- a/examples/mainwindow/main.cpp
+++ b/examples/mainwindow/main.cpp
@@ -1,4 +1,5 @@
-#include <QApplication>
+#include <QtWidgets/QApplication>
+#include <QtCore/QDebug>
 
 #include "mainwindow.h"
 
@@ -12,7 +13,9 @@
     qputenv("QSG_RHI_HDR", "scrgb");
     qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1");
 #endif
-    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
+
+    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
+        Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
     QApplication a(argc, argv);
     MainWindow w;
     w.show();
diff --git a/examples/qml/main.cpp b/examples/qml/main.cpp
index 1898550..386e4d4 100644
--- a/examples/qml/main.cpp
+++ b/examples/qml/main.cpp
@@ -1,9 +1,9 @@
-#include <QWKQuick/qwkquickglobal.h>
-#include <QGuiApplication>
-#include <QQmlApplicationEngine>
+#include <QtGui/QGuiApplication>
+#include <QtQml/QQmlApplicationEngine>
 
-int main(int argc, char *argv[])
-{
+#include <QWKQuick/qwkquickglobal.h>
+
+int main(int argc, char *argv[]) {
     qputenv("QT_WIN_DEBUG_CONSOLE", "1");
     qputenv("QSG_INFO", "1");
 #if 1
@@ -11,7 +11,8 @@
     qputenv("QSG_RHI_HDR", "scrgb");
     qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1");
 #endif
-    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
+    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
+        Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
     QGuiApplication application(argc, argv);
     QQmlApplicationEngine engine;
     QWK::registerTypes(&engine);
diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp
index e1187fa..fa8ee74 100644
--- a/src/core/contexts/abstractwindowcontext.cpp
+++ b/src/core/contexts/abstractwindowcontext.cpp
@@ -1,9 +1,13 @@
 #include "abstractwindowcontext_p.h"
 
+#include <QtGui/QPen>
+#include <QtGui/QPainter>
+
+#include "qwkglobal_p.h"
+
 namespace QWK {
 
-    AbstractWindowContext::AbstractWindowContext()
-        : m_host(nullptr), m_delegate(nullptr), m_windowHandle(nullptr) {
+    AbstractWindowContext::AbstractWindowContext() {
     }
 
     AbstractWindowContext::~AbstractWindowContext() = default;
@@ -168,13 +172,54 @@
         return {};
     }
 
+    static constexpr struct {
+        const quint32 lilac = MAKE_RGBA_COLOR(210, 233, 189, 226);
+        const quint32 pink = MAKE_RGBA_COLOR(177, 205, 190, 240);
+        const quint32 tile = MAKE_RGBA_COLOR(193, 195, 211, 203);
+        const quint32 azure = MAKE_RGBA_COLOR(25, 90, 190, 255);
+    } kSampleColorSet;
+
     void AbstractWindowContext::virtual_hook(int id, void *data) {
         switch (id) {
+            case ShowSystemMenuHook: {
+                const auto &pos = *reinterpret_cast<const QPoint *>(data);
+                // ...
+                return;
+            }
             case NeedsDrawBordersHook: {
                 auto &result = *reinterpret_cast<bool *>(data);
                 result = false;
                 break;
             }
+            case DrawBordersHook: {
+                auto args = reinterpret_cast<void **>(data);
+                auto &painter = *reinterpret_cast<QPainter *>(args[0]);
+                auto &rect = *reinterpret_cast<const QRect *>(args[1]);
+
+                QPen pen;
+                pen.setWidth(1);
+
+                // Top
+                pen.setColor(kSampleColorSet.lilac);
+                painter.setPen(pen);
+                painter.drawLine(rect.topLeft(), rect.topRight());
+
+                // Right
+                pen.setColor(kSampleColorSet.pink);
+                painter.setPen(pen);
+                painter.drawLine(rect.topRight(), rect.bottomRight());
+
+                // Bottom
+                pen.setColor(kSampleColorSet.tile);
+                painter.setPen(pen);
+                painter.drawLine(rect.bottomLeft(), rect.bottomRight());
+
+                // Right
+                pen.setColor(kSampleColorSet.azure);
+                painter.setPen(pen);
+                painter.drawLine(rect.topLeft(), rect.bottomLeft());
+                return;
+            }
             default:
                 break;
         }
diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 2a2dffb..ccc318d 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -37,10 +37,13 @@
     // 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 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
+    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 *>;
@@ -238,26 +241,27 @@
         if (!registry.isValid()) {
             return false;
         }
-        const QVariant value = registry.value(L"ColorPrevalence");
-        if (!value.isValid()) {
+        const auto value = registry.dwordValue(L"ColorPrevalence");
+        if (!value.second) {
             return false;
         }
-        return qvariant_cast<DWORD>(value);
+        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)");
+        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()) {
+        const auto value = registry.dwordValue(L"AppsUseLightTheme");
+        if (!value.second) {
             return false;
         }
-        return !qvariant_cast<DWORD>(value);
+        return !value.first;
 #endif
     }
 
@@ -269,13 +273,13 @@
         if (!registry.isValid()) {
             return {};
         }
-        const QVariant value = registry.value(L"AccentColor");
-        if (!value.isValid()) {
+        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(qvariant_cast<DWORD>(value));
+        const QColor abgr = QColor::fromRgba(value.first);
         if (!abgr.isValid()) {
             return {};
         }
@@ -534,7 +538,8 @@
             case HTBORDER:
                 return Win32WindowContext::FixedBorder;
             default:
-                break; // unreachable
+                // unreachable
+                break;
         }
         return Win32WindowContext::Outside;
     }
@@ -753,8 +758,10 @@
                 // ### TODO
                 return;
             }
-            default:
+            default: {
+                // unreachable
                 break;
+            }
         }
         AbstractWindowContext::virtual_hook(id, data);
     }
@@ -826,11 +833,10 @@
         return false; // Not handled
     }
 
-    static constexpr const auto kMessageTag = WPARAM(0xF1C9ADD4);
-
-    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;
@@ -870,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)) {
@@ -948,6 +954,7 @@
                 SEND_MESSAGE(hWnd, WM_MOUSELEAVE, wParamNew, lParamNew);
                 break;
             default:
+                // unreachable
                 break;
         }
 
@@ -970,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
@@ -1282,7 +1289,8 @@
                                 *result = HTCLOSE;
                                 break;
                             default:
-                                break; // unreachable
+                                // unreachable
+                                break;
                         }
                     }
                     if (*result == HTNOWHERE) {
diff --git a/src/core/qwkglobal_p.h b/src/core/qwkglobal_p.h
index 1a1f626..5ad8a40 100644
--- a/src/core/qwkglobal_p.h
+++ b/src/core/qwkglobal_p.h
@@ -15,4 +15,9 @@
 #  define QWK_FATAL qCFatal(qWindowKitLog)
 #endif
 
+#define MAKE_RGB_COLOR(r, g, b) ((quint32) | (((r) &0xFF) << 16) | (((g) &0xFF) << 8) | ((b) &0xFF))
+
+#define MAKE_RGBA_COLOR(r, g, b, a)                                                                \
+    ((quint32) (((a) &0xFF) << 24) | (((r) &0xFF) << 16) | (((g) &0xFF) << 8) | ((b) &0xFF))
+
 #endif // QWKGLOBAL_P_H

--
Gitblit v1.9.1