From abae836d3f0a1818e5404936ef298f93221b68fe Mon Sep 17 00:00:00 2001 From: Sine Striker <trueful@163.com> Date: 周日, 25 2月 2024 00:46:18 +0800 Subject: [PATCH] minor tweaks --- src/core/contexts/win32windowcontext.cpp | 147 ++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 133 insertions(+), 14 deletions(-) diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp index e9a6731..4fda1eb 100644 --- a/src/core/contexts/win32windowcontext.cpp +++ b/src/core/contexts/win32windowcontext.cpp @@ -6,13 +6,16 @@ #include <optional> +#include <QtCore/QAbstractEventDispatcher> +#include <QtCore/QDateTime> #include <QtCore/QHash> #include <QtCore/QScopeGuard> #include <QtCore/QTimer> -#include <QtCore/QDateTime> #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) @@ -29,6 +32,14 @@ #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 issue exists only in Qt 6.6.1, and was introduced by QTBUG-113736 patch and fixed by +// QTBUG-117704 patch. +#if !QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS) && QT_VERSION == QT_VERSION_CHECK(6, 6, 1) +# define QTBUG_113736_WORKAROUND +#endif namespace QWK { @@ -153,7 +164,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 @@ -359,6 +370,107 @@ return true; } + static inline constexpr bool isNonClientMessage(const UINT message) { + if (((message >= WM_NCCREATE) && (message <= WM_NCACTIVATE)) || + ((message >= WM_NCMOUSEMOVE) && (message <= WM_NCMBUTTONDBLCLK)) || + ((message >= WM_NCXBUTTONDOWN) && (message <= WM_NCXBUTTONDBLCLK)) +#if (WINVER >= _WIN32_WINNT_WIN8) + || ((message >= WM_NCPOINTERUPDATE) && (message <= WM_NCPOINTERUP)) +#endif + || ((message == WM_NCMOUSEHOVER) || (message == WM_NCMOUSELEAVE))) { + return true; + } else { + return false; + } + } + + static MSG createMessageBlock(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + MSG msg; + msg.hwnd = hWnd; // re-create MSG structure + msg.message = message; // time and pt fields ignored + msg.wParam = wParam; + msg.lParam = lParam; + + const DWORD dwScreenPos = ::GetMessagePos(); + msg.pt.x = GET_X_LPARAM(dwScreenPos); + msg.pt.y = GET_Y_LPARAM(dwScreenPos); + if (!isNonClientMessage(message)) { + ::ScreenToClient(hWnd, &msg.pt); + } + return msg; + } + + static inline constexpr bool isInputMessage(UINT m) { + switch (m) { + case WM_IME_STARTCOMPOSITION: + case WM_IME_ENDCOMPOSITION: + case WM_IME_COMPOSITION: + case WM_INPUT: + case WM_TOUCH: + case WM_MOUSEHOVER: + case WM_MOUSELEAVE: + case WM_NCMOUSEHOVER: + case WM_NCMOUSELEAVE: + case WM_SIZING: + case WM_MOVING: + case WM_SYSCOMMAND: + case WM_COMMAND: + case WM_DWMNCRENDERINGCHANGED: + case WM_PAINT: + return true; + default: + break; + } + return (m >= WM_MOUSEFIRST && m <= WM_MOUSELAST) || + (m >= WM_NCMOUSEMOVE && m <= WM_NCXBUTTONDBLCLK) || + (m >= WM_KEYFIRST && m <= WM_KEYLAST); + } + + static inline QByteArray nativeEventType() { + return QByteArrayLiteral("windows_generic_MSG"); + } + + // Send to QAbstractEventDispatcher + bool filterNativeEvent(MSG *msg, LRESULT *result) { + auto dispatcher = QAbstractEventDispatcher::instance(); + QT_NATIVE_EVENT_RESULT_TYPE filterResult = *result; + if (dispatcher && dispatcher->filterNativeEvent(nativeEventType(), msg, &filterResult)) { + *result = LRESULT(filterResult); + return true; + } + return false; + } + + // Send to QWindowSystemInterface + bool filterNativeEvent(QWindow *window, MSG *msg, LRESULT *result) { + QT_NATIVE_EVENT_RESULT_TYPE filterResult = *result; + if (QWindowSystemInterface::handleNativeEvent(window, nativeEventType(), msg, + &filterResult)) { + *result = LRESULT(filterResult); + return true; + } + return false; + } + + 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() + if (!isInputMessage(msg.message) && filterNativeEvent(&msg, result)) + return true; + + auto platformWindow = window->handle(); + if (platformWindow && filterNativeEvent(platformWindow->window(), &msg, result)) + return true; + + return false; + } + // https://github.com/qt/qtbase/blob/e26a87f1ecc40bc8c6aa5b889fce67410a57a702/src/plugins/platforms/windows/qwindowscontext.cpp#L1556 // In QWindowsContext::windowsProc(), the messages will be passed to all global native event // filters, but because we have already filtered the messages in the hook WndProc function for @@ -489,6 +601,10 @@ // Try hooked procedure and save result LRESULT result; if (ctx->windowProc(hWnd, message, wParam, lParam, &result)) { + // Forward the event to user-defined native event filters, there may be some messages + // that need to be processed by the user. + std::ignore = + forwardFilteredEvent(ctx->window(), hWnd, message, wParam, lParam, &result); return result; } @@ -497,7 +613,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}; @@ -505,10 +622,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) { @@ -631,7 +749,7 @@ } case DrawWindows10BorderHook2: { - if (!m_windowHandle) + if (!m_windowId) return; // https://github.com/microsoft/terminal/blob/71a6f26e6ece656084e87de1a528c4a8072eeabd/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp#L1025 @@ -663,7 +781,7 @@ QVariant Win32WindowContext::windowAttribute(const QString &key) const { if (key == QStringLiteral("window-rect")) { - if (!m_windowHandle) + if (!m_windowId) return {}; RECT frame{}; @@ -686,7 +804,7 @@ } if (key == QStringLiteral("border-thickness")) { - return m_windowHandle + return m_windowId ? int(getWindowFrameBorderThickness(reinterpret_cast<HWND>(m_windowId))) : 0; } @@ -699,12 +817,17 @@ 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) { removeManagedWindow(reinterpret_cast<HWND>(oldWinId)); } - if (!m_windowHandle || !winId) { + if (!winId) { return; } @@ -774,13 +897,9 @@ // Forward to native event filter subscribers if (!m_nativeEventFilters.isEmpty()) { - MSG msg; - msg.hwnd = hWnd; - msg.message = message; - msg.wParam = wParam; - msg.lParam = lParam; + MSG msg = createMessageBlock(hWnd, message, wParam, lParam); QT_NATIVE_EVENT_RESULT_TYPE res = 0; - if (nativeDispatch(QByteArrayLiteral("windows_generic_MSG"), &msg, &res)) { + if (nativeDispatch(nativeEventType(), &msg, &res)) { *result = LRESULT(res); return true; } -- Gitblit v1.9.1