https://kotlinlang.org logo
#compose
Title
# compose
t

Thiago

03/24/2020, 6:26 PM
I would like to request the compose team for a fractional layout modifier. Its works better than LayoutOffset because I don't have to manager pixels. Its make sense?
Copy code
data class LayoutFractional(val dx: Float, val dy: Float) : LayoutModifier {
    override fun ModifierScope.modifyPosition(
        childSize: IntPxSize,
        containerSize: IntPxSize
    ): IntPxPosition = IntPxPosition(
        x = childSize.width * dx.coerceIn(MIN_FRACTION, MAX_FRACTION) *
                if (layoutDirection == LayoutDirection.Ltr) MAX_FRACTION else MIN_FRACTION,
        y = childSize.height * dy.coerceIn(MIN_FRACTION, MAX_FRACTION)
    )

    companion object {
        private const val MAX_FRACTION = 1f
        private const val MIN_FRACTION = -1f

        val Origin = LayoutFractional(0f, 0f)
    }
}
a

Adam Powell

03/24/2020, 7:45 PM
Why restrict it from -1 to 1? Seems like multiples would be valid
t

Thiago

03/24/2020, 7:57 PM
Restricted because I would like to let user to set range between [-1, 1] only.
a

Adam Powell

03/24/2020, 8:07 PM
I ask because we would not apply that restriction if we added something like this upstream to make it more general purpose 🙂
t

Thiago

03/24/2020, 8:12 PM
Its make sense. I restricted because the name "Fractional" means a range 0%..100% for me.
Thanks @Adam Powell . Removing the restriction really increase the possibilities. Will Compose have something like that in next versions?
a

Adam Powell

03/24/2020, 9:09 PM
Probably not the next version since the next version is cut tomorrow and I'm working on some other major modifier-related changes first, but it seems potentially useful
👍 1
🍾 1
m

Mihai Popa

03/26/2020, 3:27 PM
I am not completely sold that this is a general enough modifier to merge it in compose
Unless you meant computing x and y as fraction of
containerSize - childSize
? In this case I agree it would make sense to have it
Probably without the MAX_FRACTION and MIN_FRACTION coercing though, as Adam suggested
t

Thiago

03/26/2020, 11:24 PM
Yes, @Mihai Popa, I have applied the Adam suggestions and its make sense. And about merge, I aggre that only if have a general purpose. For me was easier to use something like that instead of works with pixels using LayoutOffset. https://github.com/programadorthi/compose-weather-forecast/blob/02884a698153a7cd0b81df7472bee26ca2e92829/app/src/main/java/br/com/programadorthi/compose/helpers/modifiers.kt#L21
2 Views