fix: mousetap on mac

This commit is contained in:
rankun 2020-03-07 21:49:39 +08:00
commit 41ef9e780a
3 changed files with 23 additions and 34 deletions

View file

@ -81,17 +81,9 @@ void VideoForm::onGrabCursor(bool grab)
rc.setBottomRight(rc.bottomRight() * m_videoWidget->devicePixelRatio()); rc.setBottomRight(rc.bottomRight() * m_videoWidget->devicePixelRatio());
MouseTap::getInstance()->enableMouseEventTap(rc, grab); MouseTap::getInstance()->enableMouseEventTap(rc, grab);
#elif defined(Q_OS_OSX) #elif defined(Q_OS_OSX)
// get nswindow from qt widget QRect rc = m_videoWidget->geometry();
NSView *nsview = (NSView *)m_videoWidget->window()->winId(); rc.setTopLeft(m_videoWidget->mapToGlobal(rc.topLeft()));
if (!nsview) { rc.setBottomRight(m_videoWidget->mapToGlobal(rc.bottomRight()));
return;
}
NSWindow *nswindow = [nsview window];
NSRect windowRect = [nswindow contentRectForFrameRect:[nswindow frame]];
QRect rc(windowRect.origin.x, windowRect.origin.y,
windowRect.size.width, windowRect.size.height);
rc.setX(rc.x() + 100); rc.setX(rc.x() + 100);
rc.setY(rc.y() + 30); rc.setY(rc.y() + 30);
rc.setWidth(rc.width() - 180); rc.setWidth(rc.width() - 180);

View file

@ -45,33 +45,29 @@ static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type
return event; return event;
} }
NSRect windowRect = NSMakeRect(tapdata->rc.left(), tapdata->rc.top(), NSRect limitWindowRect = NSMakeRect(tapdata->rc.left(), tapdata->rc.top(),
tapdata->rc.width(), tapdata->rc.height()); tapdata->rc.width(), tapdata->rc.height());
NSRect newWindowRect = NSMakeRect(windowRect.origin.x, windowRect.origin.y, // check rect samll than limit rect
windowRect.size.width - 10, windowRect.size.height - 10); NSRect checkWindowRect = NSMakeRect(limitWindowRect.origin.x + 10, limitWindowRect.origin.y + 10,
CGPoint eventLocation = CGEventGetUnflippedLocation(event); limitWindowRect.size.width - 10, limitWindowRect.size.height - 10);
//qDebug() << newWindowRect.origin.x << newWindowRect.origin.y << newWindowRect.size.width << newWindowRect.size.height; /* This is in CGs global screenspace coordinate system, which has a
* flipped Y.
if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), newWindowRect, NO)) { */
CGPoint eventLocation = CGEventGetLocation(event);
/* This is in CGs global screenspace coordinate system, which has a if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), checkWindowRect, NO)) {
* flipped Y. if (eventLocation.x <= NSMinX(limitWindowRect)) {
*/ eventLocation.x = NSMinX(limitWindowRect) + 1.0;
CGPoint newLocation = CGEventGetLocation(event); } else if (eventLocation.x >= NSMaxX(limitWindowRect)) {
eventLocation.x = NSMaxX(limitWindowRect) - 1.0;
if (eventLocation.x < NSMinX(windowRect)) {
newLocation.x = NSMinX(windowRect);
} else if (eventLocation.x >= NSMaxX(windowRect)) {
newLocation.x = NSMaxX(windowRect) - 1.0;
} }
if (eventLocation.y <= NSMinY(windowRect)) { if (eventLocation.y <= NSMinY(limitWindowRect)) {
newLocation.y -= (NSMinY(windowRect) - eventLocation.y + 1); eventLocation.y = NSMinY(limitWindowRect) + 1.0;
} else if (eventLocation.y > NSMaxY(windowRect)) { } else if (eventLocation.y >= NSMaxY(limitWindowRect)) {
newLocation.y += (eventLocation.y - NSMaxY(windowRect)); eventLocation.y = NSMaxY(limitWindowRect) - 1.0;
} }
CGWarpMouseCursorPosition(newLocation); CGWarpMouseCursorPosition(eventLocation);
CGAssociateMouseAndMouseCursorPosition(YES); CGAssociateMouseAndMouseCursorPosition(YES);
if ((CGEventMaskBit(type) & movementEventsMask) == 0) { if ((CGEventMaskBit(type) & movementEventsMask) == 0) {
@ -80,7 +76,7 @@ static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type
* movement events, since they mean that our warp cursor above * movement events, since they mean that our warp cursor above
* behaves strangely. * behaves strangely.
*/ */
CGEventSetLocation(event, newLocation); CGEventSetLocation(event, eventLocation);
} }
} }

View file

@ -8,6 +8,7 @@ public:
static MouseTap* getInstance(); static MouseTap* getInstance();
virtual void initMouseEventTap() = 0; virtual void initMouseEventTap() = 0;
virtual void quitMouseEventTap() = 0; virtual void quitMouseEventTap() = 0;
// rc base global screenspace coordinate system, which has a flipped Y.
virtual void enableMouseEventTap(QRect rc, bool enabled) = 0; virtual void enableMouseEventTap(QRect rc, bool enabled) = 0;
private: private: