Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/ruisapp/glue/linux/wayland/application.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions src/ruisapp/glue/linux/wayland/application.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once

#include <atomic>
#include <map>
#include <utility>

#include <nitki/queue.hpp>

Expand Down Expand Up @@ -167,11 +167,11 @@ public:
const utki::shared_ref<ruis::style_provider> ruis_style_provider;

private:
std::map<
native_window_type::window_id_type, //
utki::shared_ref<app_window> //
>
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<std::pair<native_window_type::window_id_type, utki::shared_ref<app_window>>> windows;
Comment thread
igagis marked this conversation as resolved.
Resolved

public:
std::vector<utki::shared_ref<app_window>> windows_to_destroy;
Expand Down Expand Up @@ -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;
}
Expand Down
38 changes: 23 additions & 15 deletions src/ruisapp/glue/linux/xorg/glue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <array>
#include <string_view>
#include <utility>
#include <vector>

#include <nitki/queue.hpp>
Expand Down Expand Up @@ -148,18 +149,28 @@ class application_glue : public utki::destructable
utki::shared_ref<ruis::style_provider> ruis_style_provider;

private:
std::map<
native_window_type::window_id_type, //
utki::shared_ref<app_window> //
>
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<std::pair<native_window_type::window_id_type, utki::shared_ref<app_window>>> windows;

public:
const auto& get_windows()
{
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<utki::shared_ref<app_window>> windows_to_destroy;

application_glue(const utki::version_duplet& gl_version) :
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
26 changes: 20 additions & 6 deletions src/ruisapp/glue/macos/application.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 4 additions & 14 deletions src/ruisapp/glue/macos/application.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <atomic>
#include <functional>
#include <set>

#include <utki/destructable.hpp>

Expand All @@ -43,18 +42,6 @@ public:
utki::shared_ref<native_window> 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
Expand All @@ -74,7 +61,10 @@ public:
const utki::shared_ref<ruis::style_provider> ruis_style_provider;

private:
std::set<utki::shared_ref<app_window>> 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<utki::shared_ref<app_window>> windows;

public:
std::atomic_bool quit_flag = false;
Expand Down
27 changes: 17 additions & 10 deletions src/ruisapp/glue/sdl/application.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<ruis::real>())
);

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));
Expand Down
13 changes: 8 additions & 5 deletions src/ruisapp/glue/sdl/application.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <atomic>
#include <functional>
#include <utility>

#include <utki/destructable.hpp>

Expand Down Expand Up @@ -76,11 +77,11 @@ public:
const utki::shared_ref<ruis::style_provider> ruis_style_provider;
#endif

std::map<
native_window::window_id_type, //
utki::shared_ref<app_window> //
>
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<std::pair<native_window::window_id_type, utki::shared_ref<app_window>>> windows;

std::vector<utki::shared_ref<app_window>> windows_to_destroy;

Expand All @@ -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
Expand Down
27 changes: 17 additions & 10 deletions src/ruisapp/glue/windows/application.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,19 @@ void application_glue::post_to_ui_thread(std::function<void()> 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;
}
Expand Down Expand Up @@ -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));
Expand Down
10 changes: 8 additions & 2 deletions src/ruisapp/glue/windows/application.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <atomic>
#include <functional>
#include <map>
#include <utility>

#include "../../application.hpp"

Expand Down Expand Up @@ -84,7 +84,11 @@ public:
const utki::shared_ref<ruis::style_provider> ruis_style_provider;

private:
std::map<native_window::window_id_type, utki::shared_ref<app_window>> 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<std::pair<native_window::window_id_type, utki::shared_ref<app_window>>> windows;

public:
std::atomic_bool quit_flag = false;
Expand All @@ -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();
Expand Down
Loading