Fix popup closed when an ancestor window is focused

Previously, only the direct parent were taken into account.

Popups like contextual menus could stay open if an ancestor which is
not a direct parent was focused.

Reproduction steps (any platform):
- Select a node in the scene tree
- Left click the node to start renaming
- Right click to open the copy/paste contextual menu
- Left click in the scene tree to deselect the node

Also closing popup when focusing out of the application, without waiting
for the parent to get focus to do so.
This commit is contained in:
PouleyKetchoupp 2020-08-21 16:11:20 +02:00
parent 2b49cb0b73
commit 6d1ef8efac
2 changed files with 49 additions and 28 deletions

View File

@ -41,55 +41,71 @@ void Popup::_input_from_window(const Ref<InputEvent> &p_event) {
} }
} }
void Popup::_parent_focused() { void Popup::_initialize_visible_parents() {
_close_pressed(); visible_parents.clear();
Window *parent_window = this;
while (parent_window) {
parent_window = parent_window->get_parent_visible_window();
if (parent_window) {
visible_parents.push_back(parent_window);
parent_window->connect("focus_entered", callable_mp(this, &Popup::_parent_focused));
parent_window->connect("tree_exited", callable_mp(this, &Popup::_deinitialize_visible_parents));
}
}
}
void Popup::_deinitialize_visible_parents() {
for (uint32_t i = 0; i < visible_parents.size(); ++i) {
visible_parents[i]->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
visible_parents[i]->disconnect("tree_exited", callable_mp(this, &Popup::_deinitialize_visible_parents));
}
visible_parents.clear();
} }
void Popup::_notification(int p_what) { void Popup::_notification(int p_what) {
switch (p_what) { switch (p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: { case NOTIFICATION_VISIBILITY_CHANGED: {
if (is_visible()) { if (is_visible()) {
parent_visible = get_parent_visible_window(); _initialize_visible_parents();
if (parent_visible) {
parent_visible->connect("focus_entered", callable_mp(this, &Popup::_parent_focused));
}
} else { } else {
if (parent_visible) { _deinitialize_visible_parents();
parent_visible->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
parent_visible = nullptr;
}
emit_signal("popup_hide"); emit_signal("popup_hide");
} }
} break; } break;
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
if (parent_visible) { if (has_focus()) {
parent_visible->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused)); popped_up = true;
parent_visible = nullptr;
} }
} break; } break;
case NOTIFICATION_EXIT_TREE: {
_deinitialize_visible_parents();
} break;
case NOTIFICATION_WM_CLOSE_REQUEST: { case NOTIFICATION_WM_CLOSE_REQUEST: {
_close_pressed(); _close_pressed();
} break;
case NOTIFICATION_APPLICATION_FOCUS_OUT: {
_close_pressed();
} break; } break;
} }
} }
void Popup::_close_pressed() { void Popup::_parent_focused() {
Window *parent_window = parent_visible; if (popped_up) {
if (parent_visible) { _close_pressed();
parent_visible->disconnect("focus_entered", callable_mp(this, &Popup::_parent_focused));
parent_visible = nullptr;
} }
}
void Popup::_close_pressed() {
popped_up = false;
_deinitialize_visible_parents();
call_deferred("hide"); call_deferred("hide");
emit_signal("cancelled"); emit_signal("cancelled");
if (parent_window) {
//parent_window->grab_focus();
}
} }
void Popup::set_as_minsize() { void Popup::set_as_minsize() {
@ -130,8 +146,6 @@ Rect2i Popup::_popup_adjust_rect() const {
} }
Popup::Popup() { Popup::Popup() {
parent_visible = nullptr;
set_wrap_controls(true); set_wrap_controls(true);
set_visible(false); set_visible(false);
set_transient(true); set_transient(true);

View File

@ -33,12 +33,19 @@
#include "scene/main/window.h" #include "scene/main/window.h"
#include "core/local_vector.h"
class Popup : public Window { class Popup : public Window {
GDCLASS(Popup, Window); GDCLASS(Popup, Window);
Window *parent_visible; LocalVector<Window *> visible_parents;
bool popped_up = false;
void _input_from_window(const Ref<InputEvent> &p_event); void _input_from_window(const Ref<InputEvent> &p_event);
void _initialize_visible_parents();
void _deinitialize_visible_parents();
void _parent_focused(); void _parent_focused();
protected: protected: