From 1cfd8298de72e27ef68f8e4ff43ecdde39d5a94f Mon Sep 17 00:00:00 2001 From: Ivan Gagis's Bot Date: Wed, 25 Nov 2026 01:32:01 +0200 Subject: [PATCH 1/2] glue: store windows in std::vector to preserve creation order Replace std::map (and std::set on macos) used to store application windows in the glue adaptations with std::vector>> (std::vector> on macos, where no window id lookup exists). This way application::get_windows() returns the windows in the order they were opened, which is stable even when new windows are inserted and old ones deleted. The number of windows is expected to be small (2-3), so linear lookup by window_id via find_window() is still fast. Fixes #27. --- .../glue/linux/wayland/application.cxx | 15 ++++---- .../glue/linux/wayland/application.hxx | 20 +++++++---- src/ruisapp/glue/linux/xorg/glue.cxx | 34 +++++++++++-------- src/ruisapp/glue/macos/application.cxx | 26 ++++++++++---- src/ruisapp/glue/macos/application.hxx | 15 +------- src/ruisapp/glue/sdl/application.cxx | 27 +++++++++------ src/ruisapp/glue/sdl/application.hxx | 9 +++-- src/ruisapp/glue/windows/application.cxx | 27 +++++++++------ src/ruisapp/glue/windows/application.hxx | 6 ++-- 9 files changed, 101 insertions(+), 78 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.cxx b/src/ruisapp/glue/linux/wayland/application.cxx index 94ddab33..d0ee42ba 100644 --- a/src/ruisapp/glue/linux/wayland/application.cxx +++ b/src/ruisapp/glue/linux/wayland/application.cxx @@ -286,15 +286,12 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ) ); - auto res = this->windows.insert( // - std::make_pair( - ruisapp_window.get().ruis_native_window.get().get_id(), // - std::move(ruisapp_window) - ) - ); - utki::assert(res.second, SL); + auto id = ruisapp_window.get().ruis_native_window.get().get_id(); + // assert that the window with this id is not already in the list + utki::assert(this->find_window(id) == this->windows.end(), SL); + this->windows.emplace_back(id, std::move(ruisapp_window)); - auto& ret_win = res.first->second.get(); + auto& ret_win = this->windows.back().second.get(); #if defined(RUISAPP_RENDER_OPENGL) || defined(RUISAPP_RENDER_OPENGLES) // EGL surface for the window is not created yet at this point. @@ -323,7 +320,7 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window void application_glue::destroy_window(app_window& w) { - auto i = this->windows.find(w.ruis_native_window.get().get_id()); + auto i = this->find_window(w.ruis_native_window.get().get_id()); utki::assert(i != this->windows.end(), SL); // TODO: add comment explaining why deferring is needed, do it in all glue implementations diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 680d8f3d..965d3f6f 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -22,7 +22,7 @@ along with this program. If not, see . #pragma once #include -#include +#include #include @@ -167,11 +167,7 @@ public: const utki::shared_ref ruis_style_provider; private: - std::map< - native_window_type::window_id_type, // - utki::shared_ref // - > - windows; + std::vector>> windows; public: std::vector> windows_to_destroy; @@ -256,9 +252,19 @@ public: ruisapp::window& make_window(ruisapp::window_parameters window_params); void destroy_window(app_window& w); + auto find_window(native_window_type::window_id_type id) + { + for (auto i = this->windows.begin(); i != this->windows.end(); ++i) { + if (i->first == id) { + return i; + } + } + return this->windows.end(); + } + app_window* get_window(native_window_type::window_id_type id) { - auto i = this->windows.find(id); + auto i = this->find_window(id); if (i == this->windows.end()) { return nullptr; } diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index c17db4bd..a0b5496a 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -21,6 +21,7 @@ along with this program. If not, see . #include #include +#include #include #include @@ -148,11 +149,7 @@ class application_glue : public utki::destructable utki::shared_ref ruis_style_provider; private: - std::map< - native_window_type::window_id_type, // - utki::shared_ref // - > - windows; + std::vector>> windows; public: const auto& get_windows() @@ -160,6 +157,16 @@ class application_glue : public utki::destructable return this->windows; } + auto find_window(native_window_type::window_id_type id) + { + for (auto i = this->windows.begin(); i != this->windows.end(); ++i) { + if (i->first == id) { + return i; + } + } + return this->windows.end(); + } + std::vector> windows_to_destroy; application_glue(const utki::version_duplet& gl_version) : @@ -316,21 +323,18 @@ class application_glue : public utki::destructable ) ); - auto res = this->windows.insert( // - std::make_pair( - nw.get_id(), // - std::move(ruisapp_window) - ) - ); - utki::assert(res.second, SL); + auto id = nw.get_id(); + // assert that the window with this id is not already in the list + utki::assert(this->find_window(id) == this->windows.end(), SL); + this->windows.emplace_back(id, std::move(ruisapp_window)); - return res.first->second.get(); + return this->windows.back().second.get(); } void destroy_window(app_window& w) { auto id = w.ruis_native_window.get().get_id(); - auto i = this->windows.find(id); + auto i = this->find_window(id); if (i == this->windows.end()) { return; } @@ -344,7 +348,7 @@ class application_glue : public utki::destructable app_window* get_window(native_window_type::window_id_type id) { - auto i = this->windows.find(id); + auto i = this->find_window(id); if (i == this->windows.end()) { return nullptr; } diff --git a/src/ruisapp/glue/macos/application.cxx b/src/ruisapp/glue/macos/application.cxx index 13f804ab..2355823c 100644 --- a/src/ruisapp/glue/macos/application.cxx +++ b/src/ruisapp/glue/macos/application.cxx @@ -175,19 +175,33 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ) ); - utki::logcat_debug("application_glue::make_window(): inserting window to set", '\n'); - - auto res = this->windows.insert(std::move(ruisapp_window)); - utki::assert(res.second, SL); + // assert that the window is not already in the list + utki::assert( + std::none_of( + this->windows.begin(), // + this->windows.end(), // + [&ruisapp_window](const auto& win) { + return &win.get() == &ruisapp_window.get(); + } + ), + SL + ); + this->windows.push_back(std::move(ruisapp_window)); utki::logcat_debug("application_glue::make_window(): window created", '\n'); - return res.first->get(); + return this->windows.back().get(); } void application_glue::destroy_window(app_window& w) { - auto i = std::find(this->windows.begin(), this->windows.end(), w); + auto i = std::find_if( + this->windows.begin(), // + this->windows.end(), // + [&w](const auto& win) { + return &win.get() == &w; + } + ); utki::assert(i != this->windows.end(), SL); // Defer actual window object destruction until next main loop cycle, diff --git a/src/ruisapp/glue/macos/application.hxx b/src/ruisapp/glue/macos/application.hxx index 71b532eb..f1b212f1 100644 --- a/src/ruisapp/glue/macos/application.hxx +++ b/src/ruisapp/glue/macos/application.hxx @@ -23,7 +23,6 @@ along with this program. If not, see . #include #include -#include #include @@ -43,18 +42,6 @@ public: utki::shared_ref ruis_native_window ); - // needed for using with std::set - bool operator<(const app_window& w) const noexcept - { - return this < &w; - } - - // needed for using with std::set - bool operator==(const app_window& w) const noexcept - { - return this == &w; - } - void resize(const ruis::vec2& dims); }; } // namespace @@ -74,7 +61,7 @@ public: const utki::shared_ref ruis_style_provider; private: - std::set> windows; + std::vector> windows; public: std::atomic_bool quit_flag = false; diff --git a/src/ruisapp/glue/sdl/application.cxx b/src/ruisapp/glue/sdl/application.cxx index f30679fd..f53650ce 100644 --- a/src/ruisapp/glue/sdl/application.cxx +++ b/src/ruisapp/glue/sdl/application.cxx @@ -83,9 +83,19 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : #endif {} +auto application_glue::find_window(native_window::window_id_type id) +{ + for (auto i = this->windows.begin(); i != this->windows.end(); ++i) { + if (i->first == id) { + return i; + } + } + return this->windows.end(); +} + app_window* application_glue::get_window(native_window::window_id_type id) { - auto i = this->windows.find(id); + auto i = this->find_window(id); if (i == this->windows.end()) { return nullptr; } @@ -222,20 +232,17 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ruis::rect({0, 0}, ruisapp_window.get().ruis_native_window.get().get_dims().to()) ); - auto res = this->windows.insert( // - std::make_pair( - ruisapp_window.get().ruis_native_window.get().get_id(), // - std::move(ruisapp_window) - ) - ); - utki::assert(res.second, SL); + auto id = ruisapp_window.get().ruis_native_window.get().get_id(); + // assert that the window with this id is not already in the list + utki::assert(this->find_window(id) == this->windows.end(), SL); + this->windows.emplace_back(id, std::move(ruisapp_window)); - return res.first->second.get(); + return this->windows.back().second.get(); } void application_glue::destroy_window(native_window::window_id_type id) { - auto i = this->windows.find(id); + auto i = this->find_window(id); utki::assert(i != this->windows.end(), SL); this->windows_to_destroy.push_back(std::move(i->second)); diff --git a/src/ruisapp/glue/sdl/application.hxx b/src/ruisapp/glue/sdl/application.hxx index 0cd12cd9..c8bae9cd 100644 --- a/src/ruisapp/glue/sdl/application.hxx +++ b/src/ruisapp/glue/sdl/application.hxx @@ -23,6 +23,7 @@ along with this program. If not, see . #include #include +#include #include @@ -76,11 +77,7 @@ public: const utki::shared_ref ruis_style_provider; #endif - std::map< - native_window::window_id_type, // - utki::shared_ref // - > - windows; + std::vector>> windows; std::vector> windows_to_destroy; @@ -98,6 +95,8 @@ public: ruisapp::window& make_window(ruisapp::window_parameters window_params); void destroy_window(native_window::window_id_type id); + auto find_window(native_window::window_id_type id); + app_window* get_window(native_window::window_id_type id); // get first window if any exists diff --git a/src/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index 6c20deba..8765d378 100644 --- a/src/ruisapp/glue/windows/application.cxx +++ b/src/ruisapp/glue/windows/application.cxx @@ -158,9 +158,19 @@ void application_glue::post_to_ui_thread(std::function procedure) } } +auto application_glue::find_window(native_window::window_id_type id) +{ + for (auto i = this->windows.begin(); i != this->windows.end(); ++i) { + if (i->first == id) { + return i; + } + } + return this->windows.end(); +} + app_window* application_glue::get_window(native_window::window_id_type id) { - auto i = this->windows.find(id); + auto i = this->find_window(id); if (i == this->windows.end()) { return nullptr; } @@ -227,20 +237,17 @@ ruisapp::window& application_glue::make_window(ruisapp::window_parameters window ) ); - auto res = this->windows.insert( // - std::make_pair( - ruisapp_window.get().ruis_native_window.get().get_id(), // - std::move(ruisapp_window) - ) - ); - utki::assert(res.second, SL); + auto id = ruisapp_window.get().ruis_native_window.get().get_id(); + // assert that the window with this id is not already in the list + utki::assert(this->find_window(id) == this->windows.end(), SL); + this->windows.emplace_back(id, std::move(ruisapp_window)); - return res.first->second.get(); + return this->windows.back().second.get(); } void application_glue::destroy_window(native_window::window_id_type id) { - auto i = this->windows.find(id); + auto i = this->find_window(id); utki::assert(i != this->windows.end(), SL); this->windows_to_destroy.push_back(std::move(i->second)); diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index 2f82ff9f..bde96395 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -23,7 +23,7 @@ along with this program. If not, see . #include #include -#include +#include #include "../../application.hpp" @@ -84,7 +84,7 @@ public: const utki::shared_ref ruis_style_provider; private: - std::map> windows; + std::vector>> windows; public: std::atomic_bool quit_flag = false; @@ -103,6 +103,8 @@ public: ruisapp::window& make_window(ruisapp::window_parameters window_params); void destroy_window(native_window::window_id_type id); + auto find_window(native_window::window_id_type id); + app_window* get_window(native_window::window_id_type id); void render(); From 37086d8c0e5be6dca01153af05d91aab99c5792b Mon Sep 17 00:00:00 2001 From: Ivan Gagis's Bot Date: Wed, 25 Nov 2026 02:03:03 +0200 Subject: [PATCH 2/2] glue: use std::ranges::find_if in find_window, document why vector --- src/ruisapp/glue/linux/wayland/application.hxx | 14 +++++++++----- src/ruisapp/glue/linux/xorg/glue.cxx | 14 +++++++++----- src/ruisapp/glue/macos/application.hxx | 3 +++ src/ruisapp/glue/sdl/application.cxx | 10 +++++----- src/ruisapp/glue/sdl/application.hxx | 4 ++++ src/ruisapp/glue/windows/application.cxx | 10 +++++----- src/ruisapp/glue/windows/application.hxx | 4 ++++ 7 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/ruisapp/glue/linux/wayland/application.hxx b/src/ruisapp/glue/linux/wayland/application.hxx index 965d3f6f..48a13db5 100644 --- a/src/ruisapp/glue/linux/wayland/application.hxx +++ b/src/ruisapp/glue/linux/wayland/application.hxx @@ -167,6 +167,10 @@ public: const utki::shared_ref ruis_style_provider; private: + // std::vector is used instead of std::map to preserve the order in which + // the windows were created, so that application::get_windows() returns the + // windows in a stable order. The number of windows is small (2-3), so + // linear lookup by window id is fast enough. std::vector>> windows; public: @@ -254,12 +258,12 @@ public: auto find_window(native_window_type::window_id_type id) { - for (auto i = this->windows.begin(); i != this->windows.end(); ++i) { - if (i->first == id) { - return i; + return std::ranges::find_if( + this->windows, // + [id](const auto& wp) { + return wp.first == id; } - } - return this->windows.end(); + ); } app_window* get_window(native_window_type::window_id_type id) diff --git a/src/ruisapp/glue/linux/xorg/glue.cxx b/src/ruisapp/glue/linux/xorg/glue.cxx index a0b5496a..d7527994 100644 --- a/src/ruisapp/glue/linux/xorg/glue.cxx +++ b/src/ruisapp/glue/linux/xorg/glue.cxx @@ -149,6 +149,10 @@ class application_glue : public utki::destructable utki::shared_ref ruis_style_provider; private: + // std::vector is used instead of std::map to preserve the order in which + // the windows were created, so that application::get_windows() returns the + // windows in a stable order. The number of windows is small (2-3), so + // linear lookup by window id is fast enough. std::vector>> windows; public: @@ -159,12 +163,12 @@ class application_glue : public utki::destructable auto find_window(native_window_type::window_id_type id) { - for (auto i = this->windows.begin(); i != this->windows.end(); ++i) { - if (i->first == id) { - return i; + return std::ranges::find_if( + this->windows, // + [id](const auto& wp) { + return wp.first == id; } - } - return this->windows.end(); + ); } std::vector> windows_to_destroy; diff --git a/src/ruisapp/glue/macos/application.hxx b/src/ruisapp/glue/macos/application.hxx index f1b212f1..929d94a0 100644 --- a/src/ruisapp/glue/macos/application.hxx +++ b/src/ruisapp/glue/macos/application.hxx @@ -61,6 +61,9 @@ public: const utki::shared_ref ruis_style_provider; private: + // std::vector is used instead of std::set to preserve the order in which + // the windows were created, so that application::get_windows() returns the + // windows in a stable order. std::vector> windows; public: diff --git a/src/ruisapp/glue/sdl/application.cxx b/src/ruisapp/glue/sdl/application.cxx index f53650ce..580367d8 100644 --- a/src/ruisapp/glue/sdl/application.cxx +++ b/src/ruisapp/glue/sdl/application.cxx @@ -85,12 +85,12 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : auto application_glue::find_window(native_window::window_id_type id) { - for (auto i = this->windows.begin(); i != this->windows.end(); ++i) { - if (i->first == id) { - return i; + return std::ranges::find_if( + this->windows, // + [id](const auto& wp) { + return wp.first == id; } - } - return this->windows.end(); + ); } app_window* application_glue::get_window(native_window::window_id_type id) diff --git a/src/ruisapp/glue/sdl/application.hxx b/src/ruisapp/glue/sdl/application.hxx index c8bae9cd..2d287020 100644 --- a/src/ruisapp/glue/sdl/application.hxx +++ b/src/ruisapp/glue/sdl/application.hxx @@ -77,6 +77,10 @@ public: const utki::shared_ref ruis_style_provider; #endif + // std::vector is used instead of std::map to preserve the order in which + // the windows were created, so that application::get_windows() returns the + // windows in a stable order. The number of windows is small (2-3), so + // linear lookup by window id is fast enough. std::vector>> windows; std::vector> windows_to_destroy; diff --git a/src/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index 8765d378..fca24d20 100644 --- a/src/ruisapp/glue/windows/application.cxx +++ b/src/ruisapp/glue/windows/application.cxx @@ -160,12 +160,12 @@ void application_glue::post_to_ui_thread(std::function procedure) auto application_glue::find_window(native_window::window_id_type id) { - for (auto i = this->windows.begin(); i != this->windows.end(); ++i) { - if (i->first == id) { - return i; + return std::ranges::find_if( + this->windows, // + [id](const auto& wp) { + return wp.first == id; } - } - return this->windows.end(); + ); } app_window* application_glue::get_window(native_window::window_id_type id) diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index bde96395..8da8a90d 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -84,6 +84,10 @@ public: const utki::shared_ref ruis_style_provider; private: + // std::vector is used instead of std::map to preserve the order in which + // the windows were created, so that application::get_windows() returns the + // windows in a stable order. The number of windows is small (2-3), so + // linear lookup by window id is fast enough. std::vector>> windows; public: