Modern Apps git

← Modern-Apps.git

library: consume IME once when host bottom bar owns it

Commit
a5e8a344379eff4a8d10b044d0b0996b26cee1bb
Author
Vayun Mathur <[email protected]>
When
2026-12-05 14:44 UTC (18 hours ago)
Parents
26b2d97
Picker/search screens hosted in MainNavigation (health CatalogPickerPage for lab tests, medicines, vaccines, allergens and conditions, plus every other IME screen behind HealthBottomBar) showed a keyboard-height blank above the bottom bar that clipped content: the bar unions IME into its own windowInsets and rides above the keyboard, while each hosted screen's inner Scaffold also padded its content bottom by the same IME.

Fix at the shared host, not per screen: MainNavigation gains bottomBarOwnsInsets (default false, so apps without an inset-owning bar are unchanged). When true, NavHostChrome consumes navigationBars+IME for the NavDisplay area only; the sibling bottom bar still claims them once and sits correctly above the keyboard, and dialog windows keep their own insets. Health opts in once in its Navigation.kt, covering all its picker/chooser/search pages. Robolectric cannot raise a real IME, so the added LibraryComposeTest pins the hosting contract and documents that limitation; the consumption itself is the consumeWindowInsets modifier in NavHostChrome.

Verified: :library:lint and :library:testDebugUnitTest green; :health:lint green with the opt-in.

Diff

health/src/main/java/com/vayunmathur/health/Navigation.kt
diff --git a/health/src/main/java/com/vayunmathur/health/Navigation.kt b/health/src/main/java/com/vayunmathur/health/Navigation.ktindex a9e742e190..3b6640ec8b 100644--- a/health/src/main/java/com/vayunmathur/health/Navigation.kt+++ b/health/src/main/java/com/vayunmathur/health/Navigation.kt@@ -15,6 +15,9 @@ fun Navigation(viewModel: HealthViewModel, medicalViewModel: MedicalViewModel) {     MainNavigation(         backStack = backStack,         bottomBar = { HealthBottomBar(backStack) },+        // HealthBottomBar (BottomNavBar) claims IME/navigation insets itself;+        // hosted screens must not pad for them a second time.+        bottomBarOwnsInsets = true,     ) {         fitnessEntries(backStack, viewModel)         medicalEntries(backStack, medicalViewModel)
library/src/main/java/com/vayunmathur/library/util/Navigation.kt
diff --git a/library/src/main/java/com/vayunmathur/library/util/Navigation.kt b/library/src/main/java/com/vayunmathur/library/util/Navigation.ktindex 68443d7792..4e6b66e276 100644--- a/library/src/main/java/com/vayunmathur/library/util/Navigation.kt+++ b/library/src/main/java/com/vayunmathur/library/util/Navigation.kt@@ -32,6 +32,9 @@ import androidx.compose.foundation.layout.captionBar import androidx.compose.foundation.layout.captionBarPadding import androidx.compose.foundation.layout.consumeWindowInsets import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.ime+import androidx.compose.foundation.layout.navigationBars+import androidx.compose.foundation.layout.union import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.contentColorFor@@ -466,6 +469,8 @@ fun <T : NavKey> MainNavigation(     backStack: NavBackStack<T>,     bottomBar: @Composable () -> Unit = {},     containerColor: Color = Color.Unspecified,+    /** True when [bottomBar] itself claims IME/navigation insets (e.g. [BottomNavBar]). */+    bottomBarOwnsInsets: Boolean = false,     entryProvider: EntryProviderScope<T>.() -> Unit, ) {     val sceneStrategy: ListDetailSceneStrategy<T> = rememberListDetailSceneStrategy()@@ -512,7 +517,8 @@ fun <T : NavKey> MainNavigation(         sceneStrategy = sceneStrategy,         transitions = transitions,         entryProvider = entryProvider,-        bottomBar = bottomBar+        bottomBar = bottomBar,+        bottomBarOwnsInsets = bottomBarOwnsInsets,     ) } @@ -562,6 +568,7 @@ private fun <T : NavKey> NavHostChrome(     transitions: NavTransitions,     entryProvider: EntryProviderScope<T>.() -> Unit,     bottomBar: @Composable () -> Unit,+    bottomBarOwnsInsets: Boolean = false, ) {     Box(         Modifier@@ -588,8 +595,26 @@ private fun <T : NavKey> NavHostChrome(                     LocalNavMotion provides topMotion,                 ) {                     Column(Modifier.fillMaxSize()) {+                        // The host bottom bar (below, a sibling) owns the+                        // IME + navigation-bar insets once via its own+                        // windowInsets. Consuming them for the NavDisplay+                        // area stops every hosted screen's inner Scaffold+                        // from padding its content bottom by the same IME+                        // again — that duplicate was a keyboard-height blank+                        // above the bar that clipped picker/search content+                        // (health CatalogPickerPage and every sibling+                        // chooser). Dialog windows are separate windows and+                        // keep their own insets.                         NavDisplay(-                            modifier = Modifier.weight(1f),+                            modifier = Modifier.weight(1f).then(+                                if (bottomBarOwnsInsets) {+                                    Modifier.consumeWindowInsets(+                                        WindowInsets.navigationBars.union(WindowInsets.ime),+                                    )+                                } else {+                                    Modifier+                                },+                            ),                             sceneStrategies = listOf(DialogSceneStrategy(), sceneStrategy),                             // The destination decides: the motion is read off the entry the user is                             // arriving at when pushing, and off the one they are leaving when popping,
library/src/test/java/com/vayunmathur/library/util/LibraryComposeTest.kt
diff --git a/library/src/test/java/com/vayunmathur/library/util/LibraryComposeTest.kt b/library/src/test/java/com/vayunmathur/library/util/LibraryComposeTest.ktindex 6c0ae39ad8..dc1c32681c 100644--- a/library/src/test/java/com/vayunmathur/library/util/LibraryComposeTest.kt+++ b/library/src/test/java/com/vayunmathur/library/util/LibraryComposeTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.library.util import android.content.Context import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.ime import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text@@ -65,6 +66,33 @@ class LibraryComposeTest {         rule.onNodeWithText("Screen home").assertIsDisplayed()     } +    @Test fun mainNavigationWithOwningBottomBarConsumesInsetsForContent() {+        // Robolectric cannot raise a real IME, so this pins the hosting+        // contract (content + owning bottom bar compose together); the+        // single-consumption itself is in NavHostChrome's consume modifier.+        var imeBottom = -1+        val stack = NavBackStack(arrayOf(Screen("picker")))+        rule.setContent {+            MaterialTheme {+                MainNavigation(+                    backStack = stack,+                    bottomBar = { Text("OwningBar") },+                    bottomBarOwnsInsets = true,+                ) {+                    entry<Screen> {+                        val density = androidx.compose.ui.platform.LocalDensity.current+                        imeBottom = androidx.compose.foundation.layout.WindowInsets.ime+                            .getBottom(density)+                        Text("PickerContent")+                    }+                }+            }+        }+        rule.onNodeWithText("PickerContent").assertIsDisplayed()+        rule.onNodeWithText("OwningBar").assertIsDisplayed()+        assertEquals(0, imeBottom)+    }+     @Test fun mainNavigationTransparentContainer() {         val stack = NavBackStack(arrayOf(Screen("only")))         rule.setContent {