Skip to content

Commit e0304a7

Browse files
committed
Add helper method to get Window from ID
1 parent 0eadbdb commit e0304a7

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

scene/gui/color_picker.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,18 +1601,12 @@ void ColorPicker::_pick_button_pressed_legacy() {
16011601
// Add the Texture of each Window to the Image.
16021602
Vector<DisplayServer::WindowID> wl = ds->get_window_list();
16031603
// FIXME: sort windows by visibility.
1604-
for (int index = 0; index < wl.size(); index++) {
1605-
DisplayServer::WindowID wid = wl[index];
1606-
if (wid == DisplayServer::INVALID_WINDOW_ID) {
1604+
for (const DisplayServer::WindowID &window_id : wl) {
1605+
Window *w = Window::get_from_id(window_id);
1606+
if (!w) {
16071607
continue;
16081608
}
16091609

1610-
ObjectID woid = DisplayServer::get_singleton()->window_get_attached_instance_id(wid);
1611-
if (woid == ObjectID()) {
1612-
continue;
1613-
}
1614-
1615-
Window *w = Object::cast_to<Window>(ObjectDB::get_instance(woid));
16161610
Ref<Image> img = w->get_texture()->get_image();
16171611
if (!img.is_valid() || img->is_empty()) {
16181612
continue;

scene/main/viewport.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,19 +3771,9 @@ void Viewport::set_embedding_subwindows(bool p_embed) {
37713771
}
37723772

37733773
if (allow_change) {
3774-
Vector<int> wl = DisplayServer::get_singleton()->get_window_list();
3775-
for (int index = 0; index < wl.size(); index++) {
3776-
DisplayServer::WindowID wid = wl[index];
3777-
if (wid == DisplayServer::INVALID_WINDOW_ID) {
3778-
continue;
3779-
}
3780-
3781-
ObjectID woid = DisplayServer::get_singleton()->window_get_attached_instance_id(wid);
3782-
if (woid.is_null()) {
3783-
continue;
3784-
}
3785-
3786-
Window *w = Object::cast_to<Window>(ObjectDB::get_instance(woid));
3774+
Vector<DisplayServer::WindowID> wl = DisplayServer::get_singleton()->get_window_list();
3775+
for (const DisplayServer::WindowID &window_id : wl) {
3776+
const Window *w = Window::get_from_id(window_id);
37873777
if (w && is_ancestor_of(w)) {
37883778
// Prevent change when this viewport has child windows that are displayed as native windows.
37893779
allow_change = false;

scene/main/window.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ void Window::_validate_property(PropertyInfo &p_property) const {
270270

271271
//
272272

273+
Window *Window::get_from_id(DisplayServer::WindowID p_window_id) {
274+
if (p_window_id == DisplayServer::INVALID_WINDOW_ID) {
275+
return nullptr;
276+
}
277+
return Object::cast_to<Window>(ObjectDB::get_instance(DisplayServer::get_singleton()->window_get_attached_instance_id(p_window_id)));
278+
}
279+
273280
void Window::set_title(const String &p_title) {
274281
ERR_MAIN_THREAD_GUARD;
275282

@@ -912,7 +919,7 @@ void Window::_make_transient() {
912919
if (!is_embedded() && transient_to_focused) {
913920
DisplayServer::WindowID focused_window_id = DisplayServer::get_singleton()->get_focused_window();
914921
if (focused_window_id != DisplayServer::INVALID_WINDOW_ID) {
915-
window = Object::cast_to<Window>(ObjectDB::get_instance(DisplayServer::get_singleton()->window_get_attached_instance_id(focused_window_id)));
922+
window = Window::get_from_id(focused_window_id);
916923
}
917924
}
918925

scene/main/window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class Window : public Viewport {
271271
};
272272

273273
static void set_root_layout_direction(int p_root_dir);
274+
static Window *get_from_id(DisplayServer::WindowID p_window_id);
274275

275276
void set_title(const String &p_title);
276277
String get_title() const;

0 commit comments

Comments
 (0)