Download components from the session drawer (#679)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-12 22:18:47 -04:00
1 parent 6c35297dba
commit 1c1a96cb19
14 files changed
+227 -22

No files matched your search

@@ -9,10 +9,10 @@ import com.droiddeck.launcher.session.ComponentsManager
import com.droiddeck.launcher.session.ProtonDefault
/**
* The Components page's state and work: the Protons and their DXVK/VKD3D sets, the Nightlies
* catalog, downloads and the swap/restore actions. The launcher screen keeps one and shows it.
* Component state and work for the launcher and session drawer: the Protons and their
* DXVK/VKD3D sets, the Nightlies catalog, downloads and the swap/restore actions.
*/
internal class ComponentsMenu(private val activity: android.app.Activity, private val ui: android.os.Handler) {
internal class ComponentsMenu(private val activity: android.app.Activity, private val ui: Handler) {
var compSnapshot by mutableStateOf<ComponentsManager.Snapshot?>(null)
var compCatalog by mutableStateOf<List<ComponentsManager.CatalogItem>>(emptyList())
var compCatalogAt by mutableStateOf(0L)
@@ -20,7 +20,10 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat
var compComp by mutableStateOf(com.droiddeck.launcher.ui.GPU_TAB)
var compChecking by mutableStateOf(false)
var compBusy by mutableStateOf<String?>(null)
var compDownloads by mutableStateOf<Map<String, Int>>(emptyMap())
var compDownloads: Map<String, Int>
get() = downloads
private set(value) { downloads = value }
val compDownloadRevision: Long get() = downloadRevision
private var shownDefault: String? = null
/** Reads the Protons (and on first open saves their originals) off the UI thread; the Nightlies list comes from its cache. */
@@ -72,15 +75,15 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat
}, "components-action").start()
}
fun refreshComponentCatalog() {
fun refreshComponentCatalog(force: Boolean = true) {
if (compChecking) return
compChecking = true
Thread({
val cat = runCatching { ComponentsManager.catalog(activity, true) }.getOrNull()
val cat = runCatching { ComponentsManager.catalog(activity, force) }.getOrNull()
ui.post {
compChecking = false
if (cat == null || cat.items.isEmpty()) android.widget.Toast.makeText(activity, activity.getString(R.string.comp_nightlies_unreachable), android.widget.Toast.LENGTH_LONG).show()
else { compCatalog = cat.items; compCatalogAt = cat.fetchedAt }
if (cat != null && cat.items.isNotEmpty()) { compCatalog = cat.items; compCatalogAt = cat.fetchedAt }
else if (force) android.widget.Toast.makeText(activity, activity.getString(R.string.comp_nightlies_unreachable), android.widget.Toast.LENGTH_LONG).show()
}
}, "components-catalog").start()
}
@@ -95,6 +98,7 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat
}.getOrElse { e -> activity.getString(R.string.comp_download_failed, e.message ?: e.javaClass.simpleName) }
ui.post {
compDownloads = compDownloads - item.file
downloadRevision++
android.widget.Toast.makeText(activity, message, android.widget.Toast.LENGTH_SHORT).show()
refreshComponents()
}
@@ -102,6 +106,10 @@ internal class ComponentsMenu(private val activity: android.app.Activity, privat
}
private companion object {
const val TAG = "MainActivity"
const val TAG = "ComponentsMenu"
// Both screens use the same package store and .part files. Keep the in-flight registry
// across menu/activity instances so a package cannot be downloaded twice at once.
var downloads by mutableStateOf<Map<String, Int>>(emptyMap())
var downloadRevision by mutableStateOf(0L)
}
}
@@ -1230,6 +1230,8 @@ class MainActivity : ComponentActivity() {
)
},
)
// A download can finish in the session drawer while this page is visible.
androidx.compose.runtime.LaunchedEffect(components.compDownloadRevision) { components.refreshComponents() }
// A Proton installed or removed changes the stack's list of Protons.
androidx.compose.runtime.LaunchedEffect(protons.protonBusyId) { if (protons.protonBusyId == null) components.refreshComponents() }
}
@@ -263,6 +263,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private var closeAtOnce = false
/** The drawer's Components tab: the Protons as last read (ComponentsManager). */
private var drawerComponents by mutableStateOf<ComponentsManager.Snapshot?>(null)
private val drawerComponentMenu by lazy { ComponentsMenu(this, uiHandler) }
private var drawerGameProfileId by mutableStateOf<Long?>(null)
private val drawerGameInventory = GameProfileInventoryCache<Library.SteamGame>()
private var drawerGameInventoryRevision = 0L
@@ -735,7 +736,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
},
onClose = { drawerOpen = false },
components = drawerComponents,
onComponentsRefresh = { refreshDrawerComponents() },
onComponentsRefresh = {
refreshDrawerComponents()
drawerComponentMenu.refreshComponentCatalog(force = false)
},
componentCatalog = drawerComponentMenu.compCatalog,
componentCatalogAt = drawerComponentMenu.compCatalogAt,
componentCatalogChecking = drawerComponentMenu.compChecking,
componentDownloads = drawerComponentMenu.compDownloads,
componentDownloadRevision = drawerComponentMenu.compDownloadRevision,
onComponentCatalogRefresh = { drawerComponentMenu.refreshComponentCatalog() },
onComponentDownload = drawerComponentMenu::downloadComponent,
gameProfiles = drawerGameProfileRefresh.data?.profiles.orEmpty(),
selectedGameProfileId = drawerGameProfileId,
selectedGameProfile = drawerGameProfileRefresh.data
@@ -73,6 +73,7 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
@@ -265,6 +266,13 @@ class DrawerActions(
val components: ComponentsManager.Snapshot? = null,
/** Re-reads the Protons (and runs swaps that waited for a game to close). */
val onComponentsRefresh: () -> Unit = {},
val componentCatalog: List<ComponentsManager.CatalogItem> = emptyList(),
val componentCatalogAt: Long = 0,
val componentCatalogChecking: Boolean = false,
val componentDownloads: Map<String, Int> = emptyMap(),
val componentDownloadRevision: Long = 0,
val onComponentCatalogRefresh: () -> Unit = {},
val onComponentDownload: (ComponentsManager.CatalogItem) -> Unit = {},
/** Installed games available for profile inspection in the session drawer. */
val gameProfiles: List<Pair<Long, String>> = emptyList(),
/** The selected game profile, or null for the Default profile. */
@@ -314,7 +322,7 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
LaunchedEffect(a.selectedGameProfileId) {
if (host.open == "fex") host.open = null
}
LaunchedEffect(open, page) {
LaunchedEffect(open, page, a.componentDownloadRevision) {
if (open && page == SessionDrawerPage.GAMES) {
a.onGameProfilesPageOpened()
a.onComponentsRefresh()
@@ -535,6 +543,17 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
)
}
ComponentsDrawerPage(host, a) { key -> focus.track(page, key) }
ComponentDownloadsDrawerGroup(
host = host,
catalog = a.componentCatalog,
catalogAt = a.componentCatalogAt,
checking = a.componentCatalogChecking,
packages = a.components?.packages.orEmpty(),
downloads = a.componentDownloads,
onRefresh = a.onComponentCatalogRefresh,
onDownload = a.onComponentDownload,
track = { key -> focus.track(page, key) },
)
}
SessionDrawerPage.SESSION -> {
if (a.isHomeApp) SettingsGroup(stringResource(R.string.drawer_android_apps)) {
@@ -1117,8 +1136,8 @@ private fun ProfileSummaryRow(label: String, value: String) {
/**
* The Games tab's components: quick swaps between what is already installed, per Proton. A swap
* into the Proton a running game uses waits until that game closes; downloading, importing and
* deleting stay on the Components page in the app.
* into the Proton a running game uses waits until that game closes. Downloads below share the
* launcher's package store; importing and deleting stay on the Components page.
*/
@Composable
private fun ComponentsDrawerPage(host: MenuHost, a: DrawerActions, track: (String) -> Modifier) {
@@ -1251,6 +1270,84 @@ private fun ComponentsDrawerPage(host: MenuHost, a: DrawerActions, track: (Strin
)
}
/** Downloads only store packages; the profile/Proton choices above apply them separately. */
@Composable
internal fun ComponentDownloadsDrawerGroup(
host: MenuHost,
catalog: List<ComponentsManager.CatalogItem>,
catalogAt: Long,
checking: Boolean,
packages: List<ComponentsManager.Package>,
downloads: Map<String, Int>,
onRefresh: () -> Unit,
onDownload: (ComponentsManager.CatalogItem) -> Unit,
track: (String) -> Modifier = { Modifier },
) {
val colors = MaterialTheme.colorScheme
val installed = packages.map { it.file }.toSet()
SettingsGroup(stringResource(R.string.drawer_component_downloads)) {
Text(stringResource(R.string.drawer_component_downloads_hint), fontSize = 12.sp,
color = colors.onSurfaceVariant, modifier = Modifier.padding(horizontal = 14.dp, vertical = 8.dp))
SettingsRow(stringResource(R.string.comp_nightlies_checked,
if (catalogAt > 0) java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT)
.format(java.util.Date(catalogAt * 1000)) else stringResource(R.string.comp_never)), null, stacked = true) {
ValueChip(stringResource(if (checking) R.string.common_checking else R.string.stores_refresh),
false, enabled = !checking, modifier = track("cmp-download-refresh"), onClick = onRefresh)
}
for (comp in ComponentsManager.COMPONENTS) {
val key = "cmp-download-$comp"
val items = catalog.filter { it.comp == comp }
val open = host.open == key
Column(Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 10.dp)) {
Text(ComponentsManager.LABEL.getValue(comp), fontSize = 15.sp,
fontWeight = FontWeight.SemiBold, color = colors.onBackground)
Box(Modifier.fillMaxWidth().padding(top = 6.dp)) {
ValueChip(stringResource(R.string.drawer_component_choose_download), open,
enabled = items.isNotEmpty(), modifier = track(key).fillMaxWidth()) {
host.open = if (open) null else key
}
AnchoredMenu(open, onDismiss = { if (host.open == key) host.open = null },
title = ComponentsManager.LABEL.getValue(comp)) { firstItemFocus ->
val firstAvailable = items.firstOrNull { ComponentsManager.safeName(it.file) !in installed && it.file !in downloads }
items.forEach { item ->
val stored = ComponentsManager.safeName(item.file) in installed
val progress = downloads[item.file]
MenuItem(item.file.removeSuffix(".wcp"), checked = stored,
enabled = !stored && progress == null,
detail = when {
stored -> stringResource(R.string.wincomp_downloaded)
progress != null -> if (progress < 0) stringResource(R.string.wincomp_downloading, item.file.removeSuffix(".wcp")) else "$progress%"
else -> item.release + " · " + stringResource(R.string.common_size_mb, item.size / 1048576.0)
},
focusRequester = if (item == firstAvailable) firstItemFocus else null,
) {
onDownload(item)
host.open = null
}
}
}
}
}
}
if (catalog.isEmpty() && !checking) Text(
stringResource(if (catalogAt == 0L) R.string.comp_refresh_to_list else R.string.comp_nothing_new),
fontSize = 12.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(14.dp),
)
for ((file, progress) in downloads) {
Column(Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 8.dp)) {
Text(stringResource(R.string.wincomp_downloading, file.removeSuffix(".wcp")),
fontSize = 12.sp, color = colors.onSurfaceVariant)
if (progress < 0) LinearProgressIndicator(modifier = Modifier.fillMaxWidth().padding(top = 4.dp))
else {
Text("$progress%", fontSize = 12.sp, color = colors.onSurfaceVariant)
LinearProgressIndicator(progress = { progress / 100f },
modifier = Modifier.fillMaxWidth().padding(top = 4.dp))
}
}
}
}
}
/**
* A choice for the drawer's narrow column: label, then the box across the full width, then the
* detail underneath - so a long Proton or package name never squeezes the text beside it.