diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm index d2509fcfa4..92820541de 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm @@ -470,6 +470,13 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base, if (!_shown) { return NO; } + + // NSTextInputContext sometimes deactivates itself without calling + // deactivate. One such example is when the composing region is deleted. + // TODO(LongCatIsLooong): put FlutterTextInputPlugin in the view hierarchy and + // request/resign first responder when needed. Activate/deactivate shouldn't + // be called by the application. + [_textInputContext activate]; return [_textInputContext handleEvent:event]; } @@ -536,6 +543,10 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base, [self.flutterViewController scrollWheel:event]; } +- (NSTextInputContext*)inputContext { + return _textInputContext; +} + #pragma mark - #pragma mark NSTextInputClient diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm index 74dc303efc..8bc3588d41 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm @@ -28,6 +28,11 @@ @end +@interface NSTextInputContext (Private) +// This is a private method. +- (BOOL)isActive; +@end + @interface FlutterInputPluginTestObjc : NSObject - (bool)testEmptyCompositionRange; - (bool)testClearClientDuringComposing; @@ -245,6 +250,60 @@ return true; } +- (bool)testInputContextIsKeptActive { + id engineMock = OCMClassMock([FlutterEngine class]); + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock + nibName:@"" + bundle:nil]; + + FlutterTextInputPlugin* plugin = + [[FlutterTextInputPlugin alloc] initWithViewController:viewController]; + + [plugin handleMethodCall:[FlutterMethodCall + methodCallWithMethodName:@"TextInput.setClient" + arguments:@[ + @(1), @{ + @"inputAction" : @"action", + @"inputType" : @{@"name" : @"inputName"}, + } + ]] + result:^(id){ + }]; + + [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState" + arguments:@{ + @"text" : @"", + @"selectionBase" : @(0), + @"selectionExtent" : @(0), + @"composingBase" : @(-1), + @"composingExtent" : @(-1), + }] + result:^(id){ + }]; + + [plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.show" + arguments:@[]] + result:^(id){ + }]; + + [plugin.inputContext deactivate]; + EXPECT_EQ(plugin.inputContext.isActive, NO); + NSEvent* keyEvent = [NSEvent keyEventWithType:NSEventTypeKeyDown + location:NSZeroPoint + modifierFlags:0x100 + timestamp:0 + windowNumber:0 + context:nil + characters:@"" + charactersIgnoringModifiers:@"" + isARepeat:NO + keyCode:0x50]; + + [plugin handleKeyEvent:keyEvent]; + EXPECT_EQ(plugin.inputContext.isActive, YES); + return true; +} + - (bool)testClearClientDuringComposing { // Set up FlutterTextInputPlugin. id engineMock = OCMClassMock([FlutterEngine class]); @@ -637,6 +696,10 @@ TEST(FlutterTextInputPluginTest, TestComposingRegionRemovedByFramework) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testComposingRegionRemovedByFramework]); } +TEST(FlutterTextInputPluginTest, TestTextInputContextIsKeptAlive) { + ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testInputContextIsKeptActive]); +} + TEST(FlutterTextInputPluginTest, TestClearClientDuringComposing) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testClearClientDuringComposing]); }