Skip to content

Instantly share code, notes, and snippets.

@PiotrPrus
Created December 15, 2021 20:21
@Composable
fun Modifier.tapOrPress(
onStart: (offsetX: Float) -> Unit,
onCancel: (offsetX: Float) -> Unit,
onCompleted: (offsetX: Float) -> Unit
): Modifier {
val interactionSource = remember { MutableInteractionSource() }
return this.pointerInput(interactionSource) {
forEachGesture {
coroutineScope {
awaitPointerEventScope {
val tap = awaitFirstDown().also { it.consumeDownChange() }
onStart(tap.position.x)
val up = waitForUpOrCancellation()
if (up == null) {
onCancel(tap.position.x)
} else {
up.consumeDownChange()
onCompleted(tap.position.x)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment