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

Jorge Castillo

07/24/2020, 1:43 PM
Hi. What you'd use to get the position of a composable on screen relative to the parent? as in: view.top / view.bottom etc.
Layout
?
a

Adam Powell

07/24/2020, 1:59 PM
As this is a hot topic for further API iteration right now, what's the use case?
j

Jorge Castillo

07/24/2020, 2:08 PM
Drawing on
Canvas
, want to clip the canvas only up to a specific height, and
clipRect
would require absolute position in px if I'm not mistaken?
Let me paste code so it's easier.
Copy code
fun DrawScope.plainClip(currentFillPhase: Float, block: DrawScope.() -> Unit) {
  val halfWidth = (size.width / 2f)
  val halfHeight = (size.height / 2f)

  val left = center.dx - halfWidth
  val top = center.dy - halfHeight
  val right = center.dx + halfWidth
  val bottom = center.dy + halfHeight

  this.clipRect(
    left = left,
    top = (bottom - top) * (1f - currentFillPhase),
    right = right,
    bottom = bottom,
    clipOp = ClipOp.intersect,
    block = block
  )
}
I'd like to clip up to a given height percent on the canvas, but not 100% sure that'd need to require global y for the clipping rect or should it be relative like this.
I feel like I should be using relative measures here, so might have something wrong regarding some scaling and translation of the canvas I'm doing.
a

Adam Powell

07/24/2020, 2:21 PM
clipping like that is going to be relative to your current coordinate space
so I don't think you should need to know additional info about the parent positioning? maybe I misunderstood something or need another 😅
j

Jorge Castillo

07/24/2020, 2:25 PM
right, that's the answer I needed to hear tbh
if you can confirm clipping needs to use relative coordinates to the canvas (as in 0,0 would be top left position on the canvas regardless of where it's positioned) then I suspect I've got some other issues caused by the scaling / translation I'm doing. Will keep experimenting a bit
a

Adam Powell

07/24/2020, 2:32 PM
please file bugs if this doesn't work in expected ways. I can't think of a way to make any of this sort of thing compose without working from relative frames of reference.
otherwise, as you suggest, the alternative would be trying to determine what your surroundings are doing to undo them along the way; let's skip that 🙂
j

Jorge Castillo

07/24/2020, 2:33 PM
yep I'll remove those transformations and work over the canvas as is first to double check it does what I'd expect
Worked like a charm. Was able to detect where the problem came from. My assumptions weren't correct and I didn't really need to clip a height % of the canvas but a height % of what I'm drawing on it, which is smaller. That was causing a lot of confusion. Thanks for the suggestion 👍
👍 1
Results so far @Adam Powell
🎉 2
👍 2
5 Views