[macOS] TextInputPlugin should not consume unhandled key equivalent events (flutter/engine#34250)

Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
This commit is contained in:
Matej Knopp
2022-07-07 15:00:36 +02:00
committed by GitHub
parent 90455e2a33
commit a538b9bda7
2 changed files with 131 additions and 1 deletions

View File

@@ -76,6 +76,35 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
return flutter::TextRange([base unsignedLongValue], [extent unsignedLongValue]);
}
@interface NSEvent (KeyEquivalentMarker)
// Internally marks that the event was received through performKeyEquivalent:.
// When text editing is active, keyboard events that have modifier keys pressed
// are received through performKeyEquivalent: instead of keyDown:. If such event
// is passed to TextInputContext but doesn't result in a text editing action it
// needs to be forwarded by FlutterKeyboardManager to the next responder.
- (void)markAsKeyEquivalent;
// Returns YES if the event is marked as a key equivalent.
- (BOOL)isKeyEquivalent;
@end
@implementation NSEvent (KeyEquivalentMarker)
// This field doesn't need a value because only its address is used as a unique identifier.
static char markerKey;
- (void)markAsKeyEquivalent {
objc_setAssociatedObject(self, &markerKey, @true, OBJC_ASSOCIATION_RETAIN);
}
- (BOOL)isKeyEquivalent {
return [objc_getAssociatedObject(self, &markerKey) boolValue] == YES;
}
@end
/**
* Private properties of FlutterTextInputPlugin.
*/
@@ -130,6 +159,13 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
*/
@property(nonatomic, nonnull) NSString* inputAction;
/**
* Set to true if the last event fed to the input context produced a text editing command
* or text output. It is reset to false at the beginning of every key event, and is only
* used while processing this event.
*/
@property(nonatomic) BOOL eventProducedOutput;
/**
* Whether to enable the sending of text input updates from the engine to the
* framework as TextEditingDeltas rather than as one TextEditingValue.
@@ -482,7 +518,17 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
return NO;
}
return [_textInputContext handleEvent:event];
_eventProducedOutput = NO;
BOOL res = [_textInputContext handleEvent:event];
// NSTextInputContext#handleEvent returns YES if the context handles the event. One of the reasons
// the event is handled is because it's a key equivalent. But a key equivalent might produce a
// 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) {
return NO;
}
return res;
}
#pragma mark -
@@ -509,6 +555,7 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
// send the event back to [keyboardManager handleEvent:].
return NO;
}
[event markAsKeyEquivalent];
[self.flutterViewController keyDown:event];
return YES;
}
@@ -573,6 +620,8 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
return;
}
_eventProducedOutput |= true;
if (range.location != NSNotFound) {
// The selected range can actually have negative numbers, since it can start
// at the end of the range if the user selected the text going backwards.
@@ -612,6 +661,7 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
}
- (void)doCommandBySelector:(SEL)selector {
_eventProducedOutput |= selector != NSSelectorFromString(@"noop:");
if ([self respondsToSelector:selector]) {
// Note: The more obvious [self performSelector...] doesn't give ARC enough information to
// handle retain semantics properly. See https://stackoverflow.com/questions/7017281/ for more

View File

@@ -920,6 +920,82 @@
return true;
}
- (bool)unhandledKeyEquivalent {
id engineMock = OCMClassMock([FlutterEngine class]);
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
[engineMock binaryMessenger])
.andReturn(binaryMessengerMock);
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",
@"enableDeltaModel" : @"true",
@"inputType" : @{@"name" : @"inputName"},
}
]]
result:^(id){
}];
[plugin handleMethodCall:[FlutterMethodCall methodCallWithMethodName:@"TextInput.show"
arguments:@[]]
result:^(id){
}];
// CTRL+H (delete backwards)
NSEvent* event = [NSEvent keyEventWithType:NSEventTypeKeyDown
location:NSZeroPoint
modifierFlags:0x40101
timestamp:0
windowNumber:0
context:nil
characters:@""
charactersIgnoringModifiers:@"h"
isARepeat:NO
keyCode:0x4];
// Plugin should mark the event as key equivalent.
[plugin performKeyEquivalent:event];
// Simulate KeyboardManager sending unhandled event to plugin. This must return
// true because it is a known editing command.
if ([plugin handleKeyEvent:event] != true) {
return false;
}
// CMD+W
event = [NSEvent keyEventWithType:NSEventTypeKeyDown
location:NSZeroPoint
modifierFlags:0x100108
timestamp:0
windowNumber:0
context:nil
characters:@"w"
charactersIgnoringModifiers:@"w"
isARepeat:NO
keyCode:0x13];
// Plugin should mark the event as key equivalent.
[plugin performKeyEquivalent:event];
// This is not a valid editing command, plugin must return false so that
// KeyboardManager sends the event to next responder.
if ([plugin handleKeyEvent:event] != false) {
return false;
}
return true;
}
- (bool)testLocalTextAndSelectionUpdateAfterDelta {
id engineMock = OCMClassMock([FlutterEngine class]);
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
@@ -1040,6 +1116,10 @@ TEST(FlutterTextInputPluginTest, TestPerformKeyEquivalent) {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testPerformKeyEquivalent]);
}
TEST(FlutterTextInputPluginTest, UnhandledKeyEquivalent) {
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] unhandledKeyEquivalent]);
}
TEST(FlutterTextInputPluginTest, CanWorkWithFlutterTextField) {
FlutterEngine* engine = CreateTestEngine();
NSString* fixtures = @(testing::GetFixturesPath());