getOrCreateSegmentBounds method

List<Rect> getOrCreateSegmentBounds({
  1. required Connection connection,
  2. required Node sourceNode,
  3. required Node targetNode,
  4. required ConnectionStyle connectionStyle,
})

Get or compute segment bounds for a connection. Creates the path if not cached or if the cache is stale.

Implementation

List<Rect> getOrCreateSegmentBounds({
  required Connection connection,
  required Node sourceNode,
  required Node targetNode,
  required ConnectionStyle connectionStyle,
}) {
  final currentSourcePos = sourceNode.position.value;
  final currentTargetPos = targetNode.position.value;
  final currentSourceSize = sourceNode.size.value;
  final currentTargetSize = targetNode.size.value;
  final connectionTheme = theme.connectionTheme;
  final currentStartGap = connection.startGap ?? connectionTheme.startGap;
  final currentEndGap = connection.endGap ?? connectionTheme.endGap;

  // Check if cache is valid (including node sizes for node-aware routing)
  final existing = _getCachedPath(connection.id);
  if (existing != null &&
      existing.sourcePosition == currentSourcePos &&
      existing.targetPosition == currentTargetPos &&
      existing.startGap == currentStartGap &&
      existing.endGap == currentEndGap &&
      existing.sourceNodeSize == currentSourceSize &&
      existing.targetNodeSize == currentTargetSize) {
    return existing.segmentBounds;
  }

  // Create new path and get segments
  final newCachedPath = _createAndCachePath(
    connection: connection,
    sourceNode: sourceNode,
    targetNode: targetNode,
    sourcePosition: currentSourcePos,
    targetPosition: currentTargetPos,
    connectionStyle: connectionStyle,
    startGap: currentStartGap,
    endGap: currentEndGap,
  );

  return newCachedPath?.segmentBounds ?? [];
}