Skip to main content

jellyflow_runtime/runtime/drag/
types.rs

1use serde::{Deserialize, Serialize};
2
3use jellyflow_core::core::{CanvasPoint, NodeId};
4use jellyflow_core::ops::GraphTransaction;
5
6/// Default transaction label used for committed node drag updates.
7pub const NODE_DRAG_TRANSACTION_LABEL: &str = "node drag";
8
9/// Default transaction label used for committed keyboard nudge updates.
10pub const NODE_NUDGE_TRANSACTION_LABEL: &str = "node nudge";
11
12/// Canvas-space request for moving a primary node to a target position.
13///
14/// When used through [`crate::runtime::store::NodeGraphStore`], currently selected nodes are
15/// co-dragged with the primary node when policy allows them to move.
16#[derive(Debug, Clone, Copy, PartialEq)]
17pub struct NodeDragRequest {
18    /// Primary node being moved.
19    pub node: NodeId,
20    /// Target primary-node position in canvas space.
21    pub to: CanvasPoint,
22}
23
24/// Keyboard nudge direction.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "snake_case")]
27pub enum NodeNudgeDirection {
28    Up,
29    Down,
30    Left,
31    Right,
32}
33
34impl NodeNudgeDirection {
35    pub fn unit_delta(self) -> CanvasPoint {
36        match self {
37            Self::Up => CanvasPoint { x: 0.0, y: -1.0 },
38            Self::Down => CanvasPoint { x: 0.0, y: 1.0 },
39            Self::Left => CanvasPoint { x: -1.0, y: 0.0 },
40            Self::Right => CanvasPoint { x: 1.0, y: 0.0 },
41        }
42    }
43}
44
45/// Request for nudging the current selected nodes.
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
47pub struct NodeNudgeRequest {
48    pub direction: NodeNudgeDirection,
49    pub fast: bool,
50}
51
52/// One node movement inside a drag plan.
53#[derive(Debug, Clone, Copy, PartialEq)]
54pub struct NodeDragItem {
55    /// Node being moved.
56    pub node: NodeId,
57    /// Current node position.
58    pub from: CanvasPoint,
59    /// Target node position.
60    pub to: CanvasPoint,
61}
62
63/// Planned node drag transaction.
64#[derive(Debug, Clone)]
65pub struct NodeDragPlan {
66    /// Primary node being moved.
67    pub node: NodeId,
68    /// Current primary-node position.
69    pub from: CanvasPoint,
70    /// Target primary-node position.
71    pub to: CanvasPoint,
72    items: Vec<NodeDragItem>,
73    transaction: GraphTransaction,
74}
75
76/// Planned keyboard nudge transaction.
77#[derive(Debug, Clone)]
78pub struct NodeNudgePlan {
79    /// Direction requested by the adapter.
80    pub direction: NodeNudgeDirection,
81    /// Final canvas-space delta after step resolution and snapping.
82    pub delta: CanvasPoint,
83    items: Vec<NodeDragItem>,
84    transaction: GraphTransaction,
85}
86
87impl NodeDragPlan {
88    pub(super) fn new(
89        node: NodeId,
90        from: CanvasPoint,
91        to: CanvasPoint,
92        items: Vec<NodeDragItem>,
93        transaction: GraphTransaction,
94    ) -> Self {
95        Self {
96            node,
97            from,
98            to,
99            items,
100            transaction,
101        }
102    }
103
104    /// Returns ordered drag items. Items are sorted by node id for deterministic transactions.
105    pub fn items(&self) -> &[NodeDragItem] {
106        &self.items
107    }
108
109    /// Returns the transaction that applies this drag update.
110    pub fn transaction(&self) -> &GraphTransaction {
111        &self.transaction
112    }
113
114    /// Consumes the plan and returns its transaction.
115    pub fn into_transaction(self) -> GraphTransaction {
116        self.transaction
117    }
118}
119
120impl NodeNudgePlan {
121    pub(super) fn new(
122        direction: NodeNudgeDirection,
123        delta: CanvasPoint,
124        items: Vec<NodeDragItem>,
125        transaction: GraphTransaction,
126    ) -> Self {
127        Self {
128            direction,
129            delta,
130            items,
131            transaction,
132        }
133    }
134
135    /// Returns ordered nudge items. Items are sorted by node id for deterministic transactions.
136    pub fn items(&self) -> &[NodeDragItem] {
137        &self.items
138    }
139
140    /// Returns the transaction that applies this nudge update.
141    pub fn transaction(&self) -> &GraphTransaction {
142        &self.transaction
143    }
144
145    /// Consumes the plan and returns its transaction.
146    pub fn into_transaction(self) -> GraphTransaction {
147        self.transaction
148    }
149}