Remove keyLabel and debugName from Logical/PhysicalKeyboardKey (#78263)

- Remove `LogicalKeyboardKey.keyLabel`
- Make `Physical/LogicalKeyboardKey.debugName` getters
- Make `Physical/LogicalKeyboardKey()` factory constructors, which cache non-predefined instances.
This commit is contained in:
Tong Mu
2021-03-24 04:59:57 -07:00
committed by GitHub
parent 2f05751973
commit 515ef91ba1
18 changed files with 1497 additions and 808 deletions

View File

@@ -0,0 +1,49 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:path/path.dart' as path;
import 'base_code_gen.dart';
import 'key_data.dart';
import 'utils.dart';
/// Generates the event_simulation_keylabels.dart based on the information in the
/// key data structure given to it.
class KeyLabelsCodeGenerator extends BaseCodeGenerator {
KeyLabelsCodeGenerator(KeyData keyData) : super(keyData);
/// Gets the generated definitions of logicalKeyLabels.
String get _logicalKeyLabels {
String escapeLabel(String label) => label.contains("'") ? 'r"$label"' : "r'$label'";
final StringBuffer result = StringBuffer();
void printKey(int flutterId, String keyLabel) {
if (keyLabel != null)
result.write('''
${toHex(flutterId, digits: 11)}: ${escapeLabel(keyLabel)},
''');
}
for (final Key entry in keyData.data) {
printKey(entry.flutterId, entry.keyLabel);
}
for (final String name in Key.synonyms.keys) {
// Use the first item in the synonyms as a template for the ID to use.
// It won't end up being the same value because it'll be in the pseudo-key
// plane.
final Key entry = keyData.data.firstWhere((Key item) => item.name == Key.synonyms[name][0]);
printKey(Key.synonymPlane | entry.flutterId, entry.keyLabel);
}
return result.toString();
}
@override
String get templatePath => path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_labels.tmpl');
@override
Map<String, String> mappings() {
return <String, String>{
'LOGICAL_KEY_LABELS': _logicalKeyLabels,
};
}
}

View File

@@ -45,38 +45,38 @@ class KeyboardKeysCodeGenerator extends BaseCodeGenerator {
definitions.write('''
$firstComment ///
$otherComments static const PhysicalKeyboardKey ${entry.constantName} = PhysicalKeyboardKey(${toHex(entry.usbHidCode, digits: 8)}, debugName: kReleaseMode ? null : '${entry.commentName}');
$otherComments static const PhysicalKeyboardKey ${entry.constantName} = PhysicalKeyboardKey._(${toHex(entry.usbHidCode, digits: 8)});
''');
}
return definitions.toString();
}
String get _physicalDebugNames {
final StringBuffer result = StringBuffer();
for (final Key entry in keyData.data) {
result.write('''
${toHex(entry.usbHidCode, digits: 8)}: '${entry.commentName}',
''');
}
return result.toString();
}
/// Gets the generated definitions of LogicalKeyboardKeys.
String get _logicalDefinitions {
String escapeLabel(String label) => label.contains("'") ? 'r"$label"' : "r'$label'";
final StringBuffer definitions = StringBuffer();
void printKey(int flutterId, String keyLabel, String constantName, String commentName, {String otherComments}) {
void printKey(int flutterId, String constantName, String commentName, {String otherComments}) {
final String firstComment = _wrapString('Represents the logical "$commentName" key on the keyboard.');
otherComments ??= _wrapString('See the function [RawKeyEvent.logicalKey] for more information.');
if (keyLabel == null) {
definitions.write('''
definitions.write('''
$firstComment ///
$otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardKey(${toHex(flutterId, digits: 11)}, debugName: kReleaseMode ? null : '$commentName');
$otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardKey._(${toHex(flutterId, digits: 11)});
''');
} else {
definitions.write('''
$firstComment ///
$otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardKey(${toHex(flutterId, digits: 11)}, keyLabel: ${escapeLabel(keyLabel)}, debugName: kReleaseMode ? null : '$commentName');
''');
}
}
for (final Key entry in keyData.data) {
printKey(
entry.flutterId,
entry.keyLabel,
entry.constantName,
entry.commentName,
);
@@ -89,7 +89,7 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK
final Set<String> unionNames = Key.synonyms[name].map<String>((dynamic name) {
return upperCamelToLowerCamel(name as String);
}).toSet();
printKey(Key.synonymPlane | entry.flutterId, entry.keyLabel, name, Key.getCommentName(name),
printKey(Key.synonymPlane | entry.flutterId, name, Key.getCommentName(name),
otherComments: _wrapString('This key represents the union of the keys '
'$unionNames when comparing keys. This key will never be generated '
'directly, its main use is in defining key maps.'));
@@ -108,6 +108,25 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK
return synonyms.toString();
}
String get _logicalDebugNames {
final StringBuffer result = StringBuffer();
for (final Key entry in keyData.data) {
result.write('''
${toHex(entry.flutterId, digits: 11)}: '${entry.commentName}',
''');
}
for (final String name in Key.synonyms.keys) {
// Use the first item in the synonyms as a template for the ID to use.
// It won't end up being the same value because it'll be in the pseudo-key
// plane.
final Key entry = keyData.data.firstWhere((Key item) => item.name == Key.synonyms[name][0]);
result.write('''
${toHex(Key.synonymPlane | entry.flutterId, digits: 11)}: '${Key.getCommentName(name)}',
''');
}
return result.toString();
}
/// This generates the map of USB HID codes to physical keys.
String get _predefinedHidCodeMap {
final StringBuffer scanCodeMap = StringBuffer();
@@ -146,7 +165,9 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK
'LOGICAL_KEY_MAP': _predefinedKeyCodeMap,
'LOGICAL_KEY_DEFINITIONS': _logicalDefinitions,
'LOGICAL_KEY_SYNONYMS': _logicalSynonyms,
'LOGICAL_KEY_DEBUG_NAMES': _logicalDebugNames,
'PHYSICAL_KEY_DEFINITIONS': _physicalDefinitions,
'PHYSICAL_KEY_DEBUG_NAMES': _physicalDebugNames,
};
}
}