focusNodesById method

void focusNodesById(
  1. Set<String> ids
)

////////////////////////////////////////////////////////////////// Miscellaneous helpers useful for node editors. ////////////////////////////////////////////////////////////////// This method is used to focus the viweport on a set of nodes by their IDs.

The method calculates the encompassing rectangle of the nodes and then centers the viewport on the center of the rectangle. The method also calculates the zoom level required to fit all the nodes in the viewport.

See calculateEncompassingRect, selectNodesById, setViewportOffset, and setViewportZoom for more information.

Implementation

/// This method is used to focus the viweport on a set of nodes by their IDs.
///
/// The method calculates the encompassing rectangle of the nodes and then
/// centers the viewport on the center of the rectangle. The method also
/// calculates the zoom level required to fit all the nodes in the viewport.
///
/// See [calculateEncompassingRect], [selectNodesById], [setViewportOffset], and [setViewportZoom] for more information.
void focusNodesById(Set<String> ids) {
  final encompassingRect = calculateEncompassingRect(
    ids,
    nodes,
    margin: 256,
  );

  selectNodesById(ids, holdSelection: false);

  final nodeEditorSize = getSizeFromGlobalKey(kNodeEditorWidgetKey)!;

  setViewportOffset(
    -encompassingRect.center,
    animate: true,
    absolute: true,
  );

  final fitZoom = min(
    nodeEditorSize.width / encompassingRect.width,
    nodeEditorSize.height / encompassingRect.height,
  );

  setViewportZoom(fitZoom, animate: true);
}