Merge pull request #87341 from KoBeWi/must_prot3.x_selection

[3.x] Preserve selection when focusing SpinBox
This commit is contained in:
Rémi Verschelde 2024-01-18 21:23:23 +01:00 committed by GitHub
commit c6fb284441
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 1 deletions

View file

@ -1400,6 +1400,26 @@ void LineEdit::set_text(String p_text) {
scroll_offset = 0; scroll_offset = 0;
} }
void LineEdit::set_text_with_selection(const String &p_text) {
Selection selection_copy = selection;
clear_internal();
append_at_cursor(p_text);
_create_undo_state();
if (expand_to_text_length) {
minimum_size_changed();
}
int tlen = text.length();
selection = selection_copy;
selection.begin = MIN(selection.begin, tlen);
selection.end = MIN(selection.end, tlen);
selection.cursor_start = MIN(selection.cursor_start, tlen);
update();
}
void LineEdit::clear() { void LineEdit::clear() {
clear_internal(); clear_internal();
_text_changed(); _text_changed();

View file

@ -208,6 +208,7 @@ public:
void delete_char(); void delete_char();
void delete_text(int p_from_column, int p_to_column); void delete_text(int p_from_column, int p_to_column);
void set_text(String p_text); void set_text(String p_text);
void set_text_with_selection(const String &p_text); // Set text, while preserving selection.
String get_text() const; String get_text() const;
void set_placeholder(String p_text); void set_placeholder(String p_text);
String get_placeholder() const; String get_placeholder() const;

View file

@ -50,7 +50,7 @@ void SpinBox::_value_changed(double) {
} }
} }
line_edit->set_text(value); line_edit->set_text_with_selection(value);
} }
void SpinBox::_text_entered(const String &p_string) { void SpinBox::_text_entered(const String &p_string) {