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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! Layout node data structures
//!
//! Internal representation of layout nodes used by the custom layout engine.

use crate::style::{
    AlignItems, Display, FlexDirection, GridPlacement, GridTrack, JustifyContent, Position, Size,
    Spacing,
};

/// A node in the layout tree
#[derive(Debug, Clone)]
pub struct LayoutNode {
    /// Node identifier (matches DomId)
    pub id: u64,

    /// Display mode
    pub display: Display,

    /// Position mode
    pub position: Position,

    /// Flexbox properties
    pub flex: FlexProps,

    /// Grid properties (only for grid containers/items)
    pub grid: GridProps,

    /// Spacing (padding, margin, insets)
    pub spacing: LayoutSpacing,

    /// Size constraints
    pub sizing: SizeConstraints,

    /// Child node IDs (in order)
    pub children: Vec<u64>,

    /// Parent node ID (None for root)
    pub parent: Option<u64>,

    /// Computed layout result (filled after compute)
    pub computed: ComputedLayout,

    /// Dirty flag for incremental layout updates
    /// When true, this node needs layout recalculation
    pub dirty: bool,
}

impl Default for LayoutNode {
    fn default() -> Self {
        Self {
            id: 0,
            display: Display::default(),
            position: Position::default(),
            flex: FlexProps::default(),
            grid: GridProps::default(),
            spacing: LayoutSpacing::default(),
            sizing: SizeConstraints::default(),
            children: Vec::new(),
            parent: None,
            computed: ComputedLayout::default(),
            dirty: true, // New nodes are always dirty
        }
    }
}

/// Flexbox layout properties
#[derive(Debug, Clone, Default)]
pub struct FlexProps {
    /// Main axis direction
    pub direction: FlexDirection,
    /// Main axis alignment
    pub justify_content: JustifyContent,
    /// Cross axis alignment
    pub align_items: AlignItems,
    /// Flex grow factor (distributes remaining space proportionally)
    pub flex_grow: f32,
    /// Gap between items
    pub gap: u16,
    /// Column gap (overrides gap for horizontal)
    pub column_gap: Option<u16>,
    /// Row gap (overrides gap for vertical)
    pub row_gap: Option<u16>,
}

#[allow(dead_code)]
impl FlexProps {
    /// Get effective gap for main axis
    pub fn main_gap(&self) -> u16 {
        match self.direction {
            FlexDirection::Row => self.column_gap.unwrap_or(self.gap),
            FlexDirection::Column => self.row_gap.unwrap_or(self.gap),
        }
    }

    /// Get effective gap for cross axis
    pub fn cross_gap(&self) -> u16 {
        match self.direction {
            FlexDirection::Row => self.row_gap.unwrap_or(self.gap),
            FlexDirection::Column => self.column_gap.unwrap_or(self.gap),
        }
    }
}

/// Grid layout properties
#[derive(Debug, Clone, Default)]
pub struct GridProps {
    /// Column track definitions
    pub template_columns: Vec<GridTrack>,
    /// Row track definitions
    pub template_rows: Vec<GridTrack>,
    /// Column placement for grid items
    pub column: GridPlacement,
    /// Row placement for grid items
    pub row: GridPlacement,
}

/// Spacing for padding, margin, and position insets
#[derive(Debug, Clone, Copy, Default)]
pub struct LayoutSpacing {
    /// Inner padding
    pub padding: Edges,
    /// Outer margin
    pub margin: Edges,
    /// Position insets (for absolute/relative/fixed)
    pub inset: Inset,
}

/// Edge values (top, right, bottom, left)
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Edges {
    pub top: u16,
    pub right: u16,
    pub bottom: u16,
    pub left: u16,
}

#[allow(dead_code)]
impl Edges {
    /// Total horizontal spacing
    pub fn horizontal(&self) -> u16 {
        self.left.saturating_add(self.right)
    }

    /// Total vertical spacing
    pub fn vertical(&self) -> u16 {
        self.top.saturating_add(self.bottom)
    }
}

