From 53172d9860642b6099dbba1a9b72b149e3069aa5 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Sat, 30 Sep 2023 20:33:03 +0200 Subject: [PATCH] Add more context to some `Window` errors --- scene/gui/control.cpp | 24 +++++++-------- scene/main/window.cpp | 72 +++++++++++++++++++------------------------ scene/main/window.h | 6 ++-- 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 53e894593318..c7ff5980cb6b 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -2533,7 +2533,7 @@ StringName Control::get_theme_type_variation() const { Ref Control::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(Ref()); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2557,7 +2557,7 @@ Ref Control::get_theme_icon(const StringName &p_name, const StringNam Ref Control::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(Ref()); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2581,7 +2581,7 @@ Ref Control::get_theme_stylebox(const StringName &p_name, const String Ref Control::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(Ref()); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2605,7 +2605,7 @@ Ref Control::get_theme_font(const StringName &p_name, const StringName &p_ int Control::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(0); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2629,7 +2629,7 @@ int Control::get_theme_font_size(const StringName &p_name, const StringName &p_t Color Control::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(Color()); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2653,7 +2653,7 @@ Color Control::get_theme_color(const StringName &p_name, const StringName &p_the int Control::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(0); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2704,7 +2704,7 @@ Ref Control::get_editor_theme_icon(const StringName &p_name) const { bool Control::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2721,7 +2721,7 @@ bool Control::has_theme_icon(const StringName &p_name, const StringName &p_theme bool Control::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2738,7 +2738,7 @@ bool Control::has_theme_stylebox(const StringName &p_name, const StringName &p_t bool Control::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2755,7 +2755,7 @@ bool Control::has_theme_font(const StringName &p_name, const StringName &p_theme bool Control::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2772,7 +2772,7 @@ bool Control::has_theme_font_size(const StringName &p_name, const StringName &p_ bool Control::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { @@ -2789,7 +2789,7 @@ bool Control::has_theme_color(const StringName &p_name, const StringName &p_them bool Control::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!data.initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { diff --git a/scene/main/window.cpp b/scene/main/window.cpp index c345c3d179eb..f5468b29c5c6 100644 --- a/scene/main/window.cpp +++ b/scene/main/window.cpp @@ -829,16 +829,7 @@ void Window::set_visible(bool p_visible) { //update transient exclusive if (transient_parent) { - if (exclusive && visible) { - if (!is_in_edited_scene_root()) { - ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child."); - transient_parent->exclusive_child = this; - } - } else { - if (transient_parent->exclusive_child == this) { - transient_parent->exclusive_child = nullptr; - } - } + _set_transient_exclusive_child(true); } } @@ -878,15 +869,7 @@ void Window::_make_transient() { if (window) { transient_parent = window; window->transient_children.insert(this); - if (is_inside_tree() && is_visible() && exclusive) { - if (transient_parent->exclusive_child == nullptr) { - if (!is_in_edited_scene_root()) { - transient_parent->exclusive_child = this; - } - } else if (transient_parent->exclusive_child != this) { - ERR_PRINT("Making child transient exclusive, but parent has another exclusive child"); - } - } + _set_transient_exclusive_child(); } //see if we can make transient @@ -895,6 +878,22 @@ void Window::_make_transient() { } } +void Window::_set_transient_exclusive_child(bool p_clear_invalid) { + if (exclusive && visible && is_inside_tree()) { + if (!is_in_edited_scene_root()) { + // Transient parent has another exclusive child. + if (transient_parent->exclusive_child && transient_parent->exclusive_child != this) { + ERR_PRINT(vformat("Attempting to make child window exclusive, but the parent window already has another exclusive child. This window: %s, parent window: %s, current exclusive child window: %s", this->get_description(), transient_parent->get_description(), transient_parent->exclusive_child->get_description())); + } + transient_parent->exclusive_child = this; + } + } else if (p_clear_invalid) { + if (transient_parent->exclusive_child == this) { + transient_parent->exclusive_child = nullptr; + } + } +} + void Window::set_transient(bool p_transient) { ERR_MAIN_THREAD_GUARD; if (transient == p_transient) { @@ -936,16 +935,7 @@ void Window::set_exclusive(bool p_exclusive) { } if (transient_parent) { - if (p_exclusive && is_inside_tree() && is_visible()) { - ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child."); - if (!is_in_edited_scene_root()) { - transient_parent->exclusive_child = this; - } - } else { - if (transient_parent->exclusive_child == this) { - transient_parent->exclusive_child = nullptr; - } - } + _set_transient_exclusive_child(true); } } @@ -1999,7 +1989,7 @@ StringName Window::get_theme_type_variation() const { Ref Window::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(Ref()); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2023,7 +2013,7 @@ Ref Window::get_theme_icon(const StringName &p_name, const StringName Ref Window::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(Ref()); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2047,7 +2037,7 @@ Ref Window::get_theme_stylebox(const StringName &p_name, const StringN Ref Window::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(Ref()); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2071,7 +2061,7 @@ Ref Window::get_theme_font(const StringName &p_name, const StringName &p_t int Window::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(0); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2095,7 +2085,7 @@ int Window::get_theme_font_size(const StringName &p_name, const StringName &p_th Color Window::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(Color()); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2119,7 +2109,7 @@ Color Window::get_theme_color(const StringName &p_name, const StringName &p_them int Window::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(0); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2170,7 +2160,7 @@ Ref Window::get_editor_theme_icon(const StringName &p_name) const { bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2187,7 +2177,7 @@ bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_ bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2204,7 +2194,7 @@ bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_th bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2221,7 +2211,7 @@ bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_ bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2238,7 +2228,7 @@ bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_t bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { @@ -2255,7 +2245,7 @@ bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme bool Window::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const { ERR_READ_THREAD_GUARD_V(false); if (!initialized) { - WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED"); + WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", this->get_description())); } if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) { diff --git a/scene/main/window.h b/scene/main/window.h index 11b986239e2b..736d92777429 100644 --- a/scene/main/window.h +++ b/scene/main/window.h @@ -163,12 +163,14 @@ class Window : public Viewport { void _update_window_callbacks(); - void _clear_transient(); - void _make_transient(); Window *transient_parent = nullptr; Window *exclusive_child = nullptr; HashSet transient_children; + void _clear_transient(); + void _make_transient(); + void _set_transient_exclusive_child(bool p_clear_invalid = false); + ThemeOwner *theme_owner = nullptr; Ref theme; StringName theme_type_variation;