mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
fix(ui): LB, RB, LT and RT close the keyboard and menus of the page they leave (#3619)
This commit is contained in:
1 parent
cec2e4ed70
commit
153c873898
7 files changed
+135
-26
No files matched your search
@@ -2247,7 +2247,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
return true
|
||||
}
|
||||
return try {
|
||||
super.dispatchKeyEvent(event)
|
||||
super.dispatchKeyEvent(event) || com.droiddeck.launcher.ui.PageBumpers.handle(event)
|
||||
} catch (e: IllegalStateException) {
|
||||
if (e.message?.contains("active focus target") != true) throw e
|
||||
android.util.Log.w("MainActivity", "key ${event.keyCode} dropped: ${e.message}")
|
||||
@@ -2256,6 +2256,9 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
}
|
||||
|
||||
private val triggerKeys = com.droiddeck.launcher.input.TriggerKeys { dispatchKeyEvent(it) }
|
||||
|
||||
internal fun onMenuMotion(event: MotionEvent) = triggerKeys.onMotion(event)
|
||||
|
||||
private val rightStickLayouts = HashMap<Int, Boolean>()
|
||||
private val rightStickScrollHandler = android.os.Handler(android.os.Looper.getMainLooper())
|
||||
private var rightStickScrollDeviceId = -1
|
||||
|
||||
@@ -457,6 +457,15 @@ internal suspend fun focusWithinFrames(done: () -> Boolean, target: () -> FocusR
|
||||
}
|
||||
}
|
||||
|
||||
internal suspend fun holdFocus(held: () -> Boolean, target: () -> FocusRequester?) {
|
||||
val since = MainTabSteps.otherKeys
|
||||
repeat(RAIL_HOLD_FRAMES) { frame ->
|
||||
androidx.compose.runtime.withFrameNanos { }
|
||||
if (MainTabSteps.otherKeys != since) return
|
||||
if (frame == 0 || !held()) runCatching { target()?.requestFocus() }
|
||||
}
|
||||
}
|
||||
|
||||
internal suspend fun focusSoon(target: () -> FocusRequester) {
|
||||
repeat(30) {
|
||||
androidx.compose.runtime.withFrameNanos { }
|
||||
@@ -786,10 +795,13 @@ private fun FrontEndScreenBody(s: FrontEndState, a: FrontEndActions, page: (@Com
|
||||
val stepScope = androidx.compose.runtime.rememberCoroutineScope()
|
||||
val pageKeyNow = androidx.compose.runtime.rememberUpdatedState(s.pageKey)
|
||||
var railHold by remember { mutableStateOf<kotlinx.coroutines.Job?>(null) }
|
||||
val stepView = androidx.compose.ui.platform.LocalView.current
|
||||
val tabStep = androidx.compose.runtime.rememberUpdatedState { step: Int ->
|
||||
if (!processSettingsPageVisible) {
|
||||
val keys = navigationKeys(s)
|
||||
keys.getOrNull(keys.indexOf(railSelectionNow.value) + step)?.let { key ->
|
||||
releaseFocusedTextField(stepView)
|
||||
OpenMenus.leaveAll(withPage = true)
|
||||
PageSwipe.direction = step
|
||||
if (s.dualScreen) { onRailSelect(key); return@let }
|
||||
showRailPage(key, false)
|
||||
|
||||
@@ -235,7 +235,7 @@ internal fun GameDetail(
|
||||
steppedTo = null
|
||||
return@LaunchedEffect
|
||||
}
|
||||
focusSoon { tabFocus.getValue(t) }
|
||||
holdFocus({ onStrip }) { tabFocus.getValue(t) }
|
||||
steppedTo = null
|
||||
}
|
||||
fun cycle(step: Int) {
|
||||
@@ -243,7 +243,7 @@ internal fun GameDetail(
|
||||
val at = tabs.indexOf(tab).coerceAtLeast(0)
|
||||
val next = tabs[(at + step + tabs.size) % tabs.size]
|
||||
picked = next
|
||||
if (inPage) steppedTo = next
|
||||
steppedTo = next
|
||||
}
|
||||
val art: Any? = game?.hero ?: item?.imageUrl ?: installed?.sidecar?.hero ?: installed?.sidecar?.cover
|
||||
val artModel: Any? = if (art is String) cardArtRequest(art, item?.tallImageUrl) else art
|
||||
|
||||
@@ -531,13 +531,11 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String,
|
||||
val grid = remember { LazyGridState() }
|
||||
val detailScroll = rememberScrollState()
|
||||
var cycled by remember { mutableIntStateOf(0) }
|
||||
var gridFocused by remember { mutableStateOf(false) }
|
||||
LaunchedEffect(source, sort) { grid.scrollToItem(0) }
|
||||
LaunchedEffect(cycled) {
|
||||
if (cycled == 0 || ff == null || inputMode.inputMode != androidx.compose.ui.input.InputMode.Keyboard) return@LaunchedEffect
|
||||
focusWithinFrames({ gridFocused }) {
|
||||
if (ff.firstTileAttached > 0) ff.firstTile else ff.items["libchip:$source"] ?: FocusRequester.Default
|
||||
}
|
||||
val chip = "libchip:$source"
|
||||
holdFocus({ ff.last == chip }) { ff.items[chip] }
|
||||
}
|
||||
CardDetailHost(
|
||||
open, detailScroll,
|
||||
@@ -563,7 +561,7 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String,
|
||||
else { focusManager.clearFocus(force = true); onSelect(it) }
|
||||
},
|
||||
searchCleared = open != null,
|
||||
onFocus = { gridFocused = it },
|
||||
onFocus = {},
|
||||
soundMode = soundMode,
|
||||
wallArt = { wallTiles(all.filterNot(concealed).mapNotNull { entryArt(it, tall = true) }) },
|
||||
emptyText = when {
|
||||
@@ -753,20 +751,23 @@ private fun LibraryGrid(
|
||||
}
|
||||
}
|
||||
}
|
||||
val manage = !soundMode && !s.shortcutPicker
|
||||
LazyVerticalGrid(
|
||||
columns = if (listMode) GridCells.Fixed(1) else remember(baseWidth, level) { LevelCells(baseWidth, level) }, state = grid,
|
||||
modifier = Modifier.fillMaxSize().onFocusChanged { onFocus(it.hasFocus) }.focusGroup().bumpers(onPrevious = { onCycle(-1) }, onNext = { onCycle(1) }).onSizeChanged {
|
||||
val gap = with(density) { 10.dp.roundToPx() }
|
||||
columns = if (listMode) 1 else libraryColumns(it.width, with(density) { baseWidth.roundToPx() }, gap, level)
|
||||
gridH = it.height
|
||||
},
|
||||
modifier = Modifier.fillMaxSize().onFocusChanged { onFocus(it.hasFocus) }.focusGroup()
|
||||
.then(if (sourceChipsShown(sources, manage)) Modifier.bumpers(onPrevious = { onCycle(-1) }, onNext = { onCycle(1) }) else Modifier)
|
||||
.onSizeChanged {
|
||||
val gap = with(density) { 10.dp.roundToPx() }
|
||||
columns = if (listMode) 1 else libraryColumns(it.width, with(density) { baseWidth.roundToPx() }, gap, level)
|
||||
gridH = it.height
|
||||
},
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||
contentPadding = PaddingValues(top = 4.dp, bottom = 16.dp),
|
||||
) {
|
||||
val header: @Composable () -> Unit = {
|
||||
LibraryHeader(s, a, if (soundMode) total else visible.size + hiddenUninstalled, searchCleared, source == Store.XBOX.id, showUninstalled, onShowUninstalled, collections = Store.byId(source)?.takeIf { it == Store.ITCH || it == Store.GAMEJOLT }, soundMode = soundMode, onAddSoundtrack = onAddSoundtrack, onCollections = onCollections, source = source)
|
||||
}
|
||||
val filters: @Composable () -> Unit = { LibraryFilters(sources, source, onSource, manage = !soundMode && !s.shortcutPicker) }
|
||||
val filters: @Composable () -> Unit = { LibraryFilters(sources, source, onSource, manage) }
|
||||
item(key = if (soundMode) "header" else "filters", span = { GridItemSpan(maxLineSpan) }) {
|
||||
Box(Modifier.onSizeChanged { chipsH = it.height }) { Rise(0) { if (soundMode) header() else filters() } }
|
||||
}
|
||||
@@ -908,10 +909,13 @@ private fun LibraryHeader(
|
||||
if (s.steamGamesX64 && narrow) Box(Modifier.padding(top = 4.dp)) { ClientTag() }
|
||||
}
|
||||
|
||||
private fun sourceChipsShown(sources: List<String>, manage: Boolean): Boolean =
|
||||
(manage && sources.isNotEmpty()) || sources.size >= 2 || sources.singleOrNull() == LIBRARY_HIDDEN
|
||||
|
||||
@OptIn(androidx.compose.foundation.ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
private fun LibraryFilters(sources: List<String>, source: String, onSource: (String) -> Unit, manage: Boolean) {
|
||||
val chips = (manage && sources.isNotEmpty()) || sources.size >= 2 || sources.singleOrNull() == LIBRARY_HIDDEN
|
||||
val chips = sourceChipsShown(sources, manage)
|
||||
if (!chips && !manage) return
|
||||
val pal = LocalPalette.current
|
||||
val keys = if (chips) listOf(LIBRARY_ALL) + sources else emptyList()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.EditText
|
||||
import androidx.compose.foundation.gestures.awaitEachGesture
|
||||
@@ -48,10 +49,18 @@ internal fun releaseTextField(field: EditText, origin: IntArray, x: Float, y: Fl
|
||||
val insideX = x >= origin[0] - margin && x <= origin[0] + field.width + margin
|
||||
val insideY = y >= origin[1] - margin && y <= origin[1] + field.height + margin
|
||||
if (insideX && insideY) return
|
||||
releaseTextField(field)
|
||||
}
|
||||
|
||||
internal fun releaseTextField(field: EditText) {
|
||||
field.clearFocus()
|
||||
(field.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager)?.hideSoftInputFromWindow(field.windowToken, 0)
|
||||
}
|
||||
|
||||
internal fun releaseFocusedTextField(view: View) {
|
||||
(view.rootView.findFocus() as? EditText)?.let(::releaseTextField)
|
||||
}
|
||||
|
||||
/** A dialog's touches never reach the activity, so a dialog holding a search field counts its taps and releases the field itself. */
|
||||
internal fun Modifier.releaseTextFieldOnTap(): Modifier = composed {
|
||||
val view = LocalView.current
|
||||
|
||||
@@ -87,6 +87,7 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.TransformOrigin
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.input.key.onPreviewKeyEvent
|
||||
import androidx.compose.ui.node.currentValueOf
|
||||
import androidx.compose.foundation.focusable
|
||||
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
|
||||
import androidx.compose.foundation.gestures.detectTapGestures
|
||||
@@ -152,14 +153,43 @@ internal fun Modifier.blockTouches(): Modifier = pointerInput(Unit) { detectTapG
|
||||
internal fun Modifier.tapOutside(onTap: () -> Unit): Modifier =
|
||||
pointerInput(onTap) { detectTapGestures(onTap = { onTap() }) }
|
||||
|
||||
internal fun Modifier.bumpers(onPrevious: () -> Unit, onNext: () -> Unit): Modifier = onPreviewKeyEvent { event ->
|
||||
val keyEvent = event.nativeKeyEvent
|
||||
val previous = keyEvent.keyCode == KeyEvent.KEYCODE_BUTTON_L1
|
||||
if (!previous && keyEvent.keyCode != KeyEvent.KEYCODE_BUTTON_R1) {
|
||||
false
|
||||
} else {
|
||||
if (keyEvent.action == KeyEvent.ACTION_DOWN && keyEvent.repeatCount == 0) { if (previous) onPrevious() else onNext() }
|
||||
true
|
||||
internal fun Modifier.bumpers(onPrevious: () -> Unit, onNext: () -> Unit): Modifier = this then BumpersElement(onPrevious, onNext)
|
||||
|
||||
private data class BumpersElement(val onPrevious: () -> Unit, val onNext: () -> Unit) :
|
||||
androidx.compose.ui.node.ModifierNodeElement<BumpersNode>() {
|
||||
override fun create() = BumpersNode(onPrevious, onNext)
|
||||
override fun update(node: BumpersNode) {
|
||||
node.onPrevious = onPrevious
|
||||
node.onNext = onNext
|
||||
}
|
||||
}
|
||||
|
||||
internal class BumpersNode(var onPrevious: () -> Unit, var onNext: () -> Unit) :
|
||||
Modifier.Node(), androidx.compose.ui.input.key.KeyInputModifierNode, androidx.compose.ui.node.CompositionLocalConsumerModifierNode {
|
||||
override fun onAttach() { PageBumpers.attached += this }
|
||||
override fun onDetach() { PageBumpers.attached -= this }
|
||||
override fun onPreKeyEvent(event: androidx.compose.ui.input.key.KeyEvent): Boolean = PageBumpers.handle(event.nativeKeyEvent, this)
|
||||
override fun onKeyEvent(event: androidx.compose.ui.input.key.KeyEvent): Boolean = false
|
||||
|
||||
@OptIn(androidx.compose.ui.ExperimentalComposeUiApi::class)
|
||||
fun step(previous: Boolean) {
|
||||
releaseFocusedTextField(currentValueOf(androidx.compose.ui.platform.LocalView))
|
||||
OpenMenus.leaveAll(withPage = false)
|
||||
if (previous) onPrevious() else onNext()
|
||||
// Android keeps touch mode through gamepad buttons, and the pages move the focus only outside it.
|
||||
val input = currentValueOf(LocalInputModeManager)
|
||||
if (input.inputMode != InputMode.Keyboard) input.requestInputMode(InputMode.Keyboard)
|
||||
}
|
||||
}
|
||||
|
||||
internal object PageBumpers {
|
||||
val attached = ArrayList<BumpersNode>()
|
||||
|
||||
fun handle(event: KeyEvent, node: BumpersNode? = attached.lastOrNull()): Boolean {
|
||||
val previous = event.keyCode == KeyEvent.KEYCODE_BUTTON_L1
|
||||
if (node == null || (!previous && event.keyCode != KeyEvent.KEYCODE_BUTTON_R1)) return false
|
||||
if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) node.step(previous)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,6 +297,25 @@ private class BelowEndProvider(private val gap: Int) : PopupPositionProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private val StepKeys = setOf(KeyEvent.KEYCODE_BUTTON_L1, KeyEvent.KEYCODE_BUTTON_R1, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_BUTTON_R2)
|
||||
|
||||
private fun launcherOf(view: android.view.View) = view.context.findActivity() as? com.droiddeck.launcher.FrontEndActivity
|
||||
|
||||
// A popup is a window of its own, so controller input sent to it never reaches the launcher's.
|
||||
private fun Modifier.passSteps(host: android.view.View): Modifier = onPreviewKeyEvent { event ->
|
||||
event.nativeKeyEvent.keyCode in StepKeys && launcherOf(host) != null && host.rootView.dispatchKeyEvent(event.nativeKeyEvent)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PassTriggerMotion(host: android.view.View) {
|
||||
val popup = androidx.compose.ui.platform.LocalView.current
|
||||
androidx.compose.runtime.DisposableEffect(popup, host) {
|
||||
val launcher = launcherOf(host)?.takeIf { it.window.decorView === host.rootView }
|
||||
if (launcher != null) popup.setOnGenericMotionListener { _, event -> launcher.onMenuMotion(event); false }
|
||||
onDispose { popup.setOnGenericMotionListener(null) }
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(androidx.compose.ui.ExperimentalComposeUiApi::class)
|
||||
@Composable
|
||||
fun AnchoredMenu(
|
||||
@@ -277,6 +326,13 @@ fun AnchoredMenu(
|
||||
val state = remember { MutableTransitionState(false) }
|
||||
state.targetState = open
|
||||
if (!state.currentState && !state.targetState && state.isIdle) return
|
||||
val dismissNow = rememberUpdatedState(onDismiss)
|
||||
if (open) androidx.compose.runtime.DisposableEffect(Unit) {
|
||||
val leave = { _: Boolean -> dismissNow.value() }
|
||||
OpenMenus.leave += leave
|
||||
onDispose { OpenMenus.leave -= leave }
|
||||
}
|
||||
val hostView = androidx.compose.ui.platform.LocalView.current
|
||||
val firstItemFocus = remember { FocusRequester() }
|
||||
val inputMode = LocalInputModeManager.current.inputMode
|
||||
LaunchedEffect(open, inputMode) {
|
||||
@@ -294,6 +350,7 @@ fun AnchoredMenu(
|
||||
val pal = LocalPalette.current
|
||||
Popup(popupPositionProvider = provider, onDismissRequest = onDismiss, properties = PopupProperties(focusable = true)) {
|
||||
TrackKeyWindow()
|
||||
PassTriggerMotion(hostView)
|
||||
val popupInput = LocalInputModeManager.current
|
||||
LaunchedEffect(open, inputMode) {
|
||||
if (!open || inputMode != InputMode.Keyboard) return@LaunchedEffect
|
||||
@@ -323,6 +380,7 @@ fun AnchoredMenu(
|
||||
.clip(RowShape)
|
||||
.background(pal.surfaceVariant.copy(alpha = 0.95f))
|
||||
.border(1.dp, pal.signal.copy(alpha = 0.22f), RowShape)
|
||||
.passSteps(hostView)
|
||||
.controllerBack(onDismiss)
|
||||
.padding(6.dp),
|
||||
) {
|
||||
|
||||
@@ -145,6 +145,12 @@ internal class StepOutScope(private val items: List<Animatable<Float, AnimationV
|
||||
}
|
||||
}
|
||||
|
||||
internal object OpenMenus {
|
||||
val leave = ArrayList<(Boolean) -> Unit>()
|
||||
|
||||
fun leaveAll(withPage: Boolean) = leave.toList().forEach { it(withPage) }
|
||||
}
|
||||
|
||||
private val OutPull = 6.dp
|
||||
private val OutBoxCorner = 16.dp
|
||||
private val OutFillet = 10.dp
|
||||
@@ -194,13 +200,25 @@ private fun StepOutPanel(
|
||||
val glide = LocalFocusGlide.current
|
||||
val opener = remember { arrayOfNulls<GlideSource>(1) }
|
||||
val openNow = androidx.compose.runtime.rememberUpdatedState(open)
|
||||
val dismissNow = androidx.compose.runtime.rememberUpdatedState(onDismiss)
|
||||
val leavesWithPage = remember { booleanArrayOf(false) }
|
||||
val presence = remember { Animatable(1f) }
|
||||
if (open) androidx.compose.runtime.DisposableEffect(Unit) {
|
||||
val leave = { withPage: Boolean ->
|
||||
opener[0] = null
|
||||
leavesWithPage[0] = withPage
|
||||
dismissNow.value()
|
||||
}
|
||||
OpenMenus.leave += leave
|
||||
onDispose { OpenMenus.leave -= leave }
|
||||
}
|
||||
var moving by remember { mutableStateOf(false) }
|
||||
val placed = remember { arrayOfNulls<Any>(3) }
|
||||
if (moving) VeilRing()
|
||||
val pull = with(density) { OutPull.toPx() }
|
||||
val corner = pillCorner?.let { with(density) { it.toPx() } } ?: minOf(pill.height, pill.width) / 2f
|
||||
|
||||
BoxWithConstraints(Modifier.fillMaxSize()) {
|
||||
BoxWithConstraints(Modifier.fillMaxSize().graphicsLayer { alpha = presence.value }) {
|
||||
val hostWidth = constraints.maxWidth.toFloat()
|
||||
val hostHeight = constraints.maxHeight.toFloat()
|
||||
val availableHeight = with(density) {
|
||||
@@ -221,9 +239,11 @@ private fun StepOutPanel(
|
||||
if (grown && flipped) edge.snapTo(if (open) out else rest)
|
||||
val boxBottom = pill.bottom + pull + c.height
|
||||
if (open) {
|
||||
leavesWithPage[0] = false
|
||||
presence.snapTo(1f)
|
||||
if (!grown) {
|
||||
opener[0] = glide?.current
|
||||
edge.snapTo(rest); bottom.snapTo(pill.bottom); tint.snapTo(0f)
|
||||
edge.snapTo(rest); bottom.snapTo(pill.bottom); tint.snapTo(0f); dim.snapTo(0f)
|
||||
pieces.forEach { it.snapTo(0f) }
|
||||
grown = true
|
||||
}
|
||||
@@ -241,7 +261,10 @@ private fun StepOutPanel(
|
||||
} else {
|
||||
opener[0]?.let { o -> if (inputMode.inputMode == InputMode.Keyboard) runCatching { o.requester.requestFocus() } }
|
||||
opener[0] = null
|
||||
coroutineScope {
|
||||
if (leavesWithPage[0]) {
|
||||
moving = true
|
||||
presence.animateTo(0f, Motion.tw(160))
|
||||
} else coroutineScope {
|
||||
pieces.forEach { launch { it.animateTo(0f, Motion.tw(90)) } }
|
||||
launch { delay(Motion.ms(60).toLong()); dim.animateTo(0f, Motion.tw(300)) }
|
||||
moving = true
|
||||
|
||||
Reference in new issue
Block a user