web_code_gen.dart 2.97 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
  WebCodeGenerator(
17 18
    super.keyData,
    super.logicalData,
19
    String logicalLocationMap,
20
  ) : _logicalLocationMap = parseMapOfListOfNullableString(logicalLocationMap);
21 22 23

  /// This generates the map of Web KeyboardEvent codes to logical key ids.
  String get _webLogicalKeyCodeMap {
24
    final OutputLines<String> lines = OutputLines<String>('Web logical map');
25
    for (final LogicalKeyEntry entry in logicalData.entries) {
26 27 28 29 30
      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)},");
        }
31
      }
32
    }
33
    return lines.sortedJoin().trimRight();
34 35 36 37
  }

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

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

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

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

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