Skip to main content

jellyflow_runtime/runtime/connection/
activation.rs

1use jellyflow_core::core::CanvasPoint;
2
3/// Screen-space input for deciding whether a connection drag should activate.
4///
5/// XyFlow evaluates `connectionDragThreshold` in client/screen coordinates, using squared
6/// distance, so zoom does not change when connection gestures start.
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct ConnectionDragActivationInput {
9    pub screen_delta: CanvasPoint,
10    pub threshold: f32,
11}
12
13impl ConnectionDragActivationInput {
14    pub fn new(screen_delta: CanvasPoint, threshold: f32) -> Self {
15        Self {
16            screen_delta,
17            threshold,
18        }
19    }
20}
21
22/// Returns whether pointer movement should start a connection drag.
23///
24/// This mirrors XyFlow's threshold shape: threshold `0` starts immediately, otherwise the squared
25/// screen-space distance must be strictly greater than `connectionDragThreshold^2`.
26pub fn connection_drag_threshold_met(input: ConnectionDragActivationInput) -> bool {
27    if !input.screen_delta.is_finite() {
28        return false;
29    }
30    if input.threshold == 0.0 {
31        return true;
32    }
33    if !input.threshold.is_finite() {
34        return false;
35    }
36
37    let threshold = input.threshold.abs();
38    let distance_squared =
39        input.screen_delta.x * input.screen_delta.x + input.screen_delta.y * input.screen_delta.y;
40    distance_squared > threshold * threshold
41}