From e517964a9640ba27fc48f6511e4f13a24ccad36c Mon Sep 17 00:00:00 2001 From: S Plotnikov Date: Sun, 12 Jul 2026 20:05:58 +0200 Subject: [PATCH] Let the filter/sort row wrap instead of starving the sort label MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two filter chips are sized to their content and are measured first, so the sort label — the only flexible item in the Row — got only the leftover width and ellipsized into it. At 360dp it loses 4 characters at the default font size; at a 1.6x font scale it loses 14. A 412dp phone has just enough room at default settings, which is why this hid until the font scale went up or the app was switched to Dutch. Same root cause as #31, one row up: fixed-size, content-sized controls sharing a Row with the one thing that has to stay readable. Make it a FlowRow so every item keeps its width and the row wraps instead. The sort menu drops to its own line when the chips fill the first one. ProductFilterSortRow is extracted so it can be rendered on its own at a forced font scale — the flow tests run at the device's scale and cannot reach the case that breaks. hasVisualOverflow turned out to be unusable as the oracle (it reports true even when the text got the width it asked for: 301.5px wanted, 302px granted), so the test asserts on visible characters. --- .../inventory/ui/ProductFilterSortRowTest.kt | 94 +++++++++++++++++++ .../inventory/ui/products/ProductsPane.kt | 67 +++++++++---- 2 files changed, 143 insertions(+), 18 deletions(-) create mode 100644 app/src/androidTest/java/dev/scuttle/inventory/ui/ProductFilterSortRowTest.kt diff --git a/app/src/androidTest/java/dev/scuttle/inventory/ui/ProductFilterSortRowTest.kt b/app/src/androidTest/java/dev/scuttle/inventory/ui/ProductFilterSortRowTest.kt new file mode 100644 index 0000000..df7b1de --- /dev/null +++ b/app/src/androidTest/java/dev/scuttle/inventory/ui/ProductFilterSortRowTest.kt @@ -0,0 +1,94 @@ +package dev.scuttle.inventory.ui + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.requiredWidth +import androidx.compose.material3.MaterialTheme +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.semantics.SemanticsActions +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.text.TextLayoutResult +import androidx.compose.ui.unit.Density +import androidx.compose.ui.unit.dp +import dev.scuttle.inventory.ui.common.SortOrder +import dev.scuttle.inventory.ui.products.ProductFilterSortRow +import org.junit.Rule +import org.junit.Test + +/** + * The filter/sort row had the same defect as the product card (#31): the two chips are sized to + * their content and take their width first, so the sort label — the only flexible thing in the + * row — was left with the remainder and ellipsized down to "…". It only shows at font scales + * above ~1.3, which is why the flow tests, which run at the device's scale, never caught it. + */ +class ProductFilterSortRowTest { + @get:Rule + val composeRule = createComposeRule() + + private fun renderAtFontScale(fontScale: Float) { + composeRule.setContent { + val base = LocalDensity.current + CompositionLocalProvider( + LocalDensity provides Density(density = base.density, fontScale = fontScale), + ) { + MaterialTheme { + // A narrow-but-ordinary phone width, so the assertion isn't a property of + // whichever device happens to run the suite. + Box(Modifier.requiredWidth(360.dp)) { + ProductFilterSortRow( + mandatoryOnly = false, + outOfStockOnly = false, + sort = SortOrder.NAME_ASC, + onToggleMandatory = {}, + onToggleOutOfStock = {}, + onSortSelect = {}, + ) + } + } + } + } + } + + private fun sortLabelLayout(): TextLayoutResult { + val layouts = mutableListOf() + composeRule + .onNodeWithTag("sort-menu") + .fetchSemanticsNode() + .config[SemanticsActions.GetTextLayoutResult] + .action + ?.invoke(layouts) + return layouts.first() + } + + /** + * Oracle: every character of the label is actually painted. `hasVisualOverflow` is not + * usable here — it reports true even when the text got the width it asked for (301.5px + * wanted, 302px granted), so it would fail a perfectly readable label. + */ + private fun truncatedChars(): Int { + val layout = sortLabelLayout() + val visible = layout.getLineEnd(layout.lineCount - 1, visibleEnd = true) + return layout.layoutInput.text.length - visible + } + + @Test + fun sort_label_is_readable_at_default_font_scale() { + renderAtFontScale(1.0f) + + assert(truncatedChars() == 0) { + "Sort label lost ${truncatedChars()} characters to ellipsis at font scale 1.0" + } + } + + @Test + fun sort_label_is_readable_at_large_font_scale() { + renderAtFontScale(1.6f) + + assert(truncatedChars() == 0) { + "Sort label lost ${truncatedChars()} characters to ellipsis at font scale 1.6 — " + + "the filter chips are starving it of width" + } + } +} diff --git a/app/src/main/java/dev/scuttle/inventory/ui/products/ProductsPane.kt b/app/src/main/java/dev/scuttle/inventory/ui/products/ProductsPane.kt index 3d26e18..31d077f 100644 --- a/app/src/main/java/dev/scuttle/inventory/ui/products/ProductsPane.kt +++ b/app/src/main/java/dev/scuttle/inventory/ui/products/ProductsPane.kt @@ -58,8 +58,49 @@ import dev.scuttle.inventory.R import dev.scuttle.inventory.data.dto.ProductDto import dev.scuttle.inventory.ui.common.SnackbarErrorEffect import dev.scuttle.inventory.ui.common.SortMenu +import dev.scuttle.inventory.ui.common.SortOrder import dev.scuttle.inventory.ui.theme.FrostCard +/** + * The mandatory / out-of-stock filter chips and the sort menu. + * + * Extracted so it can be rendered on its own at a forced font scale — the sort label is the part + * that breaks, and only at font scales the flow tests can't reach through a real Activity. + */ +@OptIn(ExperimentalLayoutApi::class) +@Composable +internal fun ProductFilterSortRow( + mandatoryOnly: Boolean, + outOfStockOnly: Boolean, + sort: SortOrder, + onToggleMandatory: () -> Unit, + onToggleOutOfStock: () -> Unit, + onSortSelect: (SortOrder) -> Unit, + modifier: Modifier = Modifier, +) { + // FlowRow, not Row: the chips are sized to their content and are measured first, so in a Row + // the sort label got only the leftover width — already too little at 360dp, and less again as + // the font scales up. Here every item keeps its width and the row wraps instead. + FlowRow( + modifier = modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(8.dp), + verticalArrangement = Arrangement.spacedBy(8.dp), + itemVerticalAlignment = Alignment.CenterVertically, + ) { + FilterChip( + selected = mandatoryOnly, + onClick = onToggleMandatory, + label = { Text(stringResource(R.string.products_pane_filter_mandatory)) }, + ) + FilterChip( + selected = outOfStockOnly, + onClick = onToggleOutOfStock, + label = { Text(stringResource(R.string.products_pane_filter_out_of_stock)) }, + ) + SortMenu(current = sort, onSelect = onSortSelect) + } +} + @OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class) @Composable fun ProductsPane( @@ -136,24 +177,14 @@ fun ProductsPane( singleLine = true, modifier = Modifier.fillMaxWidth().testTag("product-filter"), ) - Row( - modifier = Modifier.fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(8.dp), - ) { - FilterChip( - selected = state.mandatoryOnly, - onClick = viewModel::toggleMandatoryOnly, - label = { Text(stringResource(R.string.products_pane_filter_mandatory)) }, - ) - FilterChip( - selected = state.outOfStockOnly, - onClick = viewModel::toggleOutOfStockOnly, - label = { Text(stringResource(R.string.products_pane_filter_out_of_stock)) }, - ) - Spacer(Modifier.weight(1f)) - SortMenu(current = state.sort, onSelect = viewModel::setSort) - } + ProductFilterSortRow( + mandatoryOnly = state.mandatoryOnly, + outOfStockOnly = state.outOfStockOnly, + sort = state.sort, + onToggleMandatory = viewModel::toggleMandatoryOnly, + onToggleOutOfStock = viewModel::toggleOutOfStockOnly, + onSortSelect = viewModel::setSort, + ) } if (state.products.isEmpty() && !state.loading) {