feat(launcher): LT and RT step between the main tabs from anywhere in the launcher (#3035)

* feat(launcher): LT and RT step between the main tabs from anywhere in the launcher

LT and RT move to the previous and next main tab on every layout. The activity handles them before any page sees the key, so a focused control, an embedded Android view such as the terminal, or nothing being focused no longer swallows them; dialogs and popups keep their own keys. Pads that report the triggers only as axes, like the Xbox Wireless Controller, get an L2/R2 press when a trigger passes 60% of its travel; pads that send their own trigger keys are left alone.

* fix(launcher): trigger tab steps tick, drop a pending press on focus loss, and move once per pull

Controller haptics now ticks for L2 and R2. A synthetic trigger press waiting its 40 ms is cancelled when the launcher loses window focus or is destroyed, so a dialog that opens in between keeps the page behind it. Each pull is tracked: when a synthetic press has gone out, the pad's own trigger key for the same pull is swallowed instead of stepping a second tab.
This commit is contained in:
Tushar Gandhe authored and GitHub committed 2026-11-22 16:59:01 -05:00
1 parent c3809c0a75
commit 234e40157e
4 files changed
+114 -13

No files matched your search

@@ -2016,6 +2016,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (!hasFocus) triggerKeys.cancel()
if (hasFocus) {
com.droiddeck.launcher.ui.DualScreenPanel.yieldTo(taskId, false)
applyLauncherFullscreen()
@@ -2085,6 +2086,14 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
if (!compatLaunch!!.loading.ended && event.keyCode !in listOf(
KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE)) return true
}
if (!triggerKeys.synthesizing && triggerKeys.nativeKey(event)) return true
val tabStep = com.droiddeck.launcher.ui.dualPageStep(event)
val tabs = com.droiddeck.launcher.ui.MainTabSteps.handler
if (tabStep != null && tabs != null) {
if (controllerHaptics) com.droiddeck.launcher.ui.ControllerHaptics.onKey(this, event)
if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) tabs(tabStep)
return true
}
if (controllerHaptics) com.droiddeck.launcher.ui.ControllerHaptics.onKey(this, event)
if (event.keyCode == KeyEvent.KEYCODE_BUTTON_A) com.droiddeck.launcher.ui.HeldKeys.confirm = event.action == KeyEvent.ACTION_DOWN
if (showComponents && (event.keyCode == KeyEvent.KEYCODE_BUTTON_L1 || event.keyCode == KeyEvent.KEYCODE_BUTTON_R1)) {
@@ -2102,9 +2111,12 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
}
}
private val triggerKeys = com.droiddeck.launcher.input.TriggerKeys { dispatchKeyEvent(it) }
override fun dispatchGenericMotionEvent(event: android.view.MotionEvent): Boolean {
if (compatLaunch != null && !compatLaunch!!.loading.ended &&
com.droiddeck.launcher.input.PadBridge.isControllerMotionEvent(event)) return true
triggerKeys.onMotion(event)
return super.dispatchGenericMotionEvent(event)
}
@@ -2234,6 +2246,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
}
override fun onDestroy() {
triggerKeys.cancel()
gameLaunchRequest.incrementAndGet()
cancelCompatLaunch()
updates.unregister()
@@ -0,0 +1,81 @@
package com.droiddeck.launcher.input
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.view.InputDevice
import android.view.KeyEvent
import android.view.MotionEvent
internal class TriggerKeys(private val dispatch: (KeyEvent) -> Unit) {
private class Pull {
val held = BooleanArray(2)
val sent = BooleanArray(2)
}
private val handler = Handler(Looper.getMainLooper())
private val keyed = HashSet<Int>()
private val pulls = HashMap<Int, Pull>()
var synthesizing = false
private set
/** True when this real trigger key belongs to a pull that already went out as a synthetic press. */
fun nativeKey(event: KeyEvent): Boolean {
val i = index(event.keyCode) ?: return false
keyed += event.deviceId
val pull = pulls[event.deviceId] ?: return false
if (!pull.sent[i]) return false
if (event.action == KeyEvent.ACTION_UP) pull.sent[i] = false
return true
}
fun onMotion(event: MotionEvent) {
if (event.source and InputDevice.SOURCE_JOYSTICK != InputDevice.SOURCE_JOYSTICK || event.action != MotionEvent.ACTION_MOVE) return
val device = event.deviceId
if (device in keyed) return
val pull = pulls.getOrPut(device) { Pull() }
update(pull, 0, maxOf(event.getAxisValue(MotionEvent.AXIS_LTRIGGER), event.getAxisValue(MotionEvent.AXIS_BRAKE)), event)
update(pull, 1, maxOf(event.getAxisValue(MotionEvent.AXIS_RTRIGGER), event.getAxisValue(MotionEvent.AXIS_GAS)), event)
}
fun cancel() {
handler.removeCallbacksAndMessages(null)
pulls.clear()
}
private fun update(pull: Pull, i: Int, value: Float, event: MotionEvent) {
if (!pull.held[i] && value >= PRESS) {
pull.held[i] = true
pull.sent[i] = false
val device = event.deviceId
val source = event.source
// A pad that also sends trigger keys delivers them close to the axis; waiting lets that key win.
handler.postDelayed({
if (device !in keyed && pulls[device] === pull) {
pull.sent[i] = true
press(CODES[i], device, source)
}
}, KEY_WAIT_MS)
} else if (pull.held[i] && value <= RELEASE) pull.held[i] = false
}
private fun press(code: Int, device: Int, source: Int) {
val now = SystemClock.uptimeMillis()
synthesizing = true
try {
dispatch(KeyEvent(now, now, KeyEvent.ACTION_DOWN, code, 0, 0, device, 0, 0, source))
dispatch(KeyEvent(now, now, KeyEvent.ACTION_UP, code, 0, 0, device, 0, 0, source))
} finally {
synthesizing = false
}
}
private fun index(code: Int): Int? = CODES.indexOf(code).takeIf { it >= 0 }
private companion object {
val CODES = intArrayOf(KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_BUTTON_R2)
const val PRESS = 0.6f
const val RELEASE = 0.3f
const val KEY_WAIT_MS = 40L
}
}
@@ -12,6 +12,7 @@ object ControllerHaptics {
private val navKeys = setOf(
KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT,
KeyEvent.KEYCODE_BUTTON_A, KeyEvent.KEYCODE_BUTTON_L1, KeyEvent.KEYCODE_BUTTON_R1,
KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_BUTTON_R2,
)
fun onKey(context: Context, event: KeyEvent) {
@@ -761,6 +761,20 @@ private fun FrontEndScreenBody(s: FrontEndState, a: FrontEndActions, page: (@Com
focusManager.clearFocus(force = true)
onRailSelect(key)
}, a)
val tabStep = androidx.compose.runtime.rememberUpdatedState { step: Int ->
if (!processSettingsPageVisible) {
val keys = navigationKeys(s)
keys.getOrNull(keys.indexOf(railSelectionNow.value) + step)?.let { key ->
PageSwipe.direction = step
onRailSelect(key)
}
}
}
DisposableEffect(Unit) {
val handler: (Int) -> Unit = { tabStep.value(it) }
MainTabSteps.handler = handler
onDispose { if (MainTabSteps.handler === handler) MainTabSteps.handler = null }
}
CompositionLocalProvider(LocalShowSection provides { key -> selected = key }) {
val swipeContext = androidx.compose.ui.platform.LocalContext.current
remember { com.droiddeck.launcher.core.AppUiPrefs.pageSwipe(swipeContext).also { PageSwipe.on = it } }
@@ -768,19 +782,7 @@ private fun FrontEndScreenBody(s: FrontEndState, a: FrontEndActions, page: (@Com
LauncherLayout(
bottomNavigation && !s.dualScreen, pal.line,
Modifier.fillMaxSize().onGloballyPositioned { screenAt = it.positionInRoot() }
.then(if (openTab == "agent") Modifier.imePadding() else Modifier)
.then(if (s.dualScreen) Modifier.onKeyEvent { event ->
val step = dualPageStep(event.nativeKeyEvent) ?: return@onKeyEvent false
if (event.type == KeyEventType.KeyDown && !processSettingsPageVisible) {
val keys = navigationKeys(s)
val at = keys.indexOf(railSelectionNow.value)
keys.getOrNull(at + step)?.let { key ->
PageSwipe.direction = step
onRailSelect(key)
}
}
true
} else Modifier),
.then(if (openTab == "agent") Modifier.imePadding() else Modifier),
navigation = {
if (s.dualScreen || (bottomNavigation && agentKeyboardOpen)) Unit
else if (bottomNavigation) BottomBar(s, railSelection, onRailSelect, onRailFocus, a, railFocus)
@@ -902,3 +904,7 @@ private fun FrontEndScreenBody(s: FrontEndState, a: FrontEndActions, page: (@Com
}
}
}
internal object MainTabSteps {
@Volatile var handler: ((Int) -> Unit)? = null
}