-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5505a9c
commit 793b3d4
Showing
2 changed files
with
69 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...editor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/handleInteractions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.mohamedrejeb.richeditor.ui | ||
|
||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.input.pointer.PointerEventPass | ||
import androidx.compose.ui.input.pointer.PointerEventType | ||
import androidx.compose.ui.input.pointer.PointerType | ||
import androidx.compose.ui.input.pointer.isPrimaryPressed | ||
import androidx.compose.ui.input.pointer.isSecondaryPressed | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
|
||
public enum class InteractionType { PrimaryClick, SecondaryClick, Tap, DoubleTap } | ||
|
||
/** | ||
* Provide a unified callback for listening for different types of interactions | ||
*/ | ||
internal fun Modifier.handleInteractions( | ||
enabled: Boolean = true, | ||
onInteraction: ((InteractionType, Offset) -> Boolean)? = null | ||
): Modifier = composed { | ||
this | ||
.pointerInput(enabled) { | ||
awaitPointerEventScope { | ||
while (true) { | ||
val event = awaitPointerEvent(PointerEventPass.Main) | ||
if (!enabled) continue | ||
|
||
if (event.type == PointerEventType.Press) { | ||
val position = event.changes.first().position | ||
|
||
when (event.changes.first().type) { | ||
PointerType.Touch -> { | ||
onInteraction?.invoke( | ||
InteractionType.Tap, | ||
event.changes.first().position | ||
) | ||
} | ||
|
||
PointerType.Mouse -> { | ||
if (event.buttons.isPrimaryPressed) { | ||
val consumed = | ||
onInteraction?.invoke( | ||
InteractionType.PrimaryClick, | ||
position | ||
) | ||
?: false | ||
if (consumed) { | ||
event.changes.forEach { it.consume() } | ||
} | ||
} else if (event.buttons.isSecondaryPressed) { | ||
val consumed = | ||
onInteraction?.invoke( | ||
InteractionType.SecondaryClick, | ||
position | ||
) | ||
?: false | ||
if (consumed) { | ||
event.changes.forEach { it.consume() } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |