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

Respect PointerEventPass for onDown #1

Closed diousk closed 1 year ago

diousk commented 1 year ago

the current implementation of onDown() depends on awaitFirstDown(). But by default it uses PointerEventPass.Main internally. In order to interact with PointerEventPass.Initial or PointerEventPass.Final we should use the method awaitFirstDownOnPass() instead inside awaitFirstDown():

suspend fun AwaitPointerEventScope.awaitFirstDown(
    requireUnconsumed: Boolean = true
): PointerInputChange =
    awaitFirstDownOnPass(pass = PointerEventPass.Main, requireUnconsumed = requireUnconsumed)

internal suspend fun AwaitPointerEventScope.awaitFirstDownOnPass(
    pass: PointerEventPass,
    requireUnconsumed: Boolean
): PointerInputChange {
    var event: PointerEvent
    do {
        event = awaitPointerEvent(pass)
    } while (
        !event.changes.fastAll {
            if (requireUnconsumed) it.changedToDown() else it.changedToDownIgnoreConsumed()
        }
    )
    return event.changes[0]
}

and replace awaitFirstDown() with awaitFirstDownOnPass() for onDown() callback.

SmartToolFactory commented 1 year ago

Hey, new awaitFirstDown, with Jetpack Compose 1.4.0-alpha03, takes PointerEventPass, i will update version and add this param as well

suspend fun AwaitPointerEventScope.awaitFirstDown(
    requireUnconsumed: Boolean = true,
    pass: PointerEventPass = PointerEventPass.Main,
): PointerInputChange {
    var event: PointerEvent
    do {
        event = awaitPointerEvent(pass)
    } while (
        !event.changes.fastAll {
            if (requireUnconsumed) it.changedToDown() else it.changedToDownIgnoreConsumed()
        }
    )
    return event.changes[0]
}

May i ask in which situation you need this pass?

diousk commented 1 year ago

I'm just testing around this library and find this behavior is not expected. Some situation might be intercepting Press event at Initial pass from parent composable. Anyway, thanks for the investigation & modification.

SmartToolFactory commented 1 year ago

@diousk current version now adds pass as param to awaitFirstDown and awaitPointerEvent functions. Yes, i had an issue where i needed parent below a disabled button to take action when disabled click and used Initial pass. Now, these gestures have same compatibility for any potential usecases