Preserve selection when focusing SpinBox

This commit is contained in:
kobewi 2023-06-10 21:29:24 +02:00
parent 37d1dfef9d
commit 968c5f6247
3 changed files with 18 additions and 1 deletions

View File

@ -1527,6 +1527,22 @@ void LineEdit::set_text(String p_text) {
scroll_offset = 0.0; scroll_offset = 0.0;
} }
void LineEdit::set_text_with_selection(const String &p_text) {
Selection selection_copy = selection;
clear_internal();
insert_text_at_caret(p_text);
_create_undo_state();
int tlen = text.length();
selection = selection_copy;
selection.begin = MIN(selection.begin, tlen);
selection.end = MIN(selection.end, tlen);
selection.start_column = MIN(selection.start_column, tlen);
queue_redraw();
}
void LineEdit::set_text_direction(Control::TextDirection p_text_direction) { void LineEdit::set_text_direction(Control::TextDirection p_text_direction) {
ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
if (text_direction != p_text_direction) { if (text_direction != p_text_direction) {

View File

@ -283,6 +283,7 @@ public:
void set_text(String p_text); void set_text(String p_text);
String get_text() const; String get_text() const;
void set_text_with_selection(const String &p_text); // Set text, while preserving selection.
void set_text_direction(TextDirection p_text_direction); void set_text_direction(TextDirection p_text_direction);
TextDirection get_text_direction() const; TextDirection get_text_direction() const;

View File

@ -54,7 +54,7 @@ void SpinBox::_update_text() {
} }
} }
line_edit->set_text(value); line_edit->set_text_with_selection(value);
} }
void SpinBox::_text_submitted(const String &p_string) { void SpinBox::_text_submitted(const String &p_string) {