keyboard_keys_code_gen.dart 6.33 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 19 20 21 22 23 24 25 26 27 28 29 30
String _wrapString(String input) {
  return wrapString(input, prefix: '  /// ');
}

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];
  int get value => (primaryKey.value & ~kVariationMask) + kSynonymPlane;
  String get constantName => upperCamelToLowerCamel(name);
31 32
}

33
/// Generates the keyboard_key.dart based on the information in the key data
34 35
/// structure given to it.
class KeyboardKeysCodeGenerator extends BaseCodeGenerator {
36
  KeyboardKeysCodeGenerator(PhysicalKeyData keyData, LogicalKeyData logicalData) : super(keyData, logicalData);
37 38 39 40

  /// Gets the generated definitions of PhysicalKeyboardKeys.
  String get _physicalDefinitions {
    final StringBuffer definitions = StringBuffer();
41
    for (final PhysicalKeyEntry entry in keyData.entries) {
42 43 44
      final String firstComment = _wrapString('Represents the location of the '
        '"${entry.commentName}" key on a generalized keyboard.');
      final String otherComments = _wrapString('See the function '
45
        '[KeyEvent.physical] for more information.');
46 47 48
      definitions.write('''

$firstComment  ///
49
$otherComments  static const PhysicalKeyboardKey ${entry.constantName} = PhysicalKeyboardKey(${toHex(entry.usbHidCode, digits: 8)});
50 51 52 53 54
''');
    }
    return definitions.toString();
  }

55 56
  String get _physicalDebugNames {
    final StringBuffer result = StringBuffer();
57
    for (final PhysicalKeyEntry entry in keyData.entries) {
58 59 60 61 62 63 64
      result.write('''
      ${toHex(entry.usbHidCode, digits: 8)}: '${entry.commentName}',
''');
    }
    return result.toString();
  }

65 66 67
  /// Gets the generated definitions of LogicalKeyboardKeys.
  String get _logicalDefinitions {
    final StringBuffer definitions = StringBuffer();
68
    void printKey(int flutterId, String constantName, String commentName, {String? otherComments}) {
69
      final String firstComment = _wrapString('Represents the logical "$commentName" key on the keyboard.');
70
      otherComments ??= _wrapString('See the function [KeyEvent.logical] for more information.');
71
      definitions.write('''
72 73

$firstComment  ///
74
$otherComments  static const LogicalKeyboardKey $constantName = LogicalKeyboardKey(${toHex(flutterId, digits: 11)});
75
''');
76 77
    }

78
    for (final LogicalKeyEntry entry in logicalData.entries) {
79
      printKey(
80
        entry.value,
81 82 83 84
        entry.constantName,
        entry.commentName,
      );
    }
85
    for (final SynonymKeyInfo synonymInfo in synonyms) {
86 87 88
      // 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.
89 90 91
      final Set<String> unionNames = synonymInfo.keys.map(
        (LogicalKeyEntry entry) => entry.constantName).toSet();
      printKey(synonymInfo.value, synonymInfo.constantName, PhysicalKeyEntry.getCommentName(synonymInfo.name),
92 93 94 95 96 97 98 99
          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.'));
    }
    return definitions.toString();
  }

  String get _logicalSynonyms {
100 101 102 103 104
    final StringBuffer result = StringBuffer();
    for (final SynonymKeyInfo synonymInfo in synonyms) {
      for (final LogicalKeyEntry key in synonymInfo.keys) {
        final String synonymName = upperCamelToLowerCamel(synonymInfo.name);
        result.writeln('    ${key.constantName}: $synonymName,');
105 106
      }
    }
107
    return result.toString();
108 109
  }

110 111
  String get _logicalKeyLabels {
    final StringBuffer result = StringBuffer();
112
    for (final LogicalKeyEntry entry in logicalData.entries) {
113
      result.write('''
114
    ${toHex(entry.value, digits: 11)}: '${entry.commentName}',
115 116
''');
    }
117
    for (final SynonymKeyInfo synonymInfo in synonyms) {
118
      result.write('''
119
    ${toHex(synonymInfo.value)}: '${synonymInfo.name}',
120 121 122 123 124
''');
    }
    return result.toString();
  }

125 126 127
  /// This generates the map of USB HID codes to physical keys.
  String get _predefinedHidCodeMap {
    final StringBuffer scanCodeMap = StringBuffer();
128
    for (final PhysicalKeyEntry entry in keyData.entries) {
129 130 131 132 133 134 135 136
      scanCodeMap.writeln('    ${toHex(entry.usbHidCode)}: ${entry.constantName},');
    }
    return scanCodeMap.toString().trimRight();
  }

  /// This generates the map of Flutter key codes to logical keys.
  String get _predefinedKeyCodeMap {
    final StringBuffer keyCodeMap = StringBuffer();
137 138
    for (final LogicalKeyEntry entry in logicalData.entries) {
      keyCodeMap.writeln('    ${toHex(entry.value, digits: 11)}: ${entry.constantName},');
139
    }
140 141
    for (final SynonymKeyInfo synonymInfo in synonyms) {
      keyCodeMap.writeln('    ${toHex(synonymInfo.value, digits: 11)}: ${synonymInfo.constantName},');
142 143 144 145 146
    }
    return keyCodeMap.toString().trimRight();
  }

  @override
147
  String get templatePath => path.join(dataRoot, 'keyboard_key.tmpl');
148 149 150 151 152 153 154

  @override
  Map<String, String> mappings() {
    return <String, String>{
      'LOGICAL_KEY_MAP': _predefinedKeyCodeMap,
      'LOGICAL_KEY_DEFINITIONS': _logicalDefinitions,
      'LOGICAL_KEY_SYNONYMS': _logicalSynonyms,
155
      'LOGICAL_KEY_KEY_LABELS': _logicalKeyLabels,
156
      'PHYSICAL_KEY_MAP': _predefinedHidCodeMap,
157
      'PHYSICAL_KEY_DEFINITIONS': _physicalDefinitions,
158
      'PHYSICAL_KEY_DEBUG_NAMES': _physicalDebugNames,
159 160
    };
  }
161 162 163 164 165 166 167 168 169 170 171

  late final List<SynonymKeyInfo> synonyms = LogicalKeyData.synonyms.entries.map(
    (MapEntry<String, List<String>> synonymDefinition) {
      final List<LogicalKeyEntry> entries = synonymDefinition.value.map(
        (String name) => logicalData.entryByName(name)).toList();
      return SynonymKeyInfo(
        entries,
        synonymDefinition.key,
      );
    }
  ).toList();
172
}