[iOS] Using application delegate window as fallback for textinput plugin (flutter/engine#29108)

This commit is contained in:
Wu Zhong
2021-10-26 02:23:01 +08:00
committed by GitHub
parent 61633f8207
commit c50d640a58
4 changed files with 37 additions and 6 deletions

View File

@@ -312,6 +312,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
viewController ? [viewController getWeakPtr] : fml::WeakPtr<FlutterViewController>();
self.iosPlatformView->SetOwnerViewController(_viewController);
[self maybeSetupPlatformViewChannels];
_textInputPlugin.get().viewController = viewController;
if (viewController) {
__block FlutterEngine* blockSelf = self;
@@ -345,6 +346,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
- (void)notifyViewControllerDeallocated {
[[self lifecycleChannel] sendMessage:@"AppLifecycleState.detached"];
_textInputPlugin.get().viewController = nil;
if (!_allowHeadlessExecution) {
[self destroyContext];
} else {

View File

@@ -15,6 +15,7 @@
@interface FlutterTextInputPlugin : NSObject <FlutterKeySecondaryResponder>
@property(nonatomic, assign) id<FlutterTextInputDelegate> textInputDelegate;
@property(nonatomic, assign) UIViewController* viewController;
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
/**

View File

@@ -1874,12 +1874,11 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) {
}
// The UIView to add FlutterTextInputViews to.
- (UIView*)keyWindow {
UIWindow* keyWindow = [UIApplication sharedApplication].keyWindow;
NSAssert(keyWindow != nullptr,
@"The application must have a key window since the keyboard client "
- (UIView*)hostView {
NSAssert(self.viewController.view != nullptr,
@"The application must have a HostView since the keyboard client "
@"must be part of the responder chain to function");
return keyWindow;
return self.viewController.view;
}
// The UIView to add FlutterTextInputViews to.
@@ -1952,7 +1951,7 @@ static FlutterAutofillType autofillTypeOf(NSDictionary* configuration) {
if (![inputView isDescendantOfView:_inputHider]) {
[_inputHider addSubview:inputView];
}
UIView* parentView = self.keyWindow;
UIView* parentView = self.hostView;
if (_inputHider.superview != parentView) {
[parentView addSubview:_inputHider];
}

View File

@@ -9,9 +9,14 @@
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
FLUTTER_ASSERT_ARC
@interface FlutterEngine ()
- (nonnull FlutterTextInputPlugin*)textInputPlugin;
@end
@interface FlutterTextInputView ()
@property(nonatomic, copy) NSString* autofillId;
- (void)setEditableTransform:(NSArray*)matrix;
@@ -58,6 +63,8 @@ FLUTTER_ASSERT_ARC
clearText:(BOOL)clearText
delayRemoval:(BOOL)delayRemoval;
- (NSArray<UIView*>*)textInputViews;
- (UIView*)hostView;
- (void)addToInputParentViewIfNeeded:(FlutterTextInputView*)inputView;
@end
@interface FlutterTextInputPluginTest : XCTestCase
@@ -68,6 +75,7 @@ FLUTTER_ASSERT_ARC
NSDictionary* _passwordTemplate;
id engine;
FlutterTextInputPlugin* textInputPlugin;
FlutterViewController* viewController;
}
- (void)setUp {
@@ -76,6 +84,8 @@ FLUTTER_ASSERT_ARC
engine = OCMClassMock([FlutterEngine class]);
textInputPlugin = [[FlutterTextInputPlugin alloc] init];
textInputPlugin.textInputDelegate = engine;
viewController = [FlutterViewController new];
textInputPlugin.viewController = viewController;
}
- (void)tearDown {
@@ -87,6 +97,7 @@ FLUTTER_ASSERT_ARC
[textInputPlugin cleanUpViewHierarchy:YES clearText:YES delayRemoval:NO];
[[[[textInputPlugin textInputView] superview] subviews]
makeObjectsPerformSelector:@selector(removeFromSuperview)];
viewController = nil;
[super tearDown];
}
@@ -1345,8 +1356,11 @@ FLUTTER_ASSERT_ARC
}
- (void)testFlutterTextInputPluginRetainsFlutterTextInputView {
FlutterViewController* flutterViewController = [FlutterViewController new];
FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] init];
myInputPlugin.textInputDelegate = engine;
myInputPlugin.viewController = flutterViewController;
__weak UIView* activeView;
@autoreleasepool {
FlutterMethodCall* setClientCall = [FlutterMethodCall
@@ -1369,4 +1383,19 @@ FLUTTER_ASSERT_ARC
XCTAssertNotNil(activeView);
}
- (void)testFlutterTextInputPluginHostViewNilCrash {
FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] init];
myInputPlugin.viewController = nil;
XCTAssertThrows([myInputPlugin hostView], @"Throws exception if host view is nil");
}
- (void)testFlutterTextInputPluginHostViewNotNil {
FlutterViewController* flutterViewController = [FlutterViewController new];
FlutterEngine* flutterEngine = [[FlutterEngine alloc] init];
[flutterEngine runWithEntrypoint:nil];
flutterEngine.viewController = flutterViewController;
XCTAssertNotNil(flutterEngine.textInputPlugin.viewController);
XCTAssertNotNil([flutterEngine.textInputPlugin hostView]);
}
@end