web_code_gen.dart 3.02 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
import 'constants.dart';
9 10
import 'logical_key_data.dart';
import 'physical_key_data.dart';
11 12
import 'utils.dart';

13
/// Generates the key mapping for Web, based on the information in the key
14 15
/// data structure given to it.
class WebCodeGenerator extends PlatformCodeGenerator {
16 17 18 19 20 21
  WebCodeGenerator(
    PhysicalKeyData keyData,
    LogicalKeyData logicalData,
    String logicalLocationMap,
  ) : _logicalLocationMap = parseMapOfListOfNullableString(logicalLocationMap),
      super(keyData, logicalData);
22 23 24

  /// This generates the map of Web KeyboardEvent codes to logical key ids.
  String get _webLogicalKeyCodeMap {
25
    final OutputLines<String> lines = OutputLines<String>('Web logical map');
26
    for (final LogicalKeyEntry entry in logicalData.entries) {
27 28 29 30 31
      final int plane = getPlane(entry.value);
      if (plane == kUnprintablePlane.value) {
        for (final String name in entry.webNames) {
          lines.add(name, "  '$name': ${toHex(entry.value, digits: 11)},");
        }
32
      }
33
    }
34
    return lines.sortedJoin().trimRight();
35 36 37 38
  }

  /// This generates the map of Web KeyboardEvent codes to physical key USB HID codes.
  String get _webPhysicalKeyCodeMap {
39
    final OutputLines<String> lines = OutputLines<String>('Web physical map');
40
    for (final PhysicalKeyEntry entry in keyData.entries) {
41
      if (entry.name != null) {
42 43
        lines.add(entry.name,
            "  '${entry.name}': ${toHex(entry.usbHidCode)}, // ${entry.constantName}");
44 45
      }
    }
46
    return lines.sortedJoin().trimRight();
47 48 49
  }

  /// This generates the map of Web number pad codes to logical key ids.
50
  String get _webLogicalLocationMap {
51
    final OutputLines<String> lines = OutputLines<String>('Web logical location map');
52 53
    _logicalLocationMap.forEach((String webKey, List<String?> locations) {
      final String valuesString = locations.map((String? value) {
54 55 56 57
        return value == null ? 'null' : toHex(logicalData.entryByName(value).value, digits: 11);
      }).join(', ');
      final String namesString = locations.map((String? value) {
        return value == null ? 'null' : logicalData.entryByName(value).constantName;
58
      }).join(', ');
59
      lines.add(webKey, "  '$webKey': <int?>[$valuesString], // $namesString");
60
    });
61
    return lines.sortedJoin().trimRight();
62
  }
63
  final Map<String, List<String?>> _logicalLocationMap;
64 65

  @override
66
  String get templatePath => path.join(dataRoot, 'web_key_map_dart.tmpl');
67 68

  @override
69 70
  String outputPath(String platform) => path.join(PlatformCodeGenerator.engineRoot,
      'lib', 'web_ui', 'lib', 'src', 'engine', 'key_map.dart');
71 72 73 74 75 76

  @override
  Map<String, String> mappings() {
    return <String, String>{
      'WEB_LOGICAL_KEY_CODE_MAP': _webLogicalKeyCodeMap,
      'WEB_PHYSICAL_KEY_CODE_MAP': _webPhysicalKeyCodeMap,
77
      'WEB_LOGICAL_LOCATION_MAP': _webLogicalLocationMap,
78 79 80
    };
  }
}