Implement handling of framework-handled key events (flutter/engine#23655)
This commit is contained in:
@@ -80,11 +80,6 @@ class Keyboard {
|
||||
}
|
||||
|
||||
final html.KeyboardEvent keyboardEvent = event;
|
||||
|
||||
if (_shouldPreventDefault(event)) {
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
final String timerKey = keyboardEvent.code!;
|
||||
|
||||
// Don't handle synthesizing a keyup event for modifier keys
|
||||
@@ -132,16 +127,17 @@ class Keyboard {
|
||||
};
|
||||
|
||||
EnginePlatformDispatcher.instance.invokeOnPlatformMessage('flutter/keyevent',
|
||||
_messageCodec.encodeMessage(eventData), _noopCallback);
|
||||
}
|
||||
|
||||
bool _shouldPreventDefault(html.KeyboardEvent event) {
|
||||
switch (event.key) {
|
||||
case 'Tab':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
_messageCodec.encodeMessage(eventData), (ByteData? data) {
|
||||
if (data == null) {
|
||||
return;
|
||||
}
|
||||
final Map<String, dynamic> jsonResponse = _messageCodec.decodeMessage(data);
|
||||
if (jsonResponse['handled'] as bool) {
|
||||
// If the framework handled it, then don't propagate it any further.
|
||||
event.preventDefault();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _synthesizeKeyup(html.KeyboardEvent event) {
|
||||
|
||||
@@ -226,13 +226,15 @@ void testMain() {
|
||||
expect(count, 2);
|
||||
});
|
||||
|
||||
test('prevents default when "Tab" is pressed', () {
|
||||
test('prevents default when key is handled by the framework', () {
|
||||
Keyboard.initialize();
|
||||
|
||||
int count = 0;
|
||||
ui.window.onPlatformMessage = (String channel, ByteData data,
|
||||
ui.PlatformMessageResponseCallback callback) {
|
||||
count += 1;
|
||||
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': true});
|
||||
callback(response);
|
||||
};
|
||||
|
||||
final html.KeyboardEvent event = dispatchKeyboardEvent(
|
||||
@@ -247,6 +249,29 @@ void testMain() {
|
||||
Keyboard.instance.dispose();
|
||||
});
|
||||
|
||||
test("Doesn't prevent default when key is not handled by the framework", () {
|
||||
Keyboard.initialize();
|
||||
|
||||
int count = 0;
|
||||
ui.window.onPlatformMessage = (String channel, ByteData data,
|
||||
ui.PlatformMessageResponseCallback callback) {
|
||||
count += 1;
|
||||
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': false});
|
||||
callback(response);
|
||||
};
|
||||
|
||||
final html.KeyboardEvent event = dispatchKeyboardEvent(
|
||||
'keydown',
|
||||
key: 'Tab',
|
||||
code: 'Tab',
|
||||
);
|
||||
|
||||
expect(event.defaultPrevented, isFalse);
|
||||
expect(count, 1);
|
||||
|
||||
Keyboard.instance.dispose();
|
||||
});
|
||||
|
||||
test('keyboard events should be triggered on text fields', () {
|
||||
Keyboard.initialize();
|
||||
|
||||
@@ -278,6 +303,8 @@ void testMain() {
|
||||
ui.window.onPlatformMessage = (String channel, ByteData data,
|
||||
ui.PlatformMessageResponseCallback callback) {
|
||||
count += 1;
|
||||
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': true});
|
||||
callback(response);
|
||||
};
|
||||
|
||||
useTextEditingElement((html.Element element) {
|
||||
|
||||
Reference in New Issue
Block a user