mirror of
https://github.com/barry-ran/QtScrcpy.git
synced 2025-08-05 15:08:39 +00:00
refactor: GrabCursor
This commit is contained in:
parent
09331711c8
commit
81a0e9544c
7 changed files with 43 additions and 45 deletions
|
@ -7,9 +7,6 @@
|
||||||
#include "ui_toolform.h"
|
#include "ui_toolform.h"
|
||||||
#include "iconhelper.h"
|
#include "iconhelper.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
#include "videoform.h"
|
|
||||||
#include "controller.h"
|
|
||||||
#include "adbprocess.h"
|
|
||||||
|
|
||||||
ToolForm::ToolForm(QWidget* adsorbWidget, AdsorbPositions adsorbPos)
|
ToolForm::ToolForm(QWidget* adsorbWidget, AdsorbPositions adsorbPos)
|
||||||
: MagneticWidget(adsorbWidget, adsorbPos)
|
: MagneticWidget(adsorbWidget, adsorbPos)
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
#include "toolform.h"
|
#include "toolform.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
#include "filehandler.h"
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
|
@ -73,8 +72,32 @@ void VideoForm::initUI()
|
||||||
|
|
||||||
void VideoForm::onGrabCursor(bool grab)
|
void VideoForm::onGrabCursor(bool grab)
|
||||||
{
|
{
|
||||||
#if defined(Q_OS_WIN32) || defined(Q_OS_OSX)
|
#if defined(Q_OS_WIN32)
|
||||||
MouseTap::getInstance()->enableMouseEventTap(m_videoWidget, grab);
|
QRect rc;
|
||||||
|
rc = QRect(m_videoWidget->parentWidget()->mapToGlobal(m_videoWidget->pos())
|
||||||
|
, m_videoWidget->size());
|
||||||
|
// high dpi support
|
||||||
|
rc.setTopLeft(rc.topLeft() * m_videoWidget->devicePixelRatio());
|
||||||
|
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);
|
||||||
|
|
||||||
|
rc.setX(rc.x() + 100);
|
||||||
|
rc.setY(rc.y() + 30);
|
||||||
|
rc.setWidth(rc.width() - 180);
|
||||||
|
rc.setHeight(rc.height() - 60);
|
||||||
|
|
||||||
|
MouseTap::getInstance()->enableMouseEventTap(rc, grab);
|
||||||
#else
|
#else
|
||||||
Q_UNUSED(grab)
|
Q_UNUSED(grab)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -15,7 +15,7 @@ public:
|
||||||
|
|
||||||
void initMouseEventTap() override;
|
void initMouseEventTap() override;
|
||||||
void quitMouseEventTap() override;
|
void quitMouseEventTap() override;
|
||||||
void enableMouseEventTap(QWidget* widget, bool enabled) override;
|
void enableMouseEventTap(QRect rc, bool enabled) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void run() override;
|
void run() override;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QWidget>
|
|
||||||
|
|
||||||
#include "cocoamousetap.h"
|
#include "cocoamousetap.h"
|
||||||
|
|
||||||
|
@ -20,55 +19,37 @@ typedef struct MouseEventTapData{
|
||||||
CFMachPortRef tap = Q_NULLPTR;
|
CFMachPortRef tap = Q_NULLPTR;
|
||||||
CFRunLoopRef runloop = Q_NULLPTR;
|
CFRunLoopRef runloop = Q_NULLPTR;
|
||||||
CFRunLoopSourceRef runloopSource = Q_NULLPTR;
|
CFRunLoopSourceRef runloopSource = Q_NULLPTR;
|
||||||
QWidget* widget = Q_NULLPTR;
|
QRect rc;
|
||||||
} MouseEventTapData;
|
} MouseEventTapData;
|
||||||
|
|
||||||
static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
|
static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
|
||||||
{
|
{
|
||||||
Q_UNUSED(proxy);
|
Q_UNUSED(proxy);
|
||||||
MouseEventTapData *tapdata = (MouseEventTapData*)refcon;
|
MouseEventTapData *tapdata = (MouseEventTapData*)refcon;
|
||||||
|
|
||||||
NSView *nsview;
|
|
||||||
NSWindow *nswindow;
|
|
||||||
NSRect windowRect;
|
|
||||||
NSRect newWindowRect;
|
|
||||||
CGPoint eventLocation;
|
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case kCGEventTapDisabledByTimeout:
|
case kCGEventTapDisabledByTimeout:
|
||||||
{
|
{
|
||||||
CGEventTapEnable(tapdata->tap, true);
|
CGEventTapEnable(tapdata->tap, true);
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
case kCGEventTapDisabledByUserInput:
|
case kCGEventTapDisabledByUserInput:
|
||||||
{
|
{
|
||||||
return NULL;
|
return nullptr;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!tapdata->widget) {
|
if (tapdata->rc.isEmpty()) {
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
// get nswindow from qt widget
|
|
||||||
nsview = (NSView *)tapdata->widget->window()->winId();
|
|
||||||
if (!nsview) {
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
nswindow = [nsview window];
|
|
||||||
|
|
||||||
eventLocation = CGEventGetUnflippedLocation(event);
|
NSRect windowRect = NSMakeRect(tapdata->rc.left(), tapdata->rc.top(),
|
||||||
windowRect = [nswindow contentRectForFrameRect:[nswindow frame]];
|
tapdata->rc.width(), tapdata->rc.height());
|
||||||
|
NSRect newWindowRect = NSMakeRect(windowRect.origin.x, windowRect.origin.y,
|
||||||
windowRect.origin.x += 100;
|
|
||||||
windowRect.origin.y += 30;
|
|
||||||
windowRect.size.width -= 180;
|
|
||||||
windowRect.size.height -= 60;
|
|
||||||
|
|
||||||
newWindowRect = NSMakeRect(windowRect.origin.x, windowRect.origin.y,
|
|
||||||
windowRect.size.width - 10, windowRect.size.height - 10);
|
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;
|
//qDebug() << newWindowRect.origin.x << newWindowRect.origin.y << newWindowRect.size.width << newWindowRect.size.height;
|
||||||
|
|
||||||
if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), newWindowRect, NO)) {
|
if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), newWindowRect, NO)) {
|
||||||
|
@ -171,11 +152,11 @@ void CocoaMouseTap::quitMouseEventTap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CocoaMouseTap::enableMouseEventTap(QWidget* widget, bool enabled)
|
void CocoaMouseTap::enableMouseEventTap(QRect rc, bool enabled)
|
||||||
{
|
{
|
||||||
if (m_tapData && m_tapData->tap)
|
if (m_tapData && m_tapData->tap)
|
||||||
{
|
{
|
||||||
enabled ? m_tapData->widget = widget : m_tapData->widget = Q_NULLPTR;
|
enabled ? m_tapData->rc = rc : m_tapData->rc = QRect();
|
||||||
CGEventTapEnable(m_tapData->tap, enabled);
|
CGEventTapEnable(m_tapData->tap, enabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
#ifndef MOUSETAP_H
|
#ifndef MOUSETAP_H
|
||||||
#define MOUSETAP_H
|
#define MOUSETAP_H
|
||||||
|
#include <QRect>
|
||||||
|
|
||||||
class QWidget;
|
class QWidget;
|
||||||
class MouseTap {
|
class MouseTap {
|
||||||
public:
|
public:
|
||||||
static MouseTap* getInstance();
|
static MouseTap* getInstance();
|
||||||
virtual void initMouseEventTap() = 0;
|
virtual void initMouseEventTap() = 0;
|
||||||
virtual void quitMouseEventTap() = 0;
|
virtual void quitMouseEventTap() = 0;
|
||||||
virtual void enableMouseEventTap(QWidget* widget, bool enabled) = 0;
|
virtual void enableMouseEventTap(QRect rc, bool enabled) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static MouseTap *s_instance;
|
static MouseTap *s_instance;
|
||||||
|
|
|
@ -24,17 +24,12 @@ void WinMouseTap::quitMouseEventTap()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WinMouseTap::enableMouseEventTap(QWidget *widget, bool enabled)
|
void WinMouseTap::enableMouseEventTap(QRect rc, bool enabled)
|
||||||
{
|
{
|
||||||
if (!widget) {
|
if (enabled && rc.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(enabled) {
|
if(enabled) {
|
||||||
QRect rc(widget->parentWidget()->mapToGlobal(widget->pos())
|
|
||||||
, widget->size());
|
|
||||||
// high dpi support
|
|
||||||
rc.setTopLeft(rc.topLeft() * widget->devicePixelRatio());
|
|
||||||
rc.setBottomRight(rc.bottomRight() * widget->devicePixelRatio());
|
|
||||||
RECT mainRect;
|
RECT mainRect;
|
||||||
mainRect.left = (LONG)rc.left();
|
mainRect.left = (LONG)rc.left();
|
||||||
mainRect.right = (LONG)rc.right();
|
mainRect.right = (LONG)rc.right();
|
||||||
|
|
|
@ -11,7 +11,7 @@ public:
|
||||||
|
|
||||||
void initMouseEventTap() override;
|
void initMouseEventTap() override;
|
||||||
void quitMouseEventTap() override;
|
void quitMouseEventTap() override;
|
||||||
void enableMouseEventTap(QWidget* widget, bool enabled) override;
|
void enableMouseEventTap(QRect rc, bool enabled) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // WINMOUSETAP_H
|
#endif // WINMOUSETAP_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue