From 2d6393df242513daf624f1de1afa1739e771087a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Resul=20=C3=96v=C3=BCr?= <92828257+ovurrsl@users.noreply.github.com> Date: Tue, 20 Oct 2026 04:28:06 +0300 Subject: [PATCH] fix: keep touchpad two-finger scroll from jumping the cursor or starting a drag (#1142) Co-authored-by: ovurrsl --- .../launcher/input/TouchpadGestures.kt | 20 ++-- .../launcher/input/TouchpadGesturesTest.kt | 92 +++++++++++++++++++ 2 files changed, 105 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt b/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt index 2dc403a43a..47f0e4b64f 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt @@ -38,6 +38,7 @@ class TouchpadGestures( private var moverLastX = 0f private var moverLastY = 0f private var holdDrag = false + private var scrollLocked = false private var clicked = 0 private val longPress = Runnable { held = true @@ -67,6 +68,7 @@ class TouchpadGestures( dragging = false held = false holdDrag = false + scrollLocked = false anchorId = -1 moverId = -1 lastTapAt = 0L @@ -84,7 +86,7 @@ class TouchpadGestures( lastX = event.x; lastY = event.y downAt = event.eventTime travelled = 0f; held = false; twoFingers = false; scrolled = false; secondWasTap = false - anchorId = event.getPointerId(0); moverId = -1; holdDrag = false + anchorId = event.getPointerId(0); moverId = -1; holdDrag = false; scrollLocked = false if (event.eventTime - lastTapAt < DOUBLE_TAP_MS) { dragging = true listener.onButton(PointerGestures.BTN_LEFT, true, x, y) @@ -117,10 +119,12 @@ class TouchpadGestures( val mi = if (moverId == -1) -1 else event.findPointerIndex(moverId) if (ai >= 0 && mi >= 0) { val mx = event.getX(mi); val my = event.getY(mi) - if (!holdDrag && !scrolled) { + if (!holdDrag && !scrolled && !scrollLocked) { val anchorMoved = abs(event.getX(ai) - anchorStartX) + abs(event.getY(ai) - anchorStartY) val moverMoved = abs(mx - moverStartX) + abs(my - moverStartY) - if (moverMoved > slopPx && anchorMoved < slopPx) { + if (anchorMoved >= slopPx) { + scrollLocked = true + } else if (moverMoved > slopPx * HOLD_DRAG_MOVER_SLOPS && anchorMoved < slopPx * HOLD_DRAG_ANCHOR_SLOPS) { holdDrag = true secondWasTap = false if (!dragging) { @@ -186,9 +190,9 @@ class TouchpadGestures( if (gone == moverId) moverId = -1 if (gone == anchorId) { anchorId = event.getPointerId(if (event.actionIndex == 0) 1 else 0) - event.findPointerIndex(anchorId).takeIf { it >= 0 }?.let { - lastX = event.getX(it); lastY = event.getY(it) - } + } + event.findPointerIndex(anchorId).takeIf { it >= 0 && it != event.actionIndex }?.let { + lastX = event.getX(it); lastY = event.getY(it) } if (twoFingers && secondWasTap && event.eventTime - secondDownAt < TAP_MS && !scrolled) { click(PointerGestures.BTN_RIGHT) @@ -212,7 +216,7 @@ class TouchpadGestures( } } dragging = false; held = false; twoFingers = false - holdDrag = false; anchorId = -1; moverId = -1 + holdDrag = false; scrollLocked = false; anchorId = -1; moverId = -1 return true } } @@ -225,5 +229,7 @@ class TouchpadGestures( private const val CLICK_MS = 50L private const val DOUBLE_TAP_MS = 300L private const val SCROLL_STEP_PX = 36f + private const val HOLD_DRAG_MOVER_SLOPS = 2f + private const val HOLD_DRAG_ANCHOR_SLOPS = 0.5f } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt index ddbf4d7f92..a036849b19 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt @@ -5,6 +5,8 @@ import android.os.Looper import android.view.MotionEvent import java.time.Duration import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test import org.junit.runner.RunWith @@ -109,6 +111,96 @@ class TouchpadGesturesTest { assertEquals(listOf("left down", "left up"), events) } + @Test fun liftingTheSecondFingerAfterAScrollKeepsTheCursorStill() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 310f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 600f), 2 to (600f to 610f)) + assertTrue(events.contains("wheel -8")) + advance(10) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 600f), 2 to (600f to 610f)) + advance(10) + val sx = touchpad.x + val sy = touchpad.y + touch(MotionEvent.ACTION_MOVE, 0, 1 to (401f to 601f)) + assertEquals(sx, touchpad.x, 0.01f) + assertEquals(sy, touchpad.y, 0.01f) + assertFalse(events.contains("move")) + } + + @Test fun trackingResumesNormallyAfterTheScroll() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 310f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 600f), 2 to (600f to 610f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 600f), 2 to (600f to 610f)) + advance(10) + val sy = touchpad.y + touch(MotionEvent.ACTION_MOVE, 0, 1 to (401f to 621f)) + val delta = touchpad.y - sy + assertTrue(delta in 20f..65f) + assertTrue(events.contains("move")) + } + + @Test fun liftingTheAnchorFirstAfterAScrollKeepsTheCursorStill() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 310f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 600f), 2 to (600f to 610f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_UP, 0, 1 to (400f to 600f), 2 to (600f to 610f)) + advance(10) + val sx = touchpad.x + val sy = touchpad.y + touch(MotionEvent.ACTION_MOVE, 0, 2 to (601f to 611f)) + assertEquals(sx, touchpad.x, 0.01f) + assertEquals(sy, touchpad.y, 0.01f) + assertFalse(events.contains("move")) + } + + @Test fun twoFingerScrollWithLeadingFingerDoesNotPressLeft() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 303f), 2 to (600f to 310f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 340f), 2 to (600f to 350f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 380f), 2 to (600f to 390f)) + assertFalse(events.contains("left down")) + assertTrue(events.any { it.startsWith("wheel ") }) + } + + @Test fun holdDragStillWorks() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 300f), 2 to (620f to 300f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 300f), 2 to (640f to 300f)) + assertEquals("left down", events.firstOrNull()) + assertTrue(events.contains("move")) + touch(MotionEvent.ACTION_POINTER_UP, 0, 1 to (400f to 300f), 2 to (640f to 300f)) + assertTrue(events.contains("left up")) + } + + @Test fun anchorMovedLocksOutHoldDrag() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 300f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 309f), 2 to (600f to 302f)) + advance(10) + touch(MotionEvent.ACTION_MOVE, 0, 1 to (400f to 309f), 2 to (640f to 302f)) + assertFalse(events.contains("left down")) + } + private fun tap() { touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) advance(80)