I have a custom SubcomposeLayout that exposes a slot in it's API. ```@Composable fun MyCustomLayout...
v
I have a custom SubcomposeLayout that exposes a slot in it's API.
Copy code
@Composable
fun MyCustomLayout(
    slot1: @Composable CustomScope1.() -> Unit,
    ....
) {...}
There are some guarantees about what's being passed in that slot. I need some information about part (children) of the slot and access them in the custom layout so that I could measure the dimension of the "sub-child" that's being passed in slot1 (it's not the entire composable that's being passed inside slot1, but part of it). Wrapping this subcomponent with a Box and tagging it with
layoutId
isn't enough because that information is lost when accessed in this custom layout. What's the best way to accomplish what I'm trying to do? Asked differently, how could I access information about the children of the slot that's being passed to my custom layout. I'm primarily interested in being able to derive dimensions of this child composable inside the slot
a
If you have control over what’s been passed as
slot1
, you can use a custom layout that updates the states of a state holder after measuring the children, and then read the states in
MyCustomLayout
, or use `AlignmentLine`s to pass the positions.
x
You should take a look at the Compose intrinsic measurements and see if that fits your use case. Intrinsic measurements allow the parent to get information of the dimension of the children
v
@xoangon in my case, I need information about the grand-child of the composable so I don't think the intrinsic by itself is enough. The dimensions of the child itself is anyway available to me, I need the ability to be able to peek into it further. I have a solution that sort of works using a CompositionLocal but my current implementation is causing some noticeable jank due to the state read and I'm trying to figure out an alternate way
@Albert Chang I do have a solution using a CompositionLocal that exposes a mutable state object but it causes a noticeable jank so I'm trying to avoid the state read. I might explore leveraging AlignmentLine 🤔
Leveraging alignment lines was a great suggestion. I think I'm now able to do what I needed while avoiding the state property altogether 🥳
s
Was it hard to work with? In the places I’ve seen people do this it looked quite complicated.
a
Yeah you shouldn't use a composition local because you can't read it in a layout scope and if you read it in a composition scope it will cause unnecessary recomposition. That's why you should use a state holder and pass it explicitly.