From 6273e2e0edfc0eba148889158b2d06ad5a6eb0b9 Mon Sep 17 00:00:00 2001 From: Wladimir Leuschner Date: Fri, 4 Apr 2025 11:25:14 +0200 Subject: [PATCH] WindowsQPA: Make custom titlebar respect swapped mouse buttons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Custom titlebar input handling is done on WM_NCHITTEST, which is handled at the WinAPI level. The WinAPI does not take into account the swapped mouse buttons when querying the state of VK_LEFTBUTTON and VK_RIGHTBUTTON with GetAsyncKeyState, so this behavior has to be implemented manually. In case of swapped mouse buttons, GetSystemMetrics(SM_SWAPBUTTON) will return true. This patch inverses the meaning of Right and Left buttons in case GetSystemMetrics(SM_SWAPBUTTON) returns true. Pick-to: 6.9 Change-Id: Ie46e130dc0bb49de318c8d04a3cc426f7a346b5b Reviewed-by: Tor Arne Vestbø Reviewed-by: Zhao Yuhang <2546789017@qq.com> --- .../platforms/windows/qwindowswindow.cpp | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp index 77e0cbfcaa6..cf22783c286 100644 --- a/src/plugins/platforms/windows/qwindowswindow.cpp +++ b/src/plugins/platforms/windows/qwindowswindow.cpp @@ -3313,16 +3313,23 @@ bool QWindowsWindow::handleNonClientHitTest(const QPoint &globalPos, LRESULT *re const int titleBarHeight = getTitleBarHeight_sys(savedDpi()); const int titleButtonWidth = titleBarHeight * 1.5; int buttons = 1; + const bool mouseButtonsSwapped = GetSystemMetrics(SM_SWAPBUTTON); + auto mouseButtons = Qt::NoButton; + if (mouseButtonsSwapped) + mouseButtons = GetAsyncKeyState(VK_LBUTTON) != 0 ? Qt::RightButton : (GetAsyncKeyState(VK_RBUTTON) ? Qt::LeftButton : Qt::NoButton); + else + mouseButtons = GetAsyncKeyState(VK_LBUTTON) != 0 ? Qt::LeftButton : (GetAsyncKeyState(VK_RBUTTON) ? Qt::RightButton : Qt::NoButton); + if (globalPos.y() < geom.top() + titleBarHeight) { if (m_data.flags.testFlags(Qt::WindowCloseButtonHint) || isDefaultTitleBar) { if ((globalPos.x() > geom.right() - titleButtonWidth * buttons) && (globalPos.x() <= geom.right())) { - if (GetAsyncKeyState(VK_LBUTTON)) + if (mouseButtons == Qt::LeftButton) *result = HTCLOSE; } buttons++; } if (m_data.flags.testFlags(Qt::WindowMaximizeButtonHint) || isDefaultTitleBar) { if ((globalPos.x() > geom.right() - titleButtonWidth * buttons) && (globalPos.x() <= geom.right() - titleButtonWidth * (buttons-1))){ - if (GetAsyncKeyState(VK_LBUTTON)) { + if (mouseButtons == Qt::LeftButton) { if (IsZoomed(m_data.hwnd)) *result = HTSIZE; else @@ -3332,18 +3339,17 @@ bool QWindowsWindow::handleNonClientHitTest(const QPoint &globalPos, LRESULT *re buttons++; } if (m_data.flags.testFlags(Qt::WindowMinimizeButtonHint) || isDefaultTitleBar) { if ((globalPos.x() > geom.right() - titleButtonWidth * buttons) && (globalPos.x() <= geom.right() - titleButtonWidth * (buttons-1))){ - if (GetAsyncKeyState(VK_LBUTTON)) + if (mouseButtons == Qt::LeftButton) *result = HTMINBUTTON; } buttons++; } if ((isCustomized || isDefaultTitleBar) && *result == HTCLIENT){ QWindow* wnd = window(); - auto buttons = GetAsyncKeyState(VK_LBUTTON) != 0 ? Qt::LeftButton : Qt::NoButton; - if (buttons != Qt::NoButton) { - QMouseEvent event(QEvent::MouseButtonPress, localPos, globalPos, buttons, buttons, Qt::NoModifier); + if (mouseButtons != Qt::NoButton) { + QMouseEvent event(QEvent::MouseButtonPress, localPos, globalPos, mouseButtons, mouseButtons, Qt::NoModifier); QGuiApplication::sendEvent(wnd, &event); - if (!event.isAccepted() && GetAsyncKeyState(VK_RBUTTON)) + if (!event.isAccepted() && mouseButtons == Qt::RightButton) *result = HTSYSMENU; else if (!event.isAccepted()) *result = HTCAPTION;