Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iOS text input not working well with password auto-fill integration #11699

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/video/uikit/SDL_uikitviewcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#endif

@interface SDLUITextField : UITextField

@property(nonatomic, assign) SDL_Window *sdlWindow;

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender;
@end

Expand Down
88 changes: 47 additions & 41 deletions src/video/uikit/SDL_uikitviewcontroller.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ static void SDLCALL SDL_HideHomeIndicatorHintChanged(void *userdata, const char
#endif

@implementation SDLUITextField : UITextField

@synthesize sdlWindow;

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:)) {
Expand All @@ -69,6 +72,46 @@ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender

return [super canPerformAction:action withSender:sender];
}

- (BOOL)becomeFirstResponder
{
if ([super becomeFirstResponder])
{
if (!SDL_TextInputActive(sdlWindow))
SDL_StartTextInput(sdlWindow);

return YES;
}

return NO;
}

- (BOOL)resignFirstResponder
{
if ([super resignFirstResponder])
{
// resigning indicates that the user dismissed the keyboard via some method,
// synchronise the text input state accordingly.
if (SDL_TextInputActive(sdlWindow))
SDL_StopTextInput(sdlWindow);

return YES;
}

return NO;
}

// these are callled internally by SDL when text input is started/stopped.
- (BOOL)becomeFirstResponderFromSDL
{
return [super becomeFirstResponder];
}

- (BOOL)resignFirstResponderFromSDL
{
return [super resignFirstResponder];
}

@end

@implementation SDL_uikitviewcontroller
Expand All @@ -80,8 +123,6 @@ @implementation SDL_uikitviewcontroller

#ifdef SDL_IPHONE_KEYBOARD
SDLUITextField *textField;
BOOL hidingKeyboard;
BOOL rotatingOrientation;
NSString *committedText;
NSString *obligateForBackspace;
#endif
Expand All @@ -96,8 +137,6 @@ - (instancetype)initWithSDLWindow:(SDL_Window *)_window

#ifdef SDL_IPHONE_KEYBOARD
[self initKeyboard];
hidingKeyboard = NO;
rotatingOrientation = NO;
#endif

#ifdef SDL_PLATFORM_TVOS
Expand Down Expand Up @@ -285,6 +324,7 @@ - (void)initKeyboard
{
obligateForBackspace = @" "; // 64 space
textField = [[SDLUITextField alloc] initWithFrame:CGRectZero];
textField.sdlWindow = window;
textField.delegate = self;
// placeholder so there is something to delete!
textField.text = obligateForBackspace;
Expand Down Expand Up @@ -360,18 +400,6 @@ - (void)setView:(UIView *)view
}
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
rotatingOrientation = YES;
[coordinator
animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
}
completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
self->rotatingOrientation = NO;
}];
}

- (void)deinitKeyboard
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
Expand Down Expand Up @@ -514,7 +542,7 @@ - (bool)startTextInput
return true;
}

return [textField becomeFirstResponder];
return [textField becomeFirstResponderFromSDL];
}

/* requests the SDL text field to lose focus and stop accepting text input.
Expand All @@ -528,7 +556,7 @@ - (bool)stopTextInput
return true;
}

return [textField resignFirstResponder];
return [textField resignFirstResponderFromSDL];
}

- (void)keyboardWillShow:(NSNotification *)notification
Expand All @@ -542,37 +570,15 @@ - (void)keyboardWillShow:(NSNotification *)notification

[self setKeyboardHeight:(int)kbrect.size.height];
#endif

/* A keyboard hide transition has been interrupted with a show (keyboardWillHide has been called but keyboardDidHide didn't).
* since text input was stopped by the hide, we have to start it again. */
if (hidingKeyboard) {
SDL_StartTextInput(window);
hidingKeyboard = NO;
}
}

- (void)keyboardWillHide:(NSNotification *)notification
{
hidingKeyboard = YES;
[self setKeyboardHeight:0];

/* When the user dismisses the software keyboard by the "hide" button in the bottom right corner,
* we want to reflect that on SDL_TextInputActive by calling SDL_StopTextInput...on certain conditions */
if (SDL_TextInputActive(window)
/* keyboardWillHide gets called when a hardware keyboard is attached,
* keep text input state active if hiding while there is a hardware keyboard.
* if the hardware keyboard gets detached, the software keyboard will appear anyway. */
&& !SDL_HasKeyboard()
/* When the device changes orientation, a sequence of hide and show transitions are triggered.
* keep text input state active in this case. */
&& !rotatingOrientation) {
SDL_StopTextInput(window);
}
}

- (void)keyboardDidHide:(NSNotification *)notification
{
hidingKeyboard = NO;
}

- (void)textFieldTextDidChange:(NSNotification *)notification
Expand Down Expand Up @@ -656,7 +662,7 @@ - (void)setKeyboardHeight:(int)height
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.markedTextRange == nil) {
if (textField.text.length < 16) {
if ([string length] == 0 && textField.text.length < 16) {
textField.text = obligateForBackspace;
committedText = textField.text;
}
Expand Down
Loading