From 8f35bf8866da06944997580d2b7721b116617330 Mon Sep 17 00:00:00 2001 From: Andrii Danylchenko Date: Tue, 17 Jan 2023 10:22:11 +0200 Subject: [PATCH] Fix android UI shift when keyboard appears --- client/gui/SDL_Extensions.cpp | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/client/gui/SDL_Extensions.cpp b/client/gui/SDL_Extensions.cpp index 6cb332865..eb19a5e97 100644 --- a/client/gui/SDL_Extensions.cpp +++ b/client/gui/SDL_Extensions.cpp @@ -863,20 +863,10 @@ SDL_Color CSDL_Ext::makeColor(ui8 r, ui8 g, ui8 b, ui8 a) void CSDL_Ext::startTextInput(SDL_Rect * where) { - auto impl = [](SDL_Rect * where) - { - if (SDL_IsTextInputActive() == SDL_FALSE) - { - SDL_StartTextInput(); - } - SDL_SetTextInputRect(where); - }; - #ifdef VCMI_APPLE dispatch_async(dispatch_get_main_queue(), ^{ #endif -#ifdef VCMI_IOS // TODO ios: looks like SDL bug actually, try fixing there auto renderer = SDL_GetRenderer(mainWindow); float scaleX, scaleY; @@ -884,17 +874,26 @@ void CSDL_Ext::startTextInput(SDL_Rect * where) SDL_RenderGetScale(renderer, &scaleX, &scaleY); SDL_RenderGetViewport(renderer, &viewport); +#ifdef VCMI_IOS const auto nativeScale = iOS_utils::screenScale(); - auto rectInScreenCoordinates = *where; - rectInScreenCoordinates.x = (viewport.x + rectInScreenCoordinates.x) * scaleX / nativeScale; - rectInScreenCoordinates.y = (viewport.y + rectInScreenCoordinates.y) * scaleY / nativeScale; - rectInScreenCoordinates.w = rectInScreenCoordinates.w * scaleX / nativeScale; - rectInScreenCoordinates.h = rectInScreenCoordinates.h * scaleY / nativeScale; - impl(&rectInScreenCoordinates); -#else - impl(where); + + scaleX /= nativeScale; + scaleY /= nativeScale; #endif + auto rectInScreenCoordinates = *where; + rectInScreenCoordinates.x = (viewport.x + rectInScreenCoordinates.x) * scaleX; + rectInScreenCoordinates.y = (viewport.y + rectInScreenCoordinates.y) * scaleY; + rectInScreenCoordinates.w = rectInScreenCoordinates.w * scaleX; + rectInScreenCoordinates.h = rectInScreenCoordinates.h * scaleY; + + SDL_SetTextInputRect(&rectInScreenCoordinates); + + if (SDL_IsTextInputActive() == SDL_FALSE) + { + SDL_StartTextInput(); + } + #ifdef VCMI_APPLE }); #endif