use serde::{Deserialize, Serialize};
use jellyflow_core::core::{CanvasPoint, NodeId};
use jellyflow_core::ops::GraphTransaction;
pub const NODE_DRAG_TRANSACTION_LABEL: &str = "node drag";
pub const NODE_NUDGE_TRANSACTION_LABEL: &str = "node nudge";
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NodeDragRequest {
pub node: NodeId,
pub to: CanvasPoint,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NodeNudgeDirection {
Up,
Down,
Left,
Right,
}
impl NodeNudgeDirection {
pub fn unit_delta(self) -> CanvasPoint {
match self {
Self::Up => CanvasPoint { x: 0.0, y: -1.0 },
Self::Down => CanvasPoint { x: 0.0, y: 1.0 },
Self::Left => CanvasPoint { x: -1.0, y: 0.0 },
Self::Right => CanvasPoint { x: 1.0, y: 0.0 },
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct NodeNudgeRequest {
pub direction: NodeNudgeDirection,
pub fast: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NodeDragItem {
pub node: NodeId,
pub from: CanvasPoint,
pub to: CanvasPoint,
}
#[derive(Debug, Clone)]
pub struct NodeDragPlan {
pub node: NodeId,
pub from: CanvasPoint,
pub to: CanvasPoint,
items: Vec<NodeDragItem>,
transaction: GraphTransaction,
}
#[derive(Debug, Clone)]
pub struct NodeNudgePlan {
pub direction: NodeNudgeDirection,
pub delta: CanvasPoint,
items: Vec<NodeDragItem>,
transaction: GraphTransaction,
}
impl NodeDragPlan {
pub(super) fn new(
node: NodeId,
from: CanvasPoint,
to: CanvasPoint,
items: Vec<NodeDragItem>,
transaction: GraphTransaction,
) -> Self {
Self {
node,
from,
to,
items,
transaction,
}
}
pub fn items(&self) -> &[NodeDragItem] {
&self.items
}
pub fn transaction(&self) -> &GraphTransaction {
&self.transaction
}
pub fn into_transaction(self) -> GraphTransaction {
self.transaction
}
}
impl NodeNudgePlan {
pub(super) fn new(
direction: NodeNudgeDirection,
delta: CanvasPoint,
items: Vec<NodeDragItem>,
transaction: GraphTransaction,
) -> Self {
Self {
direction,
delta,
items,
transaction,
}
}
pub fn items(&self) -> &[NodeDragItem] {
&self.items
}
pub fn transaction(&self) -> &GraphTransaction {
&self.transaction
}
pub fn into_transaction(self) -> GraphTransaction {
self.transaction
}
}