Skip to content

Commit 0deb173

Browse files
committed
Fix debugName
1 parent 91d4f56 commit 0deb173

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎packages/flutter/lib/src/services/keyboard_key.dart‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,21 @@ class LogicalKeyboardKey extends KeyboardKey {
145145

146146
/// The debug string to print for this keyboard key, which will be null in
147147
/// release mode.
148-
String? get debugName => _debugNames[keyId];
148+
String? get debugName {
149+
String? result;
150+
assert(() {
151+
result = _debugNames[keyId];
152+
if (result == null) {
153+
if (keyId & ~valueMask == unicodePlane) {
154+
result = 'Key ${String.fromCharCode(keyId)}';
155+
} else {
156+
result = 'Key with ID 0x${keyId.toRadixString(16).padLeft(11, '0')}';
157+
}
158+
}
159+
return true;
160+
}());
161+
return result;
162+
}
149163

150164
/// Returns the [LogicalKeyboardKey] constant that matches the given ID, or
151165
/// null, if not found.
@@ -2333,7 +2347,15 @@ class PhysicalKeyboardKey extends KeyboardKey {
23332347

23342348
/// The debug string to print for this keyboard key, which will be null in
23352349
/// release mode.
2336-
String? get debugName => _debugNames[usbHidUsage];
2350+
String? get debugName {
2351+
String? result;
2352+
assert(() {
2353+
result = _debugNames[usbHidUsage] ??
2354+
'Key with ID 0x${usbHidUsage.toRadixString(16).padLeft(8, '0')}';
2355+
return true;
2356+
}());
2357+
return result;
2358+
}
23372359

23382360
/// Finds a known [PhysicalKeyboardKey] that matches the given USB HID usage
23392361
/// code.

‎packages/flutter/test/services/keyboard_key_test.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ void main() {
1515
// Check a modifier key
1616
expect(PhysicalKeyboardKey.findKeyByCode(0x000700e1), equals(PhysicalKeyboardKey.shiftLeft));
1717
});
18+
test('Values are cached', () async {
19+
expect(identical(PhysicalKeyboardKey.keyA, PhysicalKeyboardKey(PhysicalKeyboardKey.keyA.usbHidUsage)), true);
20+
expect(identical(PhysicalKeyboardKey(0x12345), PhysicalKeyboardKey(0x12345)), true);
21+
});
22+
test('debugNames', () async {
23+
expect(PhysicalKeyboardKey.keyA.debugName, 'Key A');
24+
expect(PhysicalKeyboardKey.backslash.debugName, 'Backslash');
25+
expect(PhysicalKeyboardKey(0x12345).debugName, 'Key with ID 0x00012345');
26+
});
1827
});
1928
group(LogicalKeyboardKey, () {
2029
test('Various classes of keys can be looked up by code', () async {
@@ -94,5 +103,16 @@ void main() {
94103
LogicalKeyboardKey.meta,
95104
}));
96105
});
106+
test('Values are cached', () async {
107+
expect(identical(LogicalKeyboardKey.keyA, LogicalKeyboardKey(LogicalKeyboardKey.keyA.keyId)), true);
108+
expect(identical(LogicalKeyboardKey(0x12345), LogicalKeyboardKey(0x12345)), true);
109+
});
110+
test('debugNames', () async {
111+
expect(LogicalKeyboardKey.keyA.debugName, 'Key A');
112+
expect(LogicalKeyboardKey.backslash.debugName, 'Backslash');
113+
expect(LogicalKeyboardKey(0xD9).debugName, 'Key Ù');
114+
expect(LogicalKeyboardKey.mediaPlay.debugName, 'Media Play');
115+
expect(LogicalKeyboardKey(0x100012345).debugName, 'Key with ID 0x00100012345');
116+
});
97117
});
98118
}

0 commit comments

Comments
 (0)