Skip to content

Commit

Permalink
Merge pull request godotengine#99843 from KoBeWi/easy_windows
Browse files Browse the repository at this point in the history
Add helper method to get Window from ID
  • Loading branch information
akien-mga committed Nov 29, 2024
2 parents 31edb7e + e0304a7 commit 16ee94a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
12 changes: 3 additions & 9 deletions scene/gui/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1601,18 +1601,12 @@ void ColorPicker::_pick_button_pressed_legacy() {
// Add the Texture of each Window to the Image.
Vector<DisplayServer::WindowID> 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<Window>(ObjectDB::get_instance(woid));
Ref<Image> img = w->get_texture()->get_image();
if (!img.is_valid() || img->is_empty()) {
continue;
Expand Down
16 changes: 3 additions & 13 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3772,19 +3772,9 @@ void Viewport::set_embedding_subwindows(bool p_embed) {
}

if (allow_change) {
Vector<int> 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<Window>(ObjectDB::get_instance(woid));
Vector<DisplayServer::WindowID> 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;
Expand Down
9 changes: 8 additions & 1 deletion scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Window>(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;

Expand Down Expand Up @@ -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<Window>(ObjectDB::get_instance(DisplayServer::get_singleton()->window_get_attached_instance_id(focused_window_id)));
window = Window::get_from_id(focused_window_id);
}
}

Expand Down
1 change: 1 addition & 0 deletions scene/main/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 16ee94a

Please sign in to comment.