Is there some documentation explaining the differe...
# compose
d
Is there some documentation explaining the differences from
Layout
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? Thanks
a
there was an article about that which is a bit outdated (WithConstraints was renamed to BoxWithConstraints) https://jorgecastillo.dev/jetpack-compose-withconstraints. but it should explain the basics quite well
z
The quick answer is that Compose uses a number of passes to display UI: 1. Composition (composable functions are called) 2. Measure (
MeasurePolicy
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).
a
I've got a copy of some WIP documentation on this open in another window that I'm helping to review right now 😄 there should be some nice info around this posted soon.
d
Thank you all!