Is there a way do get some window insets, but in a...
# compose
s
Is there a way do get some window insets, but in a way which respects the already consumed insets, while not having to go with
Modifier.windowInsetsPadding
nor
Modifier.windowInsetsTopHeight
and the equivalents? My use case is that I got a layout where for one of the items, I want the
contentPadding: PaddingValues
parameter that it takes to respect insets, but doing something like
PaddingValues(horizontal = 16.dp) + WindowInsets.safeDrawing.asPaddingValues()
means that it simply gets the insets even if they are consumed, since that’s how
asPaddingValues
works. From what I can tell, this functionality hooked together though some ModifierLocals which modifiers like
windowInsetsTopHeight
respect by being
ModifierLocalConsumer
themselves, so I don’t see a good entrypoint for my code to get the benefits of the existing functionality there. Is there some way for me to get access to it in a reasonable non-hacky way since
ModifierLocalConsumedWindowInsets
is internal in the foundation compose package?
I am sorry for the ping, but this feels right up your alley @Alex Vanyo The tl;dr is: Can I can the non-consumed insets in a composable without doing it through a
Modifier
with the current APIs? And if not, for my use case regarding using non-consumed insets for
contentPadding
of a composable, is there something else I can do?
a
There’s
Modifier.onConsumedWindowInsetsChanged
, which allows pulling out the consumed insets and using that in measurement or layout.
s
Alex, thank you so much! I completely missed this API 🫣 Ended up with something like this for my specific use case
Copy code
var consumedWindowInsets by remember { mutableStateOf(WindowInsets(0.dp)) }
MyComposable(
  contentPadding = PaddingValues(horizontal = 16.dp) + WindowInsets.safeDrawing.exclude(consumedWindowInsets).only(WindowInsetsSides.Horizontal).asPaddingValues(),
  modifier = Modifier.onConsumedWindowInsetsChanged { consumedWindowInsets = it },
)
I guess that this means that for the first composition I will be wrongly assuming that the consumed window insets are 0.dp, since I need to initialize it with something until the lambda runs for the first time. But it works!! which is the important bit here. I was looking here https://developer.android.com/jetpack/compose/layouts/insets#padding-modifiers for a solution to my issue, but I couldn’t find anything. Is this function mentioned anywhere else which I may have missed? I would love to read more about it to make sure I am using it correctly too if any such docs exist.
t
I created an issue to get an easier api to get this notconsumed window insets here: https://issuetracker.google.com/issues/300949347