Clean up synonyms, key code generation. (#138192)

## Description

This cleans up how synonyms are calculated, and adds a reverse mapping from pseudo keys to the real keys they are synonyms of.

Updates the `layout_goals.json` to include the "Space" key, since that was added in the engine without updating the generation configuration.
This commit is contained in:
Greg Spencer
2023-11-13 12:13:00 -08:00
committed by GitHub
parent 5980342ead
commit 1b9cab7138
7 changed files with 105 additions and 70 deletions

View File

@@ -225,35 +225,40 @@ class LogicalKeyboardKey extends KeyboardKey {
/// Returns a set of pseudo-key synonyms for the given `key`.
///
/// This allows finding the pseudo-keys that also represents a concrete
/// `key` so that a class with a key map can match pseudo-keys as well as the
/// actual generated keys.
/// This allows finding the pseudo-keys that also represent a concrete `key`
/// so that a class with a key map can match pseudo-keys as well as the actual
/// generated keys.
///
/// The pseudo-keys returned in the set are typically used to represent keys
/// which appear in multiple places on the keyboard, such as the [shift],
/// [alt], [control], and [meta] keys. The keys in the returned set won't ever
/// be generated directly, but if a more specific key event is received, then
/// Pseudo-keys returned in the set are typically used to represent keys which
/// appear in multiple places on the keyboard, such as the [shift], [alt],
/// [control], and [meta] keys. Pseudo-keys in the returned set won't ever be
/// generated directly, but if a more specific key event is received, then
/// this set can be used to find the more general pseudo-key. For example, if
/// this is a [shiftLeft] key, this accessor will return the set
/// `<LogicalKeyboardKey>{ shift }`.
Set<LogicalKeyboardKey> get synonyms {
final LogicalKeyboardKey? result = _synonyms[this];
return result == null ? <LogicalKeyboardKey>{} : <LogicalKeyboardKey>{result};
}
Set<LogicalKeyboardKey> get synonyms => _synonyms[this] ?? <LogicalKeyboardKey>{};
/// Takes a set of keys, and returns the same set, but with any keys that have
/// synonyms replaced.
///
/// It is used, for example, to make sets of keys with members like
/// It is used, for example, to take sets of keys with members like
/// [controlRight] and [controlLeft] and convert that set to contain just
/// [control], so that the question "is any control key down?" can be asked.
static Set<LogicalKeyboardKey> collapseSynonyms(Set<LogicalKeyboardKey> input) {
final Set<LogicalKeyboardKey> result = <LogicalKeyboardKey>{};
for (final LogicalKeyboardKey key in input) {
final LogicalKeyboardKey? synonym = _synonyms[key];
result.add(synonym ?? key);
}
return result;
return input.expand((LogicalKeyboardKey element) {
return _synonyms[element] ?? <LogicalKeyboardKey>{element};
}).toSet();
}
/// Returns the given set with any pseudo-keys expanded into their synonyms.
///
/// It is used, for example, to take sets of keys with members like [control]
/// and [shift] and convert that set to contain [controlLeft], [controlRight],
/// [shiftLeft], and [shiftRight].
static Set<LogicalKeyboardKey> expandSynonyms(Set<LogicalKeyboardKey> input) {
return input.expand((LogicalKeyboardKey element) {
return _reverseSynonyms[element] ?? <LogicalKeyboardKey>{element};
}).toSet();
}
@override
@@ -277,10 +282,14 @@ class LogicalKeyboardKey extends KeyboardKey {
@@@LOGICAL_KEY_MAP@@@
};
// A map of keys to the pseudo-key synonym for that key. Used by getSynonyms.
static final Map<LogicalKeyboardKey, LogicalKeyboardKey> _synonyms = <LogicalKeyboardKey, LogicalKeyboardKey>{
// A map of keys to the pseudo-key synonym for that key.
static final Map<LogicalKeyboardKey, Set<LogicalKeyboardKey>> _synonyms = <LogicalKeyboardKey, Set<LogicalKeyboardKey>>{
@@@LOGICAL_KEY_SYNONYMS@@@ };
// A map of pseudo-key to the set of keys that are synonyms for that pseudo-key.
static final Map<LogicalKeyboardKey, Set<LogicalKeyboardKey>> _reverseSynonyms = <LogicalKeyboardKey, Set<LogicalKeyboardKey>>{
@@@LOGICAL_KEY_REVERSE_SYNONYMS@@@ };
static const Map<int, String> _keyLabels = <int, String>{
@@@LOGICAL_KEY_KEY_LABELS@@@
};

View File

@@ -1,4 +1,21 @@
{
"Backquote": false,
"Backslash": false,
"BracketLeft": false,
"BracketRight": false,
"Comma": false,
"Digit0": true,
"Digit1": true,
"Digit2": true,
"Digit3": true,
"Digit4": true,
"Digit5": true,
"Digit6": true,
"Digit7": true,
"Digit8": true,
"Digit9": true,
"Equal": false,
"IntlBackslash": false,
"KeyA": true,
"KeyB": true,
"KeyC": true,
@@ -25,26 +42,10 @@
"KeyX": true,
"KeyY": true,
"KeyZ": true,
"Digit1": true,
"Digit2": true,
"Digit3": true,
"Digit4": true,
"Digit5": true,
"Digit6": true,
"Digit7": true,
"Digit8": true,
"Digit9": true,
"Digit0": true,
"Quote": false,
"Comma": false,
"Minus": false,
"Period": false,
"Slash": false,
"Quote": false,
"Semicolon": false,
"Equal": false,
"BracketLeft": false,
"Backslash": false,
"BracketRight": false,
"Backquote": false,
"IntlBackslash": false
"Slash": false,
"Space": false
}

View File

@@ -39,7 +39,7 @@ const NSDictionary* modifierFlagToKeyCode = @{
@@@SPECIAL_KEY_CONSTANTS@@@
const std::vector<LayoutGoal> layoutGoals = {
const std::vector<LayoutGoal> kLayoutGoals = {
@@@LAYOUT_GOALS@@@
};