From 29743b030cb2412172b75e39cd14b95d6a800875 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Tue, 31 Mar 2026 16:52:22 +0000 Subject: [PATCH] fix selection tracking --- .../chat/simplex/common/views/chat/TextSelection.kt | 8 +++++++- .../chat/simplex/common/views/chat/item/TextItemView.kt | 8 ++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt index 0103ebc70f..1c8034bf04 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/TextSelection.kt @@ -28,6 +28,7 @@ import androidx.compose.ui.platform.LocalViewConfiguration import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.TextLayoutResult import androidx.compose.ui.unit.dp +import chat.simplex.common.platform.Log import chat.simplex.common.platform.appPlatform import chat.simplex.common.views.helpers.generalGetString import chat.simplex.res.MR @@ -85,7 +86,9 @@ class SelectionManager { fun register(participant: SelectionParticipant) { participants.add(participant) - coords?.let { recomputeParticipant(participant, it) } + if (selectionState == SelectionState.Selecting) { + coords?.let { recomputeParticipant(participant, it) } + } } fun unregister(participant: SelectionParticipant) { @@ -141,7 +144,9 @@ class SelectionManager { private fun recomputeParticipant(participant: SelectionParticipant, coords: SelectionCoords) { val bounds = participant.getYBounds() ?: return + Log.d("TextSelection", "recompute item=${participant.itemId} bounds=${bounds.start}..${bounds.endInclusive} coords.topY=${coords.topY} coords.bottomY=${coords.bottomY}") val highlightRange = participant.calculateHighlightRange(coords) ?: return + Log.d("TextSelection", " highlightRange=$highlightRange selectableEnd=${participant.getSelectableEnd()}") val selectableEnd = participant.getSelectableEnd() val clampedStart = highlightRange.first.coerceIn(0, selectableEnd) val clampedEnd = highlightRange.last.coerceIn(0, selectableEnd) @@ -307,6 +312,7 @@ fun BoxScope.SelectionHandler( if (!isDragging && totalDrag.getDistance() > touchSlop) { isDragging = true + Log.d("TextSelection", "startSelection: localStart=$localStart posInWindow=$positionInWindow windowStart=$windowStart") manager.startSelection(windowStart.y, windowStart.x) // → Selecting try { focusRequester.requestFocus() } catch (_: Exception) {} change.consume() diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt index f3d3ec4c3e..5337d3bc7f 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/TextItemView.kt @@ -373,8 +373,8 @@ fun ClickableText( val selectionHighlight = if (selectionRange != null) { Modifier.drawBehind { layoutResult.value?.let { result -> - if (selectionRange.first < selectionRange.last && selectionRange.last <= text.length) { - drawPath(result.getPathForRange(selectionRange.first, selectionRange.last), SelectionHighlightColor) + if (selectionRange.first < selectionRange.last && selectionRange.last + 1 <= text.length) { + drawPath(result.getPathForRange(selectionRange.first, selectionRange.last + 1), SelectionHighlightColor) } } } @@ -408,8 +408,8 @@ private fun SelectableText( val highlight = if (selectionRange != null) { Modifier.drawBehind { layoutResult.value?.let { result -> - if (selectionRange.first < selectionRange.last && selectionRange.last <= text.length) { - drawPath(result.getPathForRange(selectionRange.first, selectionRange.last), SelectionHighlightColor) + if (selectionRange.first < selectionRange.last && selectionRange.last + 1 <= text.length) { + drawPath(result.getPathForRange(selectionRange.first, selectionRange.last + 1), SelectionHighlightColor) } } }