Parcourir la source

Add `SetTextInputProperties` to video device API and fix iOS handling

Salman Alshamrani il y a 5 mois
Parent
commit
972c6d0b82

+ 1 - 0
src/video/SDL_sysvideo.h

@@ -352,6 +352,7 @@ struct SDL_VideoDevice
     bool (*HasScreenKeyboardSupport)(SDL_VideoDevice *_this);
     void (*ShowScreenKeyboard)(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props);
     void (*HideScreenKeyboard)(SDL_VideoDevice *_this, SDL_Window *window);
+    void (*SetTextInputProperties)(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props);
     bool (*IsScreenKeyboardShown)(SDL_VideoDevice *_this, SDL_Window *window);
 
     // Clipboard

+ 4 - 0
src/video/SDL_video.c

@@ -5299,6 +5299,10 @@ bool SDL_StartTextInputWithProperties(SDL_Window *window, SDL_PropertiesID props
         }
     }
 
+    if (_this->SetTextInputProperties) {
+        _this->SetTextInputProperties(_this, window, props);
+    }
+
     // Show the on-screen keyboard, if desired
     if (AutoShowingScreenKeyboard() && !SDL_ScreenKeyboardShown(window)) {
         if (_this->ShowScreenKeyboard) {

+ 1 - 0
src/video/uikit/SDL_uikitvideo.m

@@ -99,6 +99,7 @@ static SDL_VideoDevice *UIKit_CreateDevice(void)
         device->HasScreenKeyboardSupport = UIKit_HasScreenKeyboardSupport;
         device->StartTextInput = UIKit_StartTextInput;
         device->StopTextInput = UIKit_StopTextInput;
+        device->SetTextInputProperties = UIKit_SetTextInputProperties;
         device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
         device->UpdateTextInputArea = UIKit_UpdateTextInputArea;
 #endif

+ 1 - 0
src/video/uikit/SDL_uikitviewcontroller.h

@@ -90,6 +90,7 @@
 bool UIKit_HasScreenKeyboardSupport(SDL_VideoDevice *_this);
 bool UIKit_StartTextInput(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props);
 bool UIKit_StopTextInput(SDL_VideoDevice *_this, SDL_Window *window);
+void UIKit_SetTextInputProperties(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props);
 bool UIKit_IsScreenKeyboardShown(SDL_VideoDevice *_this, SDL_Window *window);
 bool UIKit_UpdateTextInputArea(SDL_VideoDevice *_this, SDL_Window *window);
 #endif

+ 23 - 1
src/video/uikit/SDL_uikitviewcontroller.m

@@ -470,6 +470,21 @@ static void SDLCALL SDL_HideHomeIndicatorHintChanged(void *userdata, const char
     } else {
         textField.enablesReturnKeyAutomatically = NO;
     }
+
+    if (!textField.window) {
+        /* textField has not been added to the view yet,
+         we don't have to do anything. */
+        return;
+    }
+
+    // the text field needs to be re-added to the view in order to update correctly.
+    UIView *superview = textField.superview;
+    [textField removeFromSuperview];
+    [superview addSubview:textField];
+
+    if (SDL_TextInputActive(window)) {
+        [textField becomeFirstResponder];
+    }
 }
 
 /* requests the SDL text field to become focused and accept text input.
@@ -672,7 +687,6 @@ bool UIKit_StartTextInput(SDL_VideoDevice *_this, SDL_Window *window, SDL_Proper
 {
     @autoreleasepool {
         SDL_uikitviewcontroller *vc = GetWindowViewController(window);
-        [vc setTextFieldProperties:props];
         return [vc startTextInput];
     }
 }
@@ -685,6 +699,14 @@ bool UIKit_StopTextInput(SDL_VideoDevice *_this, SDL_Window *window)
     }
 }
 
+void UIKit_SetTextInputProperties(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props)
+{
+    @autoreleasepool {
+        SDL_uikitviewcontroller *vc = GetWindowViewController(window);
+        [vc setTextFieldProperties:props];
+    }
+}
+
 bool UIKit_IsScreenKeyboardShown(SDL_VideoDevice *_this, SDL_Window *window)
 {
     @autoreleasepool {