parse static method

DVAccelerator parse(
  1. String text, {
  2. bool primaryIsMeta = false,
})

Reads text.

CmdOrCtrl is the one token allowed to mean different things on different machines, and it says so in its name: primaryIsMeta decides, and a binding passes what its platform is.

Throws FormatException for a modifier with no key -- Ctrl+ registers nothing a user could press -- for two keys, and for nothing at all.

Implementation

static DVAccelerator parse(String text, {bool primaryIsMeta = false}) {
  final List<String> parts = text
      .split('+')
      .map((String p) => p.trim().toLowerCase())
      .where((String p) => p.isNotEmpty)
      .toList();
  if (parts.isEmpty) {
    throw FormatException('An accelerator needs a key.', text);
  }

  final Set<DVModifierKey> modifiers = <DVModifierKey>{};
  String? key;
  for (final String part in parts) {
    if (part == 'cmdorctrl' || part == 'commandorcontrol') {
      modifiers.add(primaryIsMeta ? DVModifierKey.meta : DVModifierKey.control);
      continue;
    }
    final DVModifierKey? modifier = _names[part];
    if (modifier != null) {
      modifiers.add(modifier);
      continue;
    }
    if (key != null) {
      throw FormatException(
          'An accelerator has one key; "$text" names two ($key and $part).',
          text);
    }
    key = part;
  }
  if (key == null) {
    throw FormatException(
        'An accelerator needs a key; "$text" is only modifiers.', text);
  }
  return DVAccelerator(modifiers: modifiers, key: key);
}