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..48a13db5 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,11 @@ public: const utki::shared_ref ruis_style_provider; private: - std::map< - native_window_type::window_id_type, // - utki::shared_ref // - > - windows; + // 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: std::vector> windows_to_destroy; @@ -256,9 +256,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) + { + return std::ranges::find_if( + this->windows, // + [id](const auto& wp) { + return wp.first == id; + } + ); + } + 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..d7527994 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,11 @@ 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 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: const auto& get_windows() @@ -160,6 +161,16 @@ class application_glue : public utki::destructable return this->windows; } + auto find_window(native_window_type::window_id_type id) + { + return std::ranges::find_if( + this->windows, // + [id](const auto& wp) { + return wp.first == id; + } + ); + } + std::vector> windows_to_destroy; application_glue(const utki::version_duplet& gl_version) : @@ -316,21 +327,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 +352,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..929d94a0 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,10 @@ public: const utki::shared_ref ruis_style_provider; private: - std::set> windows; + // 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: std::atomic_bool quit_flag = false; diff --git a/src/ruisapp/glue/sdl/application.cxx b/src/ruisapp/glue/sdl/application.cxx index f30679fd..580367d8 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) +{ + return std::ranges::find_if( + this->windows, // + [id](const auto& wp) { + return wp.first == id; + } + ); +} + 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..2d287020 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,11 @@ public: const utki::shared_ref ruis_style_provider; #endif - std::map< - native_window::window_id_type, // - utki::shared_ref // - > - windows; + // 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; @@ -98,6 +99,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..fca24d20 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) +{ + return std::ranges::find_if( + this->windows, // + [id](const auto& wp) { + return wp.first == id; + } + ); +} + 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..8da8a90d 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,11 @@ public: const utki::shared_ref ruis_style_provider; private: - std::map> windows; + // 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: std::atomic_bool quit_flag = false; @@ -103,6 +107,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();