Skip to content

Commit d11f203

Browse files
committed
Merge pull request godotengine#97094 from WhalesState/expose-edit-unedit
Expose `LineEdit` `edit` and `unedit` methods.
2 parents f032af7 + f84f734 commit d11f203

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

doc/classes/LineEdit.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- When the [LineEdit] control is focused using the keyboard arrow keys, it will only gain focus and not enter edit mode.
99
- 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]).
1010
- To exit edit mode, press [code]ui_text_submit[/code] or [code]ui_cancel[/code] (by default [kbd]Escape[/kbd]) actions.
11-
- Check [method is_editing] and [signal editing_toggled] for more information.
11+
- Check [method edit], [method unedit], [method is_editing], and [signal editing_toggled] for more information.
1212
[b]Important:[/b]
1313
- 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).
1414
[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 @@
7575
Clears the current selection.
7676
</description>
7777
</method>
78+
<method name="edit">
79+
<return type="void" />
80+
<description>
81+
Allows entering edit mode whether the [LineEdit] is focused or not.
82+
Use [method Callable.call_deferred] if you want to enter edit mode on [signal text_submitted].
83+
</description>
84+
</method>
7885
<method name="get_menu" qualifiers="const">
7986
<return type="PopupMenu" />
8087
<description>
@@ -223,6 +230,12 @@
223230
Selects the whole [String].
224231
</description>
225232
</method>
233+
<method name="unedit">
234+
<return type="void" />
235+
<description>
236+
Allows exiting edit mode while preserving focus.
237+
</description>
238+
</method>
226239
</methods>
227240
<members>
228241
<member name="alignment" type="int" setter="set_horizontal_alignment" getter="get_horizontal_alignment" enum="HorizontalAlignment" default="0">

editor/code_editor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ void FindReplaceBar::_search_text_submitted(const String &p_text) {
644644
} else {
645645
search_next();
646646
}
647+
648+
callable_mp(search_text, &LineEdit::edit).call_deferred();
647649
}
648650

