clearGraph method

void clearGraph()

Clears the entire graph, removing all nodes, connections, and selections.

This operation:

  • Removes all nodes (including GroupNode and CommentNode)
  • Removes all connections
  • Clears all selections
  • Clears node monitoring reactions
  • Clears the connection painter cache

Does nothing if the graph is already empty.

Example:

controller.clearGraph();

Implementation

void clearGraph() {
  if (_nodes.isEmpty && _connections.isEmpty) return;

  // Detach context from groupable nodes before clearing
  for (final node in _nodes.values) {
    if (node is GroupableMixin<T>) {
      node.detachContext();
    }
  }

  runInAction(() {
    _nodes.clear();
    _connections.clear();
    _selectedNodeIds.clear();
    _selectedConnectionIds.clear();
  });

  // Clear spatial indexes to prevent stale hit test entries
  _spatialIndex.clear();

  // Clear connection painter cache to prevent stale paths
  _connectionPainter?.clearAllCachedPaths();
}