From bf8faf06e1b2e76079383f0a386ebc12d9162c55 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Fri, 4 Dec 2026 07:20:03 -0500 Subject: [PATCH] Make frame generation and game zoom controller-friendly (#3669) Add the session engine picker and the stepped Game zoom slider. Keep Frame Generation in its own tab and use the standard controller focus ring on its buttons. --- .../com/droiddeck/launcher/ui/FrameGenMenu.kt | 231 ++++++++++++----- .../droiddeck/launcher/ui/SessionOverlay.kt | 83 ++++-- .../droiddeck/launcher/ui/SettingsWidgets.kt | 13 +- .../main/res/drawable/ic_frame_generation.xml | 12 +- app/src/main/res/values/strings.xml | 3 + .../launcher/ui/SessionControlDesignTest.kt | 239 ++++++++++++++++++ 6 files changed, 492 insertions(+), 89 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/SessionControlDesignTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrameGenMenu.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrameGenMenu.kt index 2c7ed785ea..eb6f1854f0 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrameGenMenu.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrameGenMenu.kt @@ -1,21 +1,35 @@ 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 import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedButton import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.withFrameNanos import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.onFocusChanged +import androidx.compose.ui.input.InputMode +import androidx.compose.ui.platform.LocalInputModeManager +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.semantics.stateDescription import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp @@ -34,7 +48,9 @@ internal fun FrameGenMenu( focusModifier: Modifier = Modifier, legacyButtons: Boolean = false, listLayout: Boolean = false, + sessionHost: MenuHost? = null, ) { + val sessionEngineAnchor = remember { FocusRequester() } val need = stringResource(R.string.lsfg_needs_steam) val selectedEngine = mode.engine.takeIf { it != FrameGen.ENGINE_OFF } val offLabel = FrameGen.label(LocalContext.current, FrameGen.Mode.OFF) @@ -95,60 +111,67 @@ internal fun FrameGenMenu( } Column(Modifier.fillMaxWidth().padding(start = 10.dp, end = 10.dp, top = 0.dp, bottom = 8.dp)) { - OutlinedButton( - onClick = { onPick(FrameGen.Mode.OFF.copy(multiplier = mode.multiplier)) }, - modifier = Modifier.fillMaxWidth().controllerConfirm( - onClick = { onPick(FrameGen.Mode.OFF.copy(multiplier = mode.multiplier)) }, - ), - contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), - colors = ButtonDefaults.outlinedButtonColors( - containerColor = if (selectedEngine == null) colors.primary.copy(alpha = 0.16f) else colors.surface, - contentColor = if (selectedEngine == null) colors.primary else colors.onSurface, - ), - ) { Text(offLabel) } - - if (listLayout) { - Column(Modifier.fillMaxWidth().padding(top = 8.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { - engines.forEach { (engine, label) -> - val selected = selectedEngine == engine - val enabled = selected || engine != FrameGen.ENGINE_LSFG || lossless.ready - val pick = { - onPick(if (selected) FrameGen.Mode.OFF.copy(multiplier = mode.multiplier) else FrameGen.Mode(engine, mode.multiplier.coerceIn(2, 4))) - } - OutlinedButton( - onClick = pick, - enabled = enabled, - modifier = Modifier.fillMaxWidth().controllerConfirm(enabled = enabled, onClick = pick), - contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), - colors = ButtonDefaults.outlinedButtonColors( - containerColor = if (selected) colors.primary.copy(alpha = 0.16f) else colors.surface, - contentColor = if (selected) colors.primary else colors.onSurface, - ), - ) { Text(label) } - } - } + if (sessionHost != null) { + SessionFrameGenEngine( + sessionHost, mode, listOf(FrameGen.ENGINE_OFF to offLabel) + engines, + lossless.ready, need, focusModifier, sessionEngineAnchor, onPick, + ) } else { - Row(Modifier.fillMaxWidth().padding(top = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) { - val focusIndex = engines.indexOfFirst { (engine, _) -> - selectedEngine == engine || engine != FrameGen.ENGINE_LSFG || lossless.ready + OutlinedButton( + onClick = { onPick(FrameGen.Mode.OFF.copy(multiplier = mode.multiplier)) }, + modifier = Modifier.fillMaxWidth().controllerConfirm( + onClick = { onPick(FrameGen.Mode.OFF.copy(multiplier = mode.multiplier)) }, + ), + contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), + colors = ButtonDefaults.outlinedButtonColors( + containerColor = if (selectedEngine == null) colors.primary.copy(alpha = 0.16f) else colors.surface, + contentColor = if (selectedEngine == null) colors.primary else colors.onSurface, + ), + ) { Text(offLabel) } + + if (listLayout) { + Column(Modifier.fillMaxWidth().padding(top = 8.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { + engines.forEach { (engine, label) -> + val selected = selectedEngine == engine + val enabled = selected || engine != FrameGen.ENGINE_LSFG || lossless.ready + val pick = { + onPick(if (selected) FrameGen.Mode.OFF.copy(multiplier = mode.multiplier) else FrameGen.Mode(engine, mode.multiplier.coerceIn(2, 4))) + } + OutlinedButton( + onClick = pick, + enabled = enabled, + modifier = Modifier.fillMaxWidth().controllerConfirm(enabled = enabled, onClick = pick), + contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), + colors = ButtonDefaults.outlinedButtonColors( + containerColor = if (selected) colors.primary.copy(alpha = 0.16f) else colors.surface, + contentColor = if (selected) colors.primary else colors.onSurface, + ), + ) { Text(label) } + } } - engines.forEachIndexed { index, (engine, label) -> - val selected = selectedEngine == engine - val enabled = selected || engine != FrameGen.ENGINE_LSFG || lossless.ready - val pick = { - onPick(if (selected) FrameGen.Mode.OFF.copy(multiplier = mode.multiplier) else FrameGen.Mode(engine, mode.multiplier.coerceIn(2, 4))) + } else { + Row(Modifier.fillMaxWidth().padding(top = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) { + val focusIndex = engines.indexOfFirst { (engine, _) -> + selectedEngine == engine || engine != FrameGen.ENGINE_LSFG || lossless.ready + } + engines.forEachIndexed { index, (engine, label) -> + val selected = selectedEngine == engine + val enabled = selected || engine != FrameGen.ENGINE_LSFG || lossless.ready + val pick = { + onPick(if (selected) FrameGen.Mode.OFF.copy(multiplier = mode.multiplier) else FrameGen.Mode(engine, mode.multiplier.coerceIn(2, 4))) + } + OutlinedButton( + onClick = pick, + enabled = enabled, + modifier = (if (index == focusIndex) focusModifier else Modifier) + .weight(1f).controllerConfirm(enabled = enabled, onClick = pick), + contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), + colors = ButtonDefaults.outlinedButtonColors( + containerColor = if (selected) colors.primary.copy(alpha = 0.16f) else colors.surface, + contentColor = if (selected) colors.primary else colors.onSurface, + ), + ) { Text(label) } } - OutlinedButton( - onClick = pick, - enabled = enabled, - modifier = (if (index == focusIndex) focusModifier else Modifier) - .weight(1f).controllerConfirm(enabled = enabled, onClick = pick), - contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), - colors = ButtonDefaults.outlinedButtonColors( - containerColor = if (selected) colors.primary.copy(alpha = 0.16f) else colors.surface, - contentColor = if (selected) colors.primary else colors.onSurface, - ), - ) { Text(label) } } } } @@ -165,7 +188,8 @@ internal fun FrameGenMenu( OutlinedButton( onClick = pickMultiplier, enabled = multiplierEnabled, - modifier = Modifier.fillMaxWidth().controllerConfirm(enabled = multiplierEnabled, onClick = pickMultiplier), + modifier = Modifier.fillMaxWidth().controllerConfirm(enabled = multiplierEnabled, onClick = pickMultiplier) + .frameGenFocus(sessionHost), contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), colors = ButtonDefaults.outlinedButtonColors( containerColor = if (selected) colors.secondary.copy(alpha = 0.18f) else colors.surface, @@ -185,7 +209,8 @@ internal fun FrameGenMenu( OutlinedButton( onClick = pickMultiplier, enabled = multiplierEnabled, - modifier = Modifier.weight(1f).controllerConfirm(enabled = multiplierEnabled, onClick = pickMultiplier), + modifier = Modifier.weight(1f).controllerConfirm(enabled = multiplierEnabled, onClick = pickMultiplier) + .frameGenFocus(sessionHost), contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), colors = ButtonDefaults.outlinedButtonColors( containerColor = if (selected) colors.secondary.copy(alpha = 0.18f) else colors.surface, @@ -204,7 +229,7 @@ internal fun FrameGenMenu( OutlinedButton( onClick = togglePacer, modifier = Modifier.fillMaxWidth().padding(top = 8.dp) - .controllerConfirm(onClick = togglePacer), + .controllerConfirm(onClick = togglePacer).frameGenFocus(sessionHost), contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), colors = ButtonDefaults.outlinedButtonColors( containerColor = if (pacerEnabled) colors.primary.copy(alpha = 0.16f) else colors.surface, @@ -230,7 +255,7 @@ internal fun FrameGenMenu( modifier = Modifier.weight(1f).controllerConfirm( enabled = lossless.ready, onClick = pickTarget, - ), + ).frameGenFocus(sessionHost), contentPadding = PaddingValues(horizontal = 8.dp, vertical = 8.dp), colors = ButtonDefaults.outlinedButtonColors( containerColor = if (selected) colors.secondary.copy(alpha = 0.18f) else colors.surface, @@ -243,21 +268,107 @@ internal fun FrameGenMenu( } } - if (selectedEngine == FrameGen.ENGINE_LSFG && !lossless.ready) { + if (sessionHost == null && selectedEngine == FrameGen.ENGINE_LSFG && !lossless.ready) { Text(need, color = colors.onSurfaceVariant, style = MaterialTheme.typography.bodySmall, modifier = Modifier.padding(top = 6.dp)) } - if (gameProfileId != null) { - val resetGame = onResetGame + if (gameProfileId != null && (sessionHost == null || hasGameOverride)) { + val input = LocalInputModeManager.current + val resetGame = { + if (sessionHost != null && input.inputMode == InputMode.Keyboard) { + sessionEngineAnchor.requestFocus() + } + onResetGame() + } OutlinedButton( onClick = resetGame, modifier = Modifier.fillMaxWidth().padding(top = 8.dp) - .controllerConfirm(onClick = resetGame), + .testTag("framegen-use-global").controllerConfirm(onClick = resetGame) + .frameGenFocus(sessionHost), contentPadding = PaddingValues(horizontal = 12.dp, vertical = 8.dp), ) { - Text(stringResource(R.string.comp_game_use_general) + if (hasGameOverride) "" else " ✓") + Text(if (sessionHost != null) stringResource(R.string.frame_gen_use_global) + else stringResource(R.string.comp_game_use_general) + if (hasGameOverride) "" else " ✓") + } + } + } +} + +@Composable +private fun Modifier.frameGenFocus(sessionHost: MenuHost?): Modifier = + if (sessionHost != null) focusGlide(RoundedCornerShape(percent = 50)) else this + +@Composable +private fun SessionFrameGenEngine( + host: MenuHost, + mode: FrameGen.Mode, + options: List>, + losslessReady: Boolean, + losslessNote: String, + focusModifier: Modifier, + anchor: FocusRequester, + onPick: (FrameGen.Mode) -> Unit, +) { + val label = stringResource(R.string.frame_gen_engine) + val selectedLabel = options.firstOrNull { (engine, _) -> engine == mode.engine }?.second + ?: options.first().second + val key = "frame-generation-engine" + val open = host.open == key + val input = LocalInputModeManager.current + var anchorFocused by remember { mutableStateOf(false) } + var restoreFocus by remember { mutableStateOf(false) } + + // The menu has its own focusable window; wait for its exit transition before restoring the anchor. + LaunchedEffect(open) { + if (!open && restoreFocus && host.open == null) { + repeat(24) { + withFrameNanos { } + runCatching { anchor.requestFocus() } + if (anchorFocused) { + restoreFocus = false + return@LaunchedEffect + } + } + restoreFocus = 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 + } + } + 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))) + } + } + } } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt index d7d596aa92..2430aa45f9 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -18,9 +18,11 @@ import androidx.compose.ui.input.key.type import androidx.compose.ui.layout.LayoutCoordinates import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalLayoutDirection import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.IntSize +import androidx.compose.ui.unit.LayoutDirection import kotlin.math.roundToInt import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay @@ -89,6 +91,7 @@ import androidx.compose.foundation.verticalScroll import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedButton +import androidx.compose.material3.Slider import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -933,6 +936,7 @@ private fun DrawerFrameGenSettings(host: MenuHost, a: DrawerActions, track: (Str hasGameOverride = a.hasGameOverride, onResetGame = a.onFrameGenReset, focusModifier = track("frame-generation"), + sessionHost = host, ) } } @@ -948,7 +952,7 @@ private fun DrawerDisplayPage(host: MenuHost, a: DrawerActions, track: (String) val orientations = SessionPrefs.orientationOptions.map { (value, label) -> value to stringResource(label) } SettingsGroup(stringResource(R.string.drawer_page_display), key = "display", detail = summary(if (a.gameZoomAvailable) "${(a.gameZoom * 100.0f).toInt()}%" else null, orientations.labelOf(a.orientation))) { - if (a.gameZoomAvailable) GameZoomRow(a, track) + if (a.gameZoomAvailable) GameZoomRow(a.gameZoom, track, a.onGameZoom) ChoiceRow(host, "orientation", stringResource(R.string.setup_session_orientation), null, orientations, a.orientation, chipModifier = track("orientation"), onPick = a.onOrientation) if (a.steam) PicturePositionRow(host, a.picturePosition, track("picture-position"), onPick = a.onPicturePosition) @@ -970,27 +974,66 @@ private fun DrawerDisplayPage(host: MenuHost, a: DrawerActions, track: (String) SecondScreenGroup(host, a, track) } +private const val GAME_ZOOM_MIN = 100 +private const val GAME_ZOOM_MAX = 300 +private const val GAME_ZOOM_STEP = 25 +private const val GAME_ZOOM_INTERVALS = (GAME_ZOOM_MAX - GAME_ZOOM_MIN) / GAME_ZOOM_STEP + @Composable -private fun GameZoomRow(a: DrawerActions, track: (String) -> Modifier) { - SettingsRow(stringResource(R.string.game_zoom_title), stringResource(R.string.game_zoom_hint)) { - Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp)) { - val zoomOutEnabled = a.gameZoom > 1.0f - val zoomInEnabled = a.gameZoom < 3.0f - val zoomOut = { a.onGameZoom((a.gameZoom - 0.25f).coerceAtLeast(1.0f)) } - val zoomIn = { a.onGameZoom((a.gameZoom + 0.25f).coerceAtMost(3.0f)) } - val zoomReset = { a.onGameZoom(1.0f) } - OutlinedButton(onClick = zoomOut, enabled = zoomOutEnabled, - modifier = track("game-zoom-out").width(42.dp).controllerConfirm(enabled = zoomOutEnabled, onClick = zoomOut), - contentPadding = PaddingValues(horizontal = 4.dp, vertical = 0.dp)) { Text("−") } - Text("${(a.gameZoom * 100.0f).toInt()}%", fontSize = 12.sp) - OutlinedButton(onClick = zoomIn, enabled = zoomInEnabled, - modifier = track("game-zoom-in").width(42.dp).controllerConfirm(enabled = zoomInEnabled, onClick = zoomIn), - contentPadding = PaddingValues(horizontal = 4.dp, vertical = 0.dp)) { Text("+") } - OutlinedButton(onClick = zoomReset, enabled = zoomOutEnabled, - modifier = track("game-zoom-reset").width(48.dp).controllerConfirm(enabled = zoomOutEnabled, onClick = zoomReset), - contentPadding = PaddingValues(horizontal = 2.dp, vertical = 0.dp)) { Text("1×") } - } +internal fun GameZoomRow( + zoom: Float, + track: (String) -> Modifier, + onZoom: (Float) -> Unit, +) { + val source = remember { MutableInteractionSource() } + val focused by source.collectIsFocusedAsState() + val label = stringResource(R.string.game_zoom_title) + val percent = (zoom * 100f).roundToInt().coerceIn(GAME_ZOOM_MIN, GAME_ZOOM_MAX) + val value = stringResource(R.string.game_zoom_value, percent) + fun detent(value: Float): Int = ((value - GAME_ZOOM_MIN) / GAME_ZOOM_STEP).roundToInt() + val selectedDetent = detent(percent.toFloat()) + val rtl = LocalLayoutDirection.current == LayoutDirection.Rtl + val pal = LocalPalette.current + val colors = MaterialTheme.colorScheme + + fun chooseDetent(index: Int) { + val next = GAME_ZOOM_MIN + index.coerceIn(0, GAME_ZOOM_INTERVALS) * GAME_ZOOM_STEP + if (next != percent) onZoom(next / 100f) } + Column(Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 6.dp)) { + Row( + Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.SpaceBetween, + ) { + Text(label, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground) + Text(value, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold, + color = colors.onSurface, modifier = Modifier.testTag("game-zoom-value")) + } + Slider( + value = (GAME_ZOOM_MIN + selectedDetent * GAME_ZOOM_STEP).toFloat(), + onValueChange = { chooseDetent(detent(it)) }, + valueRange = GAME_ZOOM_MIN.toFloat()..GAME_ZOOM_MAX.toFloat(), + steps = GAME_ZOOM_INTERVALS - 1, + interactionSource = source, + modifier = track("game-zoom-slider").fillMaxWidth().heightIn(min = 48.dp) + .glideBorder(focused, RoundedCornerShape(10.dp), pal.signal) + .testTag("game-zoom-slider") + .semantics { contentDescription = label; stateDescription = value } + .onPreviewKeyEvent { event -> + val delta = when (event.key) { + Key.DirectionLeft -> if (rtl) 1 else -1 + Key.DirectionRight -> if (rtl) -1 else 1 + else -> return@onPreviewKeyEvent false + } + if (event.type == KeyEventType.KeyDown && event.nativeKeyEvent.repeatCount == 0) { + chooseDetent(selectedDetent + delta) + } + true + }, + ) + } + Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line)) } @Composable diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt index 5d09490e39..16712ad3b8 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt @@ -322,6 +322,7 @@ 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) } @@ -335,9 +336,9 @@ fun AnchoredMenu( } val hostView = androidx.compose.ui.platform.LocalView.current val firstItemFocus = remember { FocusRequester() } - val inputMode = LocalInputModeManager.current.inputMode - LaunchedEffect(open, inputMode) { - if (open && inputMode == InputMode.Keyboard) { + val requestKeyboardFocus = keyboardFocus || LocalInputModeManager.current.inputMode == InputMode.Keyboard + LaunchedEffect(open, requestKeyboardFocus) { + if (open && requestKeyboardFocus) { repeat(2) { androidx.compose.runtime.withFrameNanos { } } runCatching { firstItemFocus.requestFocus() } android.util.Log.i("AnchoredMenu", "requested first option focus") @@ -353,10 +354,10 @@ fun AnchoredMenu( TrackKeyWindow() PassTriggerMotion(hostView) val popupInput = LocalInputModeManager.current - LaunchedEffect(open, inputMode) { - if (!open || inputMode != InputMode.Keyboard) return@LaunchedEffect + LaunchedEffect(open, requestKeyboardFocus) { + if (!open || !requestKeyboardFocus) return@LaunchedEffect popupInput.requestInputMode(InputMode.Keyboard) - repeat(8) { + repeat(if (keyboardFocus) 24 else 8) { androidx.compose.runtime.withFrameNanos { } if (runCatching { firstItemFocus.captureFocus() }.getOrDefault(false)) { firstItemFocus.freeFocus() diff --git a/app/src/main/res/drawable/ic_frame_generation.xml b/app/src/main/res/drawable/ic_frame_generation.xml index ea4c55cd2f..bf643a0c8d 100644 --- a/app/src/main/res/drawable/ic_frame_generation.xml +++ b/app/src/main/res/drawable/ic_frame_generation.xml @@ -2,7 +2,13 @@ - + android:viewportWidth="24" + android:viewportHeight="24"> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a0be598c63..158461750a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2838,6 +2838,9 @@ Image scaling Adjust game magnification from 100% to 300% Game zoom + %1$d%% + Engine + Use global setting Integer scaling Uses the largest whole-number scale that fits. May crop screen edges. If the source is larger than the output, fractional scaling fits the full image. Resolution is configured separately. Fit diff --git a/app/src/test/java/com/droiddeck/launcher/ui/SessionControlDesignTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/SessionControlDesignTest.kt new file mode 100644 index 0000000000..f576756873 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/SessionControlDesignTest.kt @@ -0,0 +1,239 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.width +import androidx.compose.material3.OutlinedButton +import androidx.compose.material3.Text +import androidx.compose.runtime.mutableStateOf +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.input.InputMode +import androidx.compose.ui.input.InputModeManager +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.platform.LocalInputModeManager +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.semantics.SemanticsActions +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.assertIsFocused +import androidx.compose.ui.test.assertIsNotEnabled +import androidx.compose.ui.test.assertTextEquals +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.click +import androidx.compose.ui.test.performTouchInput +import androidx.compose.ui.test.swipe +import androidx.compose.ui.test.performKeyInput +import androidx.compose.ui.test.performSemanticsAction +import androidx.compose.ui.test.pressKey +import androidx.compose.ui.unit.dp +import com.droiddeck.launcher.gpu.FrameGen +import com.droiddeck.launcher.gpu.Lossless +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNotSame +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.robolectric.annotation.GraphicsMode + +@OptIn(ExperimentalTestApi::class, androidx.compose.ui.ExperimentalComposeUiApi::class) +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], qualifiers = "w360dp-h640dp-port", application = android.app.Application::class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class SessionControlDesignTest { + @get:Rule val compose = createRobolectricComposeRule() + + private val engineAnchor get() = compose.onNodeWithTag("setting-frame-generation-engine") + private fun engine(name: String) = compose.onNodeWithTag("framegen-engine:$name") + 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)) + lateinit var input: InputModeManager + compose.setContent { + DroidDeckTheme { + input = LocalInputModeManager.current + Box(Modifier.width(300.dp)) { + FrameGenMenu(mode.value, Lossless.State.NONE, { mode.value = it }, sessionHost = rememberMenuHost()) + } + } + } + + compose.runOnIdle { input.requestInputMode(InputMode.Keyboard) } + engineAnchor.performSemanticsAction(SemanticsActions.RequestFocus) + engineAnchor.assertIsFocused().performKeyInput { pressKey(Key.ButtonA) } + settlePopupFocus() + 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() + engine(FrameGen.ENGINE_ZEROFG).assertDoesNotExist() + assertEquals(FrameGen.Mode(FrameGen.ENGINE_ZEROFG, 3), mode.value) + engineAnchor.assertIsFocused().performKeyInput { pressKey(Key.ButtonA) } + settlePopupFocus() + engine(FrameGen.ENGINE_ZEROFG).assertIsFocused().performKeyInput { pressKey(Key.ButtonB) } + settlePopupFocus() + engine(FrameGen.ENGINE_ZEROFG).assertDoesNotExist() + assertEquals(FrameGen.Mode(FrameGen.ENGINE_ZEROFG, 3), mode.value) + engineAnchor.assertIsFocused() + } + + @Test + fun gameOverrideCanReturnToGlobalWithoutLosingControllerFocus() { + val override = mutableStateOf(true) + lateinit var input: InputModeManager + compose.setContent { + DroidDeckTheme { + input = LocalInputModeManager.current + FrameGenMenu( + mode = if (override.value) FrameGen.Mode(FrameGen.ENGINE_ZEROFG, 2) else FrameGen.Mode.OFF, + lossless = Lossless.State.NONE, + onPick = {}, + gameProfileId = "8400", + hasGameOverride = override.value, + onResetGame = { override.value = false }, + sessionHost = rememberMenuHost(), + ) + } + } + compose.runOnIdle { input.requestInputMode(InputMode.Keyboard) } + val reset = compose.onNodeWithTag("framegen-use-global") + reset.assertTextEquals("Use global setting").performSemanticsAction(SemanticsActions.RequestFocus) + reset.assertIsFocused().performKeyInput { pressKey(Key.ButtonA) } + reset.assertDoesNotExist() + engineAnchor.assertIsFocused() + } + + @Test + fun frameGenButtonsMoveTheDrawerFocusRing() { + lateinit var input: InputModeManager + lateinit var glide: FocusGlide + compose.setContent { + DroidDeckTheme { + FocusGlideHost { + input = LocalInputModeManager.current + glide = checkNotNull(LocalFocusGlide.current) + FrameGenMenu( + mode = FrameGen.Mode(FrameGen.ENGINE_ZEROFG, 2), + lossless = Lossless.State.NONE, + onPick = {}, + gameProfileId = "8400", + hasGameOverride = true, + sessionHost = rememberMenuHost(), + ) + } + } + } + compose.runOnIdle { input.requestInputMode(InputMode.Keyboard) } + var previous: GlideSource? = null + for (label in listOf("2×", "3×", "4×", "ZeroPacer", "Use global setting")) { + val button = if (label == "ZeroPacer") compose.onNodeWithText(label, substring = true) + else compose.onNodeWithText(label) + button.performSemanticsAction(SemanticsActions.RequestFocus).assertIsFocused() + compose.runOnIdle { + assertNotNull("Missing focus ring for $label", glide.current) + assertNotSame("Focus ring did not move to $label", previous, glide.current) + previous = glide.current + } + } + } + + @Test + fun legacyFrameGenerationStillUsesTheFlatOptions() { + compose.setContent { + DroidDeckTheme { + FrameGenMenu(FrameGen.Mode.OFF, Lossless.State.NONE, {}, legacyButtons = true) + } + } + engineAnchor.assertDoesNotExist() + compose.onNodeWithText("ZeroFG 2×").assertExists() + } + + @Test + fun zoomStepsBy25PercentAndDpadCanLeaveIt() { + val value = mutableStateOf(1f) + val changes = mutableListOf() + lateinit var input: InputModeManager + compose.setContent { + DroidDeckTheme { + input = LocalInputModeManager.current + Column(Modifier.width(300.dp)) { + OutlinedButton(onClick = {}, modifier = Modifier.fillMaxWidth().testTag("above")) { Text("Above") } + GameZoomRow(value.value, { Modifier }) { changes += it; value.value = it } + OutlinedButton(onClick = {}, modifier = Modifier.fillMaxWidth().testTag("below")) { Text("Below") } + } + } + } + + compose.runOnIdle { input.requestInputMode(InputMode.Keyboard) } + val slider = zoom("slider") + zoomValue(100) + zoom("reset").assertDoesNotExist() + slider.performSemanticsAction(SemanticsActions.RequestFocus) + slider.assertIsFocused().performKeyInput { pressKey(Key.DirectionLeft) } + zoomValue(100) + assertEquals(emptyList(), changes) + slider.assertIsFocused().performKeyInput { pressKey(Key.DirectionUp) } + compose.onNodeWithTag("above").assertIsFocused().performKeyInput { pressKey(Key.DirectionDown) } + slider.assertIsFocused().performKeyInput { pressKey(Key.DirectionDown) } + compose.onNodeWithTag("below").assertIsFocused().performKeyInput { pressKey(Key.DirectionUp) } + slider.assertIsFocused() + + for (percent in 125..300 step 25) { + slider.performKeyInput { pressKey(Key.DirectionRight) } + zoomValue(percent) + slider.assertIsFocused() + } + slider.performKeyInput { pressKey(Key.DirectionRight) } + zoomValue(300) + assertEquals((125..300 step 25).map { it / 100f }, changes) + slider.performKeyInput { pressKey(Key.DirectionLeft) } + zoomValue(275) + zoom("reset").assertDoesNotExist() + slider.performKeyInput { pressKey(Key.DirectionDown) } + compose.onNodeWithTag("below").assertIsFocused().performKeyInput { pressKey(Key.DirectionUp) } + slider.assertIsFocused() + for (percent in 250 downTo 100 step 25) { + slider.performKeyInput { pressKey(Key.DirectionLeft) } + zoomValue(percent) + } + } + + @Test + fun touchTapAndDragSnapToTheNineValues() { + val value = mutableStateOf(1f) + val changes = mutableListOf() + compose.setContent { + DroidDeckTheme { + Box(Modifier.width(300.dp)) { + GameZoomRow(value.value, { Modifier }) { changes += it; value.value = it } + } + } + } + val slider = zoom("slider").assertIsDisplayed() + slider.performTouchInput { click(center) } + zoomValue(200) + slider.performTouchInput { swipe(center, Offset(width - 2f, center.y)) } + zoomValue(300) + slider.performTouchInput { click(Offset(2f, center.y)) } + zoomValue(100) + val allowed = (100..300 step 25).map { it / 100f }.toSet() + assertEquals(true, changes.all { it in allowed }) + } +}