From c30b56582262afdc34d972174ae9782788589ade Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Mon, 11 Nov 2024 09:19:11 -0800 Subject: [PATCH] imgui: Dispatch SDL text input requests to main thread on macOS. --- src/imgui/renderer/imgui_impl_sdl3.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/imgui/renderer/imgui_impl_sdl3.cpp b/src/imgui/renderer/imgui_impl_sdl3.cpp index 230d396f01..60b440c245 100644 --- a/src/imgui/renderer/imgui_impl_sdl3.cpp +++ b/src/imgui/renderer/imgui_impl_sdl3.cpp @@ -11,6 +11,7 @@ #include #if defined(__APPLE__) #include +#include #endif #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN @@ -71,7 +72,14 @@ static void PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlat auto window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle; SDL_Window* window = SDL_GetWindowFromID(window_id); if ((!data->WantVisible || bd->ime_window != window) && bd->ime_window != nullptr) { - SDL_StopTextInput(bd->ime_window); + auto stop_input = [&bd] { SDL_StopTextInput(bd->ime_window); }; +#ifdef __APPLE__ + dispatch_sync(dispatch_get_main_queue(), ^{ + stop_input(); + }); +#else + stop_input(); +#endif bd->ime_window = nullptr; } if (data->WantVisible) { @@ -80,8 +88,17 @@ static void PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlat r.y = (int)data->InputPos.y; r.w = 1; r.h = (int)data->InputLineHeight; - SDL_SetTextInputArea(window, &r, 0); - SDL_StartTextInput(window); + const auto start_input = [&window, &r] { + SDL_SetTextInputArea(window, &r, 0); + SDL_StartTextInput(window); + }; +#ifdef __APPLE__ + dispatch_sync(dispatch_get_main_queue(), ^{ + start_input(); + }); +#else + start_input(); +#endif bd->ime_window = window; } }