Browse Source

Expose `LineEdit` `edit` and `unedit` methods.

Mounir Tohami 10 months ago
parent
commit
f84f734696
5 changed files with 66 additions and 28 deletions
  1. 14 1
      doc/classes/LineEdit.xml
  2. 2 0
      editor/code_editor.cpp
  3. 15 1
      scene/gui/color_picker.cpp
  4. 33 23
      scene/gui/line_edit.cpp
  5. 2 3
      scene/gui/line_edit.h

+ 14 - 1
doc/classes/LineEdit.xml

@@ -8,7 +8,7 @@
 		- When the [LineEdit] control is focused using the keyboard arrow keys, it will only gain focus and not enter edit mode.
 		- When the [LineEdit] control is focused using the keyboard arrow keys, it will only gain focus and not enter edit mode.
 		- To enter edit mode, click on the control with the mouse or press the [code]ui_text_submit[/code] action (by default [kbd]Enter[/kbd] or [kbd]Kp Enter[/kbd]).
 		- To enter edit mode, click on the control with the mouse or press the [code]ui_text_submit[/code] action (by default [kbd]Enter[/kbd] or [kbd]Kp Enter[/kbd]).
 		- To exit edit mode, press [code]ui_text_submit[/code] or [code]ui_cancel[/code] (by default [kbd]Escape[/kbd]) actions.
 		- To exit edit mode, press [code]ui_text_submit[/code] or [code]ui_cancel[/code] (by default [kbd]Escape[/kbd]) actions.
-		- Check [method is_editing] and [signal editing_toggled] for more information.
+		- Check [method edit], [method unedit], [method is_editing], and [signal editing_toggled] for more information.
 		[b]Important:[/b]
 		[b]Important:[/b]
 		- Focusing the [LineEdit] with [code]ui_focus_next[/code] (by default [kbd]Tab[/kbd]) or [code]ui_focus_prev[/code] (by default [kbd]Shift + Tab[/kbd]) or [method Control.grab_focus] still enters edit mode (for compatibility).
 		- Focusing the [LineEdit] with [code]ui_focus_next[/code] (by default [kbd]Tab[/kbd]) or [code]ui_focus_prev[/code] (by default [kbd]Shift + Tab[/kbd]) or [method Control.grab_focus] still enters edit mode (for compatibility).
 		[LineEdit] features many built-in shortcuts that are always available ([kbd]Ctrl[/kbd] here maps to [kbd]Cmd[/kbd] on macOS):
 		[LineEdit] features many built-in shortcuts that are always available ([kbd]Ctrl[/kbd] here maps to [kbd]Cmd[/kbd] on macOS):
@@ -75,6 +75,13 @@
 				Clears the current selection.
 				Clears the current selection.
 			</description>
 			</description>
 		</method>
 		</method>
+		<method name="edit">
+			<return type="void" />
+			<description>
+				Allows entering edit mode whether the [LineEdit] is focused or not.
+				Use [method Callable.call_deferred] if you want to enter edit mode on [signal text_submitted].
+			</description>
+		</method>
 		<method name="get_menu" qualifiers="const">
 		<method name="get_menu" qualifiers="const">
 			<return type="PopupMenu" />
 			<return type="PopupMenu" />
 			<description>
 			<description>
@@ -223,6 +230,12 @@
 				Selects the whole [String].
 				Selects the whole [String].
 			</description>
 			</description>
 		</method>
 		</method>
+		<method name="unedit">
+			<return type="void" />
+			<description>
+				Allows exiting edit mode while preserving focus.
+			</description>
+		</method>
 	</methods>
 	</methods>
 	<members>
 	<members>
 		<member name="alignment" type="int" setter="set_horizontal_alignment" getter="get_horizontal_alignment" enum="HorizontalAlignment" default="0">
 		<member name="alignment" type="int" setter="set_horizontal_alignment" getter="get_horizontal_alignment" enum="HorizontalAlignment" default="0">

+ 2 - 0
editor/code_editor.cpp

@@ -644,6 +644,8 @@ void FindReplaceBar::_search_text_submitted(const String &p_text) {
 	} else {
 	} else {
 		search_next();
 		search_next();
 	}
 	}
