From 5f180d909b0f7fd578a675be913534573d9be3b9 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Fri, 6 Nov 2026 13:56:25 -0500 Subject: [PATCH] fix(input): Click a touch stick only on a fast double tap (#2173) * fix(input): Click a touch stick only on a fast double tap * fix(input): Count the release position in stick tap travel * fix(input): Treat cancelled pointer-ups as cancelled stick touches --- .../launcher/input/OnScreenControls.kt | 24 +++++++- .../launcher/input/OnScreenControlsTest.kt | 57 ++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt index cd252c5255..dc26a75f55 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt @@ -15,6 +15,7 @@ import android.view.View import android.view.WindowInsets import com.droiddeck.launcher.session.SessionPrefs import kotlin.math.abs +import kotlin.math.hypot import kotlin.math.max import kotlin.math.min import kotlin.math.sin @@ -48,6 +49,10 @@ class OnScreenControls( var dirty = false var clicked = false var lastUp = 0L + var downAt = 0L + var downX = 0f + var downY = 0f + var travel = 0f val wide = id in shoulderIds val halfW get() = if (wide) radius * SHOULDER_WIDTH else radius val halfH get() = if (wide) radius * SHOULDER_HEIGHT else radius @@ -61,6 +66,7 @@ class OnScreenControls( } fun drag(x: Float, y: Float): Boolean { + travel = max(travel, hypot(x - downX, y - downY)) var dx = x - ax var dy = y - ay val d = sqrt(dx * dx + dy * dy) @@ -73,6 +79,14 @@ class OnScreenControls( } private val stickRedraw = ThrottledRedraw(this, STICK_REDRAW_MS) + private val tapSlop = android.view.ViewConfiguration.get(context).scaledTouchSlop.toFloat() + + private fun stickTap(control: Control, event: MotionEvent): Boolean { + val i = event.actionIndex + val travel = max(control.travel, hypot(event.getX(i) - control.downX, event.getY(i) - control.downY)) + return stickTap(control.downAt, event.eventTime, travel, tapSlop, control.clicked, + event.actionMasked == MotionEvent.ACTION_CANCEL || (event.flags and MotionEvent.FLAG_CANCELED) != 0) + } private val controls = listOf( Control("up", "dpad", -1, 26f, 0f, -52f), @@ -545,6 +559,10 @@ class OnScreenControls( control.pressedBy = event.getPointerId(index) if (control.stick >= 0) { control.clicked = settings.stickClick && control.lastUp > 0L && event.eventTime - control.lastUp < DOUBLE_TAP_MS + control.downAt = event.eventTime + control.downX = event.getX(index) + control.downY = event.getY(index) + control.travel = 0f val adaptive = settings.adaptiveSticks control.ax = if (adaptive) event.getX(index) else control.cx control.ay = if (adaptive) event.getY(index) else control.cy @@ -613,7 +631,7 @@ class OnScreenControls( for (control in controls) { if (control.pressedBy == pointer || event.actionMasked == MotionEvent.ACTION_CANCEL) { if (control.pressedBy != -1) changed = true - if (control.stick >= 0 && control.pressedBy != -1) control.lastUp = if (control.clicked || event.actionMasked == MotionEvent.ACTION_CANCEL) 0L else event.eventTime + if (control.stick >= 0 && control.pressedBy != -1) control.lastUp = if (stickTap(control, event)) event.eventTime else 0L control.pressedBy = -1 control.clicked = false if (control.stick >= 0 && (control.kx != 0f || control.ky != 0f)) control.dirty = true @@ -794,6 +812,10 @@ class OnScreenControls( private companion object { const val DOUBLE_TAP_MS = 300L + const val STICK_TAP_MS = 200L + + internal fun stickTap(downAt: Long, upAt: Long, travel: Float, slop: Float, clicked: Boolean, cancelled: Boolean): Boolean = + !clicked && !cancelled && upAt - downAt <= STICK_TAP_MS && travel <= slop const val STICK_REDRAW_MS = 33L const val MIN_STICK_MOVE_PX = 1f const val MIN_FACE_MM = 9.5f diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt index 646df7795a..b071b04906 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt @@ -169,6 +169,59 @@ class OnScreenControlsTest { assertEquals(true, field(control("ls"), "clicked")) } + @Test fun aCancelledSecondFingerDoesNotArmAStickClick() { + touch(MotionEvent.ACTION_DOWN, 7 to (660f to 400f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f), flags = MotionEvent.FLAG_CANCELED) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) + assertEquals(false, field(control("ls"), "clicked")) + touch(MotionEvent.ACTION_POINTER_UP or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) + touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (660f to 400f), 9 to (540f to 400f)) + assertEquals(true, field(control("ls"), "clicked")) + } + + @Test fun movingTheStickAndTouchingItAgainDoesNotClickIt() { + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + touch(MotionEvent.ACTION_MOVE, 1 to (600f to 400f)) + touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + assertEquals(false, field(control("ls"), "clicked")) + } + + @Test fun liftingFarFromTheTouchPointDoesNotArmAStickClick() { + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + assertEquals(false, field(control("ls"), "clicked")) + } + + @Test fun aLongHoldThenAQuickTouchDoesNotClickTheStick() { + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + time += 600 + touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + assertEquals(false, field(control("ls"), "clicked")) + } + + @Test fun aFastDoubleTapClicksTheStickAndCanBeHeldWhileMoving() { + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + touch(MotionEvent.ACTION_MOVE, 1 to (600f to 400f)) + assertEquals(true, field(control("ls"), "clicked")) + touch(MotionEvent.ACTION_UP, 1 to (600f to 400f)) + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + assertEquals(false, field(control("ls"), "clicked")) + } + + @Test fun aSlowSecondTapDoesNotClickTheStick() { + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + touch(MotionEvent.ACTION_UP, 1 to (540f to 400f)) + time += 400 + touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)) + assertEquals(false, field(control("ls"), "clicked")) + } + @Test fun buttonsTakePriorityOverAdaptiveRegions() { val button = control("a") touch(MotionEvent.ACTION_DOWN, 3 to (value(button, "cx") to value(button, "cy"))) @@ -461,10 +514,10 @@ class OnScreenControlsTest { return color } - private fun touch(action: Int, vararg pointers: Pair>): Boolean { + private fun touch(action: Int, vararg pointers: Pair>, flags: Int = 0): Boolean { val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } }.toTypedArray() val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray() - val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0) + val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, flags) return try { view.onTouchEvent(event) } finally { event.recycle() } } }