orbit/component/
node.rs

1//! Enhanced node implementation with event delegation support
2
3use std::collections::HashMap;
4use std::sync::{Arc, Mutex};
5
6use crate::component::ComponentInstance;
7use crate::events::delegation::EventDelegate;
8use crate::events::Event;
9
10/// A node in the UI tree with event delegation support
11#[derive(Debug, Clone)]
12#[allow(dead_code)]
13pub struct Node {
14    /// Component instance
15    component: Option<ComponentInstance>,
16
17    /// Node attributes
18    attributes: HashMap<String, String>,
19
20    /// Child nodes
21    children: Vec<Node>,
22
23    /// Unique identifier for this node
24    id: usize,
25
26    /// Event delegate for this node (not cloneable, so wrapped in Arc)
27    event_delegate: Option<Arc<Mutex<EventDelegate>>>,
28}
29
30#[allow(dead_code)]
31impl Node {
32    /// Create a new node
33    pub fn new(component: Option<ComponentInstance>) -> Self {
34        static NEXT_ID: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(1);
35        let id = NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
36
37        Self {
38            component,
39            attributes: HashMap::new(),
40            children: Vec::new(),
41            id,
42            event_delegate: Some(Arc::new(Mutex::new(EventDelegate::new(Some(id))))),
43        }
44    }
45
46    /// Get the node's ID
47    pub fn id(&self) -> Option<usize> {
48        Some(self.id)
49    }
50
51    /// Get the node's ID as usize
52    pub fn id_value(&self) -> usize {
53        self.id
54    }
55
56    /// Get a reference to the node's children
57    pub fn children(&self) -> &[Node] {
58        &self.children
59    }
60
61    /// Get the node's children mutably
62    pub fn children_mut(&mut self) -> &mut Vec<Node> {
63        &mut self.children
64    }
65
66    /// Get the node's event delegate
67    pub fn event_delegate(&self) -> Option<Arc<Mutex<EventDelegate>>> {
68        self.event_delegate.clone()
69    }
70
71    /// Add a child node
72    pub fn add_child(&mut self, child: Node) {
73        // Set up event delegation relationship
74        if let Some(parent_delegate) = &self.event_delegate {
75            if let Some(child_delegate) = &child.event_delegate {
76                if let Ok(mut child_delegate) = child_delegate.lock() {
77                    child_delegate.set_parent(parent_delegate.clone());
78                }
79
80                if let Ok(mut parent_delegate) = parent_delegate.lock() {
81                    parent_delegate.add_child(child_delegate.clone());
82                }
83            }
84        }
85
86        self.children.push(child);
87    }
88
89    /// Add an attribute
90    pub fn add_attribute(&mut self, key: String, value: String) {
91        self.attributes.insert(key, value);
92    }
93
94    /// Dispatch an event to this node
95    pub fn dispatch_event<E: Event + Clone + 'static>(&self, event: &E) {
96        if let Some(delegate) = &self.event_delegate {
97            if let Ok(delegate) = delegate.lock() {
98                delegate.dispatch(event, self.id());
99            }
100        }
101    }
102
103    /// Get the component instance
104    pub fn component(&self) -> Option<&ComponentInstance> {
105        self.component.as_ref()
106    }
107
108    /// Get the component instance mutably
109    pub fn component_mut(&mut self) -> Option<&mut ComponentInstance> {
110        self.component.as_mut()
111    }
112
113    /// Get attributes
114    pub fn attributes(&self) -> &HashMap<String, String> {
115        &self.attributes
116    }
117}
118
119impl Default for Node {
120    fn default() -> Self {
121        static NEXT_ID: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(1);
122        let id = NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
123
124        Self {
125            component: None,
126            attributes: HashMap::new(),
127            children: Vec::new(),
128            id,
129            event_delegate: Some(Arc::new(Mutex::new(EventDelegate::new(Some(id))))),
130        }
131    }
132}