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,
}) {
  // Skip segment bounds creation for hidden connections
  if (!sourceNode.isVisible || !targetNode.isVisible) {
    return [];
  }

  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;

  // Get current port offsets for cache invalidation
  final sourcePort = _findPort(sourceNode, connection.sourcePortId);
  final targetPort = _findPort(targetNode, connection.targetPortId);
  final currentSourcePortOffset = sourcePort?.offset ?? Offset.zero;
  final currentTargetPortOffset = targetPort?.offset ?? Offset.zero;

  // Check if cache is valid (including node sizes and port offsets)
  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 &&
      existing.sourcePortOffset == currentSourcePortOffset &&
      existing.targetPortOffset == currentTargetPortOffset) {
    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 ?? [];
}