Modern Apps git

← Modern-Apps.git

maps: bound the bottom sheet to its content and open at the header

Commit
f1abead30b5cd7937e620e1407e3cecc804763be
Author
vayun-mathur <[email protected]>
When
2026-08-21 01:57 UTC (3 months ago)
Parents
e1b0180
The sheet was measured at an exact height (minHeight == maxHeight) with an expanded bound of container - statusBar, so short content rendered as blank surface and every selection opened full-height. The height is now a maximum, the expanded bound is min(available, content height), and sheetPeekHeight is clamped down to that instead of acting as a floor. Content height is learned by observation because the content ends in a LazyColumn, whose intrinsics cannot be queried; contentKey discards the observation when the host swaps content.

Diff

library/ui/src/main/java/com/vayunmathur/library/ui/FreeHeightBottomSheet.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/FreeHeightBottomSheet.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/FreeHeightBottomSheet.ktindex de2188c858..33503e8f8e 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/FreeHeightBottomSheet.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/FreeHeightBottomSheet.kt@@ -58,6 +58,14 @@ import kotlinx.coroutines.launch  * and [Animatable.updateBounds] makes a fling decay stop there too, so  * "the user cannot dismiss this sheet" is enforced by construction rather than by  * vetoing state changes after the fact.+ *+ * The upper bound is the lesser of the space available and the sheet content's+ * own height, so a sheet holding two lines of text does not stretch to fill the+ * window. Content height is learned by observation — whenever the content+ * measures shorter than the height it was offered, that shorter height becomes+ * the ceiling — because the content typically ends in a `LazyColumn`, whose+ * intrinsic height cannot be queried. The observation is discarded via+ * [resetContentCap] when the host swaps in different content.  */ @Stable class FreeHeightSheetState(private val initialValue: SheetValue) {@@ -66,6 +74,10 @@ class FreeHeightSheetState(private val initialValue: SheetValue) {      private var peekPx = 0f     private var expandedPx = 0f+    /** Space the host can give the sheet, before the content ceiling applies. */+    private var availablePx = 0f+    /** Tallest the content has been seen to need. [Float.MAX_VALUE] = not yet known. */+    private var contentCapPx = Float.MAX_VALUE     /**      * False until the host has measured itself and the bounds mean anything. A      * snapshot state, because a programmatic [expand] can arrive before the first@@ -86,8 +98,8 @@ class FreeHeightSheetState(private val initialValue: SheetValue) {      * the current height stay consistent with what is on screen.      */     internal suspend fun onMeasured(peek: Float, expanded: Float) {-        peekPx = peek-        expandedPx = expanded.coerceAtLeast(peek)+        availablePx = expanded+        applyBounds(peek)         if (!measured) {             offset.snapTo(                 when (initialValue) {@@ -107,6 +119,45 @@ class FreeHeightSheetState(private val initialValue: SheetValue) {         }     } +    /**+     * Derive [expandedPx] and [peekPx] from the available space and the content+     * ceiling. The peek is clamped rather than used as a floor: content shorter+     * than the peek should shrink the sheet, not leave dead space below it.+     */+    private fun applyBounds(peek: Float) {+        expandedPx = availablePx.coerceAtMost(contentCapPx).coerceAtLeast(0f)+        peekPx = peek.coerceAtMost(expandedPx)+    }++    /**+     * Report what the sheet content actually measured when offered `offered`+     * pixels. Slack means the content is fully visible and needs no more room, so+     * `measured` becomes the new ceiling; filling the offer says nothing about how+     * much taller it might be, so the ceiling is left alone and re-probed as the+     * user drags further up.+     */+    internal fun onContentHeight(contentHeight: Float, offered: Float, scope: CoroutineScope) {+        if (!measured || contentHeight <= 0f || contentHeight >= offered) return+        if (contentHeight == contentCapPx) return+        contentCapPx = contentHeight+        applyBounds(peekPx)+        if (offset.value > expandedPx) {+            scope.launch { offset.snapTo(expandedPx) }+        }+        armBounds()+    }++    /**+     * Forget the learned content ceiling, so the next layout re-probes it. The+     * host calls this when it swaps in different content, which may well be+     * taller than whatever the last content settled on.+     */+    internal fun resetContentCap(peek: Float) {+        contentCapPx = Float.MAX_VALUE+        applyBounds(peek)+        armBounds()+    }+     private suspend fun awaitMeasured() {         if (!measured) {             snapshotFlow { measured }.first { it }@@ -200,9 +251,18 @@ fun rememberFreeHeightSheetState(  * `LazyColumn` — measured at full height, so its scroll extent would be wrong and  * the nested-scroll edge detection below would misfire.  *+ * The height is a maximum, not an exact size: content shorter than the sheet's+ * current height shrinks the sheet to fit rather than leaving blank surface below+ * it, and that shorter height also becomes the ceiling a drag can reach.+ *  * @param sheetPeekHeight visible sheet content height when collapsed, above the- *   navigation bar. Also the bottom padding handed to [content], so a collapsed- *   sheet never covers anything [content] pins to the bottom.+ *   navigation bar. This is the resting height the sheet opens at, so it should be+ *   tall enough to show the content's header. Also the bottom padding handed to+ *   [content], so a collapsed sheet never covers anything [content] pins to the+ *   bottom. Content shorter than this shrinks the sheet below it.+ * @param contentKey identifies what [sheetContent] is currently showing. When it+ *   changes, the learned content-height ceiling is discarded so taller content is+ *   not trapped at the previous content's height.  */ @Composable fun FreeHeightBottomSheetScaffold(@@ -211,6 +271,7 @@ fun FreeHeightBottomSheetScaffold(     state: FreeHeightSheetState = rememberFreeHeightSheetState(),     sheetPeekHeight: Dp = BottomSheetDefaults.SheetPeekHeight,     sheetContainerColor: Color = BottomSheetDefaults.ContainerColor,+    contentKey: Any? = null,     content: @Composable (PaddingValues) -> Unit, ) {     val density = LocalDensity.current@@ -228,6 +289,10 @@ fun FreeHeightBottomSheetScaffold(         }     } +    // New content may well be taller than whatever the last content settled at, so+    // the learned ceiling cannot carry over.+    LaunchedEffect(contentKey, peekPx) { state.resetContentCap(peekPx.toFloat()) }+     val scope = rememberCoroutineScope()     val nestedScroll = remember(state, scope) { sheetNestedScroll(state, scope) }     val contentPadding = remember(sheetPeekHeight) { PaddingValues(bottom = sheetPeekHeight) }@@ -266,14 +331,17 @@ fun FreeHeightBottomSheetScaffold(         val width = constraints.maxWidth         val height = constraints.maxHeight         // Read in the layout phase, so a drag remeasures without recomposing.-        val sheetHeight = state.offsetPx.toInt().coerceIn(0, height)+        val offered = state.offsetPx.toInt().coerceIn(0, height)         val body = measurables[0].measure(constraints)+        // A maximum, not an exact height: a short sheet wraps its content instead of+        // padding itself out with blank surface.         val sheet = measurables[1].measure(-            constraints.copy(minHeight = sheetHeight, maxHeight = sheetHeight),+            constraints.copy(minHeight = 0, maxHeight = offered),         )+        state.onContentHeight(sheet.height.toFloat(), offered.toFloat(), scope)         layout(width, height) {             body.place(0, 0)-            sheet.place(0, height - sheetHeight)+            sheet.place(0, height - sheet.height)         }     } }
maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.ktindex be3dd715f9..591b31bb42 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt@@ -254,9 +254,9 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,     LaunchedEffect(Unit) {         // Restore-on-recompose: raise the sheet if something is already selected —         // unless a deep link / auto-select is about to open the compact PANE instead-        // (handled by the pendingFocus effect below), so we don't full-expand first.+        // (handled by the pendingFocus effect below).         if (selectedFeature != null && viewModel.pendingFocus.value == null) {-            sheetState.expand()+            sheetState.partialExpand()         }     } @@ -378,7 +378,7 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,         Column(Modifier.padding(horizontal = 16.dp).padding(top = 8.dp)) {             BottomSheetContent(viewModel, selectedFeature, { viewModel.set(it) }, route, selectedRouteType, { selectedRouteType = it }, inactiveNavigation, savedPlacesViewModel, transitViewModel, navState)         }-    }, Modifier, sheetState, 170.dp) { paddingValues ->+    }, Modifier, sheetState, 170.dp, contentKey = listOf(selectedFeature, selectedRouteType)) { paddingValues ->         AppScaffold(             title = {                 // Search bar lives IN the top app bar (Google-Maps style).@@ -527,16 +527,16 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                                         selectedFeature as SpecificFeature.Route                                     )                                     viewModel.set(pinHit)-                                    sheetState.expand()+                                    sheetState.partialExpand()                                     return@launch                                 }                                  // Otherwise fall back to basemap admin labels-                                // (country/region). Native POIs are suppressed+                                // (country/region/city). Native POIs are suppressed                                 // and the amenity-DB enrichment path is gone.                                 val features = projection?.queryRenderedFeatures(                                     offset,-                                    setOf("places_country", "places_region").flatMap {+                                    setOf("places_country", "places_region", "places_locality").flatMap {                                         listOf("${it}_base", "${it}_hybrid")                                     }.toSet()                                 ) ?: emptyList()@@ -556,7 +556,7 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                                         selectedFeature as SpecificFeature.Route                                     )                                     viewModel.set(firstFeature)-                                    sheetState.expand()+                                    sheetState.partialExpand()                                     return@launch                                 } @@ -569,7 +569,7 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                                             selectedFeature as SpecificFeature.Route                                         )                                         viewModel.set(place)-                                        coroutineScope.launch { sheetState.expand() }+                                        coroutineScope.launch { sheetState.partialExpand() }                                     }                                 }                             }@@ -796,7 +796,7 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                                 }                                 viewModel.set(SpecificFeature.Route(listOf(null, feature)))                                 showParkingSheet = false-                                coroutineScope.launch { sheetState.expand() }+                                coroutineScope.launch { sheetState.partialExpand() }                             },                             onNoteChange = { parkingViewModel.updateNote(it) },                         )