hitTest method

bool hitTest({
  1. required Connection connection,
  2. required Node sourceNode,
  3. required Node targetNode,
  4. required Offset testPoint,
  5. double? tolerance,
})

Test if a point hits a connection path Returns true if the point is within the hit tolerance of the connection

Implementation

bool hitTest({
  required Connection connection,
  required Node sourceNode,
  required Node targetNode,
  required Offset testPoint,
  double? tolerance,
}) {
  final hitTolerance = tolerance ?? defaultHitTolerance;

  // Get cached path - read only, no creation during hit testing
  final cachedPath = _getCachedPath(connection.id);
  if (cachedPath == null) {
    return false; // No cached path - should be created during painting
  }

  // Validate cache is still valid
  final currentSourcePos = sourceNode.position.value;
  final currentTargetPos = targetNode.position.value;

  if (cachedPath.sourcePosition != currentSourcePos ||
      cachedPath.targetPosition != currentTargetPos) {
    return false; // Stale cache - will be recreated on next paint
  }

  // Use the pre-computed hit test path (already expanded for tolerance)
  // Only recompute if tolerance differs significantly from default
  if ((hitTolerance - defaultHitTolerance).abs() > 1.0) {
    // Custom tolerance - create temporary expanded path
    final customHitPath = _createHitTestPath(
      cachedPath.originalPath,
      hitTolerance,
      connectionStyle: theme.connectionTheme.style,
    );
    return customHitPath.contains(testPoint);
  }

  // Use cached hit test path (most common case)
  return cachedPath.hitTestPath.contains(testPoint);
}