From e0304a7d00f718a17f2e6981118edd2ceda22637 Mon Sep 17 00:00:00 2001 From: kobewi Date: Fri, 29 Nov 2024 21:15:23 +0100 Subject: [PATCH] Add helper method to get Window from ID --- scene/gui/color_picker.cpp | 12 +++--------- scene/main/viewport.cpp | 16 +++------------- scene/main/window.cpp | 9 ++++++++- scene/main/window.h | 1 + 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp index 72d24f02038e..c0f679b5c3c8 100644 --- a/scene/gui/color_picker.cpp +++ b/scene/gui/color_picker.cpp @@ -1601,18 +1601,12 @@ void ColorPicker::_pick_button_pressed_legacy() { // Add the Texture of each Window to the Image. Vector wl = ds->get_window_list(); // FIXME: sort windows by visibility. - for (int index = 0; index < wl.size(); index++) { - DisplayServer::WindowID wid = wl[index]; - if (wid == DisplayServer::INVALID_WINDOW_ID) { + for (const DisplayServer::WindowID &window_id : wl) { + Window *w = Window::get_from_id(window_id); + if (!w) { continue; } - ObjectID woid = DisplayServer::get_singleton()->window_get_attached_instance_id(wid); - if (woid == ObjectID()) { - continue; - } - - Window *w = Object::cast_to(ObjectDB::get_instance(woid)); Ref img = w->get_texture()->get_image(); if (!img.is_valid() || img->is_empty()) { continue; diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index dd3ae1ad3cbe..eeb6b6e65867 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -3771,19 +3771,9 @@ void Viewport::set_embedding_subwindows(bool p_embed) { } if (allow_change) { - Vector wl = DisplayServer::get_singleton()->get_window_list(); - for (int index = 0; index < wl.size(); index++) { - DisplayServer::WindowID wid = wl[index]; - if (wid == DisplayServer::INVALID_WINDOW_ID) { - continue; - } - - ObjectID woid = DisplayServer::get_singleton()->window_get_attached_instance_id(wid); - if (woid.is_null()) { - continue; - } - - Window *w = Object::cast_to(ObjectDB::get_instance(woid)); + Vector wl = DisplayServer::get_singleton()->get_window_list(); + for (const DisplayServer::WindowID &window_id : wl) { + const Window *w = Window::get_from_id(window_id); if (w && is_ancestor_of(w)) { // Prevent change when this viewport has child windows that are displayed as native windows. allow_change = false; diff --git a/scene/main/window.cpp b/scene/main/window.cpp index 05904fa8f999..6341575b6340 100644 --- a/scene/main/window.cpp +++ b/scene/main/window.cpp @@ -270,6 +270,13 @@ void Window::_validate_property(PropertyInfo &p_property) const { // +Window *Window::get_from_id(DisplayServer::WindowID p_window_id) { + if (p_window_id == DisplayServer::INVALID_WINDOW_ID) { + return nullptr; + } + return Object::cast_to(ObjectDB::get_instance(DisplayServer::get_singleton()->window_get_attached_instance_id(p_window_id))); +} + void Window::set_title(const String &p_title) { ERR_MAIN_THREAD_GUARD; @@ -912,7 +919,7 @@ void Window::_make_transient() { if (!is_embedded() && transient_to_focused) { DisplayServer::WindowID focused_window_id = DisplayServer::get_singleton()->get_focused_window(); if (focused_window_id != DisplayServer::INVALID_WINDOW_ID) { - window = Object::cast_to(ObjectDB::get_instance(DisplayServer::get_singleton()->window_get_attached_instance_id(focused_window_id))); + window = Window::get_from_id(focused_window_id); } } diff --git a/scene/main/window.h b/scene/main/window.h index a1d95ab91f28..6c82efcc3f9b 100644 --- a/scene/main/window.h +++ b/scene/main/window.h @@ -271,6 +271,7 @@ class Window : public Viewport { }; static void set_root_layout_direction(int p_root_dir); + static Window *get_from_id(DisplayServer::WindowID p_window_id); void set_title(const String &p_title); String get_title() const;