Stylianos Gakis
11/15/2024, 12:49 AMfun WindowInsets.asPaddingValues(): PaddingValues
alternative which does respect incoming already consumed paddings?
Modifier.windowInsetsPadding
does work, but I need my value as PaddingValues
so that I can pass it as contentPadding
to a LazyColumn
Is the answer still that we must still do the onConsumedWindowInsetsChanged dance manually?Stylianos Gakis
11/15/2024, 12:50 AMvar consumedWindowInsets by remember { mutableStateOf(WindowInsets(0.dp)) }
LazyColumn(
contentPadding = WindowInsets.safeDrawing
.exclude(consumedWindowInsets)
.asPaddingValues(),
modifier = Modifier.onConsumedWindowInsetsChanged {
consumedWindowInsets = it
}
)
But I feel like there is room for an API here which reads the composition local for you and does this for you instead.Alex Vanyo
11/15/2024, 12:57 AMreads the composition localInset consumption is done at the
ModifierLocal
level, not via a composition local, so there isn’t a general way to pull out the consumed inset values from @Composable
unless you’re at a particular spot in the hierarchy, which is what Modifier.onConsumedWindowInsetsChanged
can do, since its a Modifier
Stylianos Gakis
11/15/2024, 1:00 AMModifierLocalConsumer
but that is as you said, only for modifiersStylianos Gakis
11/15/2024, 1:01 AMAlex Vanyo
11/15/2024, 1:03 AMonConsumedWindowInsetsChanged
in this case:
val safeDrawingInsets = WindowInsets.safeDrawing
val insetsForContentPadding = remember { MutableWindowInsets() }
LazyColumn(
contentPadding = insetsForContentPadding.asPaddingValues(),
modifier = Modifier.onConsumedWindowInsetsChanged {
insetsForContentPadding.insets = safeDrawingInsets.exclude(it)
}
)
That should allow you to have the correct consumed inset values on the first frameAlex Vanyo
11/15/2024, 1:06 AMvar consumedWindowInsets = remember { MutableWindowInsets() }
LazyColumn(
contentPadding = WindowInsets.safeDrawing
.exclude(consumedWindowInsets)
.asPaddingValues(),
modifier = Modifier.onConsumedWindowInsetsChanged {
consumedWindowInsets.insets = it
}
)
Stylianos Gakis
11/15/2024, 1:08 AMAlex Vanyo
11/15/2024, 1:09 AMAlex Vanyo
11/15/2024, 1:09 AMWindowInsets
and PaddingValues
are all “lazy”, they don’t calculate the values upon construction, they defer the reads until they are neededStylianos Gakis
11/15/2024, 1:12 AMAlex Vanyo
11/15/2024, 1:18 AMAlex Vanyo
11/15/2024, 1:26 AMAlex Vanyo
11/15/2024, 1:29 AMAlex Vanyo
11/15/2024, 1:31 AMasPaddingValues().toString()
, you should see the whole chain of operations used to create the final “pointer” PaddingValues
that’s used to drive the content paddingStylianos Gakis
11/15/2024, 11:49 PMtoString()
on the PaddingValues, it prints out the chain as you said, it gave me a good picture of what I was dealing with here!