From 1b4d08e448e01a0d6fc86c203f2795b1fae1902a Mon Sep 17 00:00:00 2001 From: Kirk Date: Tue, 13 Oct 2026 03:12:20 +0800 Subject: [PATCH] fix: a touchpad tap holds its click long enough for games to see it (#644) --- .../launcher/input/TouchpadGestures.kt | 27 +++- .../launcher/input/TouchpadGesturesTest.kt | 140 ++++++++++++++++++ 2 files changed, 161 insertions(+), 6 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt 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 d17b096e94..a3ab87e5ab 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/TouchpadGestures.kt @@ -59,15 +59,31 @@ class TouchpadGestures( private var moverLastX = 0f private var moverLastY = 0f private var holdDrag = false + private var clicked = 0 private val longPress = Runnable { held = true dragging = true listener.onLongPress() listener.onButton(PointerGestures.BTN_LEFT, true, x, y) } + private val clickUp = Runnable { releaseClick() } + + private fun click(button: Int) { + releaseClick() + listener.onButton(button, true, x, y) + clicked = button + handler.postDelayed(clickUp, CLICK_MS) + } + + private fun releaseClick() { + handler.removeCallbacks(clickUp) + if (clicked != 0) listener.onButton(clicked, false, x, y) + clicked = 0 + } fun cancel() { handler.removeCallbacks(longPress) + releaseClick() if (dragging) listener.onButton(PointerGestures.BTN_LEFT, false, x, y) dragging = false held = false @@ -85,6 +101,7 @@ class TouchpadGestures( fun onTouch(event: MotionEvent): Boolean { when (event.actionMasked) { MotionEvent.ACTION_DOWN -> { + releaseClick() lastX = event.x; lastY = event.y downAt = event.eventTime travelled = 0f; held = false; twoFingers = false; scrolled = false; secondWasTap = false @@ -204,8 +221,7 @@ class TouchpadGestures( } // The second finger lifting quickly, with nothing scrolled: a right click. if (twoFingers && secondWasTap && event.eventTime - secondDownAt < TAP_MS && !scrolled) { - listener.onButton(PointerGestures.BTN_RIGHT, true, x, y) - listener.onButton(PointerGestures.BTN_RIGHT, false, x, y) + click(PointerGestures.BTN_RIGHT) secondWasTap = false } return true @@ -218,13 +234,11 @@ class TouchpadGestures( twoFingers -> { // Both fingers down and up together, no scroll: a right click. if (secondWasTap && !scrolled && !cancelled && event.eventTime - downAt < TAP_MS * 2) { - listener.onButton(PointerGestures.BTN_RIGHT, true, x, y) - listener.onButton(PointerGestures.BTN_RIGHT, false, x, y) + click(PointerGestures.BTN_RIGHT) } } !cancelled && travelled <= slopPx && event.eventTime - downAt < TAP_MS -> { - listener.onButton(PointerGestures.BTN_LEFT, true, x, y) - listener.onButton(PointerGestures.BTN_LEFT, false, x, y) + click(PointerGestures.BTN_LEFT) lastTapAt = event.eventTime } } @@ -239,6 +253,7 @@ class TouchpadGestures( companion object { private const val HOLD_MS = 500L private const val TAP_MS = 300L + private const val CLICK_MS = 50L private const val DOUBLE_TAP_MS = 300L private const val SCROLL_STEP_PX = 36f } diff --git a/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt new file mode 100644 index 0000000000..ddbf4d7f92 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/TouchpadGesturesTest.kt @@ -0,0 +1,140 @@ +package com.droiddeck.launcher.input + +import android.graphics.RectF +import android.os.Looper +import android.view.MotionEvent +import java.time.Duration +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE) +class TouchpadGesturesTest { + private val events = mutableListOf() + private lateinit var touchpad: TouchpadGestures + private var time = 1000L + private var downTime = 0L + + private val listener = object : PointerGestures.Listener { + override fun onMove(x: Float, y: Float) { events += "move" } + override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { + val name = if (button == PointerGestures.BTN_RIGHT) "right" else "left" + events += "$name ${if (pressed) "down" else "up"}" + } + override fun onWheel(steps: Int) { events += "wheel $steps" } + override fun onLongPress() { events += "long press" } + } + + @Before fun setUp() { + touchpad = TouchpadGestures(SLOP, listener) + touchpad.bounds = RectF(0f, 0f, 1920f, 1080f) + touchpad.place(960f, 540f) + } + + @Test fun tapHoldsTheLeftButtonBeforeReleasingIt() { + tap() + assertEquals(listOf("left down"), events) + advance(40) + assertEquals(listOf("left down"), events) + advance(100) + assertEquals(listOf("left down", "left up"), events) + } + + @Test fun twoFingerTapHoldsTheRightButtonBeforeReleasingIt() { + 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(60) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 310f)) + advance(10) + touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f)) + assertEquals(listOf("right down"), events) + advance(30) + assertEquals(listOf("right down"), events) + advance(100) + assertEquals(listOf("right down", "right up"), events) + } + + @Test fun slowTwoFingerTapHoldsTheRightButtonBeforeReleasingIt() { + 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(350) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 310f)) + advance(10) + touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f)) + assertEquals(listOf("right down"), events) + advance(40) + assertEquals(listOf("right down"), events) + advance(100) + assertEquals(listOf("right down", "right up"), events) + } + + @Test fun secondTapPressesOnlyAfterTheFirstClickIsReleased() { + tap() + advance(20) + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + assertEquals(listOf("left down", "left up", "left down"), events) + advance(80) + touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f)) + advance(200) + assertEquals(listOf("left down", "left up", "left down", "left up"), events) + } + + @Test fun secondFingerTappingAgainReleasesTheFirstRightClickFirst() { + 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(60) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 310f)) + advance(10) + touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 310f)) + advance(20) + touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 310f)) + assertEquals(listOf("right down", "right up", "right down"), events) + advance(200) + assertEquals(listOf("right down", "right up", "right down", "right up"), events) + } + + @Test fun cancelReleasesAClickThatIsStillHeld() { + tap() + touchpad.cancel() + assertEquals(listOf("left down", "left up"), events) + advance(200) + assertEquals(listOf("left down", "left up"), events) + } + + private fun tap() { + touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f)) + advance(80) + touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f)) + } + + private fun advance(ms: Long) { + time += ms + shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms)) + } + + private fun touch(action: Int, index: Int, vararg pointers: Pair>) { + if (action == MotionEvent.ACTION_DOWN) downTime = time + val properties = pointers.map { (id, _) -> + MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } + }.toTypedArray() + val coords = pointers.map { (_, at) -> + MotionEvent.PointerCoords().apply { x = at.first; y = at.second; pressure = 1f; size = 1f } + }.toTypedArray() + val masked = action or (index shl MotionEvent.ACTION_POINTER_INDEX_SHIFT) + val event = MotionEvent.obtain(downTime, time, masked, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0) + touchpad.onTouch(event) + event.recycle() + } + + private companion object { + const val SLOP = 8f + } +}