Skip to main content

jellyflow_runtime/runtime/resize/
session.rs

1use crate::runtime::events::{
2    NodeGraphGestureEvent, NodeResizeEnd, NodeResizeEndOutcome, NodeResizeStart, NodeResizeUpdate,
3};
4use jellyflow_core::core::{CanvasPoint, NodeId};
5
6use super::types::{
7    NodePointerResizeRequest, NodeResizeAxis, NodeResizeConstraints, NodeResizeDirection,
8    NodeResizePlan,
9};
10
11/// Headless state for one pointer-driven node resize gesture.
12#[derive(Debug, Clone, Copy, PartialEq)]
13pub struct NodeResizeSession {
14    pub node: NodeId,
15    pub direction: NodeResizeDirection,
16    pub start: CanvasPoint,
17}
18
19impl NodeResizeSession {
20    pub fn new(node: NodeId, start: CanvasPoint, direction: NodeResizeDirection) -> Self {
21        Self {
22            node,
23            direction,
24            start,
25        }
26    }
27
28    pub fn start(self) -> NodeResizeStart {
29        NodeResizeStart {
30            node: self.node,
31            direction: self.direction,
32            pointer: self.start,
33        }
34    }
35
36    pub fn start_event(self) -> NodeGraphGestureEvent {
37        NodeGraphGestureEvent::NodeResizeStart(self.start())
38    }
39
40    pub fn update(
41        self,
42        plan: &NodeResizePlan,
43        request: NodeResizeSessionUpdateRequest,
44    ) -> NodeResizeUpdate {
45        NodeResizeUpdate {
46            node: self.node,
47            direction: self.direction,
48            pointer: request.current,
49            position: plan.to_pos,
50            size: plan.to,
51        }
52    }
53
54    pub fn update_event(
55        self,
56        plan: &NodeResizePlan,
57        request: NodeResizeSessionUpdateRequest,
58    ) -> NodeGraphGestureEvent {
59        NodeGraphGestureEvent::NodeResizeUpdate(self.update(plan, request))
60    }
61
62    pub fn end(self, pointer: CanvasPoint, outcome: NodeResizeEndOutcome) -> NodeResizeEnd {
63        NodeResizeEnd {
64            node: self.node,
65            direction: self.direction,
66            pointer,
67            outcome,
68        }
69    }
70
71    pub fn end_event(
72        self,
73        pointer: CanvasPoint,
74        outcome: NodeResizeEndOutcome,
75    ) -> NodeGraphGestureEvent {
76        NodeGraphGestureEvent::NodeResizeEnd(self.end(pointer, outcome))
77    }
78
79    pub fn pointer_resize_request(
80        self,
81        request: NodeResizeSessionUpdateRequest,
82    ) -> NodePointerResizeRequest {
83        NodePointerResizeRequest::new(self.node, self.start, request.current, self.direction)
84            .with_constraints(request.constraints)
85            .with_keep_aspect_ratio(request.keep_aspect_ratio)
86            .with_axis(request.axis)
87    }
88}
89
90/// Per-update inputs for a node resize session.
91#[derive(Debug, Clone, Copy, PartialEq)]
92pub struct NodeResizeSessionUpdateRequest {
93    pub current: CanvasPoint,
94    pub constraints: NodeResizeConstraints,
95    pub keep_aspect_ratio: bool,
96    pub axis: NodeResizeAxis,
97}
98
99impl NodeResizeSessionUpdateRequest {
100    pub fn new(current: CanvasPoint) -> Self {
101        Self {
102            current,
103            constraints: NodeResizeConstraints::default(),
104            keep_aspect_ratio: false,
105            axis: NodeResizeAxis::default(),
106        }
107    }
108
109    pub fn with_constraints(mut self, constraints: NodeResizeConstraints) -> Self {
110        self.constraints = constraints;
111        self
112    }
113
114    pub fn with_keep_aspect_ratio(mut self, keep_aspect_ratio: bool) -> Self {
115        self.keep_aspect_ratio = keep_aspect_ratio;
116        self
117    }
118
119    pub fn with_axis(mut self, axis: NodeResizeAxis) -> Self {
120        self.axis = axis;
121        self
122    }
123}