+
+	callable_mp(search_text, &LineEdit::edit).call_deferred();
 }
 }
 
 
 void FindReplaceBar::_replace_text_submitted(const String &p_text) {
 void FindReplaceBar::_replace_text_submitted(const String &p_text) {

+ 15 - 1
scene/gui/color_picker.cpp

@@ -245,7 +245,21 @@ void ColorPicker::finish_shaders() {
 }
 }
 
 
 void ColorPicker::set_focus_on_line_edit() {
 void ColorPicker::set_focus_on_line_edit() {
-	callable_mp((Control *)c_text, &Control::grab_focus).call_deferred();
+	bool has_hardware_keyboard = true;
+#if defined(ANDROID_ENABLED) || defined(IOS_ENABLED)
+	has_hardware_keyboard = DisplayServer::get_singleton()->has_hardware_keyboard();
+#endif // ANDROID_ENABLED || IOS_ENABLED
+	if (has_hardware_keyboard) {
+		callable_mp((Control *)c_text, &Control::grab_focus).call_deferred();
+	} else {
+		// A hack to avoid showing the virtual keyboard when the ColorPicker window popups and
+		// no hardware keyboard is detected on Android and IOS.
+		// This will only focus the LineEdit without editing, the virtual keyboard will only be visible when
+		// we touch the LineEdit to enter edit mode.
+		callable_mp(c_text, &LineEdit::set_editable).call_deferred(false);
+		callable_mp((Control *)c_text, &Control::grab_focus).call_deferred();
+		callable_mp(c_text, &LineEdit::set_editable).call_deferred(true);
+	}
 }
 }
 
 
 void ColorPicker::_update_controls() {
 void ColorPicker::_update_controls() {

+ 33 - 23
scene/gui/line_edit.cpp

@@ -45,28 +45,37 @@
 #include "editor/editor_settings.h"
 #include "editor/editor_settings.h"
 #endif
 #endif
 
 
-void LineEdit::_edit() {
+void LineEdit::edit() {
 	if (!is_inside_tree()) {
 	if (!is_inside_tree()) {
 		return;
 		return;
 	}
 	}
 
 
 	if (!has_focus()) {
 	if (!has_focus()) {
 		grab_focus();
 		grab_focus();
+		return;
 	}
 	}
 
 
 	if (!editable || editing) {
 	if (!editable || editing) {
 		return;
 		return;
 	}
 	}
 
 
+	if (select_all_on_focus) {
+		if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
+			// Select all when the mouse button is up.
+			pending_select_all_on_focus = true;
+		} else {
+			select_all();
+		}
+	}
+
 	editing = true;
 	editing = true;
 	_validate_caret_can_draw();
 	_validate_caret_can_draw();
 
 
 	show_virtual_keyboard();
 	show_virtual_keyboard();
 	queue_redraw();
 	queue_redraw();
-	emit_signal(SNAME("editing_toggled"), true);
 }
 }
 
 
-void LineEdit::_unedit() {
+void LineEdit::unedit() {
 	if (!editing) {
 	if (!editing) {
 		return;
 		return;
 	}
 	}
@@ -84,8 +93,6 @@ void LineEdit::_unedit() {
 	if (deselect_on_focus_loss_enabled && !selection.drag_attempt) {
 	if (deselect_on_focus_loss_enabled && !selection.drag_attempt) {
 		deselect();
 		deselect();
 	}
 	}
-
-	emit_signal(SNAME("editing_toggled"), false);
 }
 }
 
 
 bool LineEdit::is_editing() const {
 bool LineEdit::is_editing() const {
@@ -390,7 +397,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
 			}
 			}
 
 
 			if (editable && !editing) {
 			if (editable && !editing) {
-				_edit();
+				edit();
+				emit_signal(SNAME("editing_toggled"), true);
 			}
 			}
 
 
 			accept_event();
 			accept_event();
@@ -406,7 +414,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
 			set_caret_at_pixel_pos(b->get_position().x);
 			set_caret_at_pixel_pos(b->get_position().x);
 
 
 			if (!editing) {
 			if (!editing) {
-				_edit();
+				edit();
+				emit_signal(SNAME("editing_toggled"), true);
 			}
 			}
 
 
 			if (!paste_buffer.is_empty()) {
 			if (!paste_buffer.is_empty()) {
@@ -506,7 +515,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
 			}
 			}
 
 
 			if (editable && !editing) {
 			if (editable && !editing) {
-				_edit();
+				edit();
+				emit_signal(SNAME("editing_toggled"), true);
 				return;
 				return;
 			}
 			}
 			queue_redraw();
 			queue_redraw();
@@ -599,7 +609,9 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
 	}
 	}
 
 
 	if (editable && !editing && k->is_action_pressed("ui_text_submit", false)) {
 	if (editable && !editing && k->is_action_pressed("ui_text_submit", false)) {
-		_edit();
+		edit();
+		emit_signal(SNAME("editing_toggled"), true);
+		accept_event();
 		return;
 		return;
 	}
 	}
 
 
@@ -734,7 +746,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
 		}
 		}
 
 
 		if (editing) {
 		if (editing) {
-			_unedit();
+			unedit();
+			emit_signal(SNAME("editing_toggled"), false);
 		}
 		}
 
 
 		accept_event();
 		accept_event();
