startNodeDrag method

void startNodeDrag(
  1. String nodeId, {
  2. MouseCursor? cursor,
})

Starts a node drag operation.

Call this from NodeWidget's GestureDetector.onPanStart. If the node is part of a selection, all selected nodes will be dragged together.

This method:

  • Selects the node if not already selected
  • Brings the node to front (increases z-index)
  • Sets up drag state
  • Fires the drag start event
  • Notifies monitoring nodes (like GroupNode) of drag start

Note: Canvas locking is handled by DragSession, not this method. Widgets should create a session and call DragSession.start to lock canvas.

Parameters:

  • nodeId: The ID of the node being dragged
  • cursor: The cursor to display during drag (optional, defaults to grabbing)

Implementation

void startNodeDrag(String nodeId, {MouseCursor? cursor}) {
  final wasSelected = selectedNodeIds.contains(nodeId);

  // Select node if not already selected
  if (!wasSelected) {
    selectNode(nodeId);
  }

  // Bring node to front
  bringNodeToFront(nodeId);

  runInAction(() {
    // Set drag state
    interaction.draggedNodeId.value = nodeId;

    // Cursor is handled by widgets via their MouseRegion

    // Note: Canvas locking is now handled by DragSession

    // Update visual dragging state on all affected nodes
    final nodeIds = selectedNodeIds.contains(nodeId)
        ? selectedNodeIds.toList()
        : [nodeId];
    for (final id in nodeIds) {
      _nodes[id]?.dragging.value = true;
    }
  });

  // Notify monitoring nodes of drag start
  final context = _createDragContext();
  final nodeIds = selectedNodeIds.contains(nodeId)
      ? selectedNodeIds.toList()
      : [nodeId];
  for (final id in nodeIds) {
    _nodes[id]?.onDragStart(context);
  }

  // Capture original positions for extension events
  final originalPositions = <String, Offset>{};
  for (final id in nodeIds) {
    final n = _nodes[id];
    if (n != null) {
      originalPositions[id] = n.position.value;
    }
  }
  interaction.captureDragStartPositions(originalPositions);

  // Fire drag start event
  final node = _nodes[nodeId];
  if (node != null) {
    events.node?.onDragStart?.call(node);
  }

  // Emit extension event
  _emitEvent(
    NodeDragStarted(nodeIds.toSet(), node?.position.value ?? Offset.zero),
  );
}