Expose LineEdit edit and unedit methods.

This commit is contained in:
Mounir Tohami 2024-10-04 10:37:33 +00:00
parent 32239d477b
commit f84f734696
5 changed files with 66 additions and 28 deletions

View File

@ -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">

View File

@ -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) {

View File

@ -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() {

View File

@ -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));

View File

@ -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;