From c8fc80e0379db548a4bed50c26c0f186a899aefa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 21 Sep 2023 13:08:15 +0200 Subject: [PATCH] Allow swiping over widgets --- .../launcher/views/MyAppWidgetHostView.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetHostView.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetHostView.kt index 112836d..7ce1bba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetHostView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetHostView.kt @@ -7,6 +7,7 @@ import android.os.Handler import android.view.MotionEvent import android.view.ViewConfiguration import com.simplemobiletools.launcher.R +import kotlin.math.abs class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { private var longPressHandler = Handler() @@ -51,10 +52,14 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { MotionEvent.ACTION_MOVE -> { currentCoords.x = event.rawX currentCoords.y = event.rawY + if (hasFingerMoved(event.rawX, event.rawY)) { + resetTouches() + return true + } } MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - longPressHandler.removeCallbacksAndMessages(null) + resetTouches() } } @@ -62,7 +67,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { } private val longPressRunnable = Runnable { - if (Math.abs(actionDownCoords.x - currentCoords.x) < moveGestureThreshold && Math.abs(actionDownCoords.y - currentCoords.y) < moveGestureThreshold) { + if (!hasFingerMoved(currentCoords.x, currentCoords.y)) { longPressHandler.removeCallbacksAndMessages(null) hasLongPressed = true longPressListener?.invoke(actionDownCoords.x, actionDownCoords.y) @@ -72,4 +77,7 @@ class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) { fun resetTouches() { longPressHandler.removeCallbacksAndMessages(null) } + + private fun hasFingerMoved(x: Float, y: Float) = + ((abs(actionDownCoords.x - x) > moveGestureThreshold) || (abs(actionDownCoords.y - y) > moveGestureThreshold)) }