imgui: Dispatch SDL text input requests to main thread on macOS. (#1519)

This commit is contained in:
squidbus 2024-11-11 23:27:30 -08:00 committed by GitHub
parent b64dcd2f56
commit f5618e0342
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,6 +11,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#if defined(__APPLE__) #if defined(__APPLE__)
#include <TargetConditionals.h> #include <TargetConditionals.h>
#include <dispatch/dispatch.h>
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN #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; auto window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
SDL_Window* window = SDL_GetWindowFromID(window_id); SDL_Window* window = SDL_GetWindowFromID(window_id);
if ((!data->WantVisible || bd->ime_window != window) && bd->ime_window != nullptr) { 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; bd->ime_window = nullptr;
} }
if (data->WantVisible) { if (data->WantVisible) {
@ -80,8 +88,17 @@ static void PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlat
r.y = (int)data->InputPos.y; r.y = (int)data->InputPos.y;
r.w = 1; r.w = 1;
r.h = (int)data->InputLineHeight; r.h = (int)data->InputLineHeight;
SDL_SetTextInputArea(window, &r, 0); const auto start_input = [&window, &r] {
SDL_StartTextInput(window); 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; bd->ime_window = window;
} }
} }