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.
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-12-04 07:20:03 -05:00
1 parent a6ffbe7d81
commit bf8faf06e1
6 files changed
+492 -89

No files matched your search

@@ -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<Pair<String, String>>,
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)))
}
}
}
}
}
}
@@ -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
@@ -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()