https://kotlinlang.org logo
b

bruno.aybar

09/24/2020, 5:16 AM
Is there a reason why
DirectionalAlignment
is a private class? Being able to create an alignment with custom vertical and horizontal bias could be really useful. In my case I need to move a component around, and this seemed like the easiest way:
Copy code
@Composable
fun Sample {

    val offset = animatedFloat(initVal = ...)
    onActive { offset.animateTo(...) }

    Stack {
        SomethingThatMoves(Modifier.align(DirectionalAlignment(offset.value,offset.value)))
    }
}
Given that it's private, I just copy & pasted the class, and worked perfectly. Is there another easy way to accomplish that?
n

Nader Jawad

09/24/2020, 6:31 PM
We do provide the
Alignment
interface and
DirectionalAlignment
implements that. We also provide some default alignment implementations for center, bottom right, top left, etc.
b

bruno.aybar

09/24/2020, 7:37 PM
Yes, and those implementations look like
Copy code
@Stable
        val TopStart: Alignment = DirectionalAlignment(-1f, -1f)
        @Stable
        val TopCenter: Alignment = DirectionalAlignment(-1f, 0f)
        @Stable
        val TopEnd: Alignment = DirectionalAlignment(-1f, 1f)
        @Stable
        val CenterStart: Alignment = DirectionalAlignment(0f, -1f)
        @Stable
        val Center: Alignment = DirectionalAlignment(0f, 0f)
But let's say that I want some element not quite at the top center, but almost there (verticalBias = -0.75f instead of -1f). Why not provide the ability to create new instances of that
DirectionalAlignment
implementation? As you say, yes it's an interface so custom implementations can be easily created, that's really good. Just wondering if there's any reason why that specific implementation is
private
instead of public.
m

Mihai Popa

09/25/2020, 3:52 PM
Could you please file a bug to make this public? Thanks 🙂
n

Nader Jawad

09/25/2020, 3:54 PM
Looks like
AbsoluteAlignment
is exposed and supports the use case you are looking for although it does not expose rtl support
😮 1
m

Mihai Popa

09/25/2020, 3:55 PM
AbsoluteAlignment
's constructor is also internal
We want to provide a few factories for `Alignment`s. You should not need to copy paste
DirectionalAlignment
to create custom Alignments by percentage
b

bruno.aybar

09/25/2020, 8:29 PM
@Nader Jawad I was not aware of
AbsoluteAlignment
! That's actually what I was looking for, as I don't need rtl support
@Mihai Popa done! Here's the bug: https://issuetracker.google.com/issues/169406772