Take mouse.y into account when computing the selection endpoint

This commit is contained in:
Fingercomp 2025-09-03 01:05:00 +03:00
parent ca8ef6eee1
commit 68350f8d62
No known key found for this signature in database
GPG Key ID: BBC71CEE45D86E37

View File

@ -181,8 +181,15 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
selectWord() selectWord()
case DragEvent(DragEvent.State.Start | DragEvent.State.Drag, MouseEvent.Button.Left, mouse) if isFocused => case DragEvent(DragEvent.State.Start | DragEvent.State.Drag, MouseEvent.Button.Left, mouse) if isFocused =>
val pos = pixelToCursorPosition(mouse.x - bounds.x) val pos = if (mouse.y < bounds.y) {
selection = Selection(selection.fold(prevCursorPosition.value)(_.start), pos) 0
} else if (mouse.y > bounds.max.y) {
_text.chars.length
} else {
pixelToCursorPosition(mouse.x - bounds.x)
}
extendSelection(pos, prevCursorPosition.value)
cursor.position = pos cursor.position = pos
case event @ KeyEvent(KeyEvent.State.Press | KeyEvent.State.Repeat, Keyboard.KEY_LEFT, _) if isFocused => case event @ KeyEvent(KeyEvent.State.Press | KeyEvent.State.Repeat, Keyboard.KEY_LEFT, _) if isFocused =>
@ -290,12 +297,16 @@ class TextInput(val initialText: String = "") extends Widget with MouseHandler w
} }
pos pos
} }
private def extendSelection(position: Int, cursorPosition: Int = cursor.position): Unit = {
selection = Selection(selection.fold(cursorPosition)(_.start), position)
}
private def handleKeyMovement(position: Int): Unit = { private def handleKeyMovement(position: Int): Unit = {
selection = if (KeyEvents.isShiftDown) { if (KeyEvents.isShiftDown) {
Selection(selection.fold(cursor.position)(_.start), position) extendSelection(position)
} else { } else {
None selection = None
} }
cursor.position = position cursor.position = position