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

Travis Griggs

10/06/2023, 5:13 PM
I have a custom view done as a Canvas with some fun drawing. This has been relatively straightforward so far. Take old view based "draw" code and realize it inside of a Canvas. The steps are a little different, but it's pretty familiar (from both Swift and "legacy" Android). Now I'm trying to add some gestures to it, for example, the scrollable modifier looks like something I want. My "source of truth" is the ClosedRange<Duration> interval that is shown to the left of the view. In the Canvas code, transforming between pixels and time offsets is easy, because I have access to both the size.height and the time interval. The scrollable lambda will consume pixels, and I can close over my mutablestate time interval, but I have no idea what size.height is in that context, so can't translate pixel changes to time changes. I miss this aspect of OO view trees, where your display methods and interaction methods had access to the same state (size, etc). I see that the pointerInputScope has a size value, so for those types of interactions, this is not a problem, but the scrollable modifier has fling/etc done already. I really don't want to try and re-emulate all of that with a transform/drag gesture. Perhaps, this is where I should use BoxWithConstraints? Though every time that gets mentioned here, people say no no no. And it's not clear how one translates the constraints thing to what would be size in a full size canvas embedded in the box. So I've got some of the pieces, but not all. Any help/nudge appreciated.
z

Zach Klippenstein (he/him) [MOD]

10/07/2023, 2:13 AM
Have you considered using a custom layout?
t

Travis Griggs

10/08/2023, 2:15 AM
I have... that was why I was messing with the Box with .layout modifiers the other day (with no real success). How would a "custom" layout close the gap between layout/drawing and interaction? Maybe I don't understand the notion of what a "custom" layout is enough?
c

Csaba Szugyiczki

10/09/2023, 7:24 AM
To learn more about custom layouts, start here: https://developer.android.com/jetpack/compose/layouts/custom#create-custom Basically, you get all the Composables that are the “children” of your Layout Composable, and you can measure and them place them however you like.