[gen_keycodes] Remove nonexistent Web keys and improve their emulation (#87098)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -11,11 +11,7 @@ import 'package:path/path.dart' as path;
|
||||
import 'constants.dart';
|
||||
import 'physical_key_data.dart';
|
||||
|
||||
bool _isControlCharacter(String label) {
|
||||
if (label.length != 1) {
|
||||
return false;
|
||||
}
|
||||
final int codeUnit = label.codeUnitAt(0);
|
||||
bool _isControlCharacter(int codeUnit) {
|
||||
return (codeUnit <= 0x1f && codeUnit >= 0x00) || (codeUnit >= 0x7f && codeUnit <= 0x9f);
|
||||
}
|
||||
|
||||
@@ -27,8 +23,11 @@ class _ModifierPair {
|
||||
final String right;
|
||||
}
|
||||
|
||||
List<T> _toNonEmptyArray<T>(dynamic source) {
|
||||
final List<dynamic>? dynamicNullableList = source as List<dynamic>?;
|
||||
// Return map[key1][key2] as a non-nullable List<T>, where both map[key1] or
|
||||
// map[key1][key2] might be null.
|
||||
List<T> _getGrandchildList<T>(Map<String, dynamic> map, String key1, String key2) {
|
||||
final dynamic value = (map[key1] as Map<String, dynamic>?)?[key2];
|
||||
final List<dynamic>? dynamicNullableList = value as List<dynamic>?;
|
||||
final List<dynamic> dynamicList = dynamicNullableList ?? <dynamic>[];
|
||||
return dynamicList.cast<T>();
|
||||
}
|
||||
@@ -155,20 +154,23 @@ class LogicalKeyData {
|
||||
final int value = match.namedGroup('unicode') != null ?
|
||||
getHex(match.namedGroup('unicode')!) :
|
||||
match.namedGroup('char')!.codeUnitAt(0);
|
||||
final String? keyLabel = match.namedGroup('kind')! == 'UNI' ? String.fromCharCode(value) : null;
|
||||
final String? keyLabel = (match.namedGroup('kind')! == 'UNI' && !_isControlCharacter(value)) ?
|
||||
String.fromCharCode(value) : null;
|
||||
// Skip modifier keys from DOM. They will be added with supplemental data.
|
||||
if (_chromeModifiers.containsKey(name) && source == 'DOM') {
|
||||
continue;
|
||||
}
|
||||
|
||||
final bool isPrintable = (keyLabel != null && !_isControlCharacter(keyLabel))
|
||||
|| printable.containsKey(name);
|
||||
final bool isPrintable = keyLabel != null;
|
||||
data.putIfAbsent(name, () {
|
||||
return LogicalKeyEntry.fromName(
|
||||
final LogicalKeyEntry entry = LogicalKeyEntry.fromName(
|
||||
value: toPlane(value, _sourceToPlane(source, isPrintable)),
|
||||
name: name,
|
||||
keyLabel: keyLabel,
|
||||
)..webNames.add(webName);
|
||||
);
|
||||
if (source == 'DOM' && !isPrintable)
|
||||
entry.webNames.add(webName);
|
||||
return entry;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -418,9 +420,11 @@ class LogicalKeyData {
|
||||
})();
|
||||
|
||||
static int _sourceToPlane(String source, bool isPrintable) {
|
||||
if (isPrintable)
|
||||
return kUnicodePlane.value;
|
||||
switch (source) {
|
||||
case 'DOM':
|
||||
return isPrintable ? kUnicodePlane.value : kUnprintablePlane.value;
|
||||
return kUnprintablePlane.value;
|
||||
case 'FLUTTER':
|
||||
return kFlutterPlane.value;
|
||||
default:
|
||||
@@ -470,20 +474,20 @@ class LogicalKeyEntry {
|
||||
LogicalKeyEntry.fromJsonMapEntry(Map<String, dynamic> map)
|
||||
: value = map['value'] as int,
|
||||
name = map['name'] as String,
|
||||
webNames = _toNonEmptyArray<String>((map['names'] as Map<String, dynamic>)['web']),
|
||||
macOSKeyCodeNames = _toNonEmptyArray<String>((map['names'] as Map<String, dynamic>)['macos']),
|
||||
macOSKeyCodeValues = _toNonEmptyArray<int>((map['values'] as Map<String, dynamic>?)?['macos']),
|
||||
iOSKeyCodeNames = _toNonEmptyArray<String>((map['names'] as Map<String, dynamic>)['ios']),
|
||||
iOSKeyCodeValues = _toNonEmptyArray<int>((map['values'] as Map<String, dynamic>?)?['ios']),
|
||||
gtkNames = _toNonEmptyArray<String>((map['names'] as Map<String, dynamic>)['gtk']),
|
||||
gtkValues = _toNonEmptyArray<int>((map['values'] as Map<String, dynamic>?)?['gtk']),
|
||||
windowsNames = _toNonEmptyArray<String>((map['names'] as Map<String, dynamic>)['windows']),
|
||||
windowsValues = _toNonEmptyArray<int>((map['values'] as Map<String, dynamic>?)?['windows']),
|
||||
androidNames = _toNonEmptyArray<String>((map['names'] as Map<String, dynamic>)['android']),
|
||||
androidValues = _toNonEmptyArray<int>((map['values'] as Map<String, dynamic>?)?['android']),
|
||||
fuchsiaValues = _toNonEmptyArray<int>((map['values'] as Map<String, dynamic>?)?['fuchsia']),
|
||||
glfwNames = _toNonEmptyArray<String>((map['names'] as Map<String, dynamic>)['glfw']),
|
||||
glfwValues = _toNonEmptyArray<int>((map['values'] as Map<String, dynamic>?)?['glfw']),
|
||||
webNames = _getGrandchildList<String>(map, 'names', 'web'),
|
||||
macOSKeyCodeNames = _getGrandchildList<String>(map, 'names', 'macos'),
|
||||
macOSKeyCodeValues = _getGrandchildList<int>(map, 'values', 'macos'),
|
||||
iOSKeyCodeNames = _getGrandchildList<String>(map, 'names', 'ios'),
|
||||
iOSKeyCodeValues = _getGrandchildList<int>(map, 'values', 'ios'),
|
||||
gtkNames = _getGrandchildList<String>(map, 'names', 'gtk'),
|
||||
gtkValues = _getGrandchildList<int>(map, 'values', 'gtk'),
|
||||
windowsNames = _getGrandchildList<String>(map, 'names', 'windows'),
|
||||
windowsValues = _getGrandchildList<int>(map, 'values', 'windows'),
|
||||
androidNames = _getGrandchildList<String>(map, 'names', 'android'),
|
||||
androidValues = _getGrandchildList<int>(map, 'values', 'android'),
|
||||
fuchsiaValues = _getGrandchildList<int>(map, 'values', 'fuchsia'),
|
||||
glfwNames = _getGrandchildList<String>(map, 'names', 'glfw'),
|
||||
glfwValues = _getGrandchildList<int>(map, 'values', 'glfw'),
|
||||
keyLabel = map['keyLabel'] as String?;
|
||||
|
||||
final int value;
|
||||
|
||||
@@ -22,8 +22,10 @@ String readDataFile(String fileName) {
|
||||
return File(path.join(dataRoot, fileName)).readAsStringSync();
|
||||
}
|
||||
|
||||
final String testPhysicalData = path.join(dataRoot, 'physical_key_data.json');
|
||||
final String testLogicalData = path.join(dataRoot,'logical_key_data.json');
|
||||
final PhysicalKeyData physicalData = PhysicalKeyData.fromJson(
|
||||
json.decode(readDataFile('physical_key_data.json')) as Map<String, dynamic>);
|
||||
final LogicalKeyData logicalData = LogicalKeyData.fromJson(
|
||||
json.decode(readDataFile('logical_key_data.json')) as Map<String, dynamic>);
|
||||
|
||||
void main() {
|
||||
setUp(() {
|
||||
@@ -45,14 +47,6 @@ void main() {
|
||||
}
|
||||
|
||||
test('Generate Keycodes for Android', () {
|
||||
PhysicalKeyData physicalData;
|
||||
LogicalKeyData logicalData;
|
||||
|
||||
physicalData = PhysicalKeyData.fromJson(
|
||||
json.decode(File(testPhysicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
logicalData = LogicalKeyData.fromJson(
|
||||
json.decode(File(testLogicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
|
||||
const String platform = 'android';
|
||||
final PlatformCodeGenerator codeGenerator = AndroidCodeGenerator(
|
||||
physicalData,
|
||||
@@ -67,14 +61,6 @@ void main() {
|
||||
checkCommonOutput(output);
|
||||
});
|
||||
test('Generate Keycodes for macOS', () {
|
||||
PhysicalKeyData physicalData;
|
||||
LogicalKeyData logicalData;
|
||||
|
||||
physicalData = PhysicalKeyData.fromJson(
|
||||
json.decode(File(testPhysicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
logicalData = LogicalKeyData.fromJson(
|
||||
json.decode(File(testLogicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
|
||||
const String platform = 'macos';
|
||||
final PlatformCodeGenerator codeGenerator = MacOSCodeGenerator(
|
||||
physicalData,
|
||||
@@ -93,14 +79,6 @@ void main() {
|
||||
checkCommonOutput(output);
|
||||
});
|
||||
test('Generate Keycodes for iOS', () {
|
||||
PhysicalKeyData physicalData;
|
||||
LogicalKeyData logicalData;
|
||||
|
||||
physicalData = PhysicalKeyData.fromJson(
|
||||
json.decode(File(testPhysicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
logicalData = LogicalKeyData.fromJson(
|
||||
json.decode(File(testLogicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
|
||||
const String platform = 'ios';
|
||||
final PlatformCodeGenerator codeGenerator = IOSCodeGenerator(
|
||||
physicalData,
|
||||
@@ -120,14 +98,6 @@ void main() {
|
||||
checkCommonOutput(output);
|
||||
});
|
||||
test('Generate Keycodes for Windows', () {
|
||||
PhysicalKeyData physicalData;
|
||||
LogicalKeyData logicalData;
|
||||
|
||||
physicalData = PhysicalKeyData.fromJson(
|
||||
json.decode(File(testPhysicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
logicalData = LogicalKeyData.fromJson(
|
||||
json.decode(File(testLogicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
|
||||
const String platform = 'windows';
|
||||
final PlatformCodeGenerator codeGenerator = WindowsCodeGenerator(
|
||||
physicalData,
|
||||
@@ -143,14 +113,6 @@ void main() {
|
||||
checkCommonOutput(output);
|
||||
});
|
||||
test('Generate Keycodes for Linux', () {
|
||||
PhysicalKeyData physicalData;
|
||||
LogicalKeyData logicalData;
|
||||
|
||||
physicalData = PhysicalKeyData.fromJson(
|
||||
json.decode(File(testPhysicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
logicalData = LogicalKeyData.fromJson(
|
||||
json.decode(File(testLogicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
|
||||
const String platform = 'gtk';
|
||||
final PlatformCodeGenerator codeGenerator = GtkCodeGenerator(
|
||||
physicalData,
|
||||
@@ -166,14 +128,6 @@ void main() {
|
||||
checkCommonOutput(output);
|
||||
});
|
||||
test('Generate Keycodes for Web', () {
|
||||
PhysicalKeyData physicalData;
|
||||
LogicalKeyData logicalData;
|
||||
|
||||
physicalData = PhysicalKeyData.fromJson(
|
||||
json.decode(File(testPhysicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
logicalData = LogicalKeyData.fromJson(
|
||||
json.decode(File(testLogicalData).readAsStringSync()) as Map<String, dynamic>);
|
||||
|
||||
const String platform = 'web';
|
||||
final PlatformCodeGenerator codeGenerator = WebCodeGenerator(
|
||||
physicalData,
|
||||
@@ -188,4 +142,25 @@ void main() {
|
||||
expect(output, contains('kWebLogicalLocationMap'));
|
||||
checkCommonOutput(output);
|
||||
});
|
||||
test('LogicalKeyData', () async {
|
||||
final List<LogicalKeyEntry> entries = logicalData.entries.toList();
|
||||
|
||||
// Regression tests for https://github.com/flutter/flutter/pull/87098
|
||||
|
||||
expect(
|
||||
entries.indexWhere((LogicalKeyEntry entry) => entry.name == 'ShiftLeft'),
|
||||
isNot(-1));
|
||||
expect(
|
||||
entries.indexWhere((LogicalKeyEntry entry) => entry.webNames.contains('ShiftLeft')),
|
||||
-1);
|
||||
// 'Shift' maps to both 'ShiftLeft' and 'ShiftRight', and should be resolved
|
||||
// by other ways.
|
||||
expect(
|
||||
entries.indexWhere((LogicalKeyEntry entry) => entry.webNames.contains('Shift')),
|
||||
-1);
|
||||
// Printable keys must not be added with Web key of their names.
|
||||
expect(
|
||||
entries.indexWhere((LogicalKeyEntry entry) => entry.webNames.contains('Slash')),
|
||||
-1);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user