fitSelectedNodes method
void
fitSelectedNodes()
Adjusts the viewport to fit all selected nodes in the view with padding.
Calculates the optimal zoom level and pan position to show only the selected nodes with 50 pixels of padding on all sides.
Does nothing if no nodes are selected or if the screen size is zero.
Example:
controller.selectNodes(['node1', 'node2']);
controller.fitSelectedNodes();
Implementation
void fitSelectedNodes() {
if (_selectedNodeIds.isEmpty || _screenSize.value == Size.zero) return;
final bounds = selectedNodesBounds;
if (bounds == null) return;
final contentWidth = bounds.width;
final contentHeight = bounds.height;
final padding = 50.0;
final scaleX = (_screenSize.value.width - padding * 2) / contentWidth;
final scaleY = (_screenSize.value.height - padding * 2) / contentHeight;
final zoom = (scaleX < scaleY ? scaleX : scaleY).clamp(
_config.minZoom.value,
_config.maxZoom.value,
);
final centerX = bounds.left + contentWidth / 2;
final centerY = bounds.top + contentHeight / 2;
setViewport(
GraphViewport(
x: _screenSize.value.width / 2 - centerX * zoom,
y: _screenSize.value.height / 2 - centerY * zoom,
zoom: zoom,
),
);
}