Skip to content

Commit 74a561c

Browse files
committed
Fix a crash on invalid cursor position
1 parent 951b9e0 commit 74a561c

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

  • ComposeTextEditor/src/commonMain/kotlin/com/darkrockstudios/texteditor/cursor

ComposeTextEditor/src/commonMain/kotlin/com/darkrockstudios/texteditor/cursor/CursorExt.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ fun TextEditorState.calculateCursorPosition(): CursorMetrics {
1414
val layout = currentWrappedLine.textLayoutResult
1515
val virtualLineIndex = currentWrappedLine.virtualLineIndex
1616

17-
val cursorX = layout.getHorizontalPosition(charIndex, usePrimaryDirection = true)
17+
// Clamp charIndex to valid range to handle race conditions when text changes asynchronously
18+
val textLength = layout.layoutInput.text.length
19+
val safeCharIndex = charIndex.coerceIn(0, textLength)
20+
21+
val cursorX = layout.getHorizontalPosition(safeCharIndex, usePrimaryDirection = true)
1822
val cursorY = currentWrappedLine.offset.y - scrollState.value
1923
val lineHeight = layout.multiParagraph.getLineHeight(virtualLineIndex)
2024

0 commit comments

Comments
 (0)