From 39f9c8cd29b11a9f13def38d4837878ef8a4549f Mon Sep 17 00:00:00 2001 From: kobewi Date: Sat, 10 Jun 2023 21:29:24 +0200 Subject: [PATCH] Preserve selection when focusing SpinBox (cherry picked from commit 968c5f6247b51691474bd1cf0cc9d0a34735a6f0) --- scene/gui/line_edit.cpp | 16 ++++++++++++++++ scene/gui/line_edit.h | 1 + scene/gui/spin_box.cpp | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 17cc81c8d34..472a1bdc6ba 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -1527,6 +1527,22 @@ void LineEdit::set_text(String p_text) { 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) { ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); if (text_direction != p_text_direction) { diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index 81c506069ac..954e565793f 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -282,6 +282,7 @@ public: void set_text(String p_text); 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); TextDirection get_text_direction() const; diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index e4c7be33f0e..f813c6d6608 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -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) {