dispatchDiagramKeyBinding function
bool
dispatchDiagramKeyBinding(
- DiagramController session,
- Iterable<
DiagramKeyBinding> bindings, - KeyEvent event, {
- String? elementId,
- String? slotId,
- bool editing = false,
Implementation
bool dispatchDiagramKeyBinding(
DiagramController session,
Iterable<DiagramKeyBinding> bindings,
KeyEvent event, {
String? elementId,
String? slotId,
bool editing = false,
}) {
if (event is! KeyDownEvent || event.logicalKey == LogicalKeyboardKey.escape) {
return false;
}
final keyboard = HardwareKeyboard.instance;
final command = defaultTargetPlatform == TargetPlatform.macOS
? keyboard.isMetaPressed
: keyboard.isControlPressed;
// Reject the other command modifier, so chords remain exact.
if (defaultTargetPlatform == TargetPlatform.macOS
? keyboard.isControlPressed
: keyboard.isMetaPressed) {
return false;
}
final scope = editing
? DiagramKeyScope.text
: session.selection is EmptySelection
? DiagramKeyScope.canvas
: DiagramKeyScope.selection;
elementId ??= switch (session.selection) {
ElementSelection(:final elementIds) when elementIds.length == 1 =>
elementIds.single,
StorySelection(:final elementId) => elementId,
ConnectorLabelSelection(:final connectorId) => connectorId,
_ => null,
};
final type = switch (session.document.elementById(elementId ?? '')) {
ShapeElement(:final type) => type,
_ => null,
};
final textType = editing
? session.diagram.resolver.registry
.textFor(
session.document.elementById(elementId ?? ''),
slotId: slotId,
)
.type
: null;
final pressed = {
if (command) DiagramKeyModifier.command,
if (keyboard.isAltPressed) DiagramKeyModifier.alt,
if (keyboard.isShiftPressed) DiagramKeyModifier.shift,
};
final candidates =
bindings
.where(
(b) =>
b.key == event.logicalKey &&
b.scope == scope &&
setEquals(b.modifiers, pressed) &&
(b.shapeType == null || b.shapeType == type) &&
(b.textType == null || b.textType == textType) &&
(b.slotId == null || b.slotId == slotId),
)
.toList()
..sort((a, b) => b._specificity.compareTo(a._specificity));
if (candidates.isEmpty) return false;
final binding = candidates.first;
session.edits.perform(
[?elementId],
binding.verb,
binding.args?.call(session, elementId) ?? const {},
);
// A matched binding owns its chord even when its verb is unavailable.
return true;
}