mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 13:05:47 +00:00
Fix initial controller focus in the frame generation picker (#3689)
Keep engine choices in the session drawer focus tree so the selected engine receives focus on open and the Engine control regains focus on close.
This commit is contained in:
1 parent
bf8faf06e1
commit
01c8991049
3 files changed
+49
-61
No files matched your search
@@ -1,7 +1,6 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
@@ -318,55 +317,52 @@ private fun SessionFrameGenEngine(
|
||||
val open = host.open == key
|
||||
val input = LocalInputModeManager.current
|
||||
var anchorFocused by remember { mutableStateOf(false) }
|
||||
var restoreFocus by remember { mutableStateOf(false) }
|
||||
val selectedOption = remember { FocusRequester() }
|
||||
var keyboardOpened by remember { mutableStateOf(false) }
|
||||
val initial = options.firstOrNull { (engine, _) -> engine == mode.engine }?.first
|
||||
?: FrameGen.ENGINE_OFF
|
||||
val close = { if (host.open == key) host.open = null }
|
||||
|
||||
// The menu has its own focusable window; wait for its exit transition before restoring the anchor.
|
||||
// Keep the options in the drawer's focus tree. A separate popup window can
|
||||
// steal focus from the controller before Compose can focus the selected row.
|
||||
LaunchedEffect(open) {
|
||||
if (!open && restoreFocus && host.open == null) {
|
||||
repeat(24) {
|
||||
withFrameNanos { }
|
||||
runCatching { anchor.requestFocus() }
|
||||
if (anchorFocused) {
|
||||
restoreFocus = false
|
||||
return@LaunchedEffect
|
||||
}
|
||||
}
|
||||
restoreFocus = false
|
||||
}
|
||||
if (!keyboardOpened || (!open && host.open != null)) return@LaunchedEffect
|
||||
val target = if (open) selectedOption else anchor
|
||||
withFrameNanos { }
|
||||
target.requestFocus()
|
||||
if (!open) keyboardOpened = false
|
||||
}
|
||||
|
||||
val blockedSelection = !losslessReady && mode.engine == FrameGen.ENGINE_LSFG
|
||||
SettingsRow(label, losslessNote.takeIf { blockedSelection && !open }, highlighted = open) {
|
||||
Box {
|
||||
ValueChip(
|
||||
selectedLabel, open,
|
||||
modifier = focusModifier.focusRequester(anchor).onFocusChanged { anchorFocused = it.isFocused }
|
||||
.heightIn(min = 48.dp).testTag("setting-$key")
|
||||
.semantics { contentDescription = label; stateDescription = selectedLabel },
|
||||
) {
|
||||
if (open) host.open = null
|
||||
else {
|
||||
restoreFocus = anchorFocused || input.inputMode == InputMode.Keyboard
|
||||
host.open = key
|
||||
}
|
||||
ValueChip(
|
||||
selectedLabel, open,
|
||||
modifier = focusModifier.focusRequester(anchor).onFocusChanged { anchorFocused = it.isFocused }
|
||||
.heightIn(min = 48.dp).testTag("setting-$key")
|
||||
.semantics { contentDescription = label; stateDescription = selectedLabel },
|
||||
) {
|
||||
if (open) close()
|
||||
else {
|
||||
keyboardOpened = anchorFocused || input.inputMode == InputMode.Keyboard
|
||||
host.open = key
|
||||
}
|
||||
AnchoredMenu(open, onDismiss = { if (host.open == key) host.open = null }, keyboardFocus = restoreFocus) { firstFocus ->
|
||||
val initial = options.firstOrNull { (engine, _) -> engine == mode.engine }?.first
|
||||
?: FrameGen.ENGINE_OFF
|
||||
options.forEach { (engine, text) ->
|
||||
val selected = engine == mode.engine
|
||||
val enabled = selected || engine != FrameGen.ENGINE_LSFG || losslessReady
|
||||
MenuItem(
|
||||
label = text, checked = selected, enabled = enabled,
|
||||
detail = losslessNote.takeIf { engine == FrameGen.ENGINE_LSFG && !losslessReady },
|
||||
focusRequester = firstFocus.takeIf { engine == initial },
|
||||
modifier = Modifier.heightIn(min = 48.dp).testTag("framegen-engine:$engine"),
|
||||
) {
|
||||
host.open = null
|
||||
if (!selected) {
|
||||
onPick(if (engine == FrameGen.ENGINE_OFF) FrameGen.Mode.OFF.copy(multiplier = mode.multiplier)
|
||||
else FrameGen.Mode(engine, mode.multiplier.coerceIn(2, 4)))
|
||||
}
|
||||
}
|
||||
}
|
||||
if (open) {
|
||||
Column(Modifier.fillMaxWidth().padding(horizontal = 8.dp).controllerBack(close)) {
|
||||
options.forEach { (engine, text) ->
|
||||
val selected = engine == mode.engine
|
||||
val enabled = selected || engine != FrameGen.ENGINE_LSFG || losslessReady
|
||||
MenuItem(
|
||||
label = text, checked = selected, enabled = enabled,
|
||||
detail = losslessNote.takeIf { engine == FrameGen.ENGINE_LSFG && !losslessReady },
|
||||
focusRequester = selectedOption.takeIf { engine == initial },
|
||||
modifier = Modifier.heightIn(min = 48.dp).testTag("framegen-engine:$engine"),
|
||||
) {
|
||||
close()
|
||||
if (!selected) {
|
||||
onPick(if (engine == FrameGen.ENGINE_OFF) FrameGen.Mode.OFF.copy(multiplier = mode.multiplier)
|
||||
else FrameGen.Mode(engine, mode.multiplier.coerceIn(2, 4)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,7 +322,6 @@ private fun PassTriggerMotion(host: android.view.View) {
|
||||
fun AnchoredMenu(
|
||||
open: Boolean, onDismiss: () -> Unit, title: String? = null, note: String? = null,
|
||||
compact: Boolean = false, minWidth: androidx.compose.ui.unit.Dp = 120.dp,
|
||||
keyboardFocus: Boolean = false,
|
||||
content: @Composable ColumnScope.(FocusRequester) -> Unit,
|
||||
) {
|
||||
val state = remember { MutableTransitionState(false) }
|
||||
@@ -336,9 +335,9 @@ fun AnchoredMenu(
|
||||
}
|
||||
val hostView = androidx.compose.ui.platform.LocalView.current
|
||||
val firstItemFocus = remember { FocusRequester() }
|
||||
val requestKeyboardFocus = keyboardFocus || LocalInputModeManager.current.inputMode == InputMode.Keyboard
|
||||
LaunchedEffect(open, requestKeyboardFocus) {
|
||||
if (open && requestKeyboardFocus) {
|
||||
val inputMode = LocalInputModeManager.current.inputMode
|
||||
LaunchedEffect(open, inputMode) {
|
||||
if (open && inputMode == InputMode.Keyboard) {
|
||||
repeat(2) { androidx.compose.runtime.withFrameNanos { } }
|
||||
runCatching { firstItemFocus.requestFocus() }
|
||||
android.util.Log.i("AnchoredMenu", "requested first option focus")
|
||||
@@ -354,10 +353,10 @@ fun AnchoredMenu(
|
||||
TrackKeyWindow()
|
||||
PassTriggerMotion(hostView)
|
||||
val popupInput = LocalInputModeManager.current
|
||||
LaunchedEffect(open, requestKeyboardFocus) {
|
||||
if (!open || !requestKeyboardFocus) return@LaunchedEffect
|
||||
LaunchedEffect(open, inputMode) {
|
||||
if (!open || inputMode != InputMode.Keyboard) return@LaunchedEffect
|
||||
popupInput.requestInputMode(InputMode.Keyboard)
|
||||
repeat(if (keyboardFocus) 24 else 8) {
|
||||
repeat(8) {
|
||||
androidx.compose.runtime.withFrameNanos { }
|
||||
if (runCatching { firstItemFocus.captureFocus() }.getOrDefault(false)) {
|
||||
firstItemFocus.freeFocus()
|
||||
|
||||
@@ -53,13 +53,6 @@ class SessionControlDesignTest {
|
||||
private fun zoom(name: String) = compose.onNodeWithTag("game-zoom-$name")
|
||||
private fun zoomValue(value: Int) = zoom("value").assertTextEquals("$value%")
|
||||
|
||||
// Popup focus transfers after the other window attaches or finishes exiting.
|
||||
private fun settlePopupFocus() {
|
||||
compose.runOnIdle { resumeDroppedUiDispatch() }
|
||||
compose.mainClock.advanceTimeBy(500)
|
||||
compose.waitForIdle()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun enginePickerStartsOnSelectionSkipsUnavailableEngineAndRestoresFocus() {
|
||||
val mode = mutableStateOf(FrameGen.Mode(FrameGen.ENGINE_DDFG, 3))
|
||||
@@ -76,19 +69,19 @@ class SessionControlDesignTest {
|
||||
compose.runOnIdle { input.requestInputMode(InputMode.Keyboard) }
|
||||
engineAnchor.performSemanticsAction(SemanticsActions.RequestFocus)
|
||||
engineAnchor.assertIsFocused().performKeyInput { pressKey(Key.ButtonA) }
|
||||
settlePopupFocus()
|
||||
compose.mainClock.advanceTimeByFrame()
|
||||
engine(FrameGen.ENGINE_LSFG).assertIsNotEnabled()
|
||||
engine(FrameGen.ENGINE_DDFG).assertIsFocused().performKeyInput { pressKey(Key.DirectionUp) }
|
||||
engine(FrameGen.ENGINE_OFF).assertIsFocused().performKeyInput { pressKey(Key.DirectionDown) }
|
||||
engine(FrameGen.ENGINE_DDFG).assertIsFocused().performKeyInput { pressKey(Key.DirectionDown) }
|
||||
engine(FrameGen.ENGINE_ZEROFG).assertIsFocused().performKeyInput { pressKey(Key.ButtonA) }
|
||||
settlePopupFocus()
|
||||
compose.mainClock.advanceTimeByFrame()
|
||||
engine(FrameGen.ENGINE_ZEROFG).assertDoesNotExist()
|
||||
assertEquals(FrameGen.Mode(FrameGen.ENGINE_ZEROFG, 3), mode.value)
|
||||
engineAnchor.assertIsFocused().performKeyInput { pressKey(Key.ButtonA) }
|
||||
settlePopupFocus()
|
||||
compose.mainClock.advanceTimeByFrame()
|
||||
engine(FrameGen.ENGINE_ZEROFG).assertIsFocused().performKeyInput { pressKey(Key.ButtonB) }
|
||||
settlePopupFocus()
|
||||
compose.mainClock.advanceTimeByFrame()
|
||||
engine(FrameGen.ENGINE_ZEROFG).assertDoesNotExist()
|
||||
assertEquals(FrameGen.Mode(FrameGen.ENGINE_ZEROFG, 3), mode.value)
|
||||
engineAnchor.assertIsFocused()
|
||||
|
||||
Reference in new issue
Block a user