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

fengdai

11/01/2019, 3:08 AM
How to understand
measurement
and
intrinsic measurement
?
a

Adam Powell

11/01/2019, 1:26 PM
Measurement is, "given these width and height constraints, what size are you?" and intrinsic measurement is, "given that your width would be X, how tall would you need to be?" and the same for height/wide. Intrinsic measurement is meant to be a lighter weight alternative to full multi-pass speculative measurement to answer the same questions.
p

Pablichjenkov

11/01/2019, 2:02 PM
It sounds like a better approach for the case when a Parent dimension is wrap_content and the Child wants to match_parent. Is there an equivalent to:
requestLayout()
and
invalidate()
that ones can call when the shape or content of a composable change? Or there is no need for that?
a

Adam Powell

11/01/2019, 2:10 PM
Intrinsic measurement is just a series of functions available that a parent can call on a measurable (child) when the parent is in its own measure or layout phases.
as for equivalents of
requestLayout
and
invalidate
, the easiest way to get those is to change an
@Model
property that is read by the measure, layout or draw stages of the relevant component
and of course, recomposing a layout or draw composable will do the trick as well.
(with some further optimizations forthcoming along the recompose path)
p

Pablichjenkov

11/01/2019, 2:22 PM
Got it. I thought the fact of explicitly telling the ViewTree if you want a
requestLayout
or
invalidate
or both, optimized things a bit. Like calling only
invalidate
explicitly tell the system to skip measurement/layout phases.
a

Adam Powell

11/01/2019, 2:25 PM
it does, and the
@Model
observation scopes perform the same duty here for triggering what phase we need to go back to
but it puts the burden on the author of the layout and draw code to determine what data dependencies they need in each phase rather than on the author of the code that alters those data dependencies to determine how far back to invalidate
p

Pablichjenkov

11/01/2019, 2:28 PM
I see, It makes lot of sense. Thanks
👍 1
a

Adam Powell

11/01/2019, 2:28 PM
it does mean that these things get kind of subtle for the layout/draw code author. Capturing the value of an
@Model
outside the scope of where you actually use it means you can end up causing more work than you intended when it changes. You want to read
@Model
as close to the usage site as possible.
p

Pablichjenkov

11/01/2019, 2:34 PM
Good point