Skip to content

Commit

Permalink
Merge pull request #92042 from bruvzg/fix_edit_popup
Browse files Browse the repository at this point in the history
Fix Tree and FileSystemList edit popup double events and ESC behavior.
  • Loading branch information
akien-mga committed May 17, 2024
2 parents b569ae3 + 7d4d63b commit 85463fd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
16 changes: 15 additions & 1 deletion editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ Control *FileSystemList::make_custom_tooltip(const String &p_text) const {
}

void FileSystemList::_line_editor_submit(const String &p_text) {
if (popup_edit_commited) {
return; // Already processed by _text_editor_popup_modal_close
}

if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
return; // ESC pressed, app focus lost, or forced close from code.
}

popup_edit_commited = true; // End edit popup processing.
popup_editor->hide();

emit_signal(SNAME("item_edited"));
Expand Down Expand Up @@ -127,6 +136,7 @@ bool FileSystemList::edit_selected() {
line_editor->set_text(name);
line_editor->select(0, name.rfind("."));

popup_edit_commited = false; // Start edit popup processing.
popup_editor->popup();
popup_editor->child_controls_changed();
line_editor->grab_focus();
Expand All @@ -138,8 +148,12 @@ String FileSystemList::get_edit_text() {
}

void FileSystemList::_text_editor_popup_modal_close() {
if (popup_edit_commited) {
return; // Already processed by _text_editor_popup_modal_close
}

if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
return;
return; // ESC pressed, app focus lost, or forced close from code.
}

_line_editor_submit(line_editor->get_text());
Expand Down
1 change: 1 addition & 0 deletions editor/filesystem_dock.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class FileSystemTree : public Tree {
class FileSystemList : public ItemList {
GDCLASS(FileSystemList, ItemList);

bool popup_edit_commited = true;
VBoxContainer *popup_editor_vb = nullptr;
Popup *popup_editor = nullptr;
LineEdit *line_editor = nullptr;
Expand Down
12 changes: 10 additions & 2 deletions scene/gui/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
if (get_flag(FLAG_POPUP) && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) {
hide_reason = HIDE_REASON_CANCELED; // ESC pressed, mark as canceled unconditionally.
_close_pressed();
}
Window::_input_from_window(p_event);
Expand Down Expand Up @@ -104,13 +105,18 @@ void Popup::_notification(int p_what) {

case NOTIFICATION_WM_CLOSE_REQUEST: {
if (!is_in_edited_scene_root()) {
hide_reason = HIDE_REASON_UNFOCUSED;
if (hide_reason == HIDE_REASON_NONE) {
hide_reason = HIDE_REASON_UNFOCUSED;
}
_close_pressed();
}
} break;

case NOTIFICATION_APPLICATION_FOCUS_OUT: {
if (!is_in_edited_scene_root() && get_flag(FLAG_POPUP)) {
if (hide_reason == HIDE_REASON_NONE) {
hide_reason = HIDE_REASON_UNFOCUSED;
}
_close_pressed();
}
} break;
Expand All @@ -119,7 +125,9 @@ void Popup::_notification(int p_what) {

void Popup::_parent_focused() {
if (popped_up && get_flag(FLAG_POPUP)) {
hide_reason = HIDE_REASON_UNFOCUSED;
if (hide_reason == HIDE_REASON_NONE) {
hide_reason = HIDE_REASON_UNFOCUSED;
}
_close_pressed();
}
}
Expand Down
26 changes: 25 additions & 1 deletion scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3152,8 +3152,12 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int
}

void Tree::_text_editor_popup_modal_close() {
if (popup_edit_commited) {
return; // Already processed by LineEdit/TextEdit commit.
}

if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
return;
return; // ESC pressed, app focus lost, or forced close from code.
}

if (value_editor->has_point(value_editor->get_local_mouse_position())) {
Expand All @@ -3172,9 +3176,18 @@ void Tree::_text_editor_popup_modal_close() {
}

void Tree::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
if (popup_edit_commited) {
return; // Already processed by _text_editor_popup_modal_close
}

if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
return; // ESC pressed, app focus lost, or forced close from code.
}

if (p_event->is_action_pressed("ui_text_newline_blank", true)) {
accept_event();
} else if (p_event->is_action_pressed("ui_text_newline")) {
popup_edit_commited = true; // End edit popup processing.
popup_editor->hide();
_apply_multiline_edit();
accept_event();
Expand Down Expand Up @@ -3205,6 +3218,15 @@ void Tree::_apply_multiline_edit() {
}

void Tree::_line_editor_submit(String p_text) {
if (popup_edit_commited) {
return; // Already processed by _text_editor_popup_modal_close
}

if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
return; // ESC pressed, app focus lost, or forced close from code.
}

popup_edit_commited = true; // End edit popup processing.
popup_editor->hide();

if (!popup_edited_item) {
Expand Down Expand Up @@ -4072,6 +4094,7 @@ bool Tree::edit_selected(bool p_force_edit) {
if (!popup_editor->is_embedded()) {
popup_editor->set_content_scale_factor(popup_scale);
}
popup_edit_commited = false; // Start edit popup processing.
popup_editor->popup();
popup_editor->child_controls_changed();

Expand All @@ -4091,6 +4114,7 @@ bool Tree::edit_selected(bool p_force_edit) {
if (!popup_editor->is_embedded()) {
popup_editor->set_content_scale_factor(popup_scale);
}
popup_edit_commited = false; // Start edit popup processing.
popup_editor->popup();
popup_editor->child_controls_changed();

Expand Down
1 change: 1 addition & 0 deletions scene/gui/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ class Tree : public Control {

VBoxContainer *popup_editor_vb = nullptr;

bool popup_edit_commited = true;
Popup *popup_editor = nullptr;
LineEdit *line_editor = nullptr;
TextEdit *text_editor = nullptr;
Expand Down

0 comments on commit 85463fd

Please sign in to comment.