Skip to main content

jellyflow_runtime/runtime/policy/
edge.rs

1use crate::io::NodeGraphInteractionState;
2use crate::runtime::geometry::EdgeHitTestOptions;
3use jellyflow_core::core::{Edge, EdgeReconnectable};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct NodeGraphEdgeInteractionPolicy {
7    pub selectable: bool,
8    pub focusable: bool,
9    pub deletable: bool,
10    pub reconnect_source: bool,
11    pub reconnect_target: bool,
12}
13
14impl NodeGraphEdgeInteractionPolicy {
15    pub fn can_delete(self) -> bool {
16        self.deletable
17    }
18
19    pub fn reconnectable(self) -> bool {
20        self.reconnect_source || self.reconnect_target
21    }
22
23    pub fn can_reconnect_source(self) -> bool {
24        self.reconnect_source
25    }
26
27    pub fn can_reconnect_target(self) -> bool {
28        self.reconnect_target
29    }
30}
31
32pub fn resolve_edge_interaction_policy(
33    edge: &Edge,
34    state: &NodeGraphInteractionState,
35) -> NodeGraphEdgeInteractionPolicy {
36    let connection = state.connection_interaction();
37    let keyboard = state.keyboard_interaction();
38    let selection = state.selection_interaction();
39    let delete = state.delete_interaction();
40    let reconnectable = edge
41        .reconnectable
42        .unwrap_or(EdgeReconnectable::Bool(connection.edges_reconnectable));
43    let (reconnect_source, reconnect_target) = reconnectable.endpoint_flags();
44
45    NodeGraphEdgeInteractionPolicy {
46        selectable: edge.selectable.unwrap_or(selection.edges_selectable),
47        focusable: edge.focusable.unwrap_or(keyboard.edges_focusable),
48        deletable: edge.deletable.unwrap_or(delete.edges_deletable),
49        reconnect_source,
50        reconnect_target,
51    }
52}
53
54pub fn resolve_edge_hit_test_options(
55    edge: &Edge,
56    state: &NodeGraphInteractionState,
57) -> EdgeHitTestOptions {
58    EdgeHitTestOptions::new(
59        edge.interaction_width
60            .unwrap_or(state.edge_interaction_width),
61        usize::from(state.bezier_hit_test_steps),
62    )
63}