Is there a way to combine `WindowInsets` with `Pad...
# compose
t
Is there a way to combine
WindowInsets
with
PaddingValues
? I need both for
contentPadding
in a
LazyColumn
s
What do you mean by
combine
? If you explain a bit better I think I might be able to help.
t
WindowInsets + PaddingValues
I've came up with this actually, but wanted to know if there's something official:
Copy code
@Composable
operator fun PaddingValues.plus(paddingValues: PaddingValues): PaddingValues {
    val layoutDirection = LocalLayoutDirection.current
    return PaddingValues(
        top = this.calculateTopPadding() + paddingValues.calculateTopPadding(),
        bottom = this.calculateBottomPadding() + paddingValues.calculateBottomPadding(),
        start = this.calculateStartPadding(layoutDirection) + paddingValues.calculateStartPadding(layoutDirection),
        end = this.calculateEndPadding(layoutDirection) + paddingValues.calculateEndPadding(layoutDirection)
    )
}
And use it like this:
Copy code
WindowInsets.navigationBars.asPaddingValues() + PaddingValues(16.dp)
s
Okay now I understood your question 😄 I don’t think I’ve seen any other official API for this either. This looks just fine, I’d do the same.
e
why not
Copy code
operator fun PaddingValues.plus(that: PaddingValues): PaddingValues = object : PaddingValues {
    override fun calculateBottomPadding(): Dp =
        this@plus.calculateBottomPadding() + that.calculateBottomPadding()

    override fun calculateLeftPadding(layoutDirection: LayoutDirection): Dp =
        this@plus.calculateLeftPadding(layoutDirection) + that.calculateLeftPadding(layoutDirection)

    override fun calculateRightPadding(layoutDirection: LayoutDirection): Dp =
        this@plus.calculateRightPadding(layoutDirection) + that.calculateRightPadding(layoutDirection)

    override fun calculateTopPadding(): Dp =
        this@plus.calculateTopPadding() + that.calculateTopPadding()
}
which avoids the need to make it
@Composable
?
t
@ephemient that's really cool, thanks for the code! 😄
a
Not only does that avoid the need to make it
@Composable
, it also delays the phase from composition to layout, which can be good for performance for animations and some inset quirks
m
I learn so much with this community lol
s
I don't think I would know even half of what I know now regarding Kotlin, compose and android in general if I never joined this slack channel. It's been a vital point in my progression, it's honestly one of the best things I did back in the day 😅
e
one thing I did a lot of as an early user of Stack Overflow, and which I think applies here too: if you see a question you don't know the answer to, try to do some research yourself. you don't have to share if you didn't find an answer or don't have confidence in it. but doing that first, and then comparing to other answers, is a great way to learn. at least in my experience
125 Views