Skip to content

Commit

Permalink
Split up the file
Browse files Browse the repository at this point in the history
  • Loading branch information
Wavesonics committed Nov 29, 2024
1 parent 5505a9c commit 793b3d4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,60 +342,4 @@ internal suspend fun adjustTextIndicatorOffset(
)
}

public enum class InteractionType { PrimaryClick, SecondaryClick, Tap, DoubleTap }
public typealias RichSpanClickListener = (RichSpanStyle, TextRange, Offset, InteractionType) -> Boolean

/**
* Provide a unified callback for listening for different types of interactions
*/
private 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() }
}
}
}
}
}
}
}
}
}
public typealias RichSpanClickListener = (RichSpanStyle, TextRange, Offset, InteractionType) -> Boolean
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() }
}
}
}
}
}
}
}
}
}

0 comments on commit 793b3d4

Please sign in to comment.