diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index 92c3acff44..fab18bd103 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -2209,7 +2209,54 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet private val triggerKeys = com.droiddeck.launcher.input.TriggerKeys { dispatchKeyEvent(it) } private val rightStickLayouts = HashMap() + private val rightStickScrollHandler = android.os.Handler(android.os.Looper.getMainLooper()) private var rightStickScrollAt = 0L + private var rightStickScrollDeviceId = android.view.InputDevice.INVALID_DEVICE_ID + private var rightStickScrollDeflection = 0f + private val rightStickScrollTick = object : Runnable { + override fun run() { + val deviceId = rightStickScrollDeviceId + if (!hasWindowFocus() || deviceId == android.view.InputDevice.INVALID_DEVICE_ID || + android.view.InputDevice.getDevice(deviceId) == null || + kotlin.math.abs(rightStickScrollDeflection) < 0.55f) { + stopRightStickScroll() + return + } + dispatchRightStickScroll(rightStickScrollDeflection, deviceId) + rightStickScrollHandler.postDelayed(this, 130L) + } + } + + private fun stopRightStickScroll() { + rightStickScrollHandler.removeCallbacks(rightStickScrollTick) + rightStickScrollDeviceId = android.view.InputDevice.INVALID_DEVICE_ID + rightStickScrollDeflection = 0f + } + + private fun dispatchRightStickScroll(value: Float, deviceId: Int) { + val now = android.os.SystemClock.uptimeMillis() + val root = window.decorView + if (root.width <= 0 || root.height <= 0) return + val properties = android.view.MotionEvent.PointerProperties().apply { + id = 0 + toolType = android.view.MotionEvent.TOOL_TYPE_MOUSE + } + val coordinates = android.view.MotionEvent.PointerCoords().apply { + x = root.width * 0.65f + y = root.height * 0.5f + setAxisValue(android.view.MotionEvent.AXIS_VSCROLL, -value * 3f) + } + val scroll = android.view.MotionEvent.obtain( + now, now, android.view.MotionEvent.ACTION_SCROLL, 1, + arrayOf(properties), arrayOf(coordinates), 0, 0, 1f, 1f, + deviceId, 0, android.view.InputDevice.SOURCE_MOUSE, 0, + ) + try { + super.dispatchGenericMotionEvent(scroll) + } finally { + scroll.recycle() + } + } private fun scrollPageWithRightStick(event: android.view.MotionEvent): Boolean { if (event.actionMasked != android.view.MotionEvent.ACTION_MOVE || @@ -2219,31 +2266,15 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet ?: return false val axis = if (rxRy) android.view.MotionEvent.AXIS_RY else android.view.MotionEvent.AXIS_RZ val value = event.getAxisValue(axis) - if (kotlin.math.abs(value) < 0.55f) return false - val now = android.os.SystemClock.uptimeMillis() - if (now - rightStickScrollAt < 130L) return true - rightStickScrollAt = now - - val properties = android.view.MotionEvent.PointerProperties().apply { - id = 0 - toolType = android.view.MotionEvent.TOOL_TYPE_MOUSE + if (kotlin.math.abs(value) < 0.55f) { + if (rightStickScrollDeviceId == event.deviceId) stopRightStickScroll() + return false } - val root = window.decorView - if (root.width <= 0 || root.height <= 0) return true - val coordinates = android.view.MotionEvent.PointerCoords().apply { - x = root.width * 0.65f - y = root.height * 0.5f - setAxisValue(android.view.MotionEvent.AXIS_VSCROLL, -value * 3f) - } - val scroll = android.view.MotionEvent.obtain( - now, now, android.view.MotionEvent.ACTION_SCROLL, 1, - arrayOf(properties), arrayOf(coordinates), 0, 0, 1f, 1f, - event.deviceId, 0, android.view.InputDevice.SOURCE_MOUSE, 0, - ) - try { - super.dispatchGenericMotionEvent(scroll) - } finally { - scroll.recycle() + rightStickScrollDeviceId = event.deviceId + rightStickScrollDeflection = value + if (!rightStickScrollHandler.hasCallbacks(rightStickScrollTick)) { + dispatchRightStickScroll(value, event.deviceId) + rightStickScrollHandler.postDelayed(rightStickScrollTick, 130L) } return true } @@ -2256,7 +2287,13 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet return super.dispatchGenericMotionEvent(event) } + override fun onWindowFocusChanged(hasFocus: Boolean) { + super.onWindowFocusChanged(hasFocus) + if (!hasFocus) stopRightStickScroll() + } + override fun onPause() { + stopRightStickScroll() compatLaunchVisible = false if (android.os.Build.VERSION.SDK_INT >= 29) com.droiddeck.launcher.ui.DualScreenPanel.ownerPaused(taskId, true) super.onPause()