From a0bda4796c9613bf423acce9f0cd95258a9d6f72 Mon Sep 17 00:00:00 2001 From: Patryk Michalik Date: Wed, 18 May 2022 15:05:38 +0200 Subject: [PATCH] =?UTF-8?q?`IconPackGrid`:=20Ensure=20that=20n=20and=20a?= =?UTF-8?q?=20half=20(n=20=E2=88=88=20=E2=84=95)=20items=20are=20initially?= =?UTF-8?q?=20visible=20when=20`LazyRow`=20is=20scrollable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/preferences/IconPackPreferences.kt | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/lawnchair/src/app/lawnchair/ui/preferences/IconPackPreferences.kt b/lawnchair/src/app/lawnchair/ui/preferences/IconPackPreferences.kt index 103d48bb70..59383d06f7 100644 --- a/lawnchair/src/app/lawnchair/ui/preferences/IconPackPreferences.kt +++ b/lawnchair/src/app/lawnchair/ui/preferences/IconPackPreferences.kt @@ -43,11 +43,14 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.key 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.layout.onGloballyPositioned import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow @@ -155,14 +158,29 @@ fun IconPackGrid( modifier: Modifier, ) { val iconPacks by LocalPreferenceInteractor.current.iconPacks.collectAsState() + val density = LocalDensity.current + var iconPackItemWidth by remember { mutableStateOf(value = 0f) } + val padding = 16.dp NestedScrollStretch { val state = rememberLazyListState() LazyRow( state = state, - horizontalArrangement = Arrangement.spacedBy(space = 16.dp), - contentPadding = PaddingValues(horizontal = 16.dp), - modifier = modifier, + horizontalArrangement = Arrangement.spacedBy(space = padding), + contentPadding = PaddingValues(horizontal = padding), + modifier = modifier + .fillMaxWidth() + .onGloballyPositioned { + with(density) { + if (iconPackItemWidth == 0f) { + iconPackItemWidth = getIconPackItemWidth( + availableWidth = it.size.width.toDp().value - padding.value, + minimumWidth = 80f, + gutterWidth = padding.value, + ) + } + } + }, ) { itemsIndexed(iconPacks, { _, item -> item.packageName }) { index, item -> val wasSelected = remember { mutableStateOf(false) } @@ -178,7 +196,7 @@ fun IconPackGrid( IconPackItem( item = item, selected = selected, - modifier = Modifier.width(80.dp), + modifier = Modifier.width(iconPackItemWidth.dp), ) { adapter.onChange(item.packageName) } @@ -187,6 +205,25 @@ fun IconPackGrid( } } +private fun getIconPackItemWidth( + availableWidth: Float, + minimumWidth: Float, + gutterWidth: Float, +): Float { + var gutterCount = 2f + var visibleItemCount = gutterCount + 0.5f + var iconPackItemWidth = minimumWidth + while (true) { + gutterCount += 1f + visibleItemCount += 1f + val possibleIconPackItemWidth = (availableWidth - gutterCount * gutterWidth) / visibleItemCount + if (possibleIconPackItemWidth >= minimumWidth) { + iconPackItemWidth = possibleIconPackItemWidth + } else break + } + return iconPackItemWidth +} + @OptIn(ExperimentalMaterial3Api::class) @Composable fun IconPackItem(