PerformableActionsContext.fromActions constructor

PerformableActionsContext.fromActions(
  1. List<PerformableAction> actions, {
  2. bool closeMenuOnEscape = true,
})

Create an instance from an actions list.

If closeMenuOnEscape is true, then the LogicalKeyboardKey.escape key will be added to every PerformableActionMenuItem in menuChildren, by passing canCloseMenu to the constructor of every PerformableActionMenuItem.

Implementation

factory PerformableActionsContext.fromActions(
  final List<PerformableAction> actions, {
  final bool closeMenuOnEscape = true,
}) {
  final customSemanticActions = <CustomSemanticsAction, VoidCallback>{};
  final menuChildren = <Widget>[];
  final bindings = <ShortcutActivator, VoidCallback>{};
  var autofocused = false;
  for (final action in actions) {
    final invoke = action.invoke;
    final activator = action.activator;
    if (invoke != null) {
      customSemanticActions[CustomSemanticsAction(label: action.name)] =
          invoke;
      if (activator != null) {
        bindings[activator] = invoke;
      }
    } else {
      assert(
        action.activator == null,
        // ignore: lines_longer_than_80_chars
        'The action "${action.name}" is acting as a label (`activate == null`). As such, it cannot have a shortcut: ${action.activator}.',
      );
    }
    final autofocus = invoke != null && !autofocused;
    menuChildren.add(
      PerformableActionMenuItem(
        action: action,
        autofocus: autofocus,
        canCloseMenu: closeMenuOnEscape,
      ),
    );
    if (autofocus) {
      autofocused = true;
    }
  }
  return PerformableActionsContext(
    actions: actions,
    customSemanticActions: customSemanticActions,
    menuChildren: menuChildren,
    bindings: bindings,
  );
}