Skip to content

Commit 7b09e16

Browse files
committed
improvement(UI,LargeType): Provide min cell width, so all the cells 0..100 have equal width #1221
1 parent eff469a commit 7b09e16

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

common/src/commonMain/kotlin/com/artemchep/keyguard/feature/largetype/LargeTypeScreen.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.ExperimentalLayoutApi
99
import androidx.compose.foundation.layout.FlowRow
1010
import androidx.compose.foundation.layout.fillMaxWidth
1111
import androidx.compose.foundation.layout.padding
12+
import androidx.compose.foundation.layout.widthIn
1213
import androidx.compose.foundation.shape.RoundedCornerShape
1314
import androidx.compose.material.icons.Icons
1415
import androidx.compose.material3.LocalAbsoluteTonalElevation
@@ -27,8 +28,13 @@ import androidx.compose.ui.Alignment
2728
import androidx.compose.ui.Modifier
2829
import androidx.compose.ui.draw.clip
2930
import androidx.compose.ui.draw.drawBehind
31+
import androidx.compose.ui.platform.LocalDensity
3032
import androidx.compose.ui.text.SpanStyle
33+
import androidx.compose.ui.text.TextMeasurer
34+
import androidx.compose.ui.text.TextStyle
3135
import androidx.compose.ui.text.buildAnnotatedString
36+
import androidx.compose.ui.text.rememberTextMeasurer
37+
import androidx.compose.ui.text.style.TextAlign
3238
import androidx.compose.ui.text.withStyle
3339
import androidx.compose.ui.unit.Dp
3440
import androidx.compose.ui.unit.coerceAtLeast
@@ -122,6 +128,7 @@ fun LargeTypeContent(
122128
groups.forEach { item ->
123129
SymbolItem(
124130
item = item,
131+
length = groups.size,
125132
elevation = elevation,
126133
selectedIndex = indexState,
127134
)
@@ -149,6 +156,7 @@ fun LargeTypeContent(
149156
private fun SymbolItem(
150157
modifier: Modifier = Modifier,
151158
item: LargeTypeState.Item,
159+
length: Int,
152160
selectedIndex: State<Int>,
153161
elevation: Dp,
154162
) {
@@ -218,11 +226,42 @@ private fun SymbolItem(
218226
fontFamily = monoFontFamily,
219227
style = MaterialTheme.typography.headlineMedium,
220228
)
229+
230+
val indexTextStyle = MaterialTheme.typography.labelSmall
231+
val indexTextWidth = kotlin.run {
232+
val text = remember(length) {
233+
val digits = length.toString().length
234+
"".padStart(digits, '0')
235+
}
236+
rememberTextMeasuredWidthDp(
237+
text = text,
238+
style = indexTextStyle,
239+
) + 1.dp // just in case the measurement is slightly incorrect
240+
}
221241
Text(
242+
modifier = Modifier
243+
.widthIn(min = indexTextWidth),
222244
text = index.plus(1).toString(),
223245
color = LocalContentColor.current
224246
.combineAlpha(MediumEmphasisAlpha),
225-
style = MaterialTheme.typography.labelSmall,
247+
style = indexTextStyle,
248+
textAlign = TextAlign.Center,
226249
)
227250
}
228251
}
252+
253+
@Composable
254+
private fun rememberTextMeasuredWidthDp(
255+
text: String,
256+
style: TextStyle,
257+
measurer: TextMeasurer = rememberTextMeasurer(),
258+
): Dp {
259+
val density = LocalDensity.current
260+
return remember(measurer, density, text) {
261+
val width = measurer.measure(
262+
text = text,
263+
style = style,
264+
).size.width / density.density
265+
width.dp
266+
}
267+
}

0 commit comments

Comments
 (0)