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 ecfd790d36..7a71e05ecf 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 @@ -27,16 +27,13 @@ import androidx.compose.ui.platform.LocalViewConfiguration import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.unit.dp import chat.simplex.common.model.* -import chat.simplex.common.platform.Log -import chat.simplex.common.platform.appPlatform +import chat.simplex.common.platform.* import chat.simplex.common.views.chat.item.displayText import chat.simplex.common.views.helpers.generalGetString import chat.simplex.res.MR import dev.icerock.moko.resources.compose.painterResource import kotlinx.coroutines.* -private const val TAG_SEL = "TextSelection" - val SelectionHighlightColor = Color(0x4D0066FF) data class ItemContext( @@ -65,7 +62,7 @@ class SelectionManager { var focusWindowX by mutableStateOf(0f) fun startSelection(startIndex: Int) { - range = SelectionRange(startIndex, 0, startIndex, 0) + range = SelectionRange(startIndex, -1, startIndex, -1) selectionState = SelectionState.Selecting } @@ -123,13 +120,15 @@ fun highlightedRange(range: SelectionRange?, index: Int): IntRange? { if (index < lo || index > hi) return null return when { index == r.startIndex && index == r.endIndex -> - if (r.startOffset == r.endOffset) null + if (r.startOffset < 0 || r.endOffset < 0 || r.startOffset == r.endOffset) null else minOf(r.startOffset, r.endOffset) until maxOf(r.startOffset, r.endOffset) index == r.startIndex -> - if (r.startIndex > r.endIndex) r.startOffset until Int.MAX_VALUE + if (r.startOffset < 0) null + else if (r.startIndex > r.endIndex) r.startOffset until Int.MAX_VALUE else 0 until r.startOffset index == r.endIndex -> - if (r.endIndex < r.startIndex) 0 until r.endOffset + if (r.endOffset < 0) null + else if (r.endIndex < r.startIndex) 0 until r.endOffset else r.endOffset until Int.MAX_VALUE else -> 0 until Int.MAX_VALUE } @@ -232,7 +231,7 @@ fun BoxScope.SelectionHandler( if (!isDragging && totalDrag.getDistance() > touchSlop) { isDragging = true val idx = resolveIndexAtY(listState.value, localStart.y) - Log.d(TAG_SEL, "dragStart localStart=$localStart windowStart=$windowStart idx=$idx") + Log.e(TAG, "dragStart localStart=$localStart windowStart=$windowStart idx=$idx") if (idx != null) { manager.startSelection(idx) manager.focusWindowY = windowStart.y @@ -249,7 +248,7 @@ fun BoxScope.SelectionHandler( val idx = resolveIndexAtY(listState.value, change.position.y) if (idx != null) { - if (idx != manager.range?.endIndex) Log.d(TAG_SEL, "focusIndexChanged idx=$idx range=${manager.range}") + if (idx != manager.range?.endIndex) Log.e(TAG, "focusIndexChanged idx=$idx range=${manager.range}") manager.updateFocusIndex(idx) } @@ -295,7 +294,7 @@ private fun resolveIndexAtY(listState: LazyListState, localY: Float): Int? { val idx = listState.layoutInfo.visibleItemsInfo.find { item -> reversedY >= item.offset && reversedY < item.offset + item.size }?.index - Log.d(TAG_SEL, "resolveIndexAtY localY=$localY reversedY=$reversedY → index=$idx") + Log.e(TAG, "resolveIndexAtY localY=$localY reversedY=$reversedY → index=$idx") return idx } 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 e9cb902a1e..d6e7efd1eb 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 @@ -391,7 +391,7 @@ fun CIMarkdownText( val offset = layout.getOffsetForPosition( Offset(selectionManager.focusWindowX - bounds.left, selectionManager.focusWindowY - bounds.top) ) - Log.d("TextSelection", "anchorOffset idx=$selectionIndex offset=$offset bounds=$bounds pointer=(${selectionManager.focusWindowX},${selectionManager.focusWindowY})") + Log.e(TAG, "anchorOffset idx=$selectionIndex offset=$offset bounds=$bounds pointer=(${selectionManager.focusWindowX},${selectionManager.focusWindowY})") selectionManager.setAnchorOffset(offset) } @@ -405,7 +405,7 @@ fun CIMarkdownText( val bounds = boundsState.value ?: return@collect val layout = layoutResultState.value ?: return@collect val offset = layout.getOffsetForPosition(Offset(px - bounds.left, py - bounds.top)) - Log.d("TextSelection", "focusOffset idx=$selectionIndex offset=$offset bounds=$bounds pointer=($px,$py)") + Log.e(TAG, "focusOffset idx=$selectionIndex offset=$offset bounds=$bounds pointer=($px,$py)") selectionManager.updateFocusOffset(offset) } } @@ -415,7 +415,7 @@ fun CIMarkdownText( val highlightRange = if (selectionManager != null && selectionIndex >= 0) { remember(selectionIndex) { derivedStateOf { highlightedRange(selectionManager.range, selectionIndex) } }.value } else null - if (highlightRange != null) Log.d("TextSelection", "highlight idx=$selectionIndex range=$highlightRange") + if (highlightRange != null) Log.e(TAG, "highlight idx=$selectionIndex range=$highlightRange") Box( Modifier