bringNodeForward method

void bringNodeForward(
  1. String nodeId
)

Moves a node one step forward in the z-order.

Swaps the z-index with the next higher node in the visual stack. Does nothing if the node is already at the front.

Parameters:

  • nodeId: The ID of the node to move forward

Example:

controller.bringNodeForward('node1');

Implementation

void bringNodeForward(String nodeId) {
  final node = _nodes[nodeId];
  if (node == null) return;

  runInAction(() {
    // Sort all nodes by z-index to get the visual order
    final sortedNodes = _nodes.values.toList()
      ..sort((a, b) => a.zIndex.value.compareTo(b.zIndex.value));

    // Find current node's position in the visual order
    final currentIndex = sortedNodes.indexOf(node);

    // If not at the top, swap with next higher node
    if (currentIndex < sortedNodes.length - 1) {
      final nextNode = sortedNodes[currentIndex + 1];

      // We need to ensure the z-indexes are different
      // If they're the same, normalize all z-indexes first
      if (node.zIndex.value == nextNode.zIndex.value) {
        // Normalize all z-indexes to be sequential
        for (int i = 0; i < sortedNodes.length; i++) {
          sortedNodes[i].zIndex.value = i;
        }
        // Now swap the normalized values
        node.zIndex.value = currentIndex + 1;
        nextNode.zIndex.value = currentIndex;
      } else {
        // Z-indexes are different, just swap them
        final currentZ = node.zIndex.value;
        final nextZ = nextNode.zIndex.value;
        node.zIndex.value = nextZ;
        nextNode.zIndex.value = currentZ;
      }
    }
  });
}