Can I access the laid out width or the constraints...
# compose
d
Can I access the laid out width or the constraints from within a modifier?
a
Yes, but there are inefficient or feedback-loop patterns to avoid depending on what you're doing with that data. What's your use case?
s
Modifier.onSizeChanged
a
yes, that is one 🙂
d
I have a modifier that creates a turned page effect based on a turn percent parameter
I need to convert the percent to pixels
In my case I happen to know that the width and height will always be the exact size of the parent (I require this anyway because I'm custom drawing a very specific thing in this component)
Right now I'm wrapping like so, which I think avoids the inefficiencies?
Copy code
BoxWithConstraints {
  Box(Modifier
    .myCustomDraw()
    .myCustomTurn(percent, constraints.minWidth))
}
a
this is why I asked 🙂 all of the custom drawing APIs have the size available in the receiver scope already - you shouldn't need to use any additional modifiers to get it
e.g.
Copy code
Canvas(...) {
  this.size
}
or
Copy code
Modifier.drawBehind {
  this.size
}
d
I'm using
graphicsLayer
, and I didn't find anything like size on it when I checked @Adam Powell
I'm separating the turn animation from drawing the page for performance reasons. I draw the page once, and then use graphicsLayer to translate it efficiently
a
ah, could be an omission there, can you file an issue for it please? We can evaluate adding it there. In the meantime, yes, as @Se7eN said you can do this:
Copy code
fun Modifier.myModifier(...) = composed {
  var size by remember { mutableStateOf(IntSize.Zero) }
  // ...
  Modifier.onSizeChanged { size = it }
    .graphicsLayer {
      size...
    }
}
d
It feels less hacky to me to just pass it in, so if that has similar characteristics I'll do that. (In my case if the size changes that would necessitate re-laying out the entire section of the book, which would involve throwing out the grandparent of the component with this modifier)
I'll do that @Adam Powell.
graphicsLayer
provides very little, I had assumed that was because getting the other information was impossible