Last active
January 14, 2026 21:44
-
-
Save alexvanyo/ff1678e465e14b0122d2092a2351de83 to your computer and use it in GitHub Desktop.
An example of using the WindowInsetRulers to drive content padding for a LazyColumn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2025 Google LLC. | |
| // SPDX-License-Identifier: Apache-2.0 | |
| @Preview | |
| @Composable | |
| fun WindowInsetRulersContentPadding() { | |
| val paddingValues = remember { | |
| object : PaddingValues { | |
| var start by mutableStateOf(0.dp) | |
| var end by mutableStateOf(0.dp) | |
| var top by mutableStateOf(0.dp) | |
| var bottom by mutableStateOf(0.dp) | |
| override fun calculateLeftPadding(layoutDirection: LayoutDirection): Dp = | |
| when (layoutDirection) { | |
| LayoutDirection.Ltr -> start | |
| LayoutDirection.Rtl -> end | |
| } | |
| override fun calculateTopPadding(): Dp = top | |
| override fun calculateRightPadding(layoutDirection: LayoutDirection): Dp = | |
| when (layoutDirection) { | |
| LayoutDirection.Ltr -> end | |
| LayoutDirection.Rtl -> start | |
| } | |
| override fun calculateBottomPadding(): Dp = bottom | |
| } | |
| } | |
| Box( | |
| modifier = Modifier | |
| .fillMaxSize() | |
| .layout { measurable, constraints -> | |
| val width = constraints.maxWidth | |
| val height = constraints.maxHeight | |
| layout(width, height) { | |
| val rectRulers = WindowInsetsRulers.SafeDrawing.current | |
| val leftContentPadding = rectRulers.left.current(0f).toDp() | |
| val rightContentPadding = (width - rectRulers.right.current(0f)).toDp() | |
| when (layoutDirection) { | |
| LayoutDirection.Ltr -> { | |
| paddingValues.start = leftContentPadding | |
| paddingValues.end = rightContentPadding | |
| } | |
| LayoutDirection.Rtl -> { | |
| paddingValues.start = rightContentPadding | |
| paddingValues.end = leftContentPadding | |
| } | |
| } | |
| paddingValues.top = rectRulers.top.current(0f).toDp() | |
| paddingValues.bottom = (height - rectRulers.bottom.current(0f)).toDp() | |
| val placeable = measurable.measure(constraints) | |
| placeable.placeRelative(0, 0) | |
| } | |
| } | |
| ) { | |
| LazyColumn( | |
| contentPadding = paddingValues, | |
| modifier = Modifier.fillMaxSize(), | |
| ) { | |
| items(100) { | |
| TextField( | |
| state = rememberTextFieldState(), | |
| modifier = Modifier.fillMaxWidth(), | |
| ) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment