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

[macOS] TextInputPlugin should mark navigation events in IME popover as handled #46141

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 64,6 @@
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
- (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange;
- (NSDictionary*)editingState;
@property(nonatomic) NSTextInputContext* textInputContext;
@property(readwrite, nonatomic) NSString* customRunLoopMode;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 620,15 @@ - (BOOL)handleKeyEvent:(NSEvent*)event {
// text command (indicated by calling doCommandBySelector) or might not (for example, Cmd Q). In
// the latter case, this command somehow has not been executed yet and Flutter must dispatch it to
// the next responder. See https://github.com/flutter/flutter/issues/106354 .
if (event.isKeyEquivalent && !_eventProducedOutput) {
// The event is also not redispatched if there is IME composition active, because it might be
// handled by the IME. See https://github.com/flutter/flutter/issues/134699

// both NSEventModifierFlagNumericPad and NSEventModifierFlagFunction are set for arrow keys.
bool is_navigation = event.modifierFlags & NSEventModifierFlagFunction &&
event.modifierFlags & NSEventModifierFlagNumericPad;
bool is_navigation_in_ime = is_navigation && self.hasMarkedText;

if (event.isKeyEquivalent && !is_navigation_in_ime && !_eventProducedOutput) {
return NO;
}
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 1361,89 @@ - (bool)testPerformKeyEquivalent {
return true;
}

- (bool)handleArrowKeyWhenImePopoverIsActive {
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
[engineMock binaryMessenger])
.andReturn(binaryMessengerMock);
OCMStub([[engineMock ignoringNonObjectArgs] sendKeyEvent:FlutterKeyEvent {}
callback:nil
userData:nil]);

NSTextInputContext* textInputContext = OCMClassMock([NSTextInputContext class]);
OCMStub([textInputContext handleEvent:[OCMArg any]]).andReturn(YES);

FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
nibName:@""
bundle:nil];

FlutterTextInputPlugin* plugin =
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];

plugin.textInputContext = textInputContext;

NSDictionary* setClientConfig = @{
@"inputAction" : @"action",
@"enableDeltaModel" : @"true",
@"inputType" : @{@"name" : @"inputName"},
};
[plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
arguments:@[ @(1), setClientConfig ]]
result:^(id){
}];

[plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.show"
arguments:@[]]
result:^(id){
}];

// Set marked text, simulate active IME popover.
[plugin setMarkedText:@"m"
selectedRange:NSMakeRange(0, 1)
replacementRange:NSMakeRange(NSNotFound, 0)];

// Right arrow key. This, unlike the key below should be handled by the plugin.
NSEvent* event = [NSEvent keyEventWithType:NSEventTypeKeyDown
location:NSZeroPoint
modifierFlags:0xa00100
timestamp:0
windowNumber:0
context:nil
characters:@"\uF702"
charactersIgnoringModifiers:@"\uF702"
isARepeat:NO
keyCode:0x4];

// Plugin should mark the event as key equivalent.
[plugin performKeyEquivalent:event];

if ([plugin handleKeyEvent:event] != true) {
return false;
}

// CTRL H (delete backwards)
event = [NSEvent keyEventWithType:NSEventTypeKeyDown
location:NSZeroPoint
modifierFlags:0x40101
timestamp:0
windowNumber:0
context:nil
characters:@"\uF702"
charactersIgnoringModifiers:@"\uF702"
isARepeat:NO
keyCode:0x4];

// Plugin should mark the event as key equivalent.
[plugin performKeyEquivalent:event];

if ([plugin handleKeyEvent:event] != false) {
return false;
}

return true;
}

- (bool)unhandledKeyEquivalent {
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
Expand Down Expand Up @@ -1814,6 1897,10 @@ - (bool)testSelectorsAreForwardedToFramework {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testPerformKeyEquivalent]);
}

TEST(FlutterTextInputPluginTest, HandleArrowKeyWhenImePopoverIsActive) {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] handleArrowKeyWhenImePopoverIsActive]);
}

TEST(FlutterTextInputPluginTest, UnhandledKeyEquivalent) {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] unhandledKeyEquivalent]);
}
Expand Down