macOS: ensure mouse events from the global monitor has the correct button set

The global monitor will be used when popup windows are visible to
catch all mouse events. But only certain kind of NSEvents has a
buttonNumber set to something useful. So we need to check for
this case before we create a QPA event.

Task-number: QTBUG-60887
Change-Id: I5631c76ae82b8bfd232642036a08b5a8a29491c7
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Richard Moe Gustavsen 2018-04-30 10:03:19 +02:00
parent 2ef83cc12c
commit da32360ffc
2 changed files with 13 additions and 3 deletions

View File

@ -265,6 +265,16 @@ QRectF qt_mac_flip(const QRectF &rect, const QRectF &reference)
// -------------------------------------------------------------------------
/*!
\fn Qt::MouseButton cocoaButton2QtButton(NSInteger buttonNum)
Returns the Qt::Button that corresponds to an NSEvent.buttonNumber.
\note AppKit will use buttonNumber 0 to indicate both "left button"
and "no button". Only NSEvents that describes mouse press/release
events (e.g NSEventTypeOtherMouseDown) will contain a valid
button number.
*/
Qt::MouseButton cocoaButton2QtButton(NSInteger buttonNum)
{
if (buttonNum >= 0 && buttonNum <= 31)

View File

@ -404,10 +404,10 @@ void QCocoaWindow::setVisible(bool visible)
removeMonitor();
monitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDownMask|NSRightMouseDownMask|NSOtherMouseDownMask|NSMouseMovedMask handler:^(NSEvent *e) {
QPointF localPoint = QCocoaScreen::mapFromNative([NSEvent mouseLocation]);
const auto eventType = e.type == NSMouseMoved ? QEvent::MouseMove : QEvent::MouseButtonPress;
const auto button = e.type == NSEventTypeMouseMoved ? Qt::NoButton : cocoaButton2QtButton([e buttonNumber]);
const auto eventType = e.type == NSEventTypeMouseMoved ? QEvent::MouseMove : QEvent::MouseButtonPress;
QWindowSystemInterface::handleMouseEvent(window(), window()->mapFromGlobal(localPoint.toPoint()), localPoint,
Qt::MouseButtons(uint(NSEvent.pressedMouseButtons & 0xFFFF)),
cocoaButton2QtButton(e.buttonNumber), eventType);
Qt::MouseButtons(uint(NSEvent.pressedMouseButtons & 0xFFFF)), button, eventType);
}];
}
}