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 a29ab2701a..b56c91a51b 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 @@ -100,26 +100,19 @@ class SelectionManager { val r = range ?: return "" val lo = minOf(r.startIndex, r.endIndex) val hi = maxOf(r.startIndex, r.endIndex) - val forward = r.startIndex <= r.endIndex - val startOff = if (forward) r.startOffset else r.endOffset - val endOff = if (forward) r.endOffset else r.startOffset return (lo..hi).mapNotNull { idx -> val ci = items.getOrNull(idx)?.newest()?.item ?: return@mapNotNull null + val sel = selectedRange(range, idx) ?: return@mapNotNull null val text = displayText(ci, linkMode, sendCommandMsg = false) - when { - idx == lo && idx == hi -> text.substring( - startOff.coerceAtMost(text.length), - endOff.coerceAtMost(text.length) - ) - idx == lo -> text.substring(startOff.coerceAtMost(text.length)) - idx == hi -> text.substring(0, endOff.coerceAtMost(text.length)) - else -> text - } + text.substring( + sel.first.coerceAtMost(text.length), + (sel.last + 1).coerceAtMost(text.length) + ) }.joinToString("\n") } } -fun highlightedRange(range: SelectionRange?, index: Int): IntRange? { +fun selectedRange(range: SelectionRange?, index: Int): IntRange? { val r = range ?: return null val lo = minOf(r.startIndex, r.endIndex) val hi = maxOf(r.startIndex, r.endIndex) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/EmojiItemView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/EmojiItemView.kt index 909e412e69..17b2a53eac 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/EmojiItemView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/EmojiItemView.kt @@ -47,7 +47,7 @@ fun EmojiItemView(chatItem: ChatItem, timedMessagesTTL: Int?, showViaProxy: Bool } val isSelected = if (selectionManager != null && selectionIndex >= 0) { - remember(selectionIndex) { derivedStateOf { highlightedRange(selectionManager.range, selectionIndex) != null } }.value + remember(selectionIndex) { derivedStateOf { selectedRange(selectionManager.range, selectionIndex) != null } }.value } else false Column( diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt index 3c4d421a71..ab72624a3a 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt @@ -413,7 +413,7 @@ fun CIMarkdownText( } val highlightRange = if (selectionManager != null && selectionIndex >= 0) { - remember(selectionIndex) { derivedStateOf { highlightedRange(selectionManager.range, selectionIndex) } }.value + remember(selectionIndex) { derivedStateOf { selectedRange(selectionManager.range, selectionIndex) } }.value } else null if (highlightRange != null) Log.e(TAG, "highlight idx=$selectionIndex range=$highlightRange")