From 18d735b4c4fdff56f53f61eb6d26129296bbfee6 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:49:09 -0500 Subject: [PATCH] feat(ui): tabs that do not fit a narrow page open as a dropdown (#3640) * feat(ui): tabs that do not fit a narrow page open as a dropdown In portrait or on a narrow page, a row of section pills that would scroll sideways becomes one chip naming the shown section, which opens the list of sections, as the second screen's picker does. Covers the sheets' pills (Steam, desktop, glasses, Settings and the others on SheetChips), TabStrip, the game page's tabs and the Stores chips. * fix(stores): the compact store picker takes controller focus, survives a hidden store and keeps the signed-out status * fix(ui): picking the shown tab from the tab menu still activates it --- .../com/droiddeck/launcher/ui/GameDetail.kt | 4 + .../com/droiddeck/launcher/ui/LiftSheet.kt | 4 + .../droiddeck/launcher/ui/SettingsWidgets.kt | 5 +- .../com/droiddeck/launcher/ui/StoresPage.kt | 17 ++- .../java/com/droiddeck/launcher/ui/TabMenu.kt | 102 ++++++++++++++++++ 5 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/TabMenu.kt 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 36075081e6..e4dcb6c4d2 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt @@ -415,6 +415,10 @@ private fun GameTabBar( if (bumpers) TabBarBumper("LB", stringResource(R.string.widgets_prev_tab), leading = true) { onSelect((selected + tabs.size - 1) % tabs.size) } androidx.compose.foundation.layout.BoxWithConstraints(Modifier.weight(1f).clip(if (bumpers) RoundedCornerShape(BumperFlat) else CircleShape).onSizeChanged { viewport = it.width }) { val share = maxWidth / tabs.size.coerceAtLeast(1) + if (tabsAsMenu(maxWidth, tabs.size)) { + TabMenu(tabs, selected, onSelect, focus = focusRequesters) + return@BoxWithConstraints + } Box(Modifier.horizontalScroll(scroll)) { spans.getOrNull(selected)?.takeIf { tabHeight > 0 }?.let { span -> TabHighlight(span, tabHeight, flatStart = bumpers && selected == 0, flatEnd = bumpers && selected == tabs.lastIndex) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/LiftSheet.kt b/app/src/main/java/com/droiddeck/launcher/ui/LiftSheet.kt index 66f6ea110a..6769798ad9 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/LiftSheet.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/LiftSheet.kt @@ -711,6 +711,10 @@ internal fun SheetChips( Modifier.fillMaxWidth().onFocusChanged { within = it.hasFocus }, contentAlignment = Alignment.CenterStart, ) { + if (tabsAsMenu(maxWidth, labels.size)) { + TabMenu(labels, selected, onPick, focus = chips) + return@BoxWithConstraints + } val equal = !width.isSpecified && maxWidth / labels.size.coerceAtLeast(1) >= 96.dp val least = if (width.isSpecified) maxOf(width, 64.dp) else 64.dp val scroll = rememberScrollState() 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 2c51442cd1..5d09490e39 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt @@ -212,7 +212,8 @@ internal fun TabStrip( Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp), modifier = modifier) { if (bumpers) BumperKey("LB", stringResource(R.string.widgets_prev_tab)) { onSelect((selected + tabs.size - 1) % tabs.size) } // Centred, the bumpers sit at the edges and the tabs in the middle, as in the bottom bar. - Box(if (centered) Modifier.weight(1f) else Modifier.weight(1f, fill = false), contentAlignment = Alignment.Center) { Row( + Box(if (centered) Modifier.weight(1f) else Modifier.weight(1f, fill = false), contentAlignment = Alignment.Center) { androidx.compose.foundation.layout.BoxWithConstraints { + if (tabsAsMenu(maxWidth, tabs.size)) TabMenu(tabs, selected, onSelect, focus = focusRequesters) else Row( modifier = Modifier.clip(shape).background(colors.surfaceVariant).border(1.dp, pal.line2, shape) .horizontalScroll(scroll).selectableGroup().padding(3.dp), ) { @@ -239,7 +240,7 @@ internal fun TabStrip( .padding(horizontal = tabPadding, vertical = if (compact) 6.dp else 12.dp), ) } - } } + } } } if (bumpers) BumperKey("RB", stringResource(R.string.widgets_next_tab)) { onSelect((selected + 1) % tabs.size) } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt index 963c2f9c99..bb6bbbc271 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt @@ -470,7 +470,21 @@ private fun StoreChips( val tabScroll = rememberScrollState() Box(Modifier.fillMaxWidth()) { Row(horizontalArrangement = Arrangement.spacedBy(gap), verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) { - Box(Modifier.weight(1f).edgeFade(tabScroll).horizontalScroll(tabScroll)) { + val shown = tabs.filter { it.shown }.map { it.store } + androidx.compose.foundation.layout.BoxWithConstraints(Modifier.weight(1f)) { + if (tabsAsMenu(maxWidth, shown.size)) TabMenu( + shown.map { it.shortLabel }, shown.indexOfFirst { it.id == selected }, { onStore(shown[it]) }, + chipModifier = shown.fold(Modifier as Modifier) { m, st -> key(st) { m.paneItem("storechip:${st.id}") } }, + descriptions = shown.map { st -> + val signedIn = StoresState.isSignedIn(st) && StoresState.expired[st] != true + st.label + if (signedIn || !StoresState.accountsLoaded) "" else " " + stringResource(R.string.stores_chip_signed_out) + }, + leading = { i, inList -> + val signedIn = StoresState.isSignedIn(shown[i]) && StoresState.expired[shown[i]] != true + if (inList) Box(Modifier.size(12.dp).background(sourceColours(shown[i].id).dot.copy(alpha = if (signedIn) 1f else 0.35f), CircleShape)) + else StoreDot(shown[i], signedIn) + }, + ) else Box(Modifier.edgeFade(tabScroll).horizontalScroll(tabScroll)) { Row(horizontalArrangement = Arrangement.spacedBy(gap), verticalAlignment = Alignment.CenterVertically, modifier = Modifier.focusGroup()) { for (tab in tabs) { val store = tab.store @@ -492,6 +506,7 @@ private fun StoreChips( } ChipGlide(keys.indexOf(highlighted), bounds[highlighted], tint, visible = fillShown) } + } var cogAt by remember { mutableStateOf(null) } Box(Modifier.onGloballyPositioned { cogAt = it.boundsInRoot() }) { IconChip(Icons.Filled.Settings, stringResource(R.string.stores_settings)) { onSettings(cogAt) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/TabMenu.kt b/app/src/main/java/com/droiddeck/launcher/ui/TabMenu.kt new file mode 100644 index 0000000000..cf2d235999 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/TabMenu.kt @@ -0,0 +1,102 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.LocalIndication +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.hoverable +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.interaction.collectIsFocusedAsState +import androidx.compose.foundation.interaction.collectIsHoveredAsState +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.ExpandLess +import androidx.compose.material.icons.outlined.ExpandMore +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.font.FontWeight +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 + +private val TAB_MENU_BELOW = 600.dp +private val TAB_MIN_WIDTH = 96.dp + +/** Whether [count] tabs in [width] become a [TabMenu]: on a narrow page, when they would not fit side by side. */ +internal fun tabsAsMenu(width: Dp, count: Int): Boolean = + count > 2 && width < TAB_MENU_BELOW && width / count < TAB_MIN_WIDTH + +/** + * Tabs as one chip naming the shown tab, which opens the list of them, as the second screen's + * picker does. Each of [focus] lands on the chip, so callers that focus a tab still reach it. + */ +@Composable +internal fun TabMenu( + labels: List, selected: Int, onPick: (Int) -> Unit, + modifier: Modifier = Modifier, focus: List? = null, + chipModifier: Modifier = Modifier, descriptions: List? = null, + leading: (@Composable (index: Int, inList: Boolean) -> Unit)? = null, +) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + var open by remember { mutableStateOf(false) } + val source = remember { MutableInteractionSource() } + val hot = source.collectIsFocusedAsState().value || source.collectIsHoveredAsState().value + val shape = RoundedCornerShape(22.dp) + val label = labels.getOrNull(selected).orEmpty() + val description = descriptions?.getOrNull(selected) ?: label + val toggle = { open = !open } + Box(modifier) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(8.dp), + modifier = focus.orEmpty().fold(chipModifier.height(44.dp)) { m, r -> m.focusRequester(r) } + .testTag("tab-menu") + .semantics { contentDescription = description } + .clip(shape).background(if (hot || open) pal.signal.copy(alpha = 0.14f) else colors.surfaceVariant) + .glideBorder(hot || open, shape, pal.signal, pal.line2) + .hoverable(source) + .clickable(role = Role.Button, interactionSource = source, indication = LocalIndication.current, onClick = toggle) + .controllerConfirm(onClick = toggle) + .padding(start = 14.dp, end = 10.dp), + ) { + if (selected in labels.indices) leading?.invoke(selected, false) + Text(label, fontSize = 14.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, maxLines = 1, + overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f, fill = false)) + Icon(if (open) Icons.Outlined.ExpandLess else Icons.Outlined.ExpandMore, contentDescription = null, + tint = colors.onSurfaceVariant, modifier = Modifier.size(20.dp)) + } + AnchoredMenu(open, { open = false }, compact = true, minWidth = 200.dp) { first -> + labels.forEachIndexed { i, item -> + MenuItem( + item, checked = i == selected, + leading = leading?.let { { it(i, true) } }, + focusRequester = first.takeIf { i == selected || (i == 0 && selected !in labels.indices) }, + modifier = Modifier.testTag("tab-menu:$i") + .then(descriptions?.getOrNull(i)?.let { d -> Modifier.semantics { contentDescription = d } } ?: Modifier), + ) { open = false; onPick(i) } + } + } + } +}