SmartToolFactory / Compose-Extended-Gestures

Counterpart of onTouchEvent, TouchDelegate, Transform gestures that notifies start, end, main pointer, pointers and option to consume PointerInputChange which defines whether other gestures should receive or not.
Apache License 2.0
93 stars 8 forks source link

How to get the screen size of the element? #4

Closed cybercoder-naj closed 3 months ago

cybercoder-naj commented 5 months ago

I am working on the Canvas object such as follows:

fun Component() {
  Canvas(
    modifier = Modifier
      .clipToBounds()
      .pointerMotionEvents(
        onDown = {/* I want the size of the Canvas here */}
      )
  ) {
    /* drawing elements on the Canvas */
  }
}

Do you think adding something to the modifier with the size of the Canvas would be possible? If the user is pressing "Down" on the canvas, it is useful to know where it is with respect to the dimensions of the canvas.

cybercoder-naj commented 5 months ago

The current workaround is having a local state to keep the size. Wondering if the library can do this without it.

@Composable
fun Component() {
  var canvasSize by remember { mutableStateOf(Size.Unspecified) }

  Canvas(
    modifier = Modifier
      /* look previous comment */
  ) {
    canvasSize = size

    /* drawing elements */
  }
}
SmartToolFactory commented 3 months ago

you can use PointerInputScope function for this as

Modifier.pointerInput(Unit){
    val size = size
    detectMotionEvents(
        onDown = {

        },
        onMove = {

        },
        onUp = {

        }
    )
}