@@ -999,17 +999,22 @@ private fun PulsePage() {
999999 CapText { BoxWithConstraints (Modifier .fillMaxSize()) {
10001000 val hero = ((maxHeight.value - 30f - (if (round) 26f else 12f )) * 0.3f ).coerceIn(40f , 68f ) // below the 30 dp top padding, above the page dots, which a round screen's curve makes 26 dp
10011001 val range = Metrics .range(samples)
1002- val avail = maxWidth.value * (if (round) 1 - TopLine .ROUND_INSET else 1f ) - 32f // end padding 16, gap 8, two 4 dp spacers (non-round start 10 dp ignored)
1002+ val avail = maxWidth.value - (if (round) maxWidth.value * TopLine .ROUND_INSET else 10f ) - 16f // the column's inner width
10031003 Column (Modifier .fillMaxSize().padding(start = if (round) (maxWidth.value * TopLine .ROUND_INSET ).dp else 10 .dp, end = 16 .dp, top = 30 .dp),
10041004 verticalArrangement = Arrangement .spacedBy(8 .dp)) {
10051005 MetricRow (Metrics .average(samples)?.toString() ? : " ‒‒‒" , stringResource(R .string.avg).uppercase(), G .HEART , hero, Og .Red )
1006- if (range != null ) Row (horizontalArrangement = Arrangement .spacedBy( 8 .dp)) {
1007- // Two items and the gap share the width: the size follows the longer label, never under 12 sp.
1006+ if (range != null ) {
1007+ val hi = " ${range.last} " ; val lo = " ${range.first} "
10081008 val maxL = stringResource(R .string.max).uppercase(); val minL = stringResource(R .string.min).uppercase()
1009- val chars = maxOf(" ${range.last} " .length + maxL.length * 0.5f , " ${range.first} " .length + minL.length * 0.5f )
1010- val rs = minOf(hero * 0.6f , avail / 2 / (chars * 0.62f )).coerceAtLeast(12f )
1011- MetricRow (" ${range.last} " , maxL, null , rs, Og .Text )
1012- MetricRow (" ${range.first} " , minL, null , rs, Og .Text )
1009+ val meter = rememberRowMeter()
1010+ fun pair (sz : Float ) = meter.width(hi, maxL, false , sz) + 8f + meter.width(lo, minL, false , sz)
1011+ // Side by side as on the Apple Watch, down to 3/4 size; past that (a 40 mm watch, "TỐI THIỂU") max over min.
1012+ val full = hero * 0.6f
1013+ val k = minOf(1f , avail / pair(full))
1014+ val sz = full * k * 0.97f
1015+ if (k >= 0.75f && pair(sz) <= avail) Row (horizontalArrangement = Arrangement .spacedBy(8 .dp)) {
1016+ MetricRow (hi, maxL, null , sz, Og .Text ); MetricRow (lo, minL, null , sz, Og .Text )
1017+ } else { MetricRow (hi, maxL, null , full, Og .Text ); MetricRow (lo, minL, null , full, Og .Text ) }
10131018 }
10141019 Sparkline (Metrics .downsample(samples, 60 ), Og .Red .copy(alpha = if (dim) 0.75f else 1f ), dot = ! dim,
10151020 Modifier .fillMaxWidth().weight(1f ).padding(bottom = 14 .dp))
@@ -1115,15 +1120,36 @@ fun MetricsPage() {
11151120 } }
11161121}
11171122
1123+ /* * Measures metric rows with the screen's own text style. */
1124+ private class RowMeter (val m : androidx.compose.ui.text.TextMeasurer , val d : androidx.compose.ui.unit.Density , val base : androidx.compose.ui.text.TextStyle )
1125+
1126+ @Composable
1127+ private fun rememberRowMeter () = RowMeter (androidx.compose.ui.text.rememberTextMeasurer(), LocalDensity .current, androidx.wear.compose.material3.LocalTextStyle .current)
1128+
1129+ /* * A metric row's width in dp at [size]: the number, the 4 dp gap, the glyph, the label (never under 12 sp). */
1130+ private fun RowMeter.width (value : String , unit : String? , glyph : Boolean , size : Float ): Float {
1131+ fun w (t : String , st : androidx.compose.ui.text.TextStyle ) = with (d) { m.measure(t, st, maxLines = 1 , softWrap = false ).size.width.toDp().value }
1132+ return w(value, base.merge(Og .Number ).copy(fontSize = (size / d.fontScale).sp)) + 4f + (if (glyph) size * 0.45f else 0f ) +
1133+ (if (unit != null ) w(unit, base.copy(fontSize = unitSp(size, d.fontScale).sp, fontWeight = FontWeight .SemiBold )) else 0f )
1134+ }
1135+
1136+ private fun unitSp (size : Float , fontScale : Float ) = (size * 0.42f ).coerceAtLeast(12f ) / fontScale
1137+
11181138@Composable
11191139private fun MetricRow (value : String , unit : String? , glyph : G ? , size : Float , color : Color ) {
11201140 val dim = LocalAmbient .current
1121- Row (verticalAlignment = Alignment .Bottom , modifier = Modifier .semantics(mergeDescendants = true ) {}) {
1122- Text (value, style = Og .Number , fontSize = (size / LocalDensity .current.fontScale).sp, color = color.copy(alpha = if (dim) 0.75f else 1f ), maxLines = 1 )
1123- Spacer (Modifier .width(4 .dp))
1124- if (glyph != null ) Glyph (glyph, color, (size * 0.45f ).dp, Modifier .padding(bottom = (size * 0.18f ).dp))
1125- if (unit != null ) Text (unit, color = color, fontSize = ((size * 0.42f ).coerceAtLeast(12f ) / LocalDensity .current.fontScale).sp, fontWeight = FontWeight .SemiBold ,
1126- modifier = Modifier .padding(bottom = (size * 0.12f ).dp))
1141+ val fs = LocalDensity .current.fontScale
1142+ BoxWithConstraints {
1143+ // Measured: a row wider than its column (a long time, a long label) shrinks instead of wrapping.
1144+ val need = rememberRowMeter().width(value, unit, glyph != null , size)
1145+ val s = if (need > maxWidth.value) size * maxWidth.value / need * 0.95f else size
1146+ Row (verticalAlignment = Alignment .Bottom , modifier = Modifier .semantics(mergeDescendants = true ) {}) {
1147+ Text (value, style = Og .Number , fontSize = (s / fs).sp, color = color.copy(alpha = if (dim) 0.75f else 1f ), maxLines = 1 , softWrap = false )
1148+ Spacer (Modifier .width(4 .dp))
1149+ if (glyph != null ) Glyph (glyph, color, (s * 0.45f ).dp, Modifier .padding(bottom = (s * 0.18f ).dp))
1150+ if (unit != null ) Text (unit, color = color, fontSize = unitSp(s, fs).sp, fontWeight = FontWeight .SemiBold , maxLines = 1 , softWrap = false ,
1151+ modifier = Modifier .padding(bottom = (s * 0.12f ).dp))
1152+ }
11271153 }
11281154}
11291155
0 commit comments