revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Layout computation orchestration
//!
//! Main entry point for computing layout across the tree.
//!
//! # Performance
//!
//! The layout engine uses efficient recursive traversal and only computes
//! nodes that are visible (not Display::None). For large trees, consider
//! splitting your UI into separate components to minimize recalculation scope.

use super::node::ComputedLayout;
use super::tree::LayoutTree;
use super::{block, flex, grid, position};
use crate::style::Display;

/// Maximum layout depth to prevent stack overflow
/// Prevents stack overflow from malicious or malformed deeply nested layouts
const MAX_LAYOUT_DEPTH: usize = 100;

/// Compute layout for the entire tree starting from root
///
/// # Arguments
/// * `tree` - The layout tree to compute
/// * `root_id` - Root node ID
/// * `width` - Available width
/// * `height` - Available height
///
/// # Performance
///
/// This function currently computes the entire tree. For incremental updates,
/// nodes marked as \`dirty\` will be recomputed, while clean nodes may skip computation.
pub fn compute_layout(tree: &mut LayoutTree, root_id: u64, width: u16, height: u16) {
    // Set root node size and position
    if let Some(root) = tree.get_mut(root_id) {
        root.computed = ComputedLayout::new(0, 0, width, height);
        root.dirty = true; // Root is always dirty on full layout
    }

    // Recursively compute layout with depth tracking
    compute_node(tree, root_id, width, height, (width, height), 0);
}

