Skip to main content

ferrum_flow/plugins/port/
command.rs

1use crate::{Edge, GraphOp, Node, Port, canvas::Command};
2
3pub struct CreateEdge {
4    edge: Edge,
5}
6
7impl CreateEdge {
8    pub fn new(edge: Edge) -> Self {
9        Self { edge }
10    }
11}
12
13impl Command for CreateEdge {
14    fn name(&self) -> &'static str {
15        "create_edge"
16    }
17    fn execute(&mut self, ctx: &mut crate::canvas::CommandContext) {
18        ctx.add_edge(self.edge.clone());
19    }
20    fn undo(&mut self, ctx: &mut crate::canvas::CommandContext) {
21        ctx.remove_edge(self.edge.id);
22    }
23
24    fn to_ops(&self, _ctx: &mut crate::CommandContext) -> Vec<crate::GraphOp> {
25        vec![GraphOp::AddEdge(self.edge.clone())]
26    }
27}
28
29pub struct CreateNode {
30    node: Node,
31}
32
33impl CreateNode {
34    pub fn new(node: Node) -> Self {
35        Self { node }
36    }
37}
38
39impl Command for CreateNode {
40    fn name(&self) -> &'static str {
41        "create_node"
42    }
43    fn execute(&mut self, ctx: &mut crate::canvas::CommandContext) {
44        ctx.add_node(self.node.clone());
45    }
46    fn to_ops(&self, _ctx: &mut crate::CommandContext) -> Vec<GraphOp> {
47        vec![
48            GraphOp::AddNode(self.node.clone()),
49            GraphOp::NodeOrderInsert { id: self.node.id },
50        ]
51    }
52    fn undo(&mut self, ctx: &mut crate::canvas::CommandContext) {
53        ctx.remove_node(&self.node.id);
54    }
55}
56
57pub struct CreatePort {
58    port: Port,
59}
60
61impl CreatePort {
62    pub fn new(port: Port) -> Self {
63        Self { port }
64    }
65}
66
67impl Command for CreatePort {
68    fn name(&self) -> &'static str {
69        "create_port"
70    }
71    fn execute(&mut self, ctx: &mut crate::canvas::CommandContext) {
72        ctx.add_port(self.port.clone());
73    }
74    fn to_ops(&self, _ctx: &mut crate::CommandContext) -> Vec<GraphOp> {
75        vec![GraphOp::AddPort(self.port.clone())]
76    }
77    fn undo(&mut self, ctx: &mut crate::canvas::CommandContext) {
78        ctx.remove_port(&self.port.id);
79    }
80}