setBehavior method

void setBehavior(
  1. GroupBehavior newBehavior, {
  2. Set<String>? captureContainedNodes,
  3. NodeLookup? nodeLookup,
  4. bool clearNodesOnBoundsSwitch = true,
})

Changes the group's behavior.

When switching behaviors:

  • bounds → explicit/parent: If captureContainedNodes is provided, those nodes are added to nodeIds. For explicit, fitToNodes is called.
  • explicit/parent → bounds: nodeIds is optionally cleared based on clearNodesOnBoundsSwitch.
  • explicit ↔ parent: nodeIds is preserved.

The annotation controller automatically updates node monitoring reactions when behavior changes.

Implementation

void setBehavior(
  GroupBehavior newBehavior, {
  Set<String>? captureContainedNodes,
  NodeLookup? nodeLookup,
  bool clearNodesOnBoundsSwitch = true,
}) {
  if (behavior == newBehavior) return;

  final oldBehavior = behavior;

  runInAction(() {
    // Handle transition from bounds to explicit/parent
    if (oldBehavior == GroupBehavior.bounds &&
        newBehavior != GroupBehavior.bounds) {
      // Capture contained nodes if provided
      if (captureContainedNodes != null && captureContainedNodes.isNotEmpty) {
        _nodeIds.addAll(captureContainedNodes);
      }
    }

    // Handle transition to bounds - optionally clear nodeIds
    if (newBehavior == GroupBehavior.bounds && clearNodesOnBoundsSwitch) {
      _nodeIds.clear();
    }

    // Update behavior
    _observableBehavior.value = newBehavior;

    // For explicit behavior, fit to nodes if we have any and a lookup
    if (newBehavior == GroupBehavior.explicit &&
        _nodeIds.isNotEmpty &&
        nodeLookup != null) {
      fitToNodes(nodeLookup);
    }
  });
}