fix selection tracking

This commit is contained in:
Evgeny @ SimpleX Chat
2026-03-31 16:52:22 +00:00
parent 7b78d68551
commit 29743b030c
2 changed files with 11 additions and 5 deletions
@@ -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()
@@ -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)
}
}
}