flow_rs_core/groups/
drag_state.rs

1//! Group drag state management
2
3use crate::graph::Graph;
4use crate::types::{GroupId, NodeId, Position};
5use std::collections::HashMap;
6
7use super::group::Group;
8
9/// Group drag state for tracking ongoing drag operations
10#[derive(Debug, Clone)]
11pub struct GroupDragState {
12    /// Group being dragged
13    pub dragging_group: GroupId,
14    /// Starting position when drag began
15    pub start_position: Position,
16    /// Current drag position
17    pub current_position: Position,
18    /// Original positions of all nodes before drag started
19    pub original_node_positions: HashMap<NodeId, Position>,
20}
21
22impl GroupDragState {
23    /// Create a new group drag state
24    pub fn new(group_id: GroupId, start_position: Position) -> Self {
25        Self {
26            dragging_group: group_id,
27            start_position,
28            current_position: start_position,
29            original_node_positions: HashMap::new(),
30        }
31    }
32
33    /// Update the current drag position and return the delta
34    pub fn update_position(&mut self, new_position: Position) -> Position {
35        self.current_position = new_position;
36        self.delta_from_start()
37    }
38
39    /// Get the delta from the starting position
40    pub fn delta_from_start(&self) -> Position {
41        Position::new(
42            self.current_position.x - self.start_position.x,
43            self.current_position.y - self.start_position.y,
44        )
45    }
46
47    /// Store original positions of all nodes in the group
48    pub fn store_original_positions<N, E>(
49        &mut self,
50        group: &Group,
51        graph: &Graph<N, E>,
52    ) -> Result<(), crate::error::FlowError>
53    where
54        N: Clone,
55        E: Clone,
56    {
57        for node_id in &group.members {
58            if let Some(node) = graph.get_node(node_id) {
59                self.original_node_positions.insert(node_id.clone(), node.position);
60            } else {
61                return Err(crate::error::FlowError::invalid_operation(format!(
62                    "Node {} not found in graph",
63                    node_id
64                )));
65            }
66        }
67        Ok(())
68    }
69}