/// Compute layout for a single node and its descendants
fn compute_node(
    tree: &mut LayoutTree,
    node_id: u64,
    available_width: u16,
    available_height: u16,
    viewport: (u16, u16),
    depth: usize,
) {
    // Prevent stack overflow from deeply nested layouts
    if depth > MAX_LAYOUT_DEPTH {
        // Mark node as clean to prevent infinite retry
        if let Some(node_mut) = tree.get_mut(node_id) {
            node_mut.computed = ComputedLayout::default();
            node_mut.dirty = false;
        }
        return;
    }
    let node = match tree.get(node_id) {
        Some(n) => n,
        None => return,
    };

    // Skip Display::None nodes entirely
    if node.display == Display::None {
        // Set zero size for hidden nodes
        if let Some(node_mut) = tree.get_mut(node_id) {
            node_mut.computed = ComputedLayout::default();
            node_mut.dirty = false; // Mark as clean
        }
        return;
    }

    // Check dirty flag - skip computation if already clean and viewport unchanged
    let node = match tree.get(node_id) {
        Some(n) => n,
        None => return,
    };

    if !node.dirty {
        // Node is clean and viewport hasn't changed, skip computation
        // but still need to process children that might be dirty
        // Collect children IDs first to release the borrow on node
        let children: Vec<u64> = node.children.to_vec();
        for child_id in children {
            compute_node(
                tree,
                child_id,
                available_width,
                available_height,
                viewport,
                depth + 1,
            );
        }
        return;
    }

    let display = node.display;
    // Collect children IDs - use to_vec() instead of clone() for better performance
    let children: Vec<u64> = node.children.to_vec();

    // Compute this node's children layout based on display mode
    match display {
        Display::Flex => {
            flex::compute_flex(tree, node_id, available_width, available_height);
        }
        Display::Block => {
            block::compute_block(tree, node_id, available_width, available_height);
        }
        Display::Grid => {
            grid::compute_grid(tree, node_id, available_width, available_height);
        }
        Display::None => {
            // Already handled above, but included for completeness
            return;
        }
    }

    // Get parent layout for position calculations
    let parent_layout = tree.get(node_id).map(|n| n.computed).unwrap_or_default();

    // Recursively compute children, then apply position offsets
    for &child_id in &children {
        let child_layout = tree.get(child_id).map(|c| c.computed).unwrap_or_default();

        // Recursively compute grandchildren
        compute_node(
            tree,
            child_id,
            child_layout.width,
            child_layout.height,
            viewport,
            depth + 1,
        );

        // Apply position offsets after children are laid out
        if let Some(child_mut) = tree.get_mut(child_id) {
            position::apply_position_offsets(child_mut, parent_layout, viewport);
        }
    }

    // Mark this node as clean after computing
    if let Some(node_mut) = tree.get_mut(node_id) {
        node_mut.dirty = false;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layout::node::LayoutNode;
    use crate::style::{FlexDirection, Size};

    fn setup_simple_tree() -> (LayoutTree, u64) {
        let mut tree = LayoutTree::new();

        let mut root = LayoutNode::default();
        root.id = 1;
        root.display = Display::Flex;

        let mut child1 = LayoutNode::default();
        child1.id = 2;
        child1.sizing.width = Size::Fixed(50);
        child1.sizing.height = Size::Fixed(30);

        let mut child2 = LayoutNode::default();
        child2.id = 3;
        child2.sizing.width = Size::Fixed(50);
        child2.sizing.height = Size::Fixed(30);

        root.children = vec![2, 3];

        tree.insert(root);
        tree.insert(child1);
        tree.insert(child2);
        tree.set_root(1);

        (tree, 1)
    }

    #[test]
    fn test_compute_layout_basic() {
        let (mut tree, root_id) = setup_simple_tree();

        compute_layout(&mut tree, root_id, 200, 100);

        let root = tree.get(root_id).unwrap();
        assert_eq!(root.computed.width, 200);
        assert_eq!(root.computed.height, 100);

        let child1 = tree.get(2).unwrap();
        assert_eq!(child1.computed.x, 0);
        assert_eq!(child1.computed.width, 50);

        let child2 = tree.get(3).unwrap();
        assert_eq!(child2.computed.x, 50);
        assert_eq!(child2.computed.width, 50);
    }

    #[test]
    fn test_compute_layout_nested() {
        let mut tree = LayoutTree::new();

        // Root (flex row)
        let mut root = LayoutNode::default();
        root.id = 1;
        root.display = Display::Flex;
        root.flex.direction = FlexDirection::Row;
        root.children = vec![2, 3];

        // Child 1 (flex column container)
        let mut child1 = LayoutNode::default();
        child1.id = 2;
        child1.display = Display::Flex;
        child1.flex.direction = FlexDirection::Column;
        child1.sizing.width = Size::Fixed(50);
        child1.sizing.height = Size::Auto;
        child1.children = vec![4, 5];

        // Child 2 (leaf)
        let mut child2 = LayoutNode::default();
        child2.id = 3;
        child2.sizing.width = Size::Auto;
        child2.sizing.height = Size::Auto;

        // Grandchildren
        let mut grandchild1 = LayoutNode::default();
        grandchild1.id = 4;
        grandchild1.sizing.height = Size::Fixed(20);

        let mut grandchild2 = LayoutNode::default();
        grandchild2.id = 5;
        grandchild2.sizing.height = Size::Fixed(20);

        tree.insert(root);
        tree.insert(child1);
        tree.insert(child2);
        tree.insert(grandchild1);
        tree.insert(grandchild2);
        tree.set_root(1);

        compute_layout(&mut tree, 1, 100, 100);

        // Check grandchildren are laid out
        let gc1 = tree.get(4).unwrap();
        assert_eq!(gc1.computed.height, 20);
        assert_eq!(gc1.computed.y, 0);

        let gc2 = tree.get(5).unwrap();
        assert_eq!(gc2.computed.height, 20);
        assert_eq!(gc2.computed.y, 20);
    }

    #[test]
    fn test_display_none_hidden() {
        let mut tree = LayoutTree::new();

        let mut root = LayoutNode::default();
        root.id = 1;
        root.display = Display::Flex;
        root.children = vec![2];

        let mut child = LayoutNode::default();
        child.id = 2;
        child.display = Display::None;
        child.sizing.width = Size::Fixed(100);
        child.sizing.height = Size::Fixed(100);

        tree.insert(root);
        tree.insert(child);
        tree.set_root(1);

        compute_layout(&mut tree, 1, 200, 200);

        let child = tree.get(2).unwrap();
        assert_eq!(child.computed.width, 0);
        assert_eq!(child.computed.height, 0);
    }

    #[test]
    fn test_mixed_display_modes() {
        let mut tree = LayoutTree::new();

        // Root (flex)
        let mut root = LayoutNode::default();
        root.id = 1;
        root.display = Display::Flex;
        root.children = vec![2];

        // Child (block container)
        let mut block_container = LayoutNode::default();
        block_container.id = 2;
        block_container.display = Display::Block;
        block_container.sizing.width = Size::Auto;
        block_container.sizing.height = Size::Auto;
        block_container.children = vec![3, 4];

        // Block children
        let mut block_child1 = LayoutNode::default();
        block_child1.id = 3;
        block_child1.sizing.height = Size::Fixed(20);

        let mut block_child2 = LayoutNode::default();
        block_child2.id = 4;
        block_child2.sizing.height = Size::Fixed(30);

        tree.insert(root);
        tree.insert(block_container);
        tree.insert(block_child1);
        tree.insert(block_child2);
        tree.set_root(1);

        compute_layout(&mut tree, 1, 100, 100);

        // Block children should stack vertically
        let bc1 = tree.get(3).unwrap();
        assert_eq!(bc1.computed.y, 0);

        let bc2 = tree.get(4).unwrap();
        assert_eq!(bc2.computed.y, 20);
    }

    #[test]
    fn test_deep_nesting_stress() {
        let mut tree = LayoutTree::new();

        // Create 10 levels of nested flex containers
        let depth = 10;
        for i in 1..=depth {
            let mut node = LayoutNode::default();
            node.id = i as u64;
            node.display = Display::Flex;
            node.flex.direction = if i % 2 == 0 {
                FlexDirection::Row
            } else {
                FlexDirection::Column
            };
            if i < depth {
                node.children = vec![(i + 1) as u64];
            }
            node.sizing.width = Size::Auto;
            node.sizing.height = Size::Auto;
            tree.insert(node);
        }
        tree.set_root(1);

        // Should not panic or stack overflow
        compute_layout(&mut tree, 1, 100, 100);

        // Deepest node should have valid layout
        let deepest = tree.get(depth as u64).unwrap();
        assert!(deepest.computed.width > 0 || deepest.computed.height > 0);
    }

    #[test]
    fn test_grid_in_flex() {
        let mut tree = LayoutTree::new();

        // Root (flex)
        let mut root = LayoutNode::default();
        root.id = 1;
        root.display = Display::Flex;
        root.children = vec![2];

        // Grid container
        let mut grid = LayoutNode::default();
        grid.id = 2;
        grid.display = Display::Grid;
        grid.sizing.width = Size::Fixed(80);
        grid.sizing.height = Size::Fixed(40);
        grid.children = vec![3, 4];

        // Grid items
        let mut item1 = LayoutNode::default();
        item1.id = 3;

        let mut item2 = LayoutNode::default();
        item2.id = 4;

        tree.insert(root);
        tree.insert(grid);
        tree.insert(item1);
        tree.insert(item2);
        tree.set_root(1);

        compute_layout(&mut tree, 1, 100, 100);

        // Grid container should be positioned
        let grid_node = tree.get(2).unwrap();
        assert_eq!(grid_node.computed.width, 80);

        // Grid items should be laid out
        let i1 = tree.get(3).unwrap();
        let i2 = tree.get(4).unwrap();
        assert!(i1.computed.width > 0);
        assert!(i2.computed.width > 0);
    }

    #[test]
    fn test_missing_node_graceful() {
        let mut tree = LayoutTree::new();

        let mut root = LayoutNode::default();
        root.id = 1;
        root.display = Display::Flex;
        root.children = vec![2, 999]; // 999 doesn't exist
        tree.insert(root);

        let mut child = LayoutNode::default();
        child.id = 2;
        child.sizing.width = Size::Fixed(50);
        tree.insert(child);

        tree.set_root(1);

        // Should not panic with missing child
        compute_layout(&mut tree, 1, 100, 100);

        let c = tree.get(2).unwrap();
        assert_eq!(c.computed.width, 50);
    }

    #[test]
    fn test_zero_available_space() {
        let (mut tree, _) = setup_simple_tree();

        // Should not panic with zero space
        compute_layout(&mut tree, 1, 0, 0);
    }
}