diff --git a/app/src/main/java/com/droiddeck/launcher/ComponentsMenu.kt b/app/src/main/java/com/droiddeck/launcher/ComponentsMenu.kt index 13bb3702cb..1fd549f808 100644 --- a/app/src/main/java/com/droiddeck/launcher/ComponentsMenu.kt +++ b/app/src/main/java/com/droiddeck/launcher/ComponentsMenu.kt @@ -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(null) var compCatalog by mutableStateOf>(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(null) - var compDownloads by mutableStateOf>(emptyMap()) + var compDownloads: Map + 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>(emptyMap()) + var downloadRevision by mutableStateOf(0L) } } diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index 6d3cf99b7a..360380956c 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -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() } } diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 8d702631d2..762d732a40 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -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(null) + private val drawerComponentMenu by lazy { ComponentsMenu(this, uiHandler) } private var drawerGameProfileId by mutableStateOf(null) private val drawerGameInventory = GameProfileInventoryCache() 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 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 205c51d1d4..00a166314a 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -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 = emptyList(), + val componentCatalogAt: Long = 0, + val componentCatalogChecking: Boolean = false, + val componentDownloads: Map = 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> = 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, + catalogAt: Long, + checking: Boolean, + packages: List, + downloads: Map, + 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. diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 349bc9909d..7fd5500643 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -568,7 +568,7 @@ juego en ejecución reaplicado al iniciar %1$s Original - Descarga, importa y elimina en la página Componentes. + Importa y elimina en la página Componentes. Hecho Requiere atención Trabajando diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index feb46b7d54..d7bb3e5833 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -451,7 +451,7 @@ Jeu en cours d\'exécution Réappliqué au lancement de %1$s Original - Veuillez télécharger, importer et supprimer depuis la page « Composants ». + Importez et supprimez depuis la page « Composants ». Terminé À traiter Traitement en cours diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index e787b47336..44da13653f 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -564,7 +564,7 @@ ゲーム実行中 %1$sの起動時に再適用しました 元 - 「コンポーネント」ページでダウンロード、インポート、削除してください。 + 「コンポーネント」ページでインポート、削除してください。 完了 要対応 処理中 diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 6305e623d4..944c8a3276 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -564,7 +564,7 @@ 게임 실행 중 %1$s 시작 시 다시 적용됨 원본 - \"구성 요소\" 페이지에서 다운로드, 가져오기 및 삭제하세요. + 구성 요소 페이지에서 가져오기 및 삭제하세요. 완료 처리 필요 처리 중 diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index f1f6765583..d98e99070a 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -526,7 +526,7 @@ игра запущена повторно применено при запуске %1$s Оригинал - Скачивайте, импортируйте и удаляйте на странице «Компоненты». + Импортируйте и удаляйте на странице «Компоненты». Готово Требует внимания Обработка diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 80b2233d9d..3f5765ea41 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -564,7 +564,7 @@ 游戏运行中 于启动 %1$s 时重新应用 原件 - 在“组件”页面下载、导入和删除。 + 在“组件”页面导入和删除。 完成 需要处理 处理中 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 9371b68312..0a2096dab1 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -564,7 +564,7 @@ 遊戲執行中 於 %1$s 啟動時重新套用 原始 - 請在「元件」頁面下載、匯入及刪除。 + 請在「元件」頁面匯入及刪除。 完成 需要處理 處理中 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 846a91e01d..0f38af5c35 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -564,7 +564,7 @@ 遊戲執行中 已在啟動 %1$s 時重新套用 原始 - 請在「元件」頁面下載、匯入和刪除。 + 請在「元件」頁面匯入和刪除。 完成 需要處理 處理中 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index df6e20e478..c0358f3b5c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -631,7 +631,10 @@ game running re-applied at launch %1$s Original - Download, import and delete on the Components page. + Import and delete on the Components page. + Download components + Downloads are shared with the Components page. Choose a downloaded version above to use it on the next game launch. + Choose a version to download Done Needs attention Working diff --git a/app/src/test/java/com/droiddeck/launcher/ui/ComponentDownloadsDrawerTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/ComponentDownloadsDrawerTest.kt new file mode 100644 index 0000000000..8da1e7fa3b --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/ComponentDownloadsDrawerTest.kt @@ -0,0 +1,84 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.test.assertIsNotEnabled +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.ComponentsManager +import org.junit.Assert.assertEquals +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28]) +class ComponentDownloadsDrawerTest { + @get:Rule val compose = createRobolectricComposeRule() + + private val dxvk = item("dxvk-2.4.1-linux.wcp", "dxvk") + private val stored = item("dxvk-2.3-linux.wcp", "dxvk") + private val inFlight = item("dxvk-2.7.1-linux.wcp", "dxvk") + private val fex = item("fex-2609-linux.wcp", "fex") + + @Test fun choosingDxvkDownloadsTheCatalogItemAndPreventsStoredOrInFlightDownloads() { + val requested = mutableListOf() + show(downloads = mapOf(inFlight.file to 45), onDownload = requested::add) + compose.onNodeWithTag("cmp-download-dxvk").performClick() + compose.onNodeWithText(stored.file.removeSuffix(".wcp")).assertIsNotEnabled() + compose.onNodeWithText(inFlight.file.removeSuffix(".wcp")).assertIsNotEnabled() + compose.onNodeWithText(fex.file.removeSuffix(".wcp")).assertDoesNotExist() + compose.onNodeWithText(dxvk.file.removeSuffix(".wcp")).performClick() + assertEquals(listOf(dxvk), requested) + compose.onNodeWithText(dxvk.file.removeSuffix(".wcp")).assertDoesNotExist() + compose.onNodeWithText("45%").assertExists() + } + + @Test fun emptyCatalogOffersRefreshAndDisablesVersionChoicesWhileChecking() { + var refreshes = 0 + show(catalog = emptyList(), checking = false, onRefresh = { refreshes++ }) + compose.onNodeWithTag("cmp-download-refresh").performClick() + assertEquals(1, refreshes) + compose.onNodeWithTag("cmp-download-dxvk").assertIsNotEnabled() + compose.onNodeWithText(RuntimeEnvironment.getApplication().getString(R.string.comp_refresh_to_list)).assertExists() + } + + @Test fun catalogRefreshCannotBeStartedTwiceWhileChecking() { + show(checking = true) + compose.onNodeWithTag("cmp-download-refresh").assertIsNotEnabled() + } + + private fun show( + catalog: List = listOf(dxvk, stored, inFlight, fex), + downloads: Map = emptyMap(), + checking: Boolean = false, + onRefresh: () -> Unit = {}, + onDownload: (ComponentsManager.CatalogItem) -> Unit = {}, + ) { + compose.setContent { + DroidDeckTheme { + Column(Modifier.verticalScroll(rememberScrollState())) { + ComponentDownloadsDrawerGroup( + host = rememberMenuHost(), catalog = catalog, catalogAt = 0, + checking = checking, + packages = listOf(ComponentsManager.Package(stored.file, "dxvk", "2.3", "", 100)), + downloads = downloads, onRefresh = onRefresh, onDownload = onDownload, + track = { Modifier.testTag(it) }, + ) + } + } + } + } + + private fun item(file: String, comp: String) = ComponentsManager.CatalogItem( + file, comp, "Nightlies", "https://github.com/example/$file", 100, "sha256:test", + ) +}