events: Emit an event when a window is destroyed

As child windows can be recursively destroyed when their parents are destroyed, emit an event to notify the application when a window is being or has been implicitly destroyed so that it can appropriately clean up any associated resources.

If the application has registered an event watch, the destroy message will be received when the window handle is still valid, so the application can retrieve and release any userdata associated with the window. If the message is processed at any time after that, the window handle is already invalid and the ID is only useful for application-side bookkeeping purposes.
This commit is contained in:
Frank Praznik
2023-04-27 09:20:40 -04:00
parent 78cfc23993
commit bee6099372
3 changed files with 8 additions and 2 deletions

View File

@@ -46,7 +46,7 @@ int SDL_SendWindowEvent(SDL_Window *window, SDL_EventType windowevent,
if (window == NULL) {
return 0;
}
if (window->is_destroying) {
if (window->is_destroying && windowevent != SDL_EVENT_WINDOW_DESTROYED) {
return 0;
}
switch (windowevent) {

View File

@@ -3410,6 +3410,8 @@ void SDL_DestroyWindow(SDL_Window *window)
SDL_DestroyWindow(window->first_child);
}
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_DESTROYED, 0, 0);
/* If this is a child window, unlink it from its siblings */
if (window->parent) {
if (window->next_sibling) {