From 47940811b4d8c6aece6ed8073044fd3c85db1db0 Mon Sep 17 00:00:00 2001 From: Josia Pietsch Date: Sun, 16 Mar 2025 02:30:49 +0100 Subject: [PATCH] fix #126 --- .../jrpie/android/launcher/ui/HomeActivity.kt | 13 ++++ .../launcher/ui/TouchGestureDetector.kt | 65 +++++++++++++++---- 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/de/jrpie/android/launcher/ui/HomeActivity.kt b/app/src/main/java/de/jrpie/android/launcher/ui/HomeActivity.kt index 7875473..14e04d5 100644 --- a/app/src/main/java/de/jrpie/android/launcher/ui/HomeActivity.kt +++ b/app/src/main/java/de/jrpie/android/launcher/ui/HomeActivity.kt @@ -69,10 +69,23 @@ class HomeActivity : UIObject, AppCompatActivity() { LauncherPreferences.enabled_gestures().edgeSwipeEdgeWidth() / 100f ) + + // Initialise layout binding = HomeBinding.inflate(layoutInflater) + setContentView(binding.root) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + binding.root.setOnApplyWindowInsetsListener { _, windowInsets -> + val insets = windowInsets.systemGestureInsets + touchGestureDetector.setSystemGestureInsets(insets) + + windowInsets + } + } + + // Handle back key / gesture on Android 13+, cf. onKeyDown() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { diff --git a/app/src/main/java/de/jrpie/android/launcher/ui/TouchGestureDetector.kt b/app/src/main/java/de/jrpie/android/launcher/ui/TouchGestureDetector.kt index 74b8351..9000fa8 100644 --- a/app/src/main/java/de/jrpie/android/launcher/ui/TouchGestureDetector.kt +++ b/app/src/main/java/de/jrpie/android/launcher/ui/TouchGestureDetector.kt @@ -1,10 +1,13 @@ package de.jrpie.android.launcher.ui import android.content.Context +import android.graphics.Insets +import android.os.Build import android.os.Handler import android.os.Looper import android.view.MotionEvent import android.view.ViewConfiguration +import androidx.annotation.RequiresApi import de.jrpie.android.launcher.actions.Gesture import de.jrpie.android.launcher.preferences.LauncherPreferences import kotlin.math.abs @@ -31,20 +34,29 @@ class TouchGestureDetector( private val longPressHandler = Handler(Looper.getMainLooper()) + private var systemGestureInsetTop = 100 + private var systemGestureInsetBottom = 0 + private var systemGestureInsetLeft = 0 + private var systemGestureInsetRight = 0 + data class Vector(val x: Float, val y: Float) { fun absSquared(): Float { return this.x * this.x + this.y * this.y } + fun plus(vector: Vector): Vector { return Vector(this.x + vector.x, this.y + vector.y) } + fun max(other: Vector): Vector { return Vector(max(this.x, other.x), max(this.y, other.y)) } + fun min(other: Vector): Vector { return Vector(min(this.x, other.x), min(this.y, other.y)) } + operator fun minus(vector: Vector): Vector { return Vector(this.x - vector.x, this.y - vector.y) } @@ -61,16 +73,35 @@ class TouchGestureDetector( fun sizeSquared(): Float { return (max - min).absSquared() } + fun getDirection(): Vector { return last - start } + fun update(vector: Vector) { min = min.min(vector) max = max.max(vector) last = vector } } + + private fun PointerPath.startIntersectsSystemGestureInsets(): Boolean { + // ignore x, since this makes edge swipes very hard to execute + return start.y < systemGestureInsetTop + || start.y > height - systemGestureInsetBottom + } + + private fun PointerPath.intersectsSystemGestureInsets(): Boolean { + return min.x < systemGestureInsetLeft + || min.y < systemGestureInsetTop + || max.x > width - systemGestureInsetRight + || max.y > height - systemGestureInsetBottom + } + private fun PointerPath.isTap(): Boolean { + if (intersectsSystemGestureInsets()) { + return false + } return sizeSquared() < TOUCH_SLOP_SQUARE } @@ -128,8 +159,8 @@ class TouchGestureDetector( } // add new pointers - for(i in 0.. { - if(startEndMax.x + MIN_TRIANGLE_HEIGHT < mainPointerPath.max.x) { + if (startEndMax.x + MIN_TRIANGLE_HEIGHT < mainPointerPath.max.x) { gesture = Gesture.SWIPE_LARGER } else if (startEndMin.x - MIN_TRIANGLE_HEIGHT > mainPointerPath.min.x) { gesture = Gesture.SWIPE_SMALLER } } + Gesture.SWIPE_UP -> { - if(startEndMax.x + MIN_TRIANGLE_HEIGHT < mainPointerPath.max.x) { + if (startEndMax.x + MIN_TRIANGLE_HEIGHT < mainPointerPath.max.x) { gesture = Gesture.SWIPE_LARGER_REVERSE } else if (startEndMin.x - MIN_TRIANGLE_HEIGHT > mainPointerPath.min.x) { gesture = Gesture.SWIPE_SMALLER_REVERSE } } + Gesture.SWIPE_RIGHT -> { - if(startEndMax.y + MIN_TRIANGLE_HEIGHT < mainPointerPath.max.y) { + if (startEndMax.y + MIN_TRIANGLE_HEIGHT < mainPointerPath.max.y) { gesture = Gesture.SWIPE_V } else if (startEndMin.y - MIN_TRIANGLE_HEIGHT > mainPointerPath.min.y) { gesture = Gesture.SWIPE_LAMBDA } } + Gesture.SWIPE_LEFT -> { - if(startEndMax.y + MIN_TRIANGLE_HEIGHT < mainPointerPath.max.y) { + if (startEndMax.y + MIN_TRIANGLE_HEIGHT < mainPointerPath.max.y) { gesture = Gesture.SWIPE_V_REVERSE } else if (startEndMin.y - MIN_TRIANGLE_HEIGHT > mainPointerPath.min.y) { gesture = Gesture.SWIPE_LAMBDA_REVERSE } } - else -> { } + + else -> {} } if (edgeActions) { @@ -283,4 +318,12 @@ class TouchGestureDetector( gesture?.invoke(context) } } + + @RequiresApi(Build.VERSION_CODES.Q) + fun setSystemGestureInsets(insets: Insets) { + systemGestureInsetTop = insets.top + systemGestureInsetBottom = insets.bottom + systemGestureInsetLeft = insets.left + systemGestureInsetRight = insets.right + } } \ No newline at end of file