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

Mohamed Elfiky

09/09/2020, 8:19 PM
What is subcomposition is it nesting composables?
z

Zach Klippenstein (he/him) [MOD]

09/09/2020, 8:20 PM
Not really nesting composables. It's more nesting compositions.
m

Mohamed Elfiky

09/09/2020, 8:25 PM
I am sorry but i can't really understand is there docs about it or example
Is it when i use withConstraints ?
z

Zach Klippenstein (he/him) [MOD]

09/09/2020, 8:28 PM
WithConstraints
uses it, yes. I haven’t seen any good docs on it, and the APIs seem to still be changing quite a bit.
It basically lets you have multiple separate compositions going on, but sharing things like Ambients
So
WithConstraints
uses it because it can’t compose its children until it knows the constraints, but it can’t know the constraints until the composition pass is done and the layout pass is happening. Since it can’t just compose its children directly, it waits until the layout happens, then creates a new composition for its content that is linked to its position in the original composition. This subcomposition gets ambients and everything from the parent, so if you wrap
WithConstraints
with a
Providers
it will jsut work.
👍 1
LazyColumnFor
uses it as well, but for a different use case – laziness. It doesn’t compose any of its children at first. Then, when it’s laid out, it sees how much space it has, then creates a subcomposition for the first child. If there’s more space it needs to fill, it creates a subcomposition for its second child, etc. When a child is scrolled out of view, it disposes the subcomposition for that child, so only the children which are actually visible are ever being composed.
m

Mohamed Elfiky

09/09/2020, 8:42 PM
thanks a lot for the detailed explanation. so basically i won't need it unless i want to know the size beforehand
z

Zach Klippenstein (he/him) [MOD]

09/09/2020, 8:50 PM
That’s one use case, yes. I think it’s more general to say you only need it if you need to write imperative logic around how to compose your children, and can’t do so in the normal declarative way. But I’m sure the Compose team has a more accurate/eloquent way of describing it.
👍 1
One more use case is how Compose does dialogs. When you write a
Dialog()
composable, it actually launches a regular Android dialog in a separate window and everything, and then it composes its content in that dialog as a subcomposition. So the compositions are effectively the same, even though part of it is rendering to an entirely different window.
m

Mohamed Elfiky

09/09/2020, 9:05 PM
thanks a lot.
35 Views