diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h index 38539f5f41..96eff563e0 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h @@ -165,4 +165,9 @@ FLUTTER_DARWIN_EXPORT - (instancetype)initWithOwner:(FlutterTextInputPlugin*)textInputPlugin NS_DESIGNATED_INITIALIZER; @end + +@interface UIView (FindFirstResponder) +@property(nonatomic, readonly) id flutterFirstResponder; +@end + #endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTINPUTPLUGIN_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index a1ec6bdc85..4101bd9ab3 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -3,6 +3,7 @@ // found in the LICENSE file. #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h" #import #import @@ -45,6 +46,8 @@ static NSString* const kDeprecatedSetSelectionRectsMethod = @"TextInput.setSelec static NSString* const kSetSelectionRectsMethod = @"Scribble.setSelectionRects"; static NSString* const kStartLiveTextInputMethod = @"TextInput.startLiveTextInput"; static NSString* const kUpdateConfigMethod = @"TextInput.updateConfig"; +static NSString* const kOnInteractiveKeyboardPointerMoveMethod = + @"TextInput.onPointerMoveForInteractiveKeyboard"; #pragma mark - TextInputConfiguration Field Names static NSString* const kSecureTextEntry = @"obscureText"; @@ -761,6 +764,7 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point, @property(nonatomic, assign) CGRect markedRect; @property(nonatomic) BOOL isVisibleToAutofill; @property(nonatomic, assign) BOOL accessibilityEnabled; +@property(nonatomic, assign) int textInputClient; // The composed character that is temporarily removed by the keyboard API. // This is cleared at the start of each keyboard interaction. (Enter a character, delete a character // etc) @@ -2214,6 +2218,11 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point, @property(nonatomic, retain) FlutterTextInputView* activeView; @property(nonatomic, retain) FlutterTextInputViewAccessibilityHider* inputHider; @property(nonatomic, readonly, weak) id viewResponder; + +@property(nonatomic, strong) UIView* keyboardViewContainer; +@property(nonatomic, strong) UIView* keyboardView; +@property(nonatomic, strong) UIView* cachedFirstResponder; +@property(nonatomic, assign) CGRect keyboardRect; @end @implementation FlutterTextInputPlugin { @@ -2222,18 +2231,29 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point, - (instancetype)initWithDelegate:(id)textInputDelegate { self = [super init]; - if (self) { // `_textInputDelegate` is a weak reference because it should retain FlutterTextInputPlugin. _textInputDelegate = textInputDelegate; _autofillContext = [[NSMutableDictionary alloc] init]; _inputHider = [[FlutterTextInputViewAccessibilityHider alloc] init]; _scribbleElements = [[NSMutableDictionary alloc] init]; + _keyboardViewContainer = [[UIView alloc] init]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleKeyboardWillShow:) + name:UIKeyboardWillShowNotification + object:nil]; } return self; } +- (void)handleKeyboardWillShow:(NSNotification*)notification { + NSDictionary* keyboardInfo = [notification userInfo]; + NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; + _keyboardRect = [keyboardFrameEnd CGRectValue]; +} + - (void)dealloc { [self hideTextInput]; } @@ -2295,11 +2315,67 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point, } else if ([method isEqualToString:kUpdateConfigMethod]) { [self updateConfig:args]; result(nil); + } else if ([method isEqualToString:kOnInteractiveKeyboardPointerMoveMethod]) { + CGFloat pointerY = (CGFloat)[args[@"pointerY"] doubleValue]; + [self handlePointerMove:pointerY]; + result(nil); } else { result(FlutterMethodNotImplemented); } } +- (void)handlePointerMove:(CGFloat)pointerY { + // View must be loaded at this point. + UIScreen* screen = _viewController.flutterScreenIfViewLoaded; + double screenHeight = screen.bounds.size.height; + double keyboardHeight = _keyboardRect.size.height; + if (screenHeight - keyboardHeight <= pointerY) { + // If the pointer is within the bounds of the keyboard. + if (_keyboardView.superview == nil) { + // If no screenshot has been taken. + [self takeKeyboardScreenshotAndDisplay]; + [self hideKeyboardWithoutAnimation]; + } else { + [self setKeyboardContainerHeight:pointerY]; + } + } else { + if (_keyboardView.superview != nil) { + // Keeps keyboard at proper height. + _keyboardViewContainer.frame = _keyboardRect; + } + } +} + +- (void)setKeyboardContainerHeight:(CGFloat)pointerY { + CGRect frameRect = _keyboardRect; + frameRect.origin.y = pointerY; + _keyboardViewContainer.frame = frameRect; +} + +- (void)hideKeyboardWithoutAnimation { + [UIView setAnimationsEnabled:NO]; + _cachedFirstResponder = UIApplication.sharedApplication.keyWindow.flutterFirstResponder; + [_cachedFirstResponder resignFirstResponder]; + [UIView setAnimationsEnabled:YES]; +} + +- (void)takeKeyboardScreenshotAndDisplay { + // View must be loaded at this point + UIScreen* screen = _viewController.flutterScreenIfViewLoaded; + UIView* keyboardSnap = [screen snapshotViewAfterScreenUpdates:YES]; + keyboardSnap = [keyboardSnap resizableSnapshotViewFromRect:_keyboardRect + afterScreenUpdates:YES + withCapInsets:UIEdgeInsetsZero]; + _keyboardView = keyboardSnap; + [_keyboardViewContainer addSubview:_keyboardView]; + if (_keyboardViewContainer.superview == nil) { + [UIApplication.sharedApplication.delegate.window.rootViewController.view + addSubview:_keyboardViewContainer]; + } + _keyboardViewContainer.layer.zPosition = NSIntegerMax; + _keyboardViewContainer.frame = _keyboardRect; +} + - (void)setEditableSizeAndTransform:(NSDictionary*)dictionary { [_activeView setEditableTransform:dictionary[@"transform"]]; if ([_activeView isScribbleAvailable]) { @@ -2764,3 +2840,21 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point, return NO; } @end + +/** + * Recursively searches the UIView's subviews to locate the First Responder + */ +@implementation UIView (FindFirstResponder) +- (id)flutterFirstResponder { + if (self.isFirstResponder) { + return self; + } + for (UIView* subView in self.subviews) { + UIView* firstResponder = subView.flutterFirstResponder; + if (firstResponder) { + return firstResponder; + } + } + return nil; +} +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index f39168fdeb..00302678a1 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -60,6 +60,9 @@ FLUTTER_ASSERT_ARC @interface FlutterTextInputPlugin () @property(nonatomic, assign) FlutterTextInputView* activeView; +@property(nonatomic, readonly) UIView* keyboardViewContainer; +@property(nonatomic, readonly) UIView* keyboardView; +@property(nonatomic, readonly) CGRect keyboardRect; @property(nonatomic, readonly) NSMutableDictionary* autofillContext; @@ -2425,4 +2428,219 @@ FLUTTER_ASSERT_ARC XCTAssertNil(activeView.superview, @"activeView must be removed from view hierarchy."); } +- (void)testInteractiveKeyboardAfterUserScrollWillResignFirstResponder { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + [UIApplication.sharedApplication.keyWindow addSubview:inputView]; + + [inputView setTextInputClient:123]; + [inputView reloadInputViews]; + [inputView becomeFirstResponder]; + XCTAssert(inputView.isFirstResponder); + + CGRect keyboardFrame = CGRectMake(0, 500, 500, 500); + [NSNotificationCenter.defaultCenter + postNotificationName:UIKeyboardWillShowNotification + object:nil + userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}]; + FlutterMethodCall* onPointerMoveCall = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.onPointerMoveForInteractiveKeyboard" + arguments:@{@"pointerY" : @(500)}]; + [textInputPlugin handleMethodCall:onPointerMoveCall + result:^(id _Nullable result){ + }]; + XCTAssertFalse(inputView.isFirstResponder); +} + +- (void)testInteractiveKeyboardAfterUserScrollToTopOfKeyboardWillTakeScreenshot { + NSSet* scenes = UIApplication.sharedApplication.connectedScenes; + XCTAssertEqual(scenes.count, 1UL, @"There must only be 1 scene for test"); + UIScene* scene = scenes.anyObject; + XCTAssert([scene isKindOfClass:[UIWindowScene class]], @"Must be a window scene for test"); + UIWindowScene* windowScene = (UIWindowScene*)scene; + XCTAssert(windowScene.windows.count > 0, @"There must be at least 1 window for test"); + UIWindow* window = windowScene.windows[0]; + [window addSubview:viewController.view]; + + [viewController loadView]; + + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + [UIApplication.sharedApplication.keyWindow addSubview:inputView]; + + [inputView setTextInputClient:123]; + [inputView reloadInputViews]; + [inputView becomeFirstResponder]; + + if (textInputPlugin.keyboardView.superview != nil) { + for (UIView* subView in textInputPlugin.keyboardViewContainer.subviews) { + [subView removeFromSuperview]; + } + } + XCTAssert(textInputPlugin.keyboardView.superview == nil); + CGRect keyboardFrame = CGRectMake(0, 500, 500, 500); + [NSNotificationCenter.defaultCenter + postNotificationName:UIKeyboardWillShowNotification + object:nil + userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}]; + FlutterMethodCall* onPointerMoveCall = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.onPointerMoveForInteractiveKeyboard" + arguments:@{@"pointerY" : @(510)}]; + [textInputPlugin handleMethodCall:onPointerMoveCall + result:^(id _Nullable result){ + }]; + XCTAssertFalse(textInputPlugin.keyboardView.superview == nil); + for (UIView* subView in textInputPlugin.keyboardViewContainer.subviews) { + [subView removeFromSuperview]; + } +} + +- (void)testInteractiveKeyboardScreenshotWillBeMovedDownAfterUserScroll { + NSSet* scenes = UIApplication.sharedApplication.connectedScenes; + XCTAssertEqual(scenes.count, 1UL, @"There must only be 1 scene for test"); + UIScene* scene = scenes.anyObject; + XCTAssert([scene isKindOfClass:[UIWindowScene class]], @"Must be a window scene for test"); + UIWindowScene* windowScene = (UIWindowScene*)scene; + XCTAssert(windowScene.windows.count > 0, @"There must be at least 1 window for test"); + UIWindow* window = windowScene.windows[0]; + [window addSubview:viewController.view]; + + [viewController loadView]; + + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + [UIApplication.sharedApplication.keyWindow addSubview:inputView]; + + [inputView setTextInputClient:123]; + [inputView reloadInputViews]; + [inputView becomeFirstResponder]; + + CGRect keyboardFrame = CGRectMake(0, 500, 500, 500); + [NSNotificationCenter.defaultCenter + postNotificationName:UIKeyboardWillShowNotification + object:nil + userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}]; + FlutterMethodCall* onPointerMoveCall = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.onPointerMoveForInteractiveKeyboard" + arguments:@{@"pointerY" : @(510)}]; + [textInputPlugin handleMethodCall:onPointerMoveCall + result:^(id _Nullable result){ + }]; + XCTAssert(textInputPlugin.keyboardView.superview != nil); + + XCTAssertEqual(textInputPlugin.keyboardViewContainer.frame.origin.y, keyboardFrame.origin.y); + + FlutterMethodCall* onPointerMoveCallMove = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.onPointerMoveForInteractiveKeyboard" + arguments:@{@"pointerY" : @(600)}]; + [textInputPlugin handleMethodCall:onPointerMoveCallMove + result:^(id _Nullable result){ + }]; + XCTAssert(textInputPlugin.keyboardView.superview != nil); + + XCTAssertEqual(textInputPlugin.keyboardViewContainer.frame.origin.y, 600.0); + + for (UIView* subView in textInputPlugin.keyboardViewContainer.subviews) { + [subView removeFromSuperview]; + } +} + +- (void)testInteractiveKeyboardScreenshotWillBeMovedToOrginalPositionAfterUserScroll { + NSSet* scenes = UIApplication.sharedApplication.connectedScenes; + XCTAssertEqual(scenes.count, 1UL, @"There must only be 1 scene for test"); + UIScene* scene = scenes.anyObject; + XCTAssert([scene isKindOfClass:[UIWindowScene class]], @"Must be a window scene for test"); + UIWindowScene* windowScene = (UIWindowScene*)scene; + XCTAssert(windowScene.windows.count > 0, @"There must be at least 1 window for test"); + UIWindow* window = windowScene.windows[0]; + [window addSubview:viewController.view]; + + [viewController loadView]; + + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + [UIApplication.sharedApplication.keyWindow addSubview:inputView]; + + [inputView setTextInputClient:123]; + [inputView reloadInputViews]; + [inputView becomeFirstResponder]; + + CGRect keyboardFrame = CGRectMake(0, 500, 500, 500); + [NSNotificationCenter.defaultCenter + postNotificationName:UIKeyboardWillShowNotification + object:nil + userInfo:@{UIKeyboardFrameEndUserInfoKey : @(keyboardFrame)}]; + FlutterMethodCall* onPointerMoveCall = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.onPointerMoveForInteractiveKeyboard" + arguments:@{@"pointerY" : @(500)}]; + [textInputPlugin handleMethodCall:onPointerMoveCall + result:^(id _Nullable result){ + }]; + XCTAssert(textInputPlugin.keyboardView.superview != nil); + XCTAssertEqual(textInputPlugin.keyboardViewContainer.frame.origin.y, keyboardFrame.origin.y); + + FlutterMethodCall* onPointerMoveCallMove = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.onPointerMoveForInteractiveKeyboard" + arguments:@{@"pointerY" : @(600)}]; + [textInputPlugin handleMethodCall:onPointerMoveCallMove + result:^(id _Nullable result){ + }]; + XCTAssert(textInputPlugin.keyboardView.superview != nil); + XCTAssertEqual(textInputPlugin.keyboardViewContainer.frame.origin.y, 600.0); + + FlutterMethodCall* onPointerMoveCallBackUp = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.onPointerMoveForInteractiveKeyboard" + arguments:@{@"pointerY" : @(10)}]; + [textInputPlugin handleMethodCall:onPointerMoveCallBackUp + result:^(id _Nullable result){ + }]; + XCTAssert(textInputPlugin.keyboardView.superview != nil); + XCTAssertEqual(textInputPlugin.keyboardViewContainer.frame.origin.y, keyboardFrame.origin.y); + for (UIView* subView in textInputPlugin.keyboardViewContainer.subviews) { + [subView removeFromSuperview]; + } +} + +- (void)testInteractiveKeyboardFindFirstResponderRecursive { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + [UIApplication.sharedApplication.keyWindow addSubview:inputView]; + [inputView setTextInputClient:123]; + [inputView reloadInputViews]; + [inputView becomeFirstResponder]; + + UIView* firstResponder = UIApplication.sharedApplication.keyWindow.flutterFirstResponder; + XCTAssertEqualObjects(inputView, firstResponder); +} + +- (void)testInteractiveKeyboardFindFirstResponderRecursiveInMultipleSubviews { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + FlutterTextInputView* subInputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + FlutterTextInputView* otherSubInputView = + [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + FlutterTextInputView* subFirstResponderInputView = + [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + [subInputView addSubview:subFirstResponderInputView]; + [inputView addSubview:subInputView]; + [inputView addSubview:otherSubInputView]; + [UIApplication.sharedApplication.keyWindow addSubview:inputView]; + [inputView setTextInputClient:123]; + [inputView reloadInputViews]; + [subInputView setTextInputClient:123]; + [subInputView reloadInputViews]; + [otherSubInputView setTextInputClient:123]; + [otherSubInputView reloadInputViews]; + [subFirstResponderInputView setTextInputClient:123]; + [subFirstResponderInputView reloadInputViews]; + [subFirstResponderInputView becomeFirstResponder]; + + UIView* firstResponder = UIApplication.sharedApplication.keyWindow.flutterFirstResponder; + XCTAssertEqualObjects(subFirstResponderInputView, firstResponder); +} + +- (void)testInteractiveKeyboardFindFirstResponderIsNilRecursive { + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; + [UIApplication.sharedApplication.keyWindow addSubview:inputView]; + [inputView setTextInputClient:123]; + [inputView reloadInputViews]; + + UIView* firstResponder = UIApplication.sharedApplication.keyWindow.flutterFirstResponder; + XCTAssertNil(firstResponder); +} + @end