mirror of
https://github.com/barry-ran/QtScrcpy.git
synced 2025-04-20 03:25:02 +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 "iconhelper.h"
|
||||
#include "device.h"
|
||||
#include "videoform.h"
|
||||
#include "controller.h"
|
||||
#include "adbprocess.h"
|
||||
|
||||
ToolForm::ToolForm(QWidget* adsorbWidget, AdsorbPositions adsorbPos)
|
||||
: MagneticWidget(adsorbWidget, adsorbPos)
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
#include "toolform.h"
|
||||
#include "device.h"
|
||||
#include "controller.h"
|
||||
#include "filehandler.h"
|
||||
#include "config.h"
|
||||
extern "C"
|
||||
{
|
||||
|
@ -73,8 +72,32 @@ void VideoForm::initUI()
|
|||
|
||||
void VideoForm::onGrabCursor(bool grab)
|
||||
{
|
||||
#if defined(Q_OS_WIN32) || defined(Q_OS_OSX)
|
||||
MouseTap::getInstance()->enableMouseEventTap(m_videoWidget, grab);
|
||||
#if defined(Q_OS_WIN32)
|
||||
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
|
||||
Q_UNUSED(grab)
|
||||
#endif
|
||||
|
|
|
@ -15,7 +15,7 @@ public:
|
|||
|
||||
void initMouseEventTap() override;
|
||||
void quitMouseEventTap() override;
|
||||
void enableMouseEventTap(QWidget* widget, bool enabled) override;
|
||||
void enableMouseEventTap(QRect rc, bool enabled) override;
|
||||
|
||||
protected:
|
||||
void run() override;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#import <Cocoa/Cocoa.h>
|
||||
#include <QDebug>
|
||||
#include <QWidget>
|
||||
|
||||
#include "cocoamousetap.h"
|
||||
|
||||
|
@ -20,55 +19,37 @@ typedef struct MouseEventTapData{
|
|||
CFMachPortRef tap = Q_NULLPTR;
|
||||
CFRunLoopRef runloop = Q_NULLPTR;
|
||||
CFRunLoopSourceRef runloopSource = Q_NULLPTR;
|
||||
QWidget* widget = Q_NULLPTR;
|
||||
QRect rc;
|
||||
} MouseEventTapData;
|
||||
|
||||
static CGEventRef Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
|
||||
{
|
||||
Q_UNUSED(proxy);
|
||||
MouseEventTapData *tapdata = (MouseEventTapData*)refcon;
|
||||
|
||||
NSView *nsview;
|
||||
NSWindow *nswindow;
|
||||
NSRect windowRect;
|
||||
NSRect newWindowRect;
|
||||
CGPoint eventLocation;
|
||||
|
||||
switch (type) {
|
||||
case kCGEventTapDisabledByTimeout:
|
||||
{
|
||||
CGEventTapEnable(tapdata->tap, true);
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
case kCGEventTapDisabledByUserInput:
|
||||
{
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (!tapdata->widget) {
|
||||
if (tapdata->rc.isEmpty()) {
|
||||
return event;
|
||||
}
|
||||
// get nswindow from qt widget
|
||||
nsview = (NSView *)tapdata->widget->window()->winId();
|
||||
if (!nsview) {
|
||||
return event;
|
||||
}
|
||||
nswindow = [nsview window];
|
||||
|
||||
eventLocation = CGEventGetUnflippedLocation(event);
|
||||
windowRect = [nswindow contentRectForFrameRect:[nswindow frame]];
|
||||
|
||||
windowRect.origin.x += 100;
|
||||
windowRect.origin.y += 30;
|
||||
windowRect.size.width -= 180;
|
||||
windowRect.size.height -= 60;
|
||||
|
||||
newWindowRect = NSMakeRect(windowRect.origin.x, windowRect.origin.y,
|
||||
NSRect windowRect = 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)) {
|
||||
|
@ -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)
|
||||
{
|
||||
enabled ? m_tapData->widget = widget : m_tapData->widget = Q_NULLPTR;
|
||||
enabled ? m_tapData->rc = rc : m_tapData->rc = QRect();
|
||||
CGEventTapEnable(m_tapData->tap, enabled);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
#ifndef MOUSETAP_H
|
||||
#define MOUSETAP_H
|
||||
#include <QRect>
|
||||
|
||||
class QWidget;
|
||||
class MouseTap {
|
||||
public:
|
||||
static MouseTap* getInstance();
|
||||
virtual void initMouseEventTap() = 0;
|
||||
virtual void quitMouseEventTap() = 0;
|
||||
virtual void enableMouseEventTap(QWidget* widget, bool enabled) = 0;
|
||||
virtual void enableMouseEventTap(QRect rc, bool enabled) = 0;
|
||||
|
||||
private:
|
||||
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;
|
||||
}
|
||||
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;
|
||||
mainRect.left = (LONG)rc.left();
|
||||
mainRect.right = (LONG)rc.right();
|
||||
|
|
|
@ -11,7 +11,7 @@ public:
|
|||
|
||||
void initMouseEventTap() override;
|
||||
void quitMouseEventTap() override;
|
||||
void enableMouseEventTap(QWidget* widget, bool enabled) override;
|
||||
void enableMouseEventTap(QRect rc, bool enabled) override;
|
||||
};
|
||||
|
||||
#endif // WINMOUSETAP_H
|
||||
|
|
Loading…
Add table
Reference in a new issue