keyboard_keys_code_gen.dart 6.99 KB
Newer Older
1 2 3 4 5 6 7
// 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';
8 9 10
import 'constants.dart';
import 'logical_key_data.dart';
import 'physical_key_data.dart';
11 12 13 14
import 'utils.dart';

/// Given an [input] string, wraps the text at 80 characters and prepends each
/// line with the [prefix] string. Use for generated comments.
15 16 17 18
String _wrapString(String input) {
  return wrapString(input, prefix: '  /// ');
}

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
final List<MaskConstant> _maskConstants = <MaskConstant>[
  kValueMask,
  kPlaneMask,
  kUnicodePlane,
  kUnprintablePlane,
  kFlutterPlane,
  kStartOfPlatformPlanes,
  kAndroidPlane,
  kFuchsiaPlane,
  kIosPlane,
  kMacosPlane,
  kGtkPlane,
  kWindowsPlane,
  kWebPlane,
  kGlfwPlane,
];

36 37 38 39 40 41 42 43 44 45 46
class SynonymKeyInfo {
  SynonymKeyInfo(this.keys, this.name);

  final List<LogicalKeyEntry> keys;
  final String name;

  // 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.
  LogicalKeyEntry get primaryKey => keys[0];
  String get constantName => upperCamelToLowerCamel(name);
47 48
}

49
/// Generates the keyboard_key.g.dart based on the information in the key data
50 51
/// structure given to it.
class KeyboardKeysCodeGenerator extends BaseCodeGenerator {
52
  KeyboardKeysCodeGenerator(super.keyData, super.logicalData);
53 54 55

  /// Gets the generated definitions of PhysicalKeyboardKeys.
  String get _physicalDefinitions {
56
    final OutputLines<int> lines = OutputLines<int>('Physical Key Definition');
57
    for (final PhysicalKeyEntry entry in keyData.entries) {
58 59 60
      final String firstComment = _wrapString('Represents the location of the '
        '"${entry.commentName}" key on a generalized keyboard.');
      final String otherComments = _wrapString('See the function '
61 62
        '[RawKeyEvent.physicalKey] for more information.');
      lines.add(entry.usbHidCode, '''
63
$firstComment  ///
64
$otherComments  static const PhysicalKeyboardKey ${entry.constantName} = PhysicalKeyboardKey(${toHex(entry.usbHidCode)});
65 66
''');
    }
67
    return lines.sortedJoin().trimRight();
68 69
  }

70
  String get _physicalDebugNames {
71
    final OutputLines<int> lines = OutputLines<int>('Physical debug names');
72
    for (final PhysicalKeyEntry entry in keyData.entries) {
73
      lines.add(entry.usbHidCode, '''
74
      ${toHex(entry.usbHidCode)}: '${entry.commentName}',''');
75
    }
76
    return lines.sortedJoin().trimRight();
77 78
  }

79 80
  /// Gets the generated definitions of LogicalKeyboardKeys.
  String get _logicalDefinitions {
81
    final OutputLines<int> lines = OutputLines<int>('Logical debug names', behavior: DeduplicateBehavior.kSkip);
82
    void printKey(int flutterId, String constantName, String commentName, {String? otherComments}) {
83
      final String firstComment = _wrapString('Represents the logical "$commentName" key on the keyboard.');
84 85
      otherComments ??= _wrapString('See the function [RawKeyEvent.logicalKey] for more information.');
      lines.add(flutterId, '''
86
$firstComment  ///
87
$otherComments  static const LogicalKeyboardKey $constantName = LogicalKeyboardKey(${toHex(flutterId, digits: 11)});
88
''');
89 90
    }

91
    for (final LogicalKeyEntry entry in logicalData.entries) {
92
      printKey(
93
        entry.value,
94 95
        entry.constantName,
        entry.commentName,
96
        otherComments: _otherComments(entry.name),
97 98
      );
    }
99 100 101 102 103 104
    return lines.sortedJoin().trimRight();
  }

  String? _otherComments(String name) {
    if (synonyms.containsKey(name)) {
      final Set<String> unionNames = synonyms[name]!.keys.map(
105
        (LogicalKeyEntry entry) => entry.constantName).toSet();
106
      return _wrapString('This key represents the union of the keys '
107
              '$unionNames when comparing keys. This key will never be generated '
108
              'directly, its main use is in defining key maps.');
109
    }
110
    return null;
111 112 113
  }

