Modern Apps git

← Modern-Apps.git

voxels: let buttons grow with large text; draw half hearts as halves (#ux-audit)

Commit
269b073bc547bdc68a531215c383d5aa2048a923
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:49 UTC (23 days ago)
Parents
b72c651
Symptom: at 200% system font size the Jump/Sneak/Use/Fly buttons and
the inventory tabs clipped their labels ("Sneaking", "Achievements",
...), because they were fixed-size boxes. Half a heart of health was
shown only as a lighter pink heart, which is hard to tell apart (and
invisible to colour-blind players and screen readers).

Root cause: HoldButton was `width(96.dp).height(52.dp)` and TabButton
`width(96.dp).height(40.dp)` (ui/Joystick.kt, ui/InventoryScreen.kt);
the hearts row used colour alone for a half heart (ui/Hotbar.kt).

Fix:
- HoldButton and TabButton use widthIn/heightIn minimums with inner
  padding and centred text, so labels grow the control instead of
  being clipped; the inventory's left tab column scrolls if the taller
  tabs no longer fit, with Close right after the tabs.
- Hearts are drawn as an empty heart with the filled heart clipped to
  its fill (1, 0.5 or 0): a half heart is literally half full. The
  row gets a content description "Health: 7.5 of 10 hearts".

User-visible effect: readable controls at any font size; half hearts
are recognisable by shape and announced by TalkBack.

Verification: new HealthOverlayTest (fill steps, read-out formatting,
semantics on the row).

Diff

games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HealthOverlay.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HealthOverlay.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HealthOverlay.ktindex fed4da6ef1..9a78d7fa5f 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HealthOverlay.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HealthOverlay.kt@@ -11,7 +11,12 @@ import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip+import androidx.compose.ui.draw.drawWithContent+import androidx.compose.ui.graphics.drawscope.clipRect+import androidx.compose.ui.semantics.clearAndSetSemantics+import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.graphics.Color+import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource import com.vayunmathur.games.voxels.R import com.vayunmathur.library.ui.MaterialTheme@@ -32,6 +37,31 @@ private val effectColors = mapOf(     "night" to Color(0xFF3B3BA0), "jump" to Color(0xFF34A02C), "slow" to Color(0xFF5A6472) ) +private val HeartFull = Color(0xFFE23A45)+private val HeartEmpty = Color(0xFF3A1516)++/** How much of heart `i` is filled given `remaining` = hearts of health minus `i`: 0, 0.5 or 1. */+internal fun heartFill(remaining: Float): Float = when { remaining >= 1f -> 1f; remaining >= 0.5f -> 0.5f; else -> 0f }++/** Hearts of health for read-out, e.g. "7.5" or "8". */+internal fun heartsLabel(hearts: Float): String {+    val halves = (hearts * 2).toInt().coerceAtLeast(0)+    return if (halves % 2 == 0) "${halves / 2}" else "${halves / 2}.5"+}++@Composable+private fun Heart(fill: Float) {+    Box {+        Text("♥", color = HeartEmpty, style = MaterialTheme.typography.bodyMedium)+        if (fill > 0f) {+            Text(+                "♥", color = HeartFull, style = MaterialTheme.typography.bodyMedium,+                modifier = Modifier.drawWithContent { clipRect(right = size.width * fill) { [email protected]() } },+            )+        }+    }+}+ private fun roman(n: Int) = when (n) { 0 -> ""; 1 -> " II"; 2 -> " III"; 3 -> " IV"; else -> " ${n + 1}" }  @Composable@@ -49,13 +79,13 @@ fun HealthOverlay(healthJson: String, modifier: Modifier = Modifier) {                 }             }         }-        // Hearts (each = 2 HP); absorption shown as gold hearts appended.+        // Hearts (each = 2 HP); absorption shown as gold hearts appended. A half heart is drawn+        // half-filled (shape, not just a lighter colour), and the row reads out as a number.         val totalHearts = (h.max / 2f).toInt().coerceIn(1, 30)-        Row {+        val healthText = pluralStringResource(R.plurals.health_hearts, totalHearts, heartsLabel(h.hp / 2f), totalHearts)+        Row(Modifier.clearAndSetSemantics { contentDescription = healthText }) {             for (i in 0 until totalHearts) {-                val filled = h.hp / 2f - i-                val c = when { filled >= 1f -> Color(0xFFE23A45); filled >= 0.5f -> Color(0xFFEE8891); else -> Color(0xFF3A1516) }-                Text("♥", color = c, style = MaterialTheme.typography.bodyMedium)+                Heart(fill = heartFill(h.hp / 2f - i))             }             val absHearts = (h.absorb / 2f).toInt().coerceIn(0, 10)             for (i in 0 until absHearts) { Text("♥", color = Color(0xFFF2C15A), style = MaterialTheme.typography.bodyMedium) }
games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HoldButton.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HoldButton.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HoldButton.ktindex 9e40322aca..9df93f9031 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HoldButton.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/HoldButton.kt@@ -4,8 +4,9 @@ import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.layout.Box-import androidx.compose.foundation.layout.height-import androidx.compose.foundation.layout.width+import androidx.compose.foundation.layout.heightIn+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue@@ -17,6 +18,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.vayunmathur.library.ui.Text@@ -37,8 +39,10 @@ fun HoldButton(     val dtap by rememberUpdatedState(onDoubleTap)     Box(         Modifier-            .width(96.dp)-            .height(52.dp)+            // Minimum, not fixed, size: at large font scales the label grows the button instead of+            // being clipped (it was a fixed 96 x 52 dp box).+            .widthIn(min = 96.dp)+            .heightIn(min = 52.dp)             .clip(RoundedCornerShape(12.dp))             .background(if (dimmed) HudFillDim else HudFill)             .border(1.5.dp, HudOutline.copy(alpha = if (dimmed) 0.4f else 0.7f), RoundedCornerShape(12.dp))@@ -50,9 +54,11 @@ fun HoldButton(             },         contentAlignment = Alignment.Center     ) {+        Box(Modifier.padding(horizontal = 10.dp, vertical = 6.dp), contentAlignment = Alignment.Center) {         Text(             label, color = Color.White.copy(alpha = if (dimmed) 0.6f else 1f), fontWeight = FontWeight.SemiBold, fontSize = 16.sp,-            style = TextStyle(shadow = HudTextShadow),+            style = TextStyle(shadow = HudTextShadow), textAlign = TextAlign.Center,         )+        }     } }
games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.ktindex 1cb0831e62..862e215b3f 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.kt@@ -20,6 +20,8 @@ import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.heightIn+import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding@@ -47,6 +49,7 @@ import androidx.compose.ui.layout.boundsInWindow import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp@@ -100,14 +103,15 @@ fun InventoryOverlay(         // Narrow landscape phones: a 3-wide catalog leaves the 9-wide grid room for usable slots.         val narrow = maxWidth < NARROW_PANEL         Row(Modifier.fillMaxSize(), horizontalArrangement = Arrangement.spacedBy(10.dp)) {-            // Left tabs (top→bottom: Crafting, Outfit, Inventory).-            Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {+            // Left tabs (top→bottom: Crafting, Outfit, Inventory). Scrolls if large text makes the+            // tabs taller than the panel.+            Column(Modifier.fillMaxHeight().verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(8.dp)) {                 TabButton(stringResource(R.string.crafting), leftTab == 2) { leftTab = 2 }                 TabButton(stringResource(R.string.blessings), leftTab == 3) { leftTab = 3 }                 TabButton(stringResource(R.string.achievements), leftTab == 4) { leftTab = 4 }                 TabButton(stringResource(R.string.outfit), leftTab == 1) { leftTab = 1 }                 TabButton(stringResource(R.string.inventory), leftTab == 0) { leftTab = 0 }-                Spacer(Modifier.weight(1f))+                Spacer(Modifier.height(8.dp))                 TabButton(stringResource(UiR.string.close), false) { onClose() }             } @@ -167,13 +171,15 @@ fun InventoryOverlay(  @Composable private fun TabButton(label: String, selected: Boolean, onClick: () -> Unit) {+    // Minimum size so large font scales grow the tab rather than clip its label.     Box(-        Modifier.width(96.dp).height(40.dp).clip(RoundedCornerShape(8.dp))+        Modifier.widthIn(min = 96.dp).heightIn(min = 40.dp).clip(RoundedCornerShape(8.dp))             .background(if (selected) Color(0xFF3A6B3A) else Color.White.copy(alpha = 0.10f))             .border(1.dp, Color.White.copy(alpha = if (selected) 0.5f else 0.15f), RoundedCornerShape(8.dp))-            .clickable { onClick() },+            .clickable { onClick() }+            .padding(horizontal = 8.dp, vertical = 4.dp),         contentAlignment = Alignment.Center-    ) { Text(label, color = Color.White.copy(0.95f), fontSize = 14.sp, fontWeight = FontWeight.SemiBold) }+    ) { Text(label, color = Color.White.copy(0.95f), fontSize = 14.sp, fontWeight = FontWeight.SemiBold, textAlign = TextAlign.Center) } }  @Composable
games/voxels/src/main/res/values/strings.xml
diff --git a/games/voxels/src/main/res/values/strings.xml b/games/voxels/src/main/res/values/strings.xmlindex 6cf4fa5a2f..f64536401b 100644--- a/games/voxels/src/main/res/values/strings.xml+++ b/games/voxels/src/main/res/values/strings.xml@@ -146,6 +146,11 @@     <string name="boss_dragon">Ender Dragon</string>     <string name="boss_wither">The Wither</string>     <string name="boss_generic">Boss</string>+    <!-- Accessibility read-out of the hearts row; %1$s = hearts left (e.g. 7.5), %2$d = max hearts. -->+    <plurals name="health_hearts">+        <item quantity="one">Health: %1$s of %2$d heart</item>+        <item quantity="other">Health: %1$s of %2$d hearts</item>+    </plurals>     <!-- Status-effect chips: %1$s = effect name (with level), %2$d = seconds left. -->     <string name="effect_chip">%1$s %2$ds</string>     <string name="effect_regen">Regen</string>
games/voxels/src/test/java/com/vayunmathur/games/voxels/ui/HealthOverlayTest.kt
diff --git a/games/voxels/src/test/java/com/vayunmathur/games/voxels/ui/HealthOverlayTest.kt b/games/voxels/src/test/java/com/vayunmathur/games/voxels/ui/HealthOverlayTest.ktnew file mode 100644index 0000000000..2420191909--- /dev/null+++ b/games/voxels/src/test/java/com/vayunmathur/games/voxels/ui/HealthOverlayTest.kt@@ -0,0 +1,34 @@+package com.vayunmathur.games.voxels.ui++import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals++@RunWith(RobolectricTestRunner::class)+class HealthOverlayTest {+    @get:Rule val rule = createComposeRule()++    @Test fun `hearts fill whole, half or not at all`() {+        assertEquals(1f, heartFill(3f))+        assertEquals(0.5f, heartFill(0.5f))+        assertEquals(0.5f, heartFill(0.9f))+        assertEquals(0f, heartFill(0.4f))+        assertEquals(0f, heartFill(-2f))+    }++    @Test fun `health reads out as hearts, halves included`() {+        assertEquals("7.5", heartsLabel(7.5f))+        assertEquals("8", heartsLabel(8f))+        assertEquals("0", heartsLabel(-1f))+    }++    // A half heart is not only a colour: the row has a spoken value for screen readers too.+    @Test fun `the hearts row is described for accessibility`() {+        rule.setContent { HealthOverlay(healthJson = """{"hp":15,"max":20}""") }+        rule.onNodeWithContentDescription("Health: 7.5 of 10 hearts").assertExists()+    }+}