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