Skip to content

Instantly share code, notes, and snippets.

@PiotrPrus
Created December 15, 2021 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PiotrPrus/c212ae5722317234beafdc6d10a6d5ca to your computer and use it in GitHub Desktop.
Save PiotrPrus/c212ae5722317234beafdc6d10a6d5ca to your computer and use it in GitHub Desktop.
@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