fix selection reset on end reached with shift pressed

This commit is contained in:
MihailRis 2026-05-03 18:12:59 +03:00
parent 47f2bf75d7
commit 1d4abd7758
2 changed files with 13 additions and 32 deletions

View file

@ -755,15 +755,16 @@ void TextBox::resetMaxLocalCaret() {
caret - label->getTextLineOffset(label->getLineByTextIndex(caret)); caret - label->getTextLineOffset(label->getLineByTextIndex(caret));
} }
void TextBox::stepLeft(bool shiftPressed, bool breakSelection) { void TextBox::stepCaret(bool shiftPressed, bool breakSelection, bool right) {
uint previousCaret = this->caret; uint previousCaret = this->caret;
size_t caret = breakSelection ? selectionStart : this->caret; size_t caret =
if (caret > 0) { breakSelection ? (right ? selectionEnd : selectionStart) : this->caret;
if (caret > input.length()) { if (right ? (caret < input.length()) : (caret > 0)) {
setCaret(input.length() - 1); setCaret(
} else { right ? (caret + 1)
setCaret(caret - 1); : ((caret > input.length()) ? (input.length() - 1)
} : (caret - 1))
);
if (shiftPressed) { if (shiftPressed) {
if (selectionStart == selectionEnd) { if (selectionStart == selectionEnd) {
selectionOrigin = previousCaret; selectionOrigin = previousCaret;
@ -774,28 +775,9 @@ void TextBox::stepLeft(bool shiftPressed, bool breakSelection) {
} }
} else { } else {
setCaret(caret); setCaret(caret);
resetSelection(); if (!shiftPressed) {
}
resetMaxLocalCaret();
}
void TextBox::stepRight(bool shiftPressed, bool breakSelection) {
uint previousCaret = this->caret;
size_t caret = breakSelection ? selectionEnd : this->caret;
if (caret < input.length()) {
setCaret(caret + 1);
caretLastMove = gui.getWindow().time();
if (shiftPressed) {
if (selectionStart == selectionEnd) {
selectionOrigin = previousCaret;
}
extendSelection(this->caret);
} else {
resetSelection(); resetSelection();
} }
} else {
setCaret(caret);
resetSelection();
} }
resetMaxLocalCaret(); resetMaxLocalCaret();
} }
@ -983,9 +965,9 @@ void TextBox::performEditingKeyboardEvents(Keycode key) {
} else if (key == Keycode::TAB) { } else if (key == Keycode::TAB) {
onTab(shiftPressed); onTab(shiftPressed);
} else if (key == Keycode::LEFT) { } else if (key == Keycode::LEFT) {
stepLeft(shiftPressed, breakSelection); stepCaret(shiftPressed, breakSelection, false);
} else if (key == Keycode::RIGHT) { } else if (key == Keycode::RIGHT) {
stepRight(shiftPressed, breakSelection); stepCaret(shiftPressed, breakSelection, true);
} else if (key == Keycode::UP && onUpPressed) { } else if (key == Keycode::UP && onUpPressed) {
onUpPressed(); onUpPressed();
} else if (key == Keycode::DOWN && onDownPressed) { } else if (key == Keycode::DOWN && onDownPressed) {

View file

@ -66,8 +66,7 @@ namespace gui {
std::string markup; std::string markup;
std::string syntax; std::string syntax;
void stepLeft(bool shiftPressed, bool breakSelection); void stepCaret(bool shiftPressed, bool breakSelection, bool right);
void stepRight(bool shiftPressed, bool breakSelection);
void stepDefaultDown(bool shiftPressed, bool breakSelection); void stepDefaultDown(bool shiftPressed, bool breakSelection);
void stepDefaultUp(bool shiftPressed, bool breakSelection); void stepDefaultUp(bool shiftPressed, bool breakSelection);