Fixed bug 2212 - SDL_SetTextInputRect has no effect on iOS philhassey Overview: While SDL_SetTextInputRect works perfectly to move my window out of the way of the virtual keyboard using SDL2/Android, on iOS this function has no effect. Steps to Reproduce: Call SDL_SetTextInputRect with a rect near the bottom of the screen before calling SDL_StartTextInput. Actual Results: The iOS virtual keyboard is displayed after calling SDL_StartTextInput, but the screen is not shifted to reveal the TextInputRect region. Expected Results: The screen should be shifted to reveal the TextInputRect region (like with SDL2/Android.) This patch implements SDL_SetTextInputRect for uikit/iOS. It sets up notification handlers to respond to changes in the display of the keyboard. These handlers then change the frame of the view so it is moved out of the way of the keyboard as per SetTextInputRect.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
diff --git a/src/video/uikit/SDL_uikitvideo.m b/src/video/uikit/SDL_uikitvideo.m
index 74b24b8..7be5b1d 100644
--- a/src/video/uikit/SDL_uikitvideo.m
+++ b/src/video/uikit/SDL_uikitvideo.m
@@ -89,6 +89,7 @@ UIKit_CreateDevice(int devindex)
device->ShowScreenKeyboard = UIKit_ShowScreenKeyboard;
device->HideScreenKeyboard = UIKit_HideScreenKeyboard;
device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
+ device->SetTextInputRect = UIKit_SetTextInputRect;
#endif
/* OpenGL (ES) functions */
diff --git a/src/video/uikit/SDL_uikitview.h b/src/video/uikit/SDL_uikitview.h
index ff8a7d2..3921be1 100644
--- a/src/video/uikit/SDL_uikitview.h
+++ b/src/video/uikit/SDL_uikitview.h
@@ -45,6 +45,8 @@
#if SDL_IPHONE_KEYBOARD
UITextField *textField;
BOOL keyboardVisible;
+ SDL_Rect textInputRect;
+ int keyboardHeight;
#endif
@public
@@ -60,11 +62,14 @@
- (void)hideKeyboard;
- (void)initializeKeyboard;
@property (readonly) BOOL keyboardVisible;
+@property (nonatomic,assign) SDL_Rect textInputRect;
+@property (nonatomic,assign) int keyboardHeight;
SDL_bool UIKit_HasScreenKeyboardSupport(_THIS);
void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window);
void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window);
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window);
+void UIKit_SetTextInputRect(_THIS, SDL_Rect *rect);
#endif
diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m
index 66129be..ca9cd06 100644
--- a/src/video/uikit/SDL_uikitview.m
+++ b/src/video/uikit/SDL_uikitview.m
@@ -35,6 +35,8 @@
#include "SDL_uikitmodes.h"
#include "SDL_uikitwindow.h"
+void _uikit_keyboard_init() ;
+
@implementation SDL_uikitview
- (void)dealloc
@@ -197,6 +199,9 @@
*/
#if SDL_IPHONE_KEYBOARD
+@synthesize textInputRect = textInputRect;
+@synthesize keyboardHeight = keyboardHeight;
+
/* Is the iPhone virtual keyboard visible onscreen? */
- (BOOL)keyboardVisible
{
@@ -225,6 +230,8 @@
/* add the UITextField (hidden) to our view */
[self addSubview: textField];
[textField release];
+
+ _uikit_keyboard_init();
}
/* reveal onscreen virtual keyboard */
@@ -352,6 +359,103 @@ SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
return view.keyboardVisible;
}
+
+void _uikit_keyboard_update() {
+ SDL_Window *window = SDL_GetFocusWindow();
+ if (!window) { return; }
+ SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
+ if (!data) { return; }
+ SDL_uikitview *view = data->view;
+ if (!view) { return; }
+
+ SDL_Rect r = view.textInputRect;
+ int height = view.keyboardHeight;
+ int offsetx = 0;
+ int offsety = 0;
+ if (height) {
+ int sw,sh;
+ SDL_GetWindowSize(window,&sw,&sh);
+ int bottom = (r.y + r.h);
+ int kbottom = sh - height;
+ if (kbottom < bottom) {
+ offsety = kbottom-bottom;
+ }
+ }
+ UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
+ if (ui_orient == UIInterfaceOrientationLandscapeLeft) {
+ int tmp = offsetx; offsetx = offsety; offsety = tmp;
+ }
+ if (ui_orient == UIInterfaceOrientationLandscapeRight) {
+ offsety = -offsety;
+ int tmp = offsetx; offsetx = offsety; offsety = tmp;
+ }
+ if (ui_orient == UIInterfaceOrientationPortraitUpsideDown) {
+ offsety = -offsety;
+ }
+ if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) {
+ float scale = [UIScreen mainScreen].scale;
+ offsetx /= scale;
+ offsety /= scale;
+ }
+ view.frame = CGRectMake(offsetx,offsety,view.frame.size.width,view.frame.size.height);
+}
+
+void _uikit_keyboard_set_height(int height) {
+ SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
+ if (view == nil) {
+ return ;
+ }
+
+ view.keyboardHeight = height;
+ _uikit_keyboard_update();
+}
+
+void _uikit_keyboard_init() {
+ NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+ NSOperationQueue *queue = [NSOperationQueue mainQueue];
+ [center addObserverForName:UIKeyboardWillShowNotification
+ object:nil
+ queue:queue
+ usingBlock:^(NSNotification *notification) {
+ int height = 0;
+ CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
+ height = keyboardSize.height;
+ UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
+ if (ui_orient == UIInterfaceOrientationLandscapeRight || ui_orient == UIInterfaceOrientationLandscapeLeft) {
+ height = keyboardSize.width;
+ }
+ if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) {
+ height *= [UIScreen mainScreen].scale;
+ }
+ _uikit_keyboard_set_height(height);
+ }
+ ];
+ [center addObserverForName:UIKeyboardDidHideNotification
+ object:nil
+ queue:queue
+ usingBlock:^(NSNotification *notification) {
+ _uikit_keyboard_set_height(0);
+ }
+ ];
+}
+
+void
+UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
+{
+ if (!rect) {
+ SDL_InvalidParamError("rect");
+ return;
+ }
+
+ SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
+ if (view == nil) {
+ return ;
+ }
+
+ view.textInputRect = *rect;
+}
+
+
#endif /* SDL_IPHONE_KEYBOARD */
#endif /* SDL_VIDEO_DRIVER_UIKIT */