libui: Fix crashes when closing non-main windows
Some checks failed
Build and test / build (push) Has been cancelled

This fix moves the actual closing of the window to after all the events are processed.
This commit is contained in:
apio 2024-09-18 21:47:31 +02:00
parent 17a31e5ea9
commit b95cfac3ec
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 22 additions and 5 deletions

View File

@ -61,6 +61,7 @@ namespace ui
HashMap<int, OwnedPtr<Window>> m_windows;
bool m_should_close { false };
os::EventLoop m_loop;
Vector<int> m_window_clear_queue;
bool process_events();

View File

@ -80,6 +80,12 @@ namespace ui
return m_id;
}
void on_close(os::Action&& action)
{
m_on_close_action = move(action);
m_has_on_close_action = true;
}
~Window();
private:
@ -93,6 +99,9 @@ namespace ui
Option<int> m_old_mouse_buttons;
bool m_decorated { false };
os::Action m_on_close_action;
bool m_has_on_close_action { false };
struct ShortcutAction
{
bool intercept;

View File

@ -75,7 +75,7 @@ namespace ui
void App::unregister_window(Window* window, Badge<Window>)
{
int id = window->id();
check(m_windows.try_remove(id));
m_window_clear_queue.try_append(id);
}
Window* App::find_window(int id)
@ -124,6 +124,15 @@ namespace ui
{
check(m_main_window);
m_client->check_for_messages().release_value();
for (int id : m_window_clear_queue)
{
check(m_windows.try_remove(id));
ui::CloseWindowRequest request;
request.window = id;
client().send_async(request);
}
m_window_clear_queue.clear_data();
return !m_should_close;
}
}

View File

@ -84,6 +84,8 @@ namespace ui
Window::~Window()
{
if (m_canvas.ptr) munmap(m_canvas.ptr, ((usize)m_canvas.width) * ((usize)m_canvas.height) * 4);
if (m_has_on_close_action) m_on_close_action();
}
void Window::set_title(StringView title)
@ -108,10 +110,6 @@ namespace ui
{
App& app = App::the();
ui::CloseWindowRequest request;
request.window = m_id;
app.client().send_async(request);
if (this == app.main_window()) app.set_should_close(true);
app.unregister_window(this, {});