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
83 stars 7 forks source link

Reduce Boilerplate in pointerMotionEvent #5

Open cybercoder-naj opened 2 months ago

cybercoder-naj commented 2 months ago

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 */}
}
SmartToolFactory commented 2 weeks ago

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

cybercoder-naj commented 2 weeks ago

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