@@ -743,7 +756,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
 
 
 	if (k->is_action("ui_cancel")) {
 	if (k->is_action("ui_cancel")) {
 		if (editing) {
 		if (editing) {
-			_unedit();
+			unedit();
+			emit_signal(SNAME("editing_toggled"), false);
 		}
 		}
 
 
 		accept_event();
 		accept_event();
@@ -1332,24 +1346,17 @@ void LineEdit::_notification(int p_what) {
 		} break;
 		} break;
 
 
 		case NOTIFICATION_FOCUS_ENTER: {
 		case NOTIFICATION_FOCUS_ENTER: {
-			if (select_all_on_focus) {
-				if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
-					// Select all when the mouse button is up.
-					pending_select_all_on_focus = true;
-				} else {
-					select_all();
-				}
-			}
-
 			// Only allow editing if the LineEdit is not focused with arrow keys.
 			// Only allow editing if the LineEdit is not focused with arrow keys.
 			if (!(Input::get_singleton()->is_action_pressed("ui_up") || Input::get_singleton()->is_action_pressed("ui_down") || Input::get_singleton()->is_action_pressed("ui_left") || Input::get_singleton()->is_action_pressed("ui_right"))) {
 			if (!(Input::get_singleton()->is_action_pressed("ui_up") || Input::get_singleton()->is_action_pressed("ui_down") || Input::get_singleton()->is_action_pressed("ui_left") || Input::get_singleton()->is_action_pressed("ui_right"))) {
-				_edit();
+				edit();
+				emit_signal(SNAME("editing_toggled"), true);
 			}
 			}
 		} break;
 		} break;
 
 
 		case NOTIFICATION_FOCUS_EXIT: {
 		case NOTIFICATION_FOCUS_EXIT: {
 			if (editing) {
 			if (editing) {
-				_unedit();
+				unedit();
+				emit_signal(SNAME("editing_toggled"), false);
 			}
 			}
 		} break;
 		} break;
 
 
@@ -2138,7 +2145,8 @@ void LineEdit::set_editable(bool p_editable) {
 	editable = p_editable;
 	editable = p_editable;
 
 
 	if (!editable && editing) {
 	if (!editable && editing) {
-		_unedit();
+		unedit();
+		emit_signal(SNAME("editing_toggled"), false);
 	}
 	}
 	_validate_caret_can_draw();
 	_validate_caret_can_draw();
 
 
@@ -2759,6 +2767,8 @@ void LineEdit::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &LineEdit::set_horizontal_alignment);
 	ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &LineEdit::set_horizontal_alignment);
 	ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &LineEdit::get_horizontal_alignment);
 	ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &LineEdit::get_horizontal_alignment);
 
 
+	ClassDB::bind_method(D_METHOD("edit"), &LineEdit::edit);
+	ClassDB::bind_method(D_METHOD("unedit"), &LineEdit::unedit);
 	ClassDB::bind_method(D_METHOD("is_editing"), &LineEdit::is_editing);
 	ClassDB::bind_method(D_METHOD("is_editing"), &LineEdit::is_editing);
 	ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
 	ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
 	ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
 	ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));

+ 2 - 3
scene/gui/line_edit.h

@@ -207,9 +207,6 @@ private:
 		float base_scale = 1.0;
 		float base_scale = 1.0;
 	} theme_cache;
 	} theme_cache;
 
 
-	void _edit();
-	void _unedit();
-
 	void _close_ime_window();
 	void _close_ime_window();
 	void _update_ime_window_position();
 	void _update_ime_window_position();
 
 
@@ -265,6 +262,8 @@ protected:
 	virtual void gui_input(const Ref<InputEvent> &p_event) override;
 	virtual void gui_input(const Ref<InputEvent> &p_event) override;
 
 
 public:
 public:
+	void edit();
+	void unedit();
 	bool is_editing() const;
 	bool is_editing() const;
 
 
 	bool has_ime_text() const;
 	bool has_ime_text() const;