From 42452085f7473e0af814abdee2828f52abcbe7ba Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周六, 24 2月 2024 18:40:04 +0800
Subject: [PATCH] Workaround for issue #23

---
 src/core/contexts/win32windowcontext.cpp |   37 ++++++++++++++++++++++++++-----------
 1 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 25a5d8a..af7d4c8 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -6,16 +6,17 @@
 
 #include <optional>
 
+#include <QtCore/QAbstractEventDispatcher>
+#include <QtCore/QDateTime>
 #include <QtCore/QHash>
 #include <QtCore/QScopeGuard>
 #include <QtCore/QTimer>
-#include <QtCore/QDateTime>
-#include <QtCore/QAbstractEventDispatcher>
 #include <QtGui/QGuiApplication>
 #include <QtGui/QPainter>
 #include <QtGui/QPalette>
 
 #include <QtGui/qpa/qwindowsysteminterface.h>
+
 #include <QtGui/private/qhighdpiscaling_p.h>
 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
 #  include <QtGui/private/qguiapplication_p.h>
@@ -31,6 +32,13 @@
 
 #include "qwkglobal_p.h"
 #include "qwkwindowsextra_p.h"
+
+// https://github.com/qt/qtbase/blob/6.6.1/src/plugins/platforms/windows/qwindowswindow.cpp#L2791
+// https://github.com/qt/qtbase/blob/6.6.1/src/plugins/platforms/windows/qwindowswindow.cpp#L3321
+// This implementation contradicts with QTBUG-113736 patch.
+#if !QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS) && QT_VERSION >= QT_VERSION_CHECK(6, 6, 1)
+#  define QTBUG_113736_WORKAROUND
+#endif
 
 namespace QWK {
 
@@ -155,7 +163,7 @@
         [[maybe_unused]] const auto &cleaner =
             qScopeGuard([windowThreadProcessId, currentThreadId]() {
                 ::AttachThreadInput(windowThreadProcessId, currentThreadId, FALSE); //
-            });
+            }); // TODO: Remove it
 
         ::BringWindowToTop(hwnd);
         // Activate the window too. This will force us to the virtual desktop this
@@ -443,9 +451,12 @@
         return false;
     }
 
-    static inline bool forwardFilteredEvents(QWindow *window, HWND hWnd, UINT message,
-                                             WPARAM wParam, LPARAM lParam, LRESULT *result) {
+    static inline bool forwardFilteredEvent(QWindow *window, HWND hWnd, UINT message, WPARAM wParam,
+                                            LPARAM lParam, LRESULT *result) {
         MSG msg = createMessageBlock(hWnd, message, wParam, lParam);
+
+        // https://github.com/qt/qtbase/blob/e26a87f1ecc40bc8c6aa5b889fce67410a57a702/src/plugins/platforms/windows/qwindowscontext.cpp#L1025
+        // Do exact the same as what Qt Windows plugin does.
 
         // Run the native event filters. QTBUG-67095: Exclude input messages which are sent
         // by QEventDispatcherWin32::processEvents()
@@ -592,7 +603,7 @@
             // Forward the event to user-defined native event filters, there may be some messages
             // that need to be processed by the user.
             std::ignore =
-                forwardFilteredEvents(ctx->window(), hWnd, message, wParam, lParam, &result);
+                forwardFilteredEvent(ctx->window(), hWnd, message, wParam, lParam, &result);
             return result;
         }
 
@@ -601,7 +612,8 @@
     }
 
     static inline void addManagedWindow(QWindow *window, HWND hWnd, Win32WindowContext *ctx) {
-        const auto margins = [hWnd]() -> QMargins {
+#ifndef QTBUG_113736_WORKAROUND
+        const auto margins = [](HWND hWnd) -> QMargins {
             const auto titleBarHeight = int(getTitleBarHeight(hWnd));
             if (isSystemBorderEnabled()) {
                 return {0, -titleBarHeight, 0, 0};
@@ -609,10 +621,11 @@
                 const auto frameSize = int(getResizeBorderThickness(hWnd));
                 return {-frameSize, -titleBarHeight, -frameSize, -frameSize};
             }
-        }();
+        }(hWnd);
 
         // Inform Qt we want and have set custom margins
         setInternalWindowFrameMargins(window, margins);
+#endif
 
         // Store original window proc
         if (!g_qtWindowProc) {
@@ -802,6 +815,11 @@
         // Reset the context data
         mouseLeaveBlocked = false;
         lastHitTestResult = WindowPart::Outside;
+
+#ifdef QTBUG_113736_WORKAROUND
+        m_delegate->setWindowFlags(m_host,
+                                   m_delegate->getWindowFlags(m_host) | Qt::FramelessWindowHint);
+#endif
 
         // If the original window id is valid, remove all resources related
         if (oldWinId) {
@@ -1480,11 +1498,8 @@
                 // Terminal does, however, later I found that if we choose a proper
                 // color, our homemade top border can almost have exactly the same
                 // appearance with the system's one.
-                qDebug() << QDateTime::currentDateTime() << "HITTEST";
                 [[maybe_unused]] const auto &hitTestRecorder = qScopeGuard([this, result]() {
                     lastHitTestResult = getHitWindowPart(int(*result)); //
-
-                    qDebug() << lastHitTestResult;
                 });
 
                 POINT nativeGlobalPos{GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};

--
Gitblit v1.9.1