[network/kdeconnect-kde] plugins/mousepad: Fix drag-and-drop on macos

Albert Vaca Cintora <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 5a32c78b6c83c047dc094d947681fed5468951e4 by Albert Vaca Cintora.
Committed on 26/07/2026 at 21:33.
Pushed by albertvaka into branch 'master'.

Fix drag-and-drop on macos

M  +7    -0    plugins/mousepad/macosremoteinput.h
M  +24   -6    plugins/mousepad/macosremoteinput.mm

https://invent.kde.org/network/kdeconnect-kde/-/commit/5a32c78b6c83c047dc094d947681fed5468951e4

diff --git a/plugins/mousepad/macosremoteinput.h b/plugins/mousepad/macosremoteinput.h
index 2806e0869..3b7028d4b 100644
--- a/plugins/mousepad/macosremoteinput.h
+++ b/plugins/mousepad/macosremoteinput.h
@@ -8,6 +8,8 @@
 
 #include "abstractremoteinput.h"
 
+#include <QPoint>
+
 class MacOSRemoteInput : public AbstractRemoteInput
 {
     Q_OBJECT
@@ -17,4 +19,9 @@ public:
 
     bool handlePacket(const NetworkPacket &np) override;
     bool hasKeyboardSupport() override;
+
+private:
+    void setMousePos(const QPoint &pos);
+
+    bool m_leftButtonPressed = false;
 };
diff --git a/plugins/mousepad/macosremoteinput.mm b/plugins/mousepad/macosremoteinput.mm
index 7a23769ac..092eec6be 100644
--- a/plugins/mousepad/macosremoteinput.mm
+++ b/plugins/mousepad/macosremoteinput.mm
@@ -93,6 +93,7 @@ bool MacOSRemoteInput::handlePacket(const NetworkPacket& np)
             CGEventSetType(event, kCGEventLeftMouseUp);
             CGEventPost(kCGHIDEventTap, event);
             CFRelease(event);
+            m_leftButtonPressed = false;
         } else if (isDoubleClick) {
             CGEventRef event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(point.x(), point.y()), kCGMouseButtonLeft);
             CGEventPost(kCGHIDEventTap, event);
@@ -110,26 +111,29 @@ bool MacOSRemoteInput::handlePacket(const NetworkPacket& np)
             CGEventSetType(event, kCGEventLeftMouseUp);
             CGEventPost(kCGHIDEventTap, event);
             CFRelease(event);
-		} else if (isMiddleClick) {
+            m_leftButtonPressed = false;
+        } else if (isMiddleClick) {
             CGEventRef event = CGEventCreateMouseEvent(NULL, kCGEventOtherMouseDown, CGPointMake(point.x(), point.y()), kCGMouseButtonLeft);
             CGEventPost(kCGHIDEventTap, event);
             CGEventSetType(event, kCGEventOtherMouseUp);
             CGEventPost(kCGHIDEventTap, event);
             CFRelease(event);
-		} else if (isRightClick) {
+        } else if (isRightClick) {
             CGEventRef event = CGEventCreateMouseEvent(NULL, kCGEventRightMouseDown, CGPointMake(point.x(), point.y()), kCGMouseButtonLeft);
             CGEventPost(kCGHIDEventTap, event);
             CGEventSetType(event, kCGEventRightMouseUp);
             CGEventPost(kCGHIDEventTap, event);
             CFRelease(event);
-		} else if (isSingleHold) {
+        } else if (isSingleHold) {
             CGEventRef event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, CGPointMake(point.x(), point.y()), kCGMouseButtonLeft);
             CGEventPost(kCGHIDEventTap, event);
             CFRelease(event);
+            m_leftButtonPressed = true;
         } else if (isSingleRelease) {
             CGEventRef event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseUp, CGPointMake(point.x(), point.y()), kCGMouseButtonLeft);
             CGEventPost(kCGHIDEventTap, event);
             CFRelease(event);
+            m_leftButtonPressed = false;
         } else if (isScroll) {
             CGEventRef event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 1, dy);
             CGEventPost(kCGHIDEventTap, event);
@@ -216,11 +220,11 @@ bool MacOSRemoteInput::handlePacket(const NetworkPacket& np)
 
         }
     } else { //Is a mouse move event
-         if (dx || dy) {
+        if (dx || dy) {
             QPoint point = QCursor::pos();
-            QCursor::setPos(point.x() + (int)dx, point.y() + (int)dy);
+            setMousePos(QPoint(point.x() + (int)dx, point.y() + (int)dy));
         } else if (np.has(QStringLiteral("x")) || np.has(QStringLiteral("y"))) {
-            QCursor::setPos(x, y);
+            setMousePos(QPoint((int)x, (int)y));
         }
     }
     return true;
@@ -230,4 +234,18 @@ bool MacOSRemoteInput::hasKeyboardSupport() {
     return true;
 }
 
+void MacOSRemoteInput::setMousePos(const QPoint &pos)
+{
+    if (m_leftButtonPressed) {
+        // Moving the cursor via QCursor::setPos() with a button pressed does not
+        // generate a kCGEventLeftMouseDragged event, so without this apps never
+        // see a "mouse dragged" event and the mouse moves without dragging anything.
+        CGEventRef event = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDragged, CGPointMake(pos.x(), pos.y()), kCGMouseButtonLeft);
+        CGEventPost(kCGHIDEventTap, event);
+        CFRelease(event);
+    } else {
+        QCursor::setPos(pos);
+    }
+}
+
 #include "moc_macosremoteinput.cpp"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.