selectConnection method

void selectConnection(
  1. String connectionId, {
  2. bool toggle = false,
})

Selects a connection in the graph.

Automatically clears selections of other element types (nodes, annotations). Requests canvas focus if not already focused.

Triggers the onConnectionSelected callback after selection changes.

Parameters:

  • connectionId: The ID of the connection to select
  • toggle: If true, toggles the connection's selection state. If false (default), clears other connection selections and selects only this connection.

Implementation

void selectConnection(String connectionId, {bool toggle = false}) {
  runInAction(() {
    // Clear other element types' selections
    clearNodeSelection();
    annotations.clearAnnotationSelection();

    // Find the connection - if it doesn't exist, we can't select it
    final connection = _connections.firstWhere((c) => c.id == connectionId);

    if (toggle) {
      if (_selectedConnectionIds.contains(connectionId)) {
        _selectedConnectionIds.remove(connectionId);
        connection.selected = false;
      } else {
        _selectedConnectionIds.add(connectionId);
        connection.selected = true;
      }
    } else {
      // Clear previous connection selection
      clearConnectionSelection();

      // Select new connection
      _selectedConnectionIds.add(connectionId);
      connection.selected = true;
    }
  });

  // Fire selection callback with current selection state
  final selectedConnection = _selectedConnectionIds.contains(connectionId)
      ? _connections.firstWhere((c) => c.id == connectionId)
      : null;
  callbacks.onConnectionSelected?.call(selectedConnection);

  if (!canvasFocusNode.hasFocus) {
    canvasFocusNode.requestFocus();
  }
}