From 839d6adae776b7aed5c1e13e411398bd4c136617 Mon Sep 17 00:00:00 2001 From: Matej Knopp Date: Thu, 28 Sep 2023 22:37:47 +0200 Subject: [PATCH] [macOS] TextInputPlugin should mark navigation events in IME popover as handled (flutter/engine#46141) Fixes https://github.com/flutter/flutter/issues/134699 Because of NSTextInputContext API limitations it is not straightforward to determine whether `TextInputPlugin` has handled a text equivalent event or whether it should pass it on. Previously we marked all event that didn't result in a TextInputClient action as unhandled, but that's does not work for arrow key events while the IME popover is active. This PR will mark arrow keys event as handled if there is active composition. *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../framework/Source/FlutterTextInputPlugin.h | 1 + .../Source/FlutterTextInputPlugin.mm | 10 ++- .../Source/FlutterTextInputPluginTest.mm | 87 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h index 70a8ff5156..fb099aa113 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h @@ -69,5 +69,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 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 1d195efe26..19c779ec2a 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 @@ -621,7 +621,15 @@ static char markerKey; // 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; 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 9c656c0ca9..4a6afa2e3f 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 @@ -1361,6 +1361,89 @@ 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)); @@ -1814,6 +1897,10 @@ TEST(FlutterTextInputPluginTest, TestPerformKeyEquivalent) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testPerformKeyEquivalent]); } +TEST(FlutterTextInputPluginTest, HandleArrowKeyWhenImePopoverIsActive) { + ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] handleArrowKeyWhenImePopoverIsActive]); +} + TEST(FlutterTextInputPluginTest, UnhandledKeyEquivalent) { ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] unhandledKeyEquivalent]); }