fix: mousetap on mac

This commit is contained in:
rankun 2020-03-07 21:49:39 +08:00
parent 104fbc7061
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());
MouseTap::getInstance()->enableMouseEventTap(rc, grab);
#elif defined(Q_OS_OSX)
// get nswindow from qt widget
NSView *nsview = (NSView *)m_videoWidget->window()->winId();
if (!nsview) {
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);
QRect rc = m_videoWidget->geometry();
rc.setTopLeft(m_videoWidget->mapToGlobal(rc.topLeft()));
rc.setBottomRight(m_videoWidget->mapToGlobal(rc.bottomRight()));
rc.setX(rc.x() + 100);
rc.setY(rc.y() + 30);
rc.setWidth(rc.width() - 180);

View file

@ -45,33 +45,29 @@ static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type
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());
NSRect newWindowRect = NSMakeRect(windowRect.origin.x, windowRect.origin.y,
windowRect.size.width - 10, windowRect.size.height - 10);
CGPoint eventLocation = CGEventGetUnflippedLocation(event);
//qDebug() << newWindowRect.origin.x << newWindowRect.origin.y << newWindowRect.size.width << newWindowRect.size.height;
if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), newWindowRect, NO)) {
/* This is in CGs global screenspace coordinate system, which has a
* flipped Y.
*/
CGPoint newLocation = CGEventGetLocation(event);
if (eventLocation.x < NSMinX(windowRect)) {
newLocation.x = NSMinX(windowRect);
} else if (eventLocation.x >= NSMaxX(windowRect)) {
newLocation.x = NSMaxX(windowRect) - 1.0;
// check rect samll than limit rect
NSRect checkWindowRect = NSMakeRect(limitWindowRect.origin.x + 10, limitWindowRect.origin.y + 10,
limitWindowRect.size.width - 10, limitWindowRect.size.height - 10);
/* This is in CGs global screenspace coordinate system, which has a
* flipped Y.
*/
CGPoint eventLocation = CGEventGetLocation(event);
if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), checkWindowRect, NO)) {
if (eventLocation.x <= NSMinX(limitWindowRect)) {
eventLocation.x = NSMinX(limitWindowRect) + 1.0;
} else if (eventLocation.x >= NSMaxX(limitWindowRect)) {
eventLocation.x = NSMaxX(limitWindowRect) - 1.0;
}
if (eventLocation.y <= NSMinY(windowRect)) {
newLocation.y -= (NSMinY(windowRect) - eventLocation.y + 1);
} else if (eventLocation.y > NSMaxY(windowRect)) {
newLocation.y += (eventLocation.y - NSMaxY(windowRect));
if (eventLocation.y <= NSMinY(limitWindowRect)) {
eventLocation.y = NSMinY(limitWindowRect) + 1.0;
} else if (eventLocation.y >= NSMaxY(limitWindowRect)) {
eventLocation.y = NSMaxY(limitWindowRect) - 1.0;
}
CGWarpMouseCursorPosition(newLocation);
CGWarpMouseCursorPosition(eventLocation);
CGAssociateMouseAndMouseCursorPosition(YES);
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
* behaves strangely.
*/
CGEventSetLocation(event, newLocation);
CGEventSetLocation(event, eventLocation);
}
}

View file

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