-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'handle-link-click-in-editor' into spell-check-test
# Conflicts: # richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/BasicRichTextEditor.kt
- Loading branch information
Showing
7 changed files
with
69 additions
and
67 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
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
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
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
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
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
69 changes: 69 additions & 0 deletions
69
...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,69 @@ | ||
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 eventChange = event.changes.first() | ||
val position = eventChange.position | ||
|
||
when (eventChange.type) { | ||
PointerType.Touch -> { | ||
onInteraction?.invoke( | ||
InteractionType.Tap, | ||
eventChange.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() } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |