Daniele Segato
10/19/2021, 11:51 AMLayout
and SubcomposeLayout
? what changes in performances?
Why do we need a SubcomposeLayout
for BoxWithConstraints
?
A lot of the time all you want is the size (width / height) of the composable, which you can get with Modifier.onSizeChanged {}
or Modifier.onGloballyPositioned {}
but it is asynchronous: you need to wait for the first composition before you get a callback to that, which means if you need the size of the widget to do something on the UI you'll have a flickering.
Can someone give me an in-depth answer or a link with an in-depth answer to these things?
What I'm most interest in is what are the drawback of using subcompositions and what are all the options to get the layout constraints / why we need subcomposition to get them before composing (modifiers have access to them).
EDIT: for instance IntrinsicSize
stop working if a child has BoxWithConstraints
right?
ThanksAndrey Kulikov
10/19/2021, 4:28 PMZach Klippenstein (he/him) [MOD]
10/19/2021, 5:02 PMMeasurePolicy
functions are called)
3. Placement (layout
functions from MeasurePolicy
functions are called)
4. Draw
So when your composable function is called, none of the layout information has been computed yet.
Subcomposition works around this by embedding a whole separate composition within the owning composition, which can then be told to explicitly compose after the parent’s Composition pass, as part of a measure pass for example (what BoxWithConstraints
does). This is expensive because, right now, the subcomposition has its own slot table, and all the bookkeeping that goes along with that, separate from the main composition. It’s also expensive because it means you’re potentially triggering a bunch of compositions during layout, and Compose assumes that layout passes will just do the minimum amount of work required to measure + place, so performing composition in that pass might break some of those assumptions (e.g. animating constraints would require recomposing the subcomposition on every frame, not just performing some quick math).Adam Powell
10/19/2021, 7:02 PMDaniele Segato
10/19/2021, 8:14 PM