diff --git a/Sources/MWBMac/CaptureQueue.swift b/Sources/MWBMac/CaptureQueue.swift index 8bf925e..cd598a0 100644 --- a/Sources/MWBMac/CaptureQueue.swift +++ b/Sources/MWBMac/CaptureQueue.swift @@ -120,7 +120,7 @@ public final class CaptureQueue: Sendable { return next // the latest pointer position is the only one that matters case (.mouse(let old), .mouse(let new)): if old.flags == WindowsMessage.mouseMove && new.flags == WindowsMessage.mouseMove { - return next + return .mouse(mergedMove(old, new)) } if old.flags == new.flags, new.flags == WindowsMessage.wheel || new.flags == WindowsMessage.horizontalWheel @@ -134,4 +134,25 @@ public final class CaptureQueue: Sendable { return nil } } + + /// Two consecutive moves become one. + /// + /// An absolute move carries a position, so the newest one wins. A *relative* + /// move carries a delta (`MoveMouseRelatively`, docs/protocol.md §4.4), and + /// dropping the older one would throw away real motion — those are summed, + /// untagged and re-tagged so the `MOVE_MOUSE_RELATIVE` bias is applied once. + private func mergedMove(_ old: MouseData, _ new: MouseData) -> MouseData { + let oldPoint = UniversalPoint(x: old.x, y: old.y) + let newPoint = UniversalPoint(x: new.x, y: new.y) + guard Coordinates.isRelative(oldPoint), Coordinates.isRelative(newPoint) else { + return new + } + let a = Coordinates.untagRelative(oldPoint) + let b = Coordinates.untagRelative(newPoint) + let summed = Coordinates.tagRelative(dx: a.x &+ b.x, dy: a.y &+ b.y) + var merged = new + merged.x = summed.x + merged.y = summed.y + return merged + } }