649651
void FindReplaceBar::_replace_text_submitted(const String &p_text) {

scene/gui/color_picker.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,21 @@ void ColorPicker::finish_shaders() {
245245
}
246246

247247
void ColorPicker::set_focus_on_line_edit() {
248-
callable_mp((Control *)c_text, &Control::grab_focus).call_deferred();
248+
bool has_hardware_keyboard = true;
249+
#if defined(ANDROID_ENABLED) || defined(IOS_ENABLED)
250+
has_hardware_keyboard = DisplayServer::get_singleton()->has_hardware_keyboard();
251+
#endif // ANDROID_ENABLED || IOS_ENABLED
252+
if (has_hardware_keyboard) {
253+
callable_mp((Control *)c_text, &Control::grab_focus).call_deferred();
254+
} else {
255+
// A hack to avoid showing the virtual keyboard when the ColorPicker window popups and
256+
// no hardware keyboard is detected on Android and IOS.
257+
// This will only focus the LineEdit without editing, the virtual keyboard will only be visible when
258+
// we touch the LineEdit to enter edit mode.
259+
callable_mp(c_text, &LineEdit::set_editable).call_deferred(false);
260+
callable_mp((Control *)c_text, &Control::grab_focus).call_deferred();
261+
callable_mp(c_text, &LineEdit::set_editable).call_deferred(true);
262+
}
249263
}
250264

251265
void ColorPicker::_update_controls() {

scene/gui/line_edit.cpp

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,37 @@
4545
#include "editor/editor_settings.h"
4646
#endif
4747

48-
void LineEdit::_edit() {
48+
void LineEdit::edit() {
4949
if (!is_inside_tree()) {
5050
return;
5151
}
5252

5353
if (!has_focus()) {
5454
grab_focus();
55+
return;
5556
}
5657

5758
if (!editable || editing) {
5859
return;
5960
}
6061

62+
if (select_all_on_focus) {
63+
if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
64+
// Select all when the mouse button is up.
65+
pending_select_all_on_focus = true;
66+
} else {
67+
select_all();
68+
}
69+
}
70+
6171
editing = true;
6272
_validate_caret_can_draw();
6373

6474
show_virtual_keyboard();
6575
queue_redraw();
66-
emit_signal(SNAME("editing_toggled"), true);
6776
}
6877

69-
void LineEdit::_unedit() {
78+
void LineEdit::unedit() {
7079
if (!editing) {
7180
return;
7281
}
@@ -84,8 +93,6 @@ void LineEdit::_unedit() {
8493
if (deselect_on_focus_loss_enabled && !selection.drag_attempt) {
8594
deselect();
8695
}
87-
88-
emit_signal(SNAME("editing_toggled"), false);
8996
}
9097

9198
bool LineEdit::is_editing() const {
@@ -390,7 +397,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
390397
}
391398

392399
if (editable && !editing) {
393-
_edit();
400+
edit();
401+
emit_signal(SNAME("editing_toggled"), true);
394402
}
395403

396404
accept_event();
@@ -406,7 +414,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
406414
set_caret_at_pixel_pos(b->get_position().x);
407415

408416
if (!editing) {
409-
_edit();
417+
edit();
418+
emit_signal(SNAME("editing_toggled"), true);
410419
}
411420

412421
if (!paste_buffer.is_empty()) {
@@ -506,7 +515,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
506515
}
507516

508517
if (editable && !editing) {
509-
_edit();
518+
edit();
519+
emit_signal(SNAME("editing_toggled"), true);
510520
return;
511521
}
512522
queue_redraw();
@@ -599,7 +609,9 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
599609
}
600610

601611
if (editable && !editing && k->is_action_pressed("ui_text_submit", false)) {
602-
_edit();
612+
edit();
613+
emit_signal(SNAME("editing_toggled"), true);
614+
accept_event();
603615
return;
604616
}
605617

@@ -734,7 +746,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
734746
}
735747

736748
if (editing) {
737-
_unedit();
749+
unedit();
750+
emit_signal(SNAME("editing_toggled"), false);
738751
}
739752

740753
accept_event();
@@ -743,7 +756,8 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
743756

744757
if (k->is_action("ui_cancel")) {
745758
if (editing) {
746-
_unedit();
759+
unedit();
760+
emit_signal(SNAME("editing_toggled"), false);
747761
}
748762

749763
accept_event();
@@ -1332,24 +1346,17 @@ void LineEdit::_notification(int p_what) {
13321346
} break;
13331347

13341348
case NOTIFICATION_FOCUS_ENTER: {
1335-
if (select_all_on_focus) {
1336-
if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
1337-
// Select all when the mouse button is up.
1338-
pending_select_all_on_focus = true;
1339-
} else {
1340-
select_all();
1341-
}
1342-
}
1343-
13441349
// Only allow editing if the LineEdit is not focused with arrow keys.
13451350
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"))) {
1346-
_edit();
1351+
edit();
1352+
emit_signal(SNAME("editing_toggled"), true);
13471353
}
13481354
} break;
13491355

13501356
case NOTIFICATION_FOCUS_EXIT: {
13511357
if (editing) {
1352-
_unedit();
1358+
unedit();
1359+
emit_signal(SNAME("editing_toggled"), false);
13531360
}
13541361
} break;
13551362

@@ -2138,7 +2145,8 @@ void LineEdit::set_editable(bool p_editable) {
21382145
editable = p_editable;
21392146

21402147
if (!editable && editing) {
2141-
_unedit();
2148+
unedit();
2149+
emit_signal(SNAME("editing_toggled"), false);
21422150
}
21432151
_validate_caret_can_draw();
21442152

@@ -2759,6 +2767,8 @@ void LineEdit::_bind_methods() {
27592767
ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &LineEdit::set_horizontal_alignment);
27602768
ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &LineEdit::get_horizontal_alignment);
27612769

2770+
ClassDB::bind_method(D_METHOD("edit"), &LineEdit::edit);
2771+
ClassDB::bind_method(D_METHOD("unedit"), &LineEdit::unedit);
27622772
ClassDB::bind_method(D_METHOD("is_editing"), &LineEdit::is_editing);
27632773
ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
27642774
ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));

scene/gui/line_edit.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,6 @@ class LineEdit : public Control {
207207
float base_scale = 1.0;
208208
} theme_cache;
209209

210-
void _edit();
211-
void _unedit();
212-
213210
void _close_ime_window();
214211
void _update_ime_window_position();
215212

@@ -265,6 +262,8 @@ class LineEdit : public Control {
265262
virtual void gui_input(const Ref<InputEvent> &p_event) override;
266263

267264
public:
265+
void edit();
266+
void unedit();
268267
bool is_editing() const;
269268

270269
bool has_ime_text() const;

0 commit comments

Comments
 (0)