1mod common {
2 pub mod image;
3 pub mod text;
4}
5use common::image::{image_measure_function, ImageContext};
6use common::text::{text_measure_function, FontMetrics, TextContext, WritingMode, LOREM_IPSUM};
7use gummy::util::print_tree;
8use gummy::{
9 compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
10 prelude::*, round_layout, Cache, CacheTree,
11};
12
13#[derive(Debug, Copy, Clone)]
14#[allow(dead_code)]
15enum NodeKind {
16 Flexbox,
17 Grid,
18 Text,
19 Image,
20}
21
22struct Node {
23 kind: NodeKind,
24 style: Style,
25 text_data: Option<TextContext>,
26 image_data: Option<ImageContext>,
27 cache: Cache,
28 unrounded_layout: Layout,
29 has_new_layout: bool,
30 final_layout: Layout,
31 children: Vec<usize>,
32}
33
34impl Default for Node {
35 fn default() -> Self {
36 Node {
37 kind: NodeKind::Flexbox,
38 style: Style::default(),
39 text_data: None,
40 image_data: None,
41 cache: Cache::new(),
42 unrounded_layout: Layout::with_order(0),
43 has_new_layout: true,
44 final_layout: Layout::with_order(0),
45 children: Vec::new(),
46 }
47 }
48}
49
50#[allow(dead_code)]
51impl Node {
52 pub fn new_row(style: Style) -> Node {
53 Node {
54 kind: NodeKind::Flexbox,
55 style: Style { display: Display::Flex, flex_direction: FlexDirection::Row, ..style },
56 ..Node::default()
57 }
58 }
59 pub fn new_column(style: Style) -> Node {
60 Node {
61 kind: NodeKind::Flexbox,
62 style: Style { display: Display::Flex, flex_direction: FlexDirection::Column, ..style },
63 ..Node::default()
64 }
65 }
66 pub fn new_grid(style: Style) -> Node {
67 Node { kind: NodeKind::Grid, style: Style { display: Display::Grid, ..style }, ..Node::default() }
68 }
69 pub fn new_text(style: Style, text_data: TextContext) -> Node {
70 Node { kind: NodeKind::Text, style, text_data: Some(text_data), ..Node::default() }
71 }
72 pub fn new_image(style: Style, image_data: ImageContext) -> Node {
73 Node { kind: NodeKind::Image, style, image_data: Some(image_data), ..Node::default() }
74 }
75}
76
77struct Tree {
78 nodes: Vec<Node>,
79}
80
81impl Tree {
82 pub fn new() -> Tree {
83 Tree { nodes: Vec::new() }
84 }
85
86 pub fn add_node(&mut self, node: Node) -> usize {
87 self.nodes.push(node);
88 self.nodes.len() - 1
89 }
90
91 pub fn append_child(&mut self, parent: usize, child: usize) {
92 self.nodes[parent].children.push(child);
93 }
94
95 #[inline(always)]
96 fn node_from_id(&self, node_id: NodeId) -> &Node {
97 &self.nodes[usize::from(node_id)]
98 }
99
100 #[inline(always)]
101 fn node_from_id_mut(&mut self, node_id: NodeId) -> &mut Node {
102 &mut self.nodes[usize::from(node_id)]
103 }
104
105 pub fn compute_layout(&mut self, root: usize, available_space: Size<AvailableSpace>, use_rounding: bool) {
106 compute_root_layout(self, NodeId::from(root), available_space);
107 if use_rounding {
108 round_layout(self, NodeId::from(root))
109 }
110 }
111
112 pub fn print_tree(&mut self, root: usize) {
113 print_tree(self, NodeId::from(root));
114 }
115}
116
117struct ChildIter<'a>(std::slice::Iter<'a, usize>);
118impl Iterator for ChildIter<'_> {
119 type Item = NodeId;
120 fn next(&mut self) -> Option<Self::Item> {
121 self.0.next().copied().map(NodeId::from)
122 }
123}
124
125impl gummy::TraversePartialTree for Tree {
126 type ChildIter<'a> = ChildIter<'a>;
127
128 fn child_ids(&self, node_id: NodeId) -> Self::ChildIter<'_> {
129 ChildIter(self.node_from_id(node_id).children.iter())
130 }
131
132 fn child_count(&self, node_id: NodeId) -> usize {
133 self.node_from_id(node_id).children.len()
134 }
135
136 fn get_child_id(&self, node_id: NodeId, index: usize) -> NodeId {
137 NodeId::from(self.node_from_id(node_id).children[index])
138 }
139}
140
141impl gummy::TraverseTree for Tree {}
142
143impl gummy::LayoutPartialTree for Tree {
144 type CustomIdent = String;
145
146 type CoreContainerStyle<'a>
147 = &'a Style
148 where
149 Self: 'a;
150
151 fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
152 &self.node_from_id(node_id).style
153 }
154
155 fn set_unrounded_layout(&mut self, node_id: NodeId, layout: &Layout) {
156 let node = self.node_from_id_mut(node_id);
157 node.has_new_layout = &node.unrounded_layout != layout;
158 self.node_from_id_mut(node_id).unrounded_layout = *layout;
159 }
160
161 fn resolve_calc_value(&self, _val: *const (), _basis: f32) -> f32 {
162 0.0
163 }
164
165 fn compute_child_layout(&mut self, node_id: NodeId, inputs: gummy::tree::LayoutInput) -> gummy::tree::LayoutOutput {
166 compute_cached_layout(self, node_id, inputs, |tree, node_id, inputs| {
167 let node = &mut tree.nodes[usize::from(node_id)];
168 let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
169
170 match node.kind {
171 NodeKind::Flexbox => compute_flexbox_layout(tree, node_id, inputs),
172 NodeKind::Grid => compute_grid_layout(tree, node_id, inputs),
173 NodeKind::Text => compute_leaf_layout(
174 inputs,
175 &node.style,
176 |_val, _basis| 0.0,
177 |known_dimensions, available_space| {
178 text_measure_function(
179 known_dimensions,
180 available_space,
181 node.text_data.as_ref().unwrap(),
182 &font_metrics,
183 )
184 },
185 ),
186 NodeKind::Image => compute_leaf_layout(
187 inputs,
188 &node.style,
189 |_val, _basis| 0.0,
190 |known_dimensions, _available_space| {
191 image_measure_function(known_dimensions, node.image_data.as_ref().unwrap())
192 },
193 ),
194 }
195 })
196 }
197}
198
199impl CacheTree for Tree {
200 fn cache_get(&self, node_id: NodeId, inputs: &gummy::LayoutInput) -> Option<gummy::LayoutOutput> {
201 self.node_from_id(node_id).cache.get(inputs)
202 }
203
204 fn cache_store(&mut self, node_id: NodeId, inputs: &gummy::LayoutInput, layout_output: gummy::LayoutOutput) {
205 self.node_from_id_mut(node_id).cache.store(inputs, layout_output)
206 }
207
208 fn cache_clear(&mut self, node_id: NodeId) {
209 self.node_from_id_mut(node_id).cache.clear();
210 }
211}
212
213impl gummy::LayoutFlexboxContainer for Tree {
214 type FlexboxContainerStyle<'a>
215 = &'a Style
216 where
217 Self: 'a;
218
219 type FlexboxItemStyle<'a>
220 = &'a Style
221 where
222 Self: 'a;
223
224 fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
225 &self.node_from_id(node_id).style
226 }
227
228 fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
229 &self.node_from_id(child_node_id).style
230 }
231}
232
233impl gummy::LayoutGridContainer for Tree {
234 type GridContainerStyle<'a>
235 = &'a Style
236 where
237 Self: 'a;
238
239 type GridItemStyle<'a>
240 = &'a Style
241 where
242 Self: 'a;
243
244 fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
245 &self.node_from_id(node_id).style
246 }
247
248 fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
249 &self.node_from_id(child_node_id).style
250 }
251}
252
253impl gummy::RoundTree for Tree {
254 fn get_unrounded_layout(&self, node_id: NodeId) -> Layout {
255 self.node_from_id(node_id).unrounded_layout
256 }
257
258 fn set_final_layout(&mut self, node_id: NodeId, layout: &Layout) {
259 self.node_from_id_mut(node_id).final_layout = *layout;
260 }
261}
262
263impl gummy::PrintTree for Tree {
264 fn get_debug_label(&self, node_id: NodeId) -> &'static str {
265 match self.node_from_id(node_id).kind {
266 NodeKind::Flexbox => "FLEX",
267 NodeKind::Grid => "GRID",
268 NodeKind::Text => "TEXT",
269 NodeKind::Image => "IMAGE",
270 }
271 }
272
273 fn get_final_layout(&self, node_id: NodeId) -> Layout {
274 self.node_from_id(node_id).final_layout
275 }
276
277 fn get_has_new_layout(&self, node_id: NodeId) -> bool {
278 self.node_from_id(node_id).has_new_layout
279 }
280}
281
282fn main() -> Result<(), gummy::GummyError> {
283 let mut tree = Tree::new();
284
285 let root = Node::new_column(Style::DEFAULT);
286 let root_id = tree.add_node(root);
287
288 let text_node = Node::new_text(
289 Style::default(),
290 TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal },
291 );
292 let text_node_id = tree.add_node(text_node);
293 tree.append_child(root_id, text_node_id);
294
295 let image_node = Node::new_image(Style::default(), ImageContext { width: 400.0, height: 300.0 });
296 let image_node_id = tree.add_node(image_node);
297 tree.append_child(root_id, image_node_id);
298
299 tree.compute_layout(root_id, Size::MAX_CONTENT, true);
301 tree.print_tree(root_id);
302
303 Ok(())
304}