[macOS TextInputPlugin] Reactivate input context on key event (flutter/engine#32117)

This commit is contained in:
LongCatIsLooong
2022-03-18 15:10:10 -07:00
committed by GitHub
parent 26b2d12902
commit ddadbbc372
2 changed files with 74 additions and 0 deletions

View File

@@ -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

View File

@@ -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]);
}