Fix Window CLOSED event when using more than one window

Fixed an issue where if there were more than one window, when they were
closed, the CLOSED event would trigger but the window count did not
decrease. This happened because after the window was destroyed the
DestroyNotify event was triggered, which when looked up in the m_windows
map, reinserted itself with default initialization.
This commit is contained in:
2025-04-30 02:52:29 +10:00
parent 49567a22a9
commit 771363bbfe
2 changed files with 17 additions and 12 deletions

View File

@@ -59,15 +59,19 @@ void X11WindowDriver::poll_events() {
XEvent event;
XNextEvent(m_display, &event);
X11::Window handle = event.xany.window;
Nova::Window& window = m_windows[handle];
auto iter = m_windows.find(event.xany.window);
if (iter == m_windows.end()) {
continue;
}
WindowID window = iter->second;
switch (event.type) {
case ConfigureNotify: {
XConfigureEvent xce = event.xconfigure;
if (xce.width != window.width || xce.height != window.height) {
window.width = xce.width;
window.height = xce.height;
if (xce.width != window->width || xce.height != window->height) {
window->width = xce.width;
window->height = xce.height;
NOVA_DEBUG("Window event: RESIZED ({}x{})", xce.width, xce.height);
}
break;
@@ -75,7 +79,7 @@ void X11WindowDriver::poll_events() {
case ClientMessage: {
if (event.xclient.data.l[0] == static_cast<long>(m_window_close_atom)) {
NOVA_DEBUG("Window event: CLOSED");
destroy_window(&window);
destroy_window(window);
}
break;
}
@@ -105,10 +109,10 @@ WindowID X11WindowDriver::create_window(const std::string_view p_title, const u3
X11::Window handle = XCreateSimpleWindow(m_display, DefaultRootWindow(m_display), 0, 0, p_width, p_height, 0, 0, 0);
Nova::Window& window = m_windows[handle];
window.width = p_width;
window.height = p_height;
window.handle = handle;
Window* window = new Window();
window->width = p_width;
window->height = p_height;
window->handle = handle;
XSetWMProtocols(m_display, handle, &m_window_close_atom, 1);
XSelectInput(m_display, handle, StructureNotifyMask);
@@ -116,7 +120,8 @@ WindowID X11WindowDriver::create_window(const std::string_view p_title, const u3
XMapWindow(m_display, handle);
XFlush(m_display);
return &window;
m_windows[handle] = window;
return window;
}
void X11WindowDriver::destroy_window(WindowID p_window) {

View File

@@ -51,7 +51,7 @@ namespace Nova {
private:
X11::Display* m_display = nullptr;
X11::Atom m_window_close_atom = 0;
std::unordered_map<X11::Window, Nova::Window> m_windows;
std::unordered_map<X11::Window, WindowID> m_windows;
};
} // namespace Nova