Skip to content

Commit

Permalink
Fixed Touch VS Mouse input
Browse files Browse the repository at this point in the history
  • Loading branch information
Wavesonics committed Nov 28, 2024
1 parent d471a4a commit 3e26ba2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
1 change: 0 additions & 1 deletion richeditor-compose/api/android/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public final class com/mohamedrejeb/richeditor/ui/ComposableSingletons$BasicRich
public final class com/mohamedrejeb/richeditor/ui/InteractionType : java/lang/Enum {
public static final field DoubleTap Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field PrimaryClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field PrimaryDoubleClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field SecondaryClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field Tap Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
Expand Down
1 change: 0 additions & 1 deletion richeditor-compose/api/desktop/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public final class com/mohamedrejeb/richeditor/ui/ComposableSingletons$BasicRich
public final class com/mohamedrejeb/richeditor/ui/InteractionType : java/lang/Enum {
public static final field DoubleTap Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field PrimaryClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field PrimaryDoubleClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field SecondaryClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field Tap Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
Expand Down
1 change: 0 additions & 1 deletion richeditor-compose/api/richeditor-compose.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ open annotation class com.mohamedrejeb.richeditor.annotation/InternalRichTextApi
final enum class com.mohamedrejeb.richeditor.ui/InteractionType : kotlin/Enum<com.mohamedrejeb.richeditor.ui/InteractionType> { // com.mohamedrejeb.richeditor.ui/InteractionType|null[0]
enum entry DoubleTap // com.mohamedrejeb.richeditor.ui/InteractionType.DoubleTap|null[0]
enum entry PrimaryClick // com.mohamedrejeb.richeditor.ui/InteractionType.PrimaryClick|null[0]
enum entry PrimaryDoubleClick // com.mohamedrejeb.richeditor.ui/InteractionType.PrimaryDoubleClick|null[0]
enum entry SecondaryClick // com.mohamedrejeb.richeditor.ui/InteractionType.SecondaryClick|null[0]
enum entry Tap // com.mohamedrejeb.richeditor.ui/InteractionType.Tap|null[0]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mohamedrejeb.richeditor.ui

import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
Expand All @@ -20,7 +19,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.isPressed
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
Expand All @@ -35,9 +34,6 @@ import androidx.compose.ui.unit.LayoutDirection
import com.mohamedrejeb.richeditor.model.RichSpanStyle
import com.mohamedrejeb.richeditor.model.RichTextState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
import kotlin.time.Duration.Companion.milliseconds


/**
Expand Down Expand Up @@ -359,43 +355,48 @@ private fun Modifier.handleInteractions(
): Modifier = composed {
this
.pointerInput(enabled) {
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent(PointerEventPass.Main)
if (!enabled) continue
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent(PointerEventPass.Main)
if (!enabled) continue

if (event.type == PointerEventType.Press) {
val position = event.changes.first().position
if (event.type == PointerEventType.Press) {
val position = event.changes.first().position

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() }
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() }
}
}
}
}
}
}
}
}
}
// Handle touch interactions
.pointerInput(enabled) {
detectTapGestures(
onTap = { offset ->
if (enabled) {
onInteraction?.invoke(InteractionType.Tap, offset)
}
},
onDoubleTap = { offset ->
if (enabled) {
onInteraction?.invoke(InteractionType.DoubleTap, offset)
}
}
)
}
}
}

0 comments on commit 3e26ba2

Please sign in to comment.