dvShortcutLabel function

String dvShortcutLabel(
  1. String keys,
  2. TargetPlatform platform
)

keys (mod+shift+r) as the platform writes a shortcut: ⇧⌘R on Apple platforms, Ctrl+Shift+R elsewhere. mod is Command on Apple platforms and Control everywhere else, as in DVShortcutScope.

Implementation

String dvShortcutLabel(String keys, TargetPlatform platform) {
  final bool apple = platform == .macOS || platform == .iOS;
  final List<String> parts =
      keys.toLowerCase().split('+').map((String part) => part.trim()).toList();
  final String key = parts.removeLast();
  final Set<String> modifiers = <String>{
    for (final String part in parts)
      switch (part) {
        'mod' => apple ? 'meta' : 'ctrl',
        'cmd' || 'command' || 'meta' || 'super' => 'meta',
        'control' || 'ctrl' => 'ctrl',
        'option' || 'alt' => 'alt',
        _ => part,
      },
  };
  final String name = key.length == 1
      ? key.toUpperCase()
      : '${key[0].toUpperCase()}${key.substring(1)}';
  if (apple) {
    // Apple's order: Control, Option, Shift, Command.
    const Map<String, String> symbols = <String, String>{
      'ctrl': '⌃',
      'alt': '⌥',
      'shift': '⇧',
      'meta': '⌘',
    };
    return '${<String>[
      for (final String modifier in symbols.keys)
        if (modifiers.contains(modifier)) symbols[modifier]!,
    ].join()}$name';
  }
  const Map<String, String> words = <String, String>{
    'ctrl': 'Ctrl',
    'meta': 'Win',
    'alt': 'Alt',
    'shift': 'Shift',
  };
  return <String>[
    for (final String modifier in words.keys)
      if (modifiers.contains(modifier)) words[modifier]!,
    name,
  ].join('+');
}