Skip to main content

jellyflow_runtime/runtime/drag/
activation.rs

1use jellyflow_core::core::CanvasPoint;
2
3/// Screen-space input for deciding whether a node drag should activate.
4///
5/// XyFlow evaluates `nodeDragThreshold` in client/screen coordinates so zoom does not change when
6/// dragging starts. Adapters should pass the screen delta from pointer-down to the current pointer.
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct NodeDragActivationInput {
9    pub screen_delta: CanvasPoint,
10    pub threshold: f32,
11}
12
13impl NodeDragActivationInput {
14    pub fn new(screen_delta: CanvasPoint, threshold: f32) -> Self {
15        Self {
16            screen_delta,
17            threshold,
18        }
19    }
20}
21
22/// Returns whether a pointer movement should start a node drag.
23///
24/// This mirrors XyFlow's threshold shape: threshold `0` starts immediately, otherwise the
25/// Euclidean screen-space distance must be strictly greater than `nodeDragThreshold`.
26pub fn node_drag_threshold_met(input: NodeDragActivationInput) -> 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    input.screen_delta.x.hypot(input.screen_delta.y) > input.threshold
38}