findTappedDependency method

GanttTask? findTappedDependency(
  1. Offset tapPosition
)

Finds the target task of a dependency line that is close to the given tap position. Returns the target GanttTask if found, otherwise null. Requires calculateDependencyPaths to have been called.

Implementation

GanttTask? findTappedDependency(Offset tapPosition) {
  const double tapToleranceSq = 6.0 * 6.0; // Squared tolerance (pixels)

  for (final entry in _dependencyPaths.entries) {
    final targetTaskId = entry.key;
    final path = entry.value;

    try {
      // Add try-catch for safety, path metrics can sometimes fail
      final pathMetrics = path.computeMetrics();
      for (final metric in pathMetrics) {
        final double len = metric.length;
        if (len == 0) continue; // Skip zero-length metrics

        // Check points along the path
        const double step = 5.0; // Check every 5 units
        for (double distance = 0; distance <= len; distance += step) {
          // Ensure distance doesn't exceed length due to floating point issues
          final clampedDistance = min(distance, len);
          final tangent = metric.getTangentForOffset(clampedDistance);
          if (tangent != null &&
              (tapPosition - tangent.position).distanceSquared <
                  tapToleranceSq) {
            return tasks.firstWhereOrNull((t) => t.id == targetTaskId);
          }
        }
        // Check the very end point explicitly if not caught by the loop
        final endTangent = metric.getTangentForOffset(len);
        if (endTangent != null &&
            (tapPosition - endTangent.position).distanceSquared <
                tapToleranceSq) {
          return tasks.firstWhereOrNull((t) => t.id == targetTaskId);
        }
      }
    } catch (e) {
      if (kDebugMode) {
        print(
            "Error computing metrics for dependency path to task $targetTaskId: $e");
      }
    }
  }
  return null; // No dependency line hit
}