Modern Apps git

← Modern-Apps.git

library: keep the achievement toast clear of the status bar (#ux-audit)

Commit
f954f741701cefc409930f6ebbf2fa1df6f338fa
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:22 UTC (23 days ago)
Parents
70b99ab
Symptom: every app draws edge-to-edge (MaActivity), and the unlock
toast sat at a fixed 16 dp + 24 dp from the top of the window. On
phones with a tall status bar or a camera cutout the card ran under
the status icons or the punch-hole; in landscape it could sit under a
side cutout.

Root cause: AchievementNotification used a hard-coded
padding(top = 24.dp) as a stand-in for the status bar and ignored
window insets entirely.

Fix: the fixed 24 dp is gone. The toast now applies
windowInsetsPadding(windowInsets) and then its 16 dp margin, where
windowInsets is a new trailing parameter defaulting to
WindowInsets.safeDrawing.only(Top + Horizontal) - status bar, display
cutout and caption bar on top, cutouts on the sides. It uses the
WindowInsets APIs, not statusBarsPadding/systemBarsPadding, as
library/ui requires, and windowInsetsPadding skips insets an ancestor
already consumed, so a caller that hosts the toast inside an inset
container does not get double padding. Existing callers (eleven games'
Navigation/MainActivity) compile unchanged.

User-visible effect: the toast always appears just below the status
bar / cutout with a consistent margin, on every device and
orientation.

Verification: AchievementNotificationTest renders the toast with zero
insets, then with a 40 dp top inset, and asserts the card's content
moves down by exactly 40 dp. Callers compile:
:games:*:compileDevKotlin in the task list.

Diff

library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.ktindex 7758286b9c..779882df6d 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.kt@@ -10,9 +10,14 @@ import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column import androidx.compose.foundation.gestures.detectVerticalDragGestures import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.WindowInsets+import androidx.compose.foundation.layout.WindowInsetsSides import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.only import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.safeDrawing import androidx.compose.foundation.layout.size+import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.Icon@@ -46,6 +51,10 @@ import kotlinx.coroutines.flow.first  * time — and goes early on a tap or an upward swipe. Only the card itself takes touches: the rest of  * the screen keeps working under and around it.  *+ * It keeps clear of the status bar, display cutouts and rounded corners through [windowInsets] (the top+ * and sides of safe drawing by default), plus a small margin. Insets an ancestor has already consumed+ * are not applied twice.+ *  * [onDismiss] runs once the exit animation has finished, which is when the caller should move on to the  * next queued unlock. Each [achievement] gets a fresh toast, so a queue that advances in place shows  * the next one from the start.@@ -54,12 +63,13 @@ import kotlinx.coroutines.flow.first fun AchievementNotification(     achievement: Achievement,     onDismiss: () -> Unit,+    windowInsets: WindowInsets = WindowInsets.safeDrawing.only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal), ) {-    key(achievement) { AchievementToast(achievement, onDismiss) }+    key(achievement) { AchievementToast(achievement, onDismiss, windowInsets) } }  @Composable-private fun AchievementToast(achievement: Achievement, onDismiss: () -> Unit) {+private fun AchievementToast(achievement: Achievement, onDismiss: () -> Unit, windowInsets: WindowInsets) {     // Starts hidden with a visible target, so it slides in on first composition.     val visibleState = remember { MutableTransitionState(false).apply { targetState = true } }     val currentOnDismiss by rememberUpdatedState(onDismiss)@@ -86,12 +96,13 @@ private fun AchievementToast(achievement: Achievement, onDismiss: () -> Unit) {         visibleState = visibleState,         enter = slideInVertically { -it } + fadeIn(),         exit = slideOutVertically { -it } + fadeOut(),-        modifier = Modifier.padding(16.dp)+        modifier = Modifier+            .windowInsetsPadding(windowInsets)+            .padding(16.dp)     ) {         Card(             modifier = Modifier                 .fillMaxWidth()-                .padding(top = 24.dp)                 // Tap to dismiss; swipe up past the threshold to dismiss too. Both play the same exit                 // animation as the timeout.                 .clickable(onClickLabel = stringResource(R.string.dismiss), onClick = hide)
library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.kt
diff --git a/library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.kt b/library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.ktindex e32ddecacf..663231e3c2 100644--- a/library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.kt+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.kt@@ -3,17 +3,23 @@ package com.vayunmathur.library.ui import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.WindowInsets+import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateListOf+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.click+import androidx.compose.ui.test.getUnclippedBoundsInRoot import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTouchInput+import androidx.compose.ui.unit.dp import com.vayunmathur.library.util.Achievement import org.junit.Rule import org.junit.runner.RunWith@@ -102,6 +108,20 @@ class AchievementNotificationTest {         assertEquals(0, queue.size)     } +    @Test+    fun `the toast keeps clear of the status bar inset`() {+        var insets by mutableStateOf(WindowInsets(0.dp))+        rule.mainClock.autoAdvance = false+        rule.setContent { AchievementNotification(achievement = first, onDismiss = {}, windowInsets = insets) }+        rule.mainClock.advanceTimeBy(ENTER_SETTLED)+        val flush = rule.onNodeWithText("First win").getUnclippedBoundsInRoot().top++        insets = WindowInsets(top = 40.dp)+        rule.mainClock.advanceTimeBy(100)+        val inset = rule.onNodeWithText("First win").getUnclippedBoundsInRoot().top+        assertEquals(40f, (inset - flush).value, 0.5f, "the whole card moves down by the inset")+    }+     private companion object {         const val ROOT = "root"