Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 50ef672

Browse files
committed
Add web changes
1 parent dfe55e3 commit 50ef672

File tree

6 files changed

+118
-52
lines changed

6 files changed

+118
-52
lines changed

lib/ui/key.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ enum KeyEventType {
1717
repeat;
1818

1919
String get label {
20-
return switch (this) {
21-
down => 'down',
22-
up => 'up',
23-
repeat => 'repeat',
24-
};
25-
}
20+
return switch (this) {
21+
down => 'Down',
22+
up => 'Up',
23+
repeat => 'Repeat',
24+
};
25+
}
2626
}
2727

2828
/// The source device for the key event.
2929
///
30-
/// Not all platforms supply an accurate source.
30+
/// Not all platforms supply an accurate type.
3131
///
3232
/// Defaults to [keyboard].
33-
// Must match the KeyEventSource enum in ui/window/key_data.h.
33+
// Must match the KeyEventDeviceType enum in ui/window/key_data.h.
3434
enum KeyEventDeviceType {
3535
/// The source is a keyboard.
3636
keyboard,

lib/ui/window/key_data.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ enum class KeyEventType : int64_t {
2727
// Not all platforms supply an accurate source.
2828
//
2929
// Defaults to [keyboard].
30-
// Must match the KeyEventSource enum in ui/key.dart.
31-
enum class KeyEventSource : int64_t {
30+
// Must match the KeyEventDeviceType enum in ui/key.dart.
31+
enum class KeyEventDeviceType : int64_t {
3232
// The source is a keyboard.
3333
kKeyboard = 0,
3434

@@ -65,7 +65,7 @@ struct alignas(8) KeyData {
6565
//
6666
// The value is 1 for true, and 0 for false.
6767
uint64_t synthesized;
68-
KeyEventSource source;
68+
KeyEventDeviceType deviceType;
6969

7070
// Sets all contents of `Keydata` to 0.
7171
void Clear();

lib/web_ui/lib/key.dart

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
65
part of ui;
76

87
/// The type of a key event.
@@ -15,7 +14,49 @@ enum KeyEventType {
1514
up,
1615

1716
/// The key is held, causing a repeated key input.
18-
repeat,
17+
repeat;
18+
19+
String get label {
20+
return switch (this) {
21+
down => 'Down',
22+
up => 'Up',
23+
repeat => 'Repeat',
24+
};
25+
}
26+
}
27+
28+
/// The source device for the key event.
29+
///
30+
/// Not all platforms supply an accurate type.
31+
///
32+
/// Defaults to [keyboard].
33+
// Must match the KeyEventDeviceType enum in ui/window/key_data.h.
34+
enum KeyEventDeviceType {
35+
/// The source is a keyboard.
36+
keyboard,
37+
38+
/// The source is a directional pad on something like a television remote
39+
/// control or similar.
40+
directionalPad,
41+
42+
/// The source is a gamepad button
43+
gamepad,
44+
45+
/// The source is a joystick button
46+
joystick,
47+
48+
/// The source is a device connected to an HDMI bus.
49+
hdmi;
50+
51+
String get label {
52+
return switch (this) {
53+
keyboard => 'Keyboard',
54+
directionalPad => 'Directional Pad',
55+
gamepad => 'Gamepad',
56+
joystick => 'Joystick',
57+
hdmi => 'HDMI',
58+
};
59+
}
1960
}
2061

2162
/// Information about a key event.
@@ -24,6 +65,7 @@ class KeyData {
2465
const KeyData({
2566
required this.timeStamp,
2667
required this.type,
68+
required this.deviceType,
2769
required this.physical,
2870
required this.logical,
2971
required this.character,
@@ -32,13 +74,18 @@ class KeyData {
3274

3375
/// Time of event dispatch, relative to an arbitrary timeline.
3476
///
35-
/// For [KeyEventType.synchronize] and [KeyEventType.cancel] events, the [timeStamp]
36-
/// might not be the actual time that the key press or release happens.
77+
/// For synthesized events, the [timeStamp] might not be the actual time that
78+
/// the key press or release happens.
3779
final Duration timeStamp;
3880

3981
/// The type of the event.
4082
final KeyEventType type;
4183

84+
/// The source device of this key event.
85+
///
86+
/// Defaults to [KeyEventDeviceType.keyboard].
87+
final KeyEventDeviceType deviceType;
88+
4289
/// The key code for the physical key that has changed.
4390
final int physical;
4491

@@ -59,17 +106,18 @@ class KeyData {
59106
///
60107
/// For example, some key downs or ups might be lost when the window loses
61108
/// focus. Some platforms provides ways to query whether a key is being held.
62-
/// If Flutter detects an inconsistency between the state Flutter records and
63-
/// the state returned by the system, Flutter will synthesize a corresponding
64-
/// event to synchronize the state without breaking the event model.
109+
/// If the embedder detects an inconsistency between its internal record and
110+
/// the state returned by the system, the embedder will synthesize a
111+
/// corresponding event to synchronize the state without breaking the event
112+
/// model.
65113
///
66114
/// As another example, macOS treats CapsLock in a special way by sending
67-
/// down and up events at the down of alterate presses to indicate the
115+
/// down and up events at the down of alternate presses to indicate the
68116
/// direction in which the lock is toggled instead of that the physical key is
69-
/// going. Flutter normalizes the behavior by converting a native down event
70-
/// into a down event followed immediately by a synthesized up event, and
71-
/// the native up event also into a down event followed immediately by a
72-
/// synthesized up event.
117+
/// going. A macOS embedder should normalize the behavior by converting a
118+
/// native down event into a down event followed immediately by a synthesized
119+
/// up event, and the native up event also into a down event followed
120+
/// immediately by a synthesized up event.
73121
///
74122
/// Synthesized events do not have a trustworthy [timeStamp], and should not be
75123
/// processed as if the key actually went down or up at the time of the
@@ -95,8 +143,22 @@ class KeyData {
95143
return ' (Unprintable)';
96144
case 0x002:
97145
return ' (Flutter)';
146+
case 0x011:
147+
return ' (Android)';
148+
case 0x012:
149+
return ' (Fuchsia)';
150+
case 0x013:
151+
return ' (iOS)';
152+
case 0x014:
153+
return ' (macOS)';
154+
case 0x015:
155+
return ' (GTK)';
156+
case 0x016:
157+
return ' (Windows)';
98158
case 0x017:
99159
return ' (Web)';
160+
case 0x018:
161+
return ' (GLFW)';
100162
}
101163
return '';
102164
})();
@@ -105,7 +167,7 @@ class KeyData {
105167

106168
String? _escapeCharacter() {
107169
if (character == null) {
108-
return character ?? '<none>';
170+
return '<none>';
109171
}
110172
switch (character!) {
111173
case '\n':
@@ -133,29 +195,25 @@ class KeyData {
133195
}
134196

135197
@override
136-
String toString() => 'KeyData(type: ${_typeToString(type)}, physical: 0x${physical.toRadixString(16)}, '
137-
'logical: ${_logicalToString()}, character: ${_escapeCharacter()}${_quotedCharCode()}${synthesized ? ', synthesized' : ''})';
198+
String toString() {
199+
return 'KeyData(key ${type.label}, '
200+
'deviceType: ${deviceType.label}), '
201+
'physical: 0x${physical.toRadixString(16)}, '
202+
'logical: ${_logicalToString()}, '
203+
'character: ${_escapeCharacter()}${_quotedCharCode()}'
204+
'${synthesized ? ', synthesized' : ''}';
205+
}
138206

139207
/// Returns a complete textual description of the information in this object.
140208
String toStringFull() {
141209
return '$runtimeType('
142-
'type: ${_typeToString(type)}, '
143-
'timeStamp: $timeStamp, '
144-
'physical: 0x${physical.toRadixString(16)}, '
145-
'logical: 0x${logical.toRadixString(16)}, '
146-
'character: $character, '
147-
'synthesized: $synthesized'
210+
'type: ${type.label}, '
211+
'deviceType: ${deviceType.label}, '
212+
'timeStamp: $timeStamp, '
213+
'physical: 0x${physical.toRadixString(16)}, '
214+
'logical: 0x${logical.toRadixString(16)}, '
215+
'character: ${_escapeCharacter()}, '
216+
'synthesized: $synthesized'
148217
')';
149218
}
150-
151-
static String _typeToString(KeyEventType type) {
152-
switch (type) {
153-
case KeyEventType.up:
154-
return 'up';
155-
case KeyEventType.down:
156-
return 'down';
157-
case KeyEventType.repeat:
158-
return 'repeat';
159-
}
160-
}
161219
}

lib/web_ui/lib/src/engine/keyboard_binding.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const int _kDeadKeyMeta = 0x80000000;
7373

7474
const ui.KeyData _emptyKeyData = ui.KeyData(
7575
type: ui.KeyEventType.down,
76+
deviceType: ui.KeyEventDeviceType.keyboard,
7677
timeStamp: Duration.zero,
7778
logical: 0,
7879
physical: 0,
@@ -357,6 +358,7 @@ class KeyboardConverter {
357358
() => ui.KeyData(
358359
timeStamp: currentTimeStamp + _kKeydownCancelDurationMac,
359360
type: ui.KeyEventType.up,
361+
deviceType: ui.KeyEventDeviceType.keyboard,
360362
physical: physicalKey,
361363
logical: logicalKey,
362364
character: null,
@@ -430,6 +432,7 @@ class KeyboardConverter {
430432
() => ui.KeyData(
431433
timeStamp: timeStamp,
432434
type: ui.KeyEventType.up,
435+
deviceType: ui.KeyEventDeviceType.keyboard,
433436
physical: physicalKey,
434437
logical: logicalKey(),
435438
character: null,
@@ -465,6 +468,7 @@ class KeyboardConverter {
465468
_dispatchKeyData!(ui.KeyData(
466469
timeStamp: timeStamp,
467470
type: ui.KeyEventType.up,
471+
deviceType: ui.KeyEventDeviceType.keyboard,
468472
physical: physicalKey,
469473
logical: logicalKey(),
470474
character: null,
@@ -533,6 +537,7 @@ class KeyboardConverter {
533537
_dispatchKeyData!(ui.KeyData(
534538
timeStamp: timeStamp,
535539
type: ui.KeyEventType.up,
540+
deviceType: ui.KeyEventDeviceType.keyboard,
536541
physical: physicalKey,
537542
logical: testeeLogicalKey,
538543
character: null,
@@ -557,6 +562,7 @@ class KeyboardConverter {
557562
final ui.KeyData keyData = ui.KeyData(
558563
timeStamp: timeStamp,
559564
type: type,
565+
deviceType: ui.KeyEventDeviceType.keyboard,
560566
physical: physicalKey,
561567
logical: lastLogicalRecord ?? logicalKey(),
562568
character: type == ui.KeyEventType.up ? null : character,
@@ -669,6 +675,7 @@ class KeyboardConverter {
669675
performDispatchKeyData(ui.KeyData(
670676
timeStamp: _eventTimeStampToDuration(domTimestamp),
671677
type: ui.KeyEventType.down,
678+
deviceType: ui.KeyEventDeviceType.keyboard,
672679
physical: physical,
673680
logical: logical,
674681
character: null,
@@ -682,6 +689,7 @@ class KeyboardConverter {
682689
performDispatchKeyData(ui.KeyData(
683690
timeStamp: _eventTimeStampToDuration(domTimestamp),
684691
type: ui.KeyEventType.up,
692+
deviceType: ui.KeyEventDeviceType.keyboard,
685693
physical: physical,
686694
logical: logical,
687695
character: null,

shell/platform/embedder/embedder.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,21 +2352,21 @@ static inline flutter::KeyEventType MapKeyEventType(
23522352
return flutter::KeyEventType::kUp;
23532353
}
23542354

2355-
static inline flutter::KeyEventSource MapKeyEventSource(
2355+
static inline flutter::KeyEventDeviceType MapKeyEventDeviceType(
23562356
FlutterKeyEventDeviceType event_kind) {
23572357
switch (event_kind) {
23582358
case kFlutterKeyEventDeviceTypeKeyboard:
2359-
return flutter::KeyEventSource::kKeyboard;
2359+
return flutter::KeyEventDeviceType::kKeyboard;
23602360
case kFlutterKeyEventDeviceTypeDirectionalPad:
2361-
return flutter::KeyEventSource::kDirectionalPad;
2361+
return flutter::KeyEventDeviceType::kDirectionalPad;
23622362
case kFlutterKeyEventDeviceTypeGamepad:
2363-
return flutter::KeyEventSource::kGamepad;
2363+
return flutter::KeyEventDeviceType::kGamepad;
23642364
case kFlutterKeyEventDeviceTypeJoystick:
2365-
return flutter::KeyEventSource::kJoystick;
2365+
return flutter::KeyEventDeviceType::kJoystick;
23662366
case kFlutterKeyEventDeviceTypeHdmi:
2367-
return flutter::KeyEventSource::kHdmi;
2367+
return flutter::KeyEventDeviceType::kHdmi;
23682368
}
2369-
return flutter::KeyEventSource::kKeyboard;
2369+
return flutter::KeyEventDeviceType::kKeyboard;
23702370
}
23712371

23722372
// Send a platform message to the framework.
@@ -2428,7 +2428,7 @@ FlutterEngineResult FlutterEngineSendKeyEvent(FLUTTER_API_SYMBOL(FlutterEngine)
24282428
key_data.timestamp = static_cast<uint64_t>(SAFE_ACCESS(event, timestamp, 0));
24292429
key_data.type = MapKeyEventType(
24302430
SAFE_ACCESS(event, type, FlutterKeyEventType::kFlutterKeyEventTypeUp));
2431-
key_data.source = MapKeyEventSource(SAFE_ACCESS(
2431+
key_data.deviceType = MapKeyEventDeviceType(SAFE_ACCESS(
24322432
event, device_type,
24332433
FlutterKeyEventDeviceType::kFlutterKeyEventDeviceTypeKeyboard));
24342434
key_data.physical = SAFE_ACCESS(event, physical, 0);

shell/platform/embedder/tests/embedder_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2098,7 +2098,7 @@ FlutterKeyEventType UnserializeKeyEventType(uint64_t kind) {
20982098

20992099
// Convert `source` in integer form to its enum form.
21002100
//
2101-
// It performs a revesed mapping from `_serializeKeyEventSource`
2101+
// It performs a revesed mapping from `_serializeKeyEventDeviceType`
21022102
// in shell/platform/embedder/fixtures/main.dart.
21032103
FlutterKeyEventDeviceType UnserializeKeyEventDeviceType(uint64_t source) {
21042104
switch (source) {

0 commit comments

Comments
 (0)