Skip to content

Commit

Permalink
Implement handling of framework-handled key events (flutter#23655)
Browse files Browse the repository at this point in the history
  • Loading branch information
gspencergoog authored Jan 20, 2021
1 parent 2927e9f commit df5f3b0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
26 changes: 11 additions & 15 deletions lib/web_ui/lib/src/engine/keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 28 additions & 1 deletion lib/web_ui/test/keyboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit df5f3b0

Please sign in to comment.