summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMounir Tohami <53877170+WhalesState@users.noreply.github.com>2024-10-04 10:37:33 +0000
committerMounir Tohami <53877170+WhalesState@users.noreply.github.com>2024-10-04 10:37:33 +0000
commitf84f7346969f0a18c24c53e6adced39af4fc0777 (patch)
treeeb1ec4f728ea483f39aa1be600d0dbe13ddeb5e9
parent32239d477be66d8c94e9a7a95c56ae0cbad54cfb (diff)
downloadredot-engine-f84f7346969f0a18c24c53e6adced39af4fc0777.tar.gz
Expose `LineEdit` `edit` and `unedit` methods.
-rw-r--r--doc/classes/LineEdit.xml15
-rw-r--r--editor/code_editor.cpp2
-rw-r--r--scene/gui/color_picker.cpp16
-rw-r--r--scene/gui/line_edit.cpp56
-rw-r--r--scene/gui/line_edit.h5
5 files changed, 66 insertions, 28 deletions
diff --git a/doc/classes/LineEdit.xml b/doc/classes/LineEdit.xml
index 41f42392de..3e0c328dcb 100644
--- a/doc/classes/LineEdit.xml
+++ b/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.
- 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.
- - 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]
- 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):
@@ -75,6 +75,13 @@
Clears the current selection.
</description>
</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">
<return type="PopupMenu" />
<description>
@@ -223,6 +230,12 @@
Selects the whole [String].
</description>
</method>
+ <method name="unedit">
+ <return type="void" />
+ <description>
+ Allows exiting edit mode while preserving focus.
+ </description>
+ </method>
</methods>
<members>
<member name="alignment" type="int" setter="set_horizontal_alignment" getter="get_horizontal_alignment" enum="HorizontalAlignment" default="0">
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp
index 07547fee8a..88a32b1a6d 100644
--- a/editor/code_editor.cpp
+++ b/editor/code_editor.cpp
@@ -644,6 +644,8 @@ void FindReplaceBar::_search_text_submitted(const String &p_text) {
} else {
search_next();
}
+
+ callable_mp(search_text, &LineEdit::edit).call_deferred();
}
void FindReplaceBar::_replace_text_submitted(const String &p_text) {
diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp
index 002a738b83..fe4c91cb56 100644
--- a/scene/gui/color_picker.cpp
+++ b/scene/gui/color_picker.cpp
@@ -245,7 +245,21 @@ void ColorPicker::finish_shaders() {
}
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() {
diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp
index 6e5b555cdf..9967805134 100644
--- a/scene/gui/line_edit.cpp
+++ b/scene/gui/line_edit.cpp
@@ -45,28 +45,37 @@
#include "editor/editor_settings.h"
#endif
-void LineEdit::_edit() {
+void LineEdit::edit() {
if (!is_inside_tree()) {
return;
}
if (!has_focus()) {
grab_focus();
+ return;
}
if (!editable || editing) {
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;
_validate_caret_can_draw();
show_virtual_keyboard();
queue_redraw();
- emit_signal(SNAME("editing_toggled"), true);
}
-void LineEdit::_unedit() {
+void LineEdit::unedit() {
if (!editing) {
return;
}
@@ -84,8 +93,6 @@ void LineEdit::_unedit() {
if (deselect_on_focus_loss_enabled && !selection.drag_attempt) {
deselect();
}
-
- emit_signal(SNAME("editing_toggled"), false);
}
bool LineEdit::is_editing() const {
@@ -390,7 +397,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
}
if (editable && !editing) {
- _edit();
+ edit();
+ emit_signal(SNAME("editing_toggled"), true);
}
accept_event();
@@ -406,7 +414,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
set_caret_at_pixel_pos(b->get_position().x);
if (!editing) {
- _edit();
+ edit();
+ emit_signal(SNAME("editing_toggled"), true);
}
if (!paste_buffer.is_empty()) {
@@ -506,7 +515,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
}
if (editable && !editing) {
- _edit();
+ edit();
+ emit_signal(SNAME("editing_toggled"), true);
return;
}
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)) {
- _edit();
+ edit();
+ emit_signal(SNAME("editing_toggled"), true);
+ accept_event();
return;
}
@@ -734,7 +746,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
}
if (editing) {
- _unedit();
+ unedit();
+ emit_signal(SNAME("editing_toggled"), false);
}
accept_event();
@@ -743,7 +756,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
if (k->is_action("ui_cancel")) {
if (editing) {
- _unedit();
+ unedit();
+ emit_signal(SNAME("editing_toggled"), false);
}
accept_event();
@@ -1332,24 +1346,17 @@ void LineEdit::_notification(int p_what) {
} break;
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.
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;
case NOTIFICATION_FOCUS_EXIT: {
if (editing) {
- _unedit();
+ unedit();
+ emit_signal(SNAME("editing_toggled"), false);
}
} break;
@@ -2138,7 +2145,8 @@ void LineEdit::set_editable(bool p_editable) {
editable = p_editable;
if (!editable && editing) {
- _unedit();
+ unedit();
+ emit_signal(SNAME("editing_toggled"), false);
}
_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("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("clear"), &LineEdit::clear);
ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h
index ac7436646b..9253dd8711 100644
--- a/scene/gui/line_edit.h
+++ b/scene/gui/line_edit.h
@@ -207,9 +207,6 @@ private:
float base_scale = 1.0;
} theme_cache;
- void _edit();
- void _unedit();
-
void _close_ime_window();
void _update_ime_window_position();
@@ -265,6 +262,8 @@ protected:
virtual void gui_input(const Ref<InputEvent> &p_event) override;
public:
+ void edit();
+ void unedit();
bool is_editing() const;
bool has_ime_text() const;