  String get _logicalSynonyms {
114
    final StringBuffer result = StringBuffer();
115
    for (final SynonymKeyInfo synonymInfo in synonyms.values) {
116
      for (final LogicalKeyEntry key in synonymInfo.keys) {
117 118
        final LogicalKeyEntry synonym = logicalData.entryByName(synonymInfo.name);
        result.writeln('    ${key.constantName}: ${synonym.constantName},');
119 120
      }
    }
121
    return result.toString();
122 123
  }

124
  String get _logicalKeyLabels {
125
    final OutputLines<int> lines = OutputLines<int>('Logical key labels', behavior: DeduplicateBehavior.kSkip);
126
    for (final LogicalKeyEntry entry in logicalData.entries) {
127 128
      lines.add(entry.value, '''
    ${toHex(entry.value, digits: 11)}: '${entry.commentName}',''');
129
    }
130
    return lines.sortedJoin().trimRight();
131 132
  }

133 134
  /// This generates the map of USB HID codes to physical keys.
  String get _predefinedHidCodeMap {
135
    final OutputLines<int> lines = OutputLines<int>('Physical key map');
136
    for (final PhysicalKeyEntry entry in keyData.entries) {
137
      lines.add(entry.usbHidCode, '    ${toHex(entry.usbHidCode)}: ${entry.constantName},');
138
    }
139
    return lines.sortedJoin().trimRight();
140 141 142 143
  }

  /// This generates the map of Flutter key codes to logical keys.
  String get _predefinedKeyCodeMap {
144
    final OutputLines<int> lines = OutputLines<int>('Logical key map', behavior: DeduplicateBehavior.kSkip);
145
    for (final LogicalKeyEntry entry in logicalData.entries) {
146
      lines.add(entry.value, '    ${toHex(entry.value, digits: 11)}: ${entry.constantName},');
147
    }
148 149 150 151
    return lines.sortedJoin().trimRight();
  }

  String get _maskConstantVariables {
152
    final OutputLines<int> lines = OutputLines<int>('Mask constants', behavior: DeduplicateBehavior.kKeep);
153 154 155 156 157 158
    for (final MaskConstant constant in _maskConstants) {
      lines.add(constant.value, '''
${_wrapString(constant.description)}  ///
  /// This is used by platform-specific code to generate Flutter key codes.
  static const int ${constant.lowerCamelName} = ${toHex(constant.value, digits: 11)};
''');
159
    }
160
    return lines.join().trimRight();
161 162 163
  }

  @override
164
  String get templatePath => path.join(dataRoot, 'keyboard_key.tmpl');
165 166 167 168 169 170 171

  @override
  Map<String, String> mappings() {
    return <String, String>{
      'LOGICAL_KEY_MAP': _predefinedKeyCodeMap,
      'LOGICAL_KEY_DEFINITIONS': _logicalDefinitions,
      'LOGICAL_KEY_SYNONYMS': _logicalSynonyms,
172
      'LOGICAL_KEY_KEY_LABELS': _logicalKeyLabels,
173
      'PHYSICAL_KEY_MAP': _predefinedHidCodeMap,
174
      'PHYSICAL_KEY_DEFINITIONS': _physicalDefinitions,
175
      'PHYSICAL_KEY_DEBUG_NAMES': _physicalDebugNames,
176
      'MASK_CONSTANTS': _maskConstantVariables,
177 178
    };
  }
179

180 181
  late final Map<String, SynonymKeyInfo> synonyms = Map<String, SynonymKeyInfo>.fromEntries(
    LogicalKeyData.synonyms.entries.map((MapEntry<String, List<String>> synonymDefinition) {
182 183
      final List<LogicalKeyEntry> entries = synonymDefinition.value.map(
        (String name) => logicalData.entryByName(name)).toList();
184
      return MapEntry<String, SynonymKeyInfo>(
185
        synonymDefinition.key,
186 187 188 189
        SynonymKeyInfo(
          entries,
          synonymDefinition.key,
        ),
190
      );
191 192
    }),
  );
193
}