Skip to content

Commit b62d67e

Browse files
committed
Fixed TextEditor's highlighting of the current line to cover full width
1 parent ff94314 commit b62d67e

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

view/editor/InputTarget.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ internal class InputTarget constructor(
138138
private var mayDragSelectByLine: Boolean by mutableStateOf(false)
139139
private var mayDragSelectByLineNumber: Boolean by mutableStateOf(false)
140140
private var selectionDragStart: Selection? by mutableStateOf(null)
141-
private var textAreaRect: Rect by mutableStateOf(Rect.Zero)
142-
private val lineNumberBorder: Float get() = textAreaRect.left - horPadding.value
141+
private var textAreaBounds: Rect by mutableStateOf(Rect.Zero)
142+
private val lineNumberBorder: Float get() = textAreaBounds.left - horPadding.value
143143
private val lineCount: Int get() = content.size
144144
private val end: Cursor get() = Cursor(content.size - 1, content.last().length)
145145
private val coroutineScope = CoroutineScope(EmptyCoroutineContext)
@@ -153,18 +153,19 @@ internal class InputTarget constructor(
153153
textWidth = 0.dp
154154
}
155155

156-
internal fun updateTextArea(rawRectangle: Rect) {
157-
textAreaRect = Rect(
156+
internal fun updateBounds(rawRectangle: Rect) {
157+
textAreaBounds = Rect(
158158
left = toDP(rawRectangle.left, density).value + horPadding.value,
159159
top = toDP(rawRectangle.top, density).value,
160160
right = toDP(rawRectangle.right, density).value - horPadding.value,
161161
bottom = toDP(rawRectangle.bottom, density).value
162162
)
163+
println("InputTarget.textAreaRect: $textAreaBounds")
163164
}
164165

165166
private fun createCursor(x: Int, y: Int): Cursor {
166-
val relX = x - textAreaRect.left + toDP(horScroller.value, density).value
167-
val relY = y - textAreaRect.top + verScroller.offset.value
167+
val relX = x - textAreaBounds.left + toDP(horScroller.value, density).value
168+
val relY = y - textAreaBounds.top + verScroller.offset.value
168169
val row = floor(relY / lineHeight.value).toInt().coerceIn(0, lineCount - 1)
169170
val offsetInLine = Offset(relX * density, (relY - (row * lineHeight.value)) * density)
170171
val col = rendering.get(row)?.getOffsetForPosition(offsetInLine) ?: 0
@@ -217,7 +218,7 @@ internal class InputTarget constructor(
217218
}
218219

219220
internal fun mayUpdateCursor(x: Int, y: Int, isSelecting: Boolean) {
220-
if (x <= textAreaRect.right) {
221+
if (x <= textAreaBounds.right) {
221222
updateCursor(createCursor(x, y), isSelecting, false)
222223
if (x > lineNumberBorder) mayDragSelectByChar = true
223224
else mayDragSelectByLineNumber = true
@@ -244,10 +245,10 @@ internal class InputTarget constructor(
244245

245246
private fun mayScrollToCursor() {
246247
fun mayScrollToCoordinate(x: Int, y: Int, padding: Int = 0) {
247-
val left = textAreaRect.left.toInt() + padding
248-
val right = textAreaRect.right.toInt() - padding
249-
val top = textAreaRect.top.toInt() + padding
250-
val bottom = textAreaRect.bottom.toInt() - padding
248+
val left = textAreaBounds.left.toInt() + padding
249+
val right = textAreaBounds.right.toInt() - padding
250+
val top = textAreaBounds.top.toInt() + padding
251+
val bottom = textAreaBounds.bottom.toInt() - padding
251252
if (x < left) coroutineScope.launch {
252253
horScroller.scrollTo(horScroller.value + ((x - left) * density).toInt())
253254
} else if (x > right) coroutineScope.launch {
@@ -260,8 +261,8 @@ internal class InputTarget constructor(
260261
val cursorRect = rendering.get(cursor.row)?.let {
261262
it.getCursorRect(cursor.col.coerceAtMost(it.getLineEnd(0)))
262263
} ?: Rect(0f, 0f, 0f, 0f)
263-
val x = textAreaRect.left + toDP(cursorRect.left - horScroller.value, density).value
264-
val y = textAreaRect.top + (lineHeight.value * (cursor.row + 0.5f)) - verScroller.offset.value
264+
val x = textAreaBounds.left + toDP(cursorRect.left - horScroller.value, density).value
265+
val y = textAreaBounds.top + (lineHeight.value * (cursor.row + 0.5f)) - verScroller.offset.value
265266
mayScrollToCoordinate(x.toInt(), y.toInt(), lineHeight.value.toInt() * 2)
266267
}
267268

@@ -388,14 +389,14 @@ internal class InputTarget constructor(
388389
}
389390

390391
internal fun moveCursorUpByPage(isSelecting: Boolean = false) {
391-
val fullyVisibleLines = floor(textAreaRect.height / lineHeight.value).toInt()
392+
val fullyVisibleLines = floor(textAreaBounds.height / lineHeight.value).toInt()
392393
val newRow = (cursor.row - fullyVisibleLines).coerceAtLeast(0)
393394
val newCol = cursor.col.coerceAtMost(content[newRow].length)
394395
updateCursor(Cursor(newRow, newCol), isSelecting)
395396
}
396397

397398
internal fun moveCursorDownByPage(isSelecting: Boolean = false) {
398-
val fullyVisibleLines = floor(textAreaRect.height / lineHeight.value).toInt()
399+
val fullyVisibleLines = floor(textAreaBounds.height / lineHeight.value).toInt()
399400
val newRow = (cursor.row + fullyVisibleLines).coerceAtMost(content.size - 1)
400401
val newCol = cursor.col.coerceAtMost(content[newRow].length)
401402
updateCursor(Cursor(newRow, newCol), isSelecting)

view/editor/TextEditor.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package com.vaticle.typedb.studio.view.editor
2020

2121
import androidx.compose.foundation.background
22+
import androidx.compose.foundation.border
2223
import androidx.compose.foundation.focusable
2324
import androidx.compose.foundation.horizontalScroll
2425
import androidx.compose.foundation.layout.Box
@@ -345,7 +346,7 @@ object TextEditor {
345346
val lazyColumnState = LazyLines.createState(state.content, state.target.verScroller)
346347
Box(modifier = Modifier.onGloballyPositioned {
347348
state.textAreaWidth = toDP(it.size.width, state.density)
348-
state.target.updateTextArea(it.boundsInWindow())
349+
state.target.updateBounds(it.boundsInWindow())
349350
}) {
350351
Box(
351352
modifier = Modifier.fillMaxSize()
@@ -371,15 +372,16 @@ object TextEditor {
371372
) {
372373
val cursor = state.target.cursor
373374
val selection = state.target.selection
375+
val minWidth = (state.target.textWidth + RIGHT_PADDING + AREA_PADDING_HOR * 2)
376+
.coerceIn(state.textAreaWidth, MAX_LINE_MIN_WIDTH)
374377
val bgColor = when {
375378
showLine && cursor.row == index && selection == null -> Theme.studio.primary
376379
else -> Theme.studio.background0
377380
}
378381
Box(
379382
contentAlignment = Alignment.TopStart,
380383
modifier = Modifier.background(bgColor)
381-
.defaultMinSize(minWidth = state.target.textWidth.coerceIn(state.textAreaWidth, MAX_LINE_MIN_WIDTH))
382-
.height(state.lineHeight)
384+
.defaultMinSize(minWidth = minWidth).height(state.lineHeight)
383385
.padding(horizontal = AREA_PADDING_HOR)
384386
) {
385387
val isRenderedUpToDate = state.rendering.hasVersion(index, state.processor.version)

0 commit comments

Comments
 (0)