Skip to content

Commit 6273e2e

Browse files
author
Wladimir Leuschner
committed
WindowsQPA: Make custom titlebar respect swapped mouse buttons
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ø <[email protected]> Reviewed-by: Zhao Yuhang <[email protected]>
1 parent a7d8d3e commit 6273e2e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/plugins/platforms/windows/qwindowswindow.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -3313,16 +3313,23 @@ bool QWindowsWindow::handleNonClientHitTest(const QPoint &globalPos, LRESULT *re
33133313
const int titleBarHeight = getTitleBarHeight_sys(savedDpi());
33143314
const int titleButtonWidth = titleBarHeight * 1.5;
33153315
int buttons = 1;
3316+
const bool mouseButtonsSwapped = GetSystemMetrics(SM_SWAPBUTTON);
3317+
auto mouseButtons = Qt::NoButton;
3318+
if (mouseButtonsSwapped)
3319+
mouseButtons = GetAsyncKeyState(VK_LBUTTON) != 0 ? Qt::RightButton : (GetAsyncKeyState(VK_RBUTTON) ? Qt::LeftButton : Qt::NoButton);
3320+
else
3321+
mouseButtons = GetAsyncKeyState(VK_LBUTTON) != 0 ? Qt::LeftButton : (GetAsyncKeyState(VK_RBUTTON) ? Qt::RightButton : Qt::NoButton);
3322+
33163323
if (globalPos.y() < geom.top() + titleBarHeight) {
33173324
if (m_data.flags.testFlags(Qt::WindowCloseButtonHint) || isDefaultTitleBar) {
33183325
if ((globalPos.x() > geom.right() - titleButtonWidth * buttons) && (globalPos.x() <= geom.right())) {
3319-
if (GetAsyncKeyState(VK_LBUTTON))
3326+
if (mouseButtons == Qt::LeftButton)
33203327
*result = HTCLOSE;
33213328
}
33223329
buttons++;
33233330
} if (m_data.flags.testFlags(Qt::WindowMaximizeButtonHint) || isDefaultTitleBar) {
33243331
if ((globalPos.x() > geom.right() - titleButtonWidth * buttons) && (globalPos.x() <= geom.right() - titleButtonWidth * (buttons-1))){
3325-
if (GetAsyncKeyState(VK_LBUTTON)) {
3332+
if (mouseButtons == Qt::LeftButton) {
33263333
if (IsZoomed(m_data.hwnd))
33273334
*result = HTSIZE;
33283335
else
@@ -3332,18 +3339,17 @@ bool QWindowsWindow::handleNonClientHitTest(const QPoint &globalPos, LRESULT *re
33323339
buttons++;
33333340
} if (m_data.flags.testFlags(Qt::WindowMinimizeButtonHint) || isDefaultTitleBar) {
33343341
if ((globalPos.x() > geom.right() - titleButtonWidth * buttons) && (globalPos.x() <= geom.right() - titleButtonWidth * (buttons-1))){
3335-
if (GetAsyncKeyState(VK_LBUTTON))
3342+
if (mouseButtons == Qt::LeftButton)
33363343
*result = HTMINBUTTON;
33373344
}
33383345
buttons++;
33393346
} if ((isCustomized || isDefaultTitleBar) &&
33403347
*result == HTCLIENT){
33413348
QWindow* wnd = window();
3342-
auto buttons = GetAsyncKeyState(VK_LBUTTON) != 0 ? Qt::LeftButton : Qt::NoButton;
3343-
if (buttons != Qt::NoButton) {
3344-
QMouseEvent event(QEvent::MouseButtonPress, localPos, globalPos, buttons, buttons, Qt::NoModifier);
3349+
if (mouseButtons != Qt::NoButton) {
3350+
QMouseEvent event(QEvent::MouseButtonPress, localPos, globalPos, mouseButtons, mouseButtons, Qt::NoModifier);
33453351
QGuiApplication::sendEvent(wnd, &event);
3346-
if (!event.isAccepted() && GetAsyncKeyState(VK_RBUTTON))
3352+
if (!event.isAccepted() && mouseButtons == Qt::RightButton)
33473353
*result = HTSYSMENU;
33483354
else if (!event.isAccepted())
33493355
*result = HTCAPTION;

0 commit comments

Comments
 (0)