Added handleCloseRequested() callback to allow applications to intercept close requests.

This commit is contained in:
Patrick Wuttke 2025-09-23 10:43:48 +02:00
parent da8d1589b3
commit 69fdae991a
2 changed files with 16 additions and 0 deletions

View File

@ -339,6 +339,11 @@ void Application::handleSDLEvent(const SDL_Event& event)
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
mRunning = false; mRunning = false;
return; return;
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
if (SDL_GetWindowFromID(event.window.windowID) == mWindow) {
handleCloseRequested();
}
break;
default: default:
ImGui_ImplSDL3_ProcessEvent(&event); ImGui_ImplSDL3_ProcessEvent(&event);
break; break;
@ -350,6 +355,13 @@ void Application::handleSDLError(const char* message)
msgError("SDL: {}", message); msgError("SDL: {}", message);
} }
void Application::handleCloseRequested()
{
SDL_Event quitEvent;
quitEvent.type = SDL_EVENT_QUIT;
SDL_PushEvent(&quitEvent);
}
bool Application::init() bool Application::init()
{ {
auto addConfigDir = [&](const fs::path& path) auto addConfigDir = [&](const fs::path& path)
@ -516,6 +528,8 @@ bool Application::initSDL()
return false; return false;
} }
SDL_SetHint(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, "0"); // let us handle ourselves
msgInfo("SDL video driver: {}", SDL_GetCurrentVideoDriver()); msgInfo("SDL video driver: {}", SDL_GetCurrentVideoDriver());
SDL_WindowFlags windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY; SDL_WindowFlags windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY;

View File

@ -152,6 +152,8 @@ protected:
virtual void handleSDLEvent(const SDL_Event& event); virtual void handleSDLEvent(const SDL_Event& event);
virtual void handleSDLError(const char* message); virtual void handleSDLError(const char* message);
virtual void handleCloseRequested();
void msgInfo(const char* text) void msgInfo(const char* text)
{ {
handleMessage({ handleMessage({