Bläddra i källkod

Merge pull request #78092 from KoBeWi/must_protecc_selection

Preserve selection when focusing SpinBox
Rémi Verschelde 2 år sedan
förälder
incheckning
b02dd1c2a3
3 ändrade filer med 18 tillägg och 1 borttagningar
  1. 16 0
      scene/gui/line_edit.cpp
  2. 1 0
      scene/gui/line_edit.h
  3. 1 1
      scene/gui/spin_box.cpp

+ 16 - 0
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) {

+ 1 - 0
scene/gui/line_edit.h

@@ -283,6 +283,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;

+ 1 - 1
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) {