Compose-Extended-Gestures icon indicating copy to clipboard operation
Compose-Extended-Gestures copied to clipboard

Reduce Boilerplate in pointerMotionEvent

Open cybercoder-naj opened this issue 1 year ago • 2 comments

Every time I use the callback function, I need to assign the motionEvent enum type to the function. Is there a way to reduce this boilerplate code?

Currently

@Composable
fun Component() {
  var motionEvent by remember { mutableStateOf(MotionEvent.Idle) } 
  Canvas(
    modifier = Modifier
      .clipToBounds()
      .pointerMotionEvents(
        onDown = { motionEvent = MotionEvent.Down; /* more code */},
        onMove = { motionEvent = MotionEvent.Move; /* more code */},
        /* etc */
      )
  ) { /* Drawing the elements */}
}

Proposal

@Composable
fun Component() {
  val motionEvent = rememberMotionEvent() 
  Canvas(
    modifier = Modifier
      .clipToBounds()
      .pointerMotionEvents(motionEvent) {
        onDown = {/* no need to manually set motionEvent */}
        onMove = {/* no need to manually set motionEvent */}
        /* etc */
      }
  ) { /* Drawing the elements */}
}

cybercoder-naj avatar Apr 19 '24 16:04 cybercoder-naj

I can add option to assign a State like LazyColumn does. That why you can get touch state via it. If you have any suggestion feel free to post it

SmartToolFactory avatar Jun 18 '24 18:06 SmartToolFactory

Sure! I'll experiment with it soon and see what we can do.

cybercoder-naj avatar Jun 18 '24 20:06 cybercoder-naj