From a1bc8467fd8c4591c5b2aa5c89b7e2215e063786 Mon Sep 17 00:00:00 2001 From: William Allen Date: Fri, 6 Nov 2026 09:43:44 -0600 Subject: [PATCH] feat: show Steam launch options on the Launch tab (#2163) * Show Steam launch options on the Launch tab * fix: Launch options is one row on the Launch tab, like the shortcut fields * fix(launch): reread Steam's launch options when the game page resumes * fix(launch): keep launch options disabled while they reload on resume * fix(launch): a saved launch option is not replaced by a read that started before it * fix(launch): an unreadable Steam config keeps the shown launch options instead of clearing them * fix(launch): an unparsable Steam config and a pending reload keep the shown launch options * fix(launch): the resume reload follows the game the panel shows --------- Co-authored-by: maxjivi05 --- .../launcher/frontend/SteamLaunchOptions.kt | 7 +- .../droiddeck/launcher/ui/AddedGameEditor.kt | 116 +++++++++++------- .../com/droiddeck/launcher/ui/GameDetail.kt | 6 + .../frontend/SteamLaunchOptionsTest.kt | 5 +- .../ui/SteamLaunchOptionsVisibilityTest.kt | 21 ++++ 5 files changed, 109 insertions(+), 46 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/SteamLaunchOptionsVisibilityTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/SteamLaunchOptions.kt b/app/src/main/java/com/droiddeck/launcher/frontend/SteamLaunchOptions.kt index cc832db347..9a8137d352 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/SteamLaunchOptions.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/SteamLaunchOptions.kt @@ -14,8 +14,8 @@ object SteamLaunchOptions { private const val TAG = "SteamLaunchOptions" private val STEAM = listOf("UserLocalConfigStore", "Software", "Valve", "Steam") - fun current(context: Context, appId: Int): String = - SteamLiveShortcuts.launchOptions(context, appId.toLong()) ?: steam(SessionPrefs.activeSteamRoot(context), appId).orEmpty() + fun current(context: Context, appId: Int): String? = + SteamLiveShortcuts.launchOptions(context, appId.toLong()) ?: steam(SessionPrefs.activeSteamRoot(context), appId) enum class Saved { YES, NO, DESKTOP } @@ -35,7 +35,8 @@ object SteamLaunchOptions { internal fun steam(steamHome: File, appId: Int): String? { val config = AddedGames.accountConfig(steamHome) ?: return null val text = runCatching { File(config, "localconfig.vdf").readText() }.getOrNull() ?: return null - return launchOptions(text, appId) + if (section(text, STEAM) == null) return null + return launchOptions(text, appId).orEmpty() } internal fun write(steamHome: File, appId: Int, options: String): Boolean { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AddedGameEditor.kt b/app/src/main/java/com/droiddeck/launcher/ui/AddedGameEditor.kt index 7e0103ec8e..89e1480869 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AddedGameEditor.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AddedGameEditor.kt @@ -49,7 +49,9 @@ import androidx.compose.ui.input.InputMode import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalConfiguration import android.content.Context +import androidx.compose.runtime.DisposableEffect import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.Role @@ -60,6 +62,8 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleEventObserver import coil.compose.AsyncImage import com.droiddeck.launcher.R import com.droiddeck.launcher.frontend.AddedGameArt @@ -192,7 +196,7 @@ internal fun SteamGameArtEditor( game: com.droiddeck.launcher.frontend.Library.SteamGame, onClose: () -> Unit, onChanged: () -> Unit, ) { val target = remember(game.appId) { ArtTarget.of(game) } - ArtOnlyEditor(game.name, target, game, { SteamGameArt.describe(it, game) }, onClose, onChanged, launchAppId = game.appId) + ArtOnlyEditor(game.name, target, game, { SteamGameArt.describe(it, game) }, onClose, onChanged) } @Composable @@ -208,25 +212,17 @@ internal fun StoreGameArtEditor(folder: String, name: String, onClose: () -> Uni @Composable private fun ArtOnlyEditor( name: String, target: ArtTarget?, refresh: Any?, describe: (Context) -> List, - onClose: () -> Unit, onChanged: () -> Unit, launchAppId: Int? = null, + onClose: () -> Unit, onChanged: () -> Unit, ) { val app = LocalContext.current.applicationContext - val colors = MaterialTheme.colorScheme - val scope = rememberCoroutineScope() val shown = rememberShown(onClose) val close = { shown.targetState = false } var version by remember { mutableIntStateOf(0) } var arts by remember { mutableStateOf>(emptyList()) } var artSlot by remember { mutableStateOf(null) } var logoOpen by remember { mutableStateOf(false) } - var launchOptions by remember { mutableStateOf(null) } - var launchVersion by remember { mutableIntStateOf(0) } - var editingLaunch by remember { mutableStateOf(null) } - var launchSaving by remember { mutableStateOf(false) } - var launchError by remember { mutableStateOf(null) } val position = rememberLogoPosition(target, version) LaunchedEffect(version, refresh) { if (target != null) arts = withContext(Dispatchers.IO) { describe(app) } } - LaunchedEffect(launchAppId, launchVersion) { if (launchAppId != null) launchOptions = withContext(Dispatchers.IO) { SteamLaunchOptions.current(app, launchAppId) } } val first = remember { FocusRequester() } LaunchedEffect(Unit) { focusWithinFrames({ false }) { first } } AppDialog(shown, close, "artEditor", wide = false, maxWidth = 460.dp) { @@ -236,19 +232,10 @@ private fun ArtOnlyEditor( verticalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.fillMaxWidth().heightIn(max = maxHeight).verticalScroll(rememberScrollState()), ) { - if (launchAppId != null) { - FieldRow(stringResource(R.string.edit_launch_options), focus = first, onClick = { if (!launchSaving) launchOptions?.let { editingLaunch = it } }) { - val options = launchOptions - if (options == null || launchSaving) Loading() else ValueText(options.ifEmpty { "—" }, muted = options.isEmpty()) - } - val error = launchError - if (error != null) Text(stringResource(error), fontSize = 12.sp, color = colors.error, modifier = Modifier.padding(start = 4.dp)) - else Text(stringResource(R.string.edit_launch_options_steam_hint), fontSize = 12.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(start = 4.dp)) - } - Text(stringResource(R.string.edit_artwork), fontSize = 12.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(start = 4.dp, top = if (launchAppId != null) 8.dp else 2.dp)) + Text(stringResource(R.string.edit_artwork), fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 4.dp, top = 2.dp)) for (slot in AddedGameArt.Slot.values()) key(slot) { val state = arts.firstOrNull { it.slot == slot } - FieldRow(slot.label(), focus = if (slot == AddedGameArt.Slot.COVER && launchAppId == null) first else null, onClick = { if (target != null) artSlot = slot }) { + FieldRow(slot.label(), focus = if (slot == AddedGameArt.Slot.COVER) first else null, onClick = { if (target != null) artSlot = slot }) { Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) { SlotThumb(slot, state?.file) if (state != null) ValueText(artSourceLabel(state.source), muted = true) else Loading() @@ -258,27 +245,6 @@ private fun ArtOnlyEditor( PickerRow(Icons.Outlined.Tune, stringResource(R.string.art_adjust_logo), logoPositionSummary(position?.value), enabled = target != null && position != null) { logoOpen = true } } } - val before = editingLaunch - if (before != null && launchAppId != null) TextEntryDialog(stringResource(R.string.edit_launch_options), before, onDismiss = { editingLaunch = null }) { value -> - if (launchSaving || value == before.trim()) return@TextEntryDialog - launchSaving = true - scope.launch { - try { - val saved = withContext(Dispatchers.IO) { SteamLaunchOptions.set(app, launchAppId, value, before) } - launchError = when (saved) { - SteamLaunchOptions.Saved.YES -> null - SteamLaunchOptions.Saved.DESKTOP -> R.string.edit_launch_options_desktop - SteamLaunchOptions.Saved.NO -> R.string.edit_launch_options_not_saved - } - if (saved == SteamLaunchOptions.Saved.YES) launchOptions = value else { - launchOptions = null - launchVersion++ - } - } finally { - launchSaving = false - } - } - } val chosen = target ?: return artSlot?.let { slot -> ArtChooserDialog(chosen, slot, arts.firstOrNull { it.slot == slot }, onBack = { artSlot = null }, onCloseAll = { artSlot = null; close() }) { version++; onChanged() } @@ -287,6 +253,72 @@ private fun ArtOnlyEditor( if (logoOpen && loaded != null) LogoPositionHost(chosen, arts, loaded, onBack = { logoOpen = false }, onCloseAll = { logoOpen = false; close() }) { version++; onChanged() } } +@Composable +internal fun SteamLaunchOptionsPanel(appId: Int) { + val app = LocalContext.current.applicationContext + val colors = MaterialTheme.colorScheme + val scope = rememberCoroutineScope() + var options by remember(appId) { mutableStateOf(null) } + var version by remember(appId) { mutableIntStateOf(0) } + var editing by remember(appId) { mutableStateOf(null) } + var saving by remember(appId) { mutableStateOf(false) } + var loading by remember(appId) { mutableStateOf(false) } + var error by remember(appId) { mutableStateOf(null) } + LaunchedEffect(appId, version) { + options = withContext(Dispatchers.IO) { SteamLaunchOptions.current(app, appId) } ?: options.orEmpty() + loading = false + } + val lifecycle = LocalLifecycleOwner.current.lifecycle + DisposableEffect(lifecycle, appId) { + val observer = LifecycleEventObserver { _, event -> + if (event == Lifecycle.Event.ON_RESUME && !saving) { + loading = true + version++ + } + } + lifecycle.addObserver(observer) + onDispose { lifecycle.removeObserver(observer) } + } + Column(Modifier.fillMaxWidth().tabPanel(), verticalArrangement = Arrangement.spacedBy(2.dp)) { + val value = options + MenuItem( + stringResource(R.string.edit_launch_options), checked = false, enabled = value != null && !saving && !loading, + detail = if (value == null || saving || loading) "…" else value.ifEmpty { "—" }, + ) { if (!saving && !loading) options?.let { editing = it } } + val message = error + Text( + stringResource(message ?: R.string.edit_launch_options_steam_hint), + fontSize = 12.sp, + color = if (message == null) colors.onSurfaceVariant else colors.error, + modifier = Modifier.padding(start = 4.dp), + ) + } + val before = editing + if (before != null) TextEntryDialog(stringResource(R.string.edit_launch_options), before, onDismiss = { editing = null }) { value -> + if (saving || value == before.trim()) return@TextEntryDialog + saving = true + scope.launch { + try { + val saved = withContext(Dispatchers.IO) { SteamLaunchOptions.set(app, appId, value, before) } + error = when (saved) { + SteamLaunchOptions.Saved.YES -> null + SteamLaunchOptions.Saved.DESKTOP -> R.string.edit_launch_options_desktop + SteamLaunchOptions.Saved.NO -> R.string.edit_launch_options_not_saved + } + if (saved == SteamLaunchOptions.Saved.YES) { + options = value + version++ + } else { + options = null + version++ + } + } finally { + saving = false + } + } + } +} + private class LoadedPosition(val value: LogoPosition.Value?) @Composable diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt index 915a621756..e1e027d78f 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt @@ -808,11 +808,17 @@ private fun LaunchTab(game: Library.SteamGame, s: FrontEndState, a: FrontEndActi val host = rememberMenuHost() Column(verticalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.fillMaxWidth()) { LaunchSettings(s, a, host, game, hopRail ?: GameRef.key(game)) + if (canEditSteamLaunchOptions(game, s.shortcutPicker)) { + SteamLaunchOptionsPanel(game.appId) + } val folder = game.gameFiles if (game.library == Library.ADDED && game.source == Library.ADDED && !s.shortcutPicker && folder != null) ShortcutFields(game, folder) } } +internal fun canEditSteamLaunchOptions(game: Library.SteamGame, shortcutPicker: Boolean): Boolean = + !shortcutPicker && game.source == Library.SOURCE_STEAM && game.library != Library.ADDED + @Composable private fun ShortcutFields(game: Library.SteamGame, folder: File) { val ctx = LocalContext.current diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/SteamLaunchOptionsTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/SteamLaunchOptionsTest.kt index 46cdd111c5..67d073fa9c 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/SteamLaunchOptionsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/SteamLaunchOptionsTest.kt @@ -117,7 +117,10 @@ class SteamLaunchOptionsTest { assertTrue(SteamLaunchOptions.write(home, 1790600, "-dx12")) config.writeText(SteamLaunchOptions.withLaunchOptions(config.readText(), 1790600, "-vulkan")!!) assertEquals("-vulkan", SteamLaunchOptions.steam(home, 1790600)) - assertNull(SteamLaunchOptions.steam(home, 400)) + assertEquals("", SteamLaunchOptions.steam(home, 400)) + assertNull(SteamLaunchOptions.steam(steamHome(tmp.newFolder("Unread")), 1790600)) + config.writeText("") + assertNull(SteamLaunchOptions.steam(home, 1790600)) } private val app get() = RuntimeEnvironment.getApplication() diff --git a/app/src/test/java/com/droiddeck/launcher/ui/SteamLaunchOptionsVisibilityTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/SteamLaunchOptionsVisibilityTest.kt new file mode 100644 index 0000000000..2d18b72922 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/SteamLaunchOptionsVisibilityTest.kt @@ -0,0 +1,21 @@ +package com.droiddeck.launcher.ui + +import com.droiddeck.launcher.frontend.Library +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class SteamLaunchOptionsVisibilityTest { + private fun game(library: String = "main", source: String = Library.SOURCE_STEAM) = + Library.SteamGame(42, "Test game", null, library, source = source) + + @Test fun nativeSteamGamesExposeLaunchOptionsOutsideShortcutPicking() { + assertTrue(canEditSteamLaunchOptions(game(), shortcutPicker = false)) + } + + @Test fun pickerAddedAndStoreGamesDoNotExposeSteamLaunchOptions() { + assertFalse(canEditSteamLaunchOptions(game(), shortcutPicker = true)) + assertFalse(canEditSteamLaunchOptions(game(library = Library.ADDED), shortcutPicker = false)) + assertFalse(canEditSteamLaunchOptions(game(source = "gog"), shortcutPicker = false)) + } +}