Rename NodeId to Node and use newtype
1#[cfg(not(feature = "std"))]
2use alloc::{vec, vec::Vec};
3
4use std::collections::HashMap;
5
6use crate::geometry::Size;
7use crate::number::Number;
8use crate::result::{Cache, Layout, Result};
9use crate::style::*;
10
11type MeasureFunc = Box<Fn(Size<Number>) -> Result<Size<f32>>>;
12
13#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
14pub struct Node(usize);
15
16pub(crate) type Storage<T> = HashMap<Node, T>;
17
18struct Allocator {
19 new_id: usize,
20 free_ids: Vec<usize>,
21}
22
23impl Allocator {
24 pub fn new() -> Self {
25 Allocator { new_id: 0, free_ids: Vec::new() }
26 }
27
28 pub fn allocate(&mut self) -> Node {
29 match self.free_ids.pop() {
30 Some(id) => Node(id),
31 None => {
32 let id = self.new_id;
33 self.new_id += 1;
34 Node(id)
35 }
36 }
37 }
38}
39
40pub struct Stretch {
41 nodes: Allocator,
42 pub(crate) style: Storage<Style>,
43 pub(crate) parents: Storage<Vec<Node>>,
44 pub(crate) children: Storage<Vec<Node>>,
45 pub(crate) measure: Storage<Option<MeasureFunc>>,
46 pub(crate) layout: Storage<Layout>,
47 pub(crate) layout_cache: Storage<Option<Cache>>,
48 pub(crate) is_dirty: Storage<bool>,
49}
50
51impl Stretch {
52 pub fn new() -> Self {
53 Stretch {
54 nodes: Allocator::new(),
55 style: Storage::new(),
56 parents: Storage::new(),
57 children: Storage::new(),
58 measure: Storage::new(),
59 layout: Storage::new(),
60 layout_cache: Storage::new(),
61 is_dirty: Storage::new(),
62 }
63 }
64
65 pub fn new_leaf(&mut self, style: Style, measure: MeasureFunc) -> Node {
66 // Node(Rc::new(RefCell::new(InternalNode {
67 // style,
68 // parents: Vec::with_capacity(1),
69 // children: Vec::with_capacity(0),
70 // measure: Some(measure),
71 // layout_cache: None,
72 // is_dirty: true,
73 // })))
74 unimplemented!()
75 }
76
77 pub fn new_node(&mut self, style: Style, children: Vec<Node>) -> Node {
78 let node = self.nodes.allocate();
79
80 for child in &children {
81 self.parents.get_mut(&child).unwrap().push(node);
82 }
83
84 self.style.insert(node, style);
85 self.parents.insert(node, Vec::with_capacity(1));
86 self.children.insert(node, children);
87 self.measure.insert(node, None);
88 self.layout.insert(node, Layout::new());
89 self.layout_cache.insert(node, None);
90 self.is_dirty.insert(node, true);
91
92 node
93 }
94
95 pub fn set_measure(&mut self, node: Node, measure: Option<MeasureFunc>) {
96 // self.0.borrow_mut().measure = measure;
97 // self.mark_dirty();
98
99 unimplemented!()
100 }
101
102 pub fn add_child(&mut self, node: Node, child: Node) {
103 // child.0.borrow_mut().parents.push(Rc::downgrade(&self.0));
104 // self.0.borrow_mut().children.push(Rc::clone(&child.0));
105 // self.mark_dirty();
106
107 unimplemented!()
108 }
109
110 pub fn set_children(&mut self, node: Node, children: Vec<Node>) {
111 // for child in &self.0.borrow().children {
112 // let position =
113 // child.borrow().parents.iter().position(|x| Rc::ptr_eq(&x.upgrade().unwrap(), &self.0)).unwrap();
114 // child.borrow_mut().parents.remove(position);
115 // }
116
117 // self.0.borrow_mut().children = Vec::with_capacity(children.len());
118
119 // for child in children {
120 // child.0.borrow_mut().parents.push(Rc::downgrade(&self.0));
121 // self.0.borrow_mut().children.push(Rc::clone(&child.0));
122 // }
123
124 // self.mark_dirty();
125
126 unimplemented!()
127 }
128
129 pub fn remove_child(&mut self, node: Node, child: Node) -> Node {
130 // self.remove_child_at_index({
131 // let parent = self.0.borrow();
132 // parent.children.iter().position(|x| Rc::ptr_eq(&x, &child.0)).unwrap()
133 // })
134
135 unimplemented!()
136 }
137
138 pub fn remove_child_at_index(&mut self, node: Node, index: usize) -> Node {
139 // let child = {
140 // let mut parent = self.0.borrow_mut();
141 // let child = parent.children.remove(index);
142 // let position =
143 // child.borrow().parents.iter().position(|x| Rc::ptr_eq(&x.upgrade().unwrap(), &self.0)).unwrap();
144 // child.borrow_mut().parents.remove(position);
145 // child
146 // };
147
148 // self.mark_dirty();
149 // Node(child)
150
151 unimplemented!()
152 }
153
154 pub fn replace_child_at_index(&mut self, node: Node, index: usize, child: Node) -> Node {
155 // child.0.borrow_mut().parents.push(Rc::downgrade(&self.0));
156 // let old_child = std::mem::replace(&mut self.0.borrow_mut().children[index], Rc::clone(&child.0));
157
158 // let position =
159 // old_child.borrow().parents.iter().position(|x| Rc::ptr_eq(&x.upgrade().unwrap(), &self.0)).unwrap();
160 // old_child.borrow_mut().parents.remove(position);
161
162 // self.mark_dirty();
163
164 // Node(old_child)
165
166 unimplemented!()
167 }
168
169 pub fn children(&self, node: Node) -> Vec<Node> {
170 // let node = self.0.borrow_mut();
171 // node.children.iter().map(|child| Node(Rc::clone(child))).collect()
172
173 unimplemented!()
174 }
175
176 pub fn child_count(&self, node: Node) -> usize {
177 // let node = self.0.borrow_mut();
178 // node.children.len()
179
180 unimplemented!()
181 }
182
183 pub fn set_style(&mut self, node: Node, style: Style) {
184 // self.0.borrow_mut().style = style;
185 // self.mark_dirty();
186
187 unimplemented!()
188 }
189
190 pub fn style(&self, node: Node) -> Style {
191 // self.0.borrow().style
192
193 unimplemented!()
194 }
195
196 pub fn layout(&self, node: Node) -> &Layout {
197 &self.layout[&node]
198 }
199
200 pub fn mark_dirty(&mut self, node: Node) {
201 // let mut node = self.0.borrow_mut();
202 // node.layout_cache = None;
203 // node.is_dirty = true;
204
205 // for parent in &node.parents {
206 // if let Some(parent) = parent.upgrade() {
207 // Node(parent).mark_dirty();
208 // }
209 // }
210
211 unimplemented!()
212 }
213
214 pub fn dirty(&self, node: Node) -> bool {
215 // self.0.borrow().is_dirty
216
217 unimplemented!()
218 }
219
220 pub fn compute_layout(&mut self, node: Node, size: Size<Number>) -> Result<()> {
221 self.compute(node, size)
222 }
223}