From 793b3d4b7dad7d15cc6c1a097be6fe1809d15e23 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 28 Nov 2024 21:25:31 -0800 Subject: [PATCH] Split up the file --- .../richeditor/ui/BasicRichTextEditor.kt | 58 +--------------- .../richeditor/ui/handleInteractions.kt | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+), 57 deletions(-) create mode 100644 richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/handleInteractions.kt diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/BasicRichTextEditor.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/BasicRichTextEditor.kt index 814fc735..c17ed5a3 100644 --- a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/BasicRichTextEditor.kt +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/BasicRichTextEditor.kt @@ -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() } - } - } - } - } - } - } - } - } -} \ No newline at end of file +public typealias RichSpanClickListener = (RichSpanStyle, TextRange, Offset, InteractionType) -> Boolean \ No newline at end of file diff --git a/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/handleInteractions.kt b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/handleInteractions.kt new file mode 100644 index 00000000..906e758c --- /dev/null +++ b/richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/handleInteractions.kt @@ -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() } + } + } + } + } + } + } + } + } +} \ No newline at end of file