impl From<Spacing> for Edges {
    fn from(s: Spacing) -> Self {
        Self {
            top: s.top,
            right: s.right,
            bottom: s.bottom,
            left: s.left,
        }
    }
}

/// Position insets (optional, for positioned elements)
#[derive(Debug, Clone, Copy, Default)]
pub struct Inset {
    pub top: Option<i16>,
    pub right: Option<i16>,
    pub bottom: Option<i16>,
    pub left: Option<i16>,
}

/// Size constraints for a node
#[derive(Debug, Clone, Copy, Default)]
pub struct SizeConstraints {
    pub width: Size,
    pub height: Size,
    pub min_width: Size,
    pub max_width: Size,
    pub min_height: Size,
    pub max_height: Size,
}

/// Computed layout result for a node
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ComputedLayout {
    pub x: u16,
    pub y: u16,
    pub width: u16,
    pub height: u16,
}

#[allow(dead_code)]
impl ComputedLayout {
    /// Create a new computed layout
    pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Content width (inner area excluding padding)
    pub fn content_width(&self, padding: &Edges) -> u16 {
        self.width
            .saturating_sub(padding.left)
            .saturating_sub(padding.right)
    }

    /// Content height (inner area excluding padding)
    pub fn content_height(&self, padding: &Edges) -> u16 {
        self.height
            .saturating_sub(padding.top)
            .saturating_sub(padding.bottom)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_edges_from_spacing() {
        let spacing = Spacing::new(1, 2, 3, 4);
        let edges: Edges = spacing.into();
        assert_eq!(edges.top, 1);
        assert_eq!(edges.right, 2);
        assert_eq!(edges.bottom, 3);
        assert_eq!(edges.left, 4);
    }

    #[test]
    fn test_edges_horizontal_vertical() {
        let edges = Edges {
            top: 5,
            right: 10,
            bottom: 15,
            left: 20,
        };
        assert_eq!(edges.horizontal(), 30);
        assert_eq!(edges.vertical(), 20);
    }

    #[test]
    fn test_flex_props_gap() {
        let mut props = FlexProps::default();
        props.gap = 5;
        props.direction = FlexDirection::Row;

        // Default uses gap
        assert_eq!(props.main_gap(), 5);
        assert_eq!(props.cross_gap(), 5);

        // Override column gap
        props.column_gap = Some(10);
        assert_eq!(props.main_gap(), 10); // Row direction uses column_gap
        assert_eq!(props.cross_gap(), 5);

        // Switch to column direction
        props.direction = FlexDirection::Column;
        assert_eq!(props.main_gap(), 5); // Column direction uses row_gap
        assert_eq!(props.cross_gap(), 10);
    }

    #[test]
    fn test_computed_layout_content() {
        let layout = ComputedLayout::new(0, 0, 100, 50);
        let padding = Edges {
            top: 5,
            right: 10,
            bottom: 5,
            left: 10,
        };
        assert_eq!(layout.content_width(&padding), 80);
        assert_eq!(layout.content_height(&padding), 40);
    }

    // =========================================================================
    // LayoutNode tests
    // =========================================================================

    #[test]
    fn test_layout_node_default() {
        let node = LayoutNode::default();
        assert_eq!(node.id, 0);
        assert!(node.children.is_empty());
        assert!(node.parent.is_none());
    }

    #[test]
    fn test_layout_node_with_children() {
        let mut node = LayoutNode::default();
        node.id = 1;
        node.children = vec![2, 3, 4];

        assert_eq!(node.children.len(), 3);
        assert!(node.children.contains(&2));
    }

    #[test]
    fn test_layout_node_with_parent() {
        let mut node = LayoutNode::default();
        node.id = 5;
        node.parent = Some(1);

        assert_eq!(node.parent, Some(1));
    }

    #[test]
    fn test_layout_node_clone() {
        let mut node = LayoutNode::default();
        node.id = 10;
        node.children = vec![20, 30];

        let cloned = node.clone();
        assert_eq!(cloned.id, 10);
        assert_eq!(cloned.children, vec![20, 30]);
    }

    // =========================================================================
    // FlexProps tests
    // =========================================================================

    #[test]
    fn test_flex_props_default() {
        let props = FlexProps::default();
        assert_eq!(props.gap, 0);
        assert_eq!(props.column_gap, None);
        assert_eq!(props.row_gap, None);
    }

    #[test]
    fn test_flex_props_main_gap_row() {
        let mut props = FlexProps::default();
        props.direction = FlexDirection::Row;
        props.gap = 8;

        assert_eq!(props.main_gap(), 8);

        props.column_gap = Some(16);
        assert_eq!(props.main_gap(), 16);
    }

    #[test]
    fn test_flex_props_main_gap_column() {
        let mut props = FlexProps::default();
        props.direction = FlexDirection::Column;
        props.gap = 8;

        assert_eq!(props.main_gap(), 8);

        props.row_gap = Some(12);
        assert_eq!(props.main_gap(), 12);
    }

    #[test]
    fn test_flex_props_cross_gap_row() {
        let mut props = FlexProps::default();
        props.direction = FlexDirection::Row;
        props.gap = 8;

        assert_eq!(props.cross_gap(), 8);

        props.row_gap = Some(4);
        assert_eq!(props.cross_gap(), 4);
    }

    #[test]
    fn test_flex_props_cross_gap_column() {
        let mut props = FlexProps::default();
        props.direction = FlexDirection::Column;
        props.gap = 8;

        assert_eq!(props.cross_gap(), 8);

        props.column_gap = Some(20);
        assert_eq!(props.cross_gap(), 20);
    }

    // =========================================================================
    // GridProps tests
    // =========================================================================

    #[test]
    fn test_grid_props_default() {
        let props = GridProps::default();
        assert!(props.template_columns.is_empty());
        assert!(props.template_rows.is_empty());
    }

    #[test]
    fn test_grid_props_clone() {
        let props = GridProps::default();
        let cloned = props.clone();
        assert!(cloned.template_columns.is_empty());
    }

    // =========================================================================
    // LayoutSpacing tests
    // =========================================================================

    #[test]
    fn test_layout_spacing_default() {
        let spacing = LayoutSpacing::default();
        assert_eq!(spacing.padding.top, 0);
        assert_eq!(spacing.margin.left, 0);
    }

    #[test]
    fn test_layout_spacing_copy() {
        let spacing = LayoutSpacing {
            padding: Edges {
                top: 1,
                right: 2,
                bottom: 3,
                left: 4,
            },
            margin: Edges::default(),
            inset: Inset::default(),
        };
        let copied = spacing;
        assert_eq!(copied.padding.top, 1);
    }

    // =========================================================================
    // Edges tests
    // =========================================================================

    #[test]
    fn test_edges_default() {
        let edges = Edges::default();
        assert_eq!(edges.top, 0);
        assert_eq!(edges.right, 0);
        assert_eq!(edges.bottom, 0);
        assert_eq!(edges.left, 0);
    }

    #[test]
    fn test_edges_horizontal() {
        let edges = Edges {
            top: 0,
            right: 15,
            bottom: 0,
            left: 5,
        };
        assert_eq!(edges.horizontal(), 20);
    }

    #[test]
    fn test_edges_vertical() {
        let edges = Edges {
            top: 10,
            right: 0,
            bottom: 20,
            left: 0,
        };
        assert_eq!(edges.vertical(), 30);
    }

    #[test]
    fn test_edges_horizontal_saturating() {
        let edges = Edges {
            top: 0,
            right: u16::MAX,
            bottom: 0,
            left: 100,
        };
        // Should saturate instead of overflow
        assert_eq!(edges.horizontal(), u16::MAX);
    }

    #[test]
    fn test_edges_vertical_saturating() {
        let edges = Edges {
            top: u16::MAX,
            right: 0,
            bottom: 50,
            left: 0,
        };
        assert_eq!(edges.vertical(), u16::MAX);
    }

    #[test]
    fn test_edges_equality() {
        let e1 = Edges {
            top: 1,
            right: 2,
            bottom: 3,
            left: 4,
        };
        let e2 = Edges {
            top: 1,
            right: 2,
            bottom: 3,
            left: 4,
        };
        let e3 = Edges {
            top: 0,
            right: 0,
            bottom: 0,
            left: 0,
        };

        assert_eq!(e1, e2);
        assert_ne!(e1, e3);
    }

    // =========================================================================
    // Inset tests
    // =========================================================================

    #[test]
    fn test_inset_default() {
        let inset = Inset::default();
        assert_eq!(inset.top, None);
        assert_eq!(inset.right, None);
        assert_eq!(inset.bottom, None);
        assert_eq!(inset.left, None);
    }

    #[test]
    fn test_inset_with_values() {
        let inset = Inset {
            top: Some(10),
            right: Some(-5),
            bottom: None,
            left: Some(20),
        };
        assert_eq!(inset.top, Some(10));
        assert_eq!(inset.right, Some(-5));
        assert_eq!(inset.bottom, None);
        assert_eq!(inset.left, Some(20));
    }

    #[test]
    fn test_inset_copy() {
        let inset = Inset {
            top: Some(5),
            right: None,
            bottom: None,
            left: None,
        };
        let copied = inset;
        assert_eq!(copied.top, Some(5));
    }

    // =========================================================================
    // SizeConstraints tests
    // =========================================================================

    #[test]
    fn test_size_constraints_default() {
        let constraints = SizeConstraints::default();
        assert_eq!(constraints.width, Size::Auto);
        assert_eq!(constraints.height, Size::Auto);
    }

    #[test]
    fn test_size_constraints_copy() {
        let constraints = SizeConstraints {
            width: Size::Fixed(100),
            height: Size::Fixed(50),
            ..Default::default()
        };
        let copied = constraints;
        assert_eq!(copied.width, Size::Fixed(100));
    }

    // =========================================================================
    // ComputedLayout tests
    // =========================================================================

    #[test]
    fn test_computed_layout_default() {
        let layout = ComputedLayout::default();
        assert_eq!(layout.x, 0);
        assert_eq!(layout.y, 0);
        assert_eq!(layout.width, 0);
        assert_eq!(layout.height, 0);
    }

    #[test]
    fn test_computed_layout_new() {
        let layout = ComputedLayout::new(10, 20, 100, 50);
        assert_eq!(layout.x, 10);
        assert_eq!(layout.y, 20);
        assert_eq!(layout.width, 100);
        assert_eq!(layout.height, 50);
    }

    #[test]
    fn test_computed_layout_content_width() {
        let layout = ComputedLayout::new(0, 0, 80, 40);
        let padding = Edges {
            top: 0,
            right: 10,
            bottom: 0,
            left: 10,
        };
        assert_eq!(layout.content_width(&padding), 60);
    }

    #[test]
    fn test_computed_layout_content_height() {
        let layout = ComputedLayout::new(0, 0, 80, 40);
        let padding = Edges {
            top: 5,
            right: 0,
            bottom: 5,
            left: 0,
        };
        assert_eq!(layout.content_height(&padding), 30);
    }

    #[test]
    fn test_computed_layout_content_saturating() {
        let layout = ComputedLayout::new(0, 0, 10, 10);
        let padding = Edges {
            top: 20,
            right: 30,
            bottom: 20,
            left: 30,
        };
        // Should not underflow
        assert_eq!(layout.content_width(&padding), 0);
        assert_eq!(layout.content_height(&padding), 0);
    }

    #[test]
    fn test_computed_layout_equality() {
        let l1 = ComputedLayout::new(0, 0, 100, 50);
        let l2 = ComputedLayout::new(0, 0, 100, 50);
        let l3 = ComputedLayout::new(10, 10, 100, 50);

        assert_eq!(l1, l2);
        assert_ne!(l1, l3);
    }

    #[test]
    fn test_computed_layout_copy() {
        let layout = ComputedLayout::new(5, 10, 200, 100);
        let copied = layout;
        assert_eq!(copied.x, 5);
        assert_eq!(copied.y, 10);
    }
}