distributeNodesHorizontally method
Distributes nodes evenly along the horizontal axis.
Requires at least 3 nodes. Sorts nodes by X position, keeps the leftmost and rightmost nodes in place, and distributes the middle nodes evenly.
Implementation
void distributeNodesHorizontally(List<String> nodeIds) {
if (nodeIds.length < 3) return;
final nodes = nodeIds.map((id) => _nodes[id]).whereType<Node<T>>().toList();
if (nodes.length < 3) return;
nodes.sort((a, b) => a.position.value.dx.compareTo(b.position.value.dx));
final leftmost = nodes.first.position.value.dx;
final rightmost = nodes.last.position.value.dx;
final spacing = (rightmost - leftmost) / (nodes.length - 1);
runInAction(() {
for (int i = 1; i < nodes.length - 1; i++) {
final targetX = leftmost + spacing * i;
final newPosition = Offset(targetX, nodes[i].position.value.dy);
nodes[i].position.value = newPosition;
nodes[i].setVisualPosition(_config.snapToGridIfEnabled(newPosition));
}
});
internalMarkNodesDirty(nodeIds);
rebuildConnectionSegmentsForNodes(nodeIds);
}