Skip to main content

uzor_framework/
flex.rs

1//! 1-D flex solve for the `view!` macro (moved from `the old framework module::layout`).
2//!
3//! Pure: a parent [`Rect`] plus child specs, nothing else. Free space on the
4//! main axis (after padding and gaps) is given to children in proportion to
5//! their `flex` weights. A negative free space does not shrink `basis`
6//! (children may overflow; that is the caller's layout).
7
8use uzor::Rect;
9
10/// Main axis of a flex solve.
11#[derive(Copy, Clone, Debug, PartialEq, Eq)]
12pub enum FlexDir {
13    /// Children run left to right.
14    Row,
15    /// Children run top to bottom.
16    Col,
17}
18
19/// One child of a flex solve.
20#[derive(Copy, Clone, Debug, PartialEq)]
21pub struct FlexChild {
22    /// Preferred size along the main axis, in logical pixels. `0.0` is
23    /// purely flex-driven.
24    pub basis: f64,
25    /// Grow weight. `0.0` stays at [`FlexChild::basis`].
26    pub flex: f64,
27}
28
29/// Solve a 1-D flex layout. One [`Rect`] per child, declaration order.
30pub fn flex_solve(
31    parent: Rect,
32    dir: FlexDir,
33    gap: f64,
34    pad: f64,
35    children: &[FlexChild],
36) -> Vec<Rect> {
37    let n = children.len();
38    if n == 0 {
39        return Vec::new();
40    }
41
42    let (main_origin, main_len, cross_origin, cross_len, is_row) = match dir {
43        FlexDir::Row => (parent.x, parent.width, parent.y, parent.height, true),
44        FlexDir::Col => (parent.y, parent.height, parent.x, parent.width, false),
45    };
46
47    let inner_main = (main_len - 2.0 * pad - gap * (n.saturating_sub(1) as f64)).max(0.0);
48    let inner_cross = (cross_len - 2.0 * pad).max(0.0);
49    let basis_sum: f64 = children.iter().map(|c| c.basis).sum();
50    let flex_sum: f64 = children.iter().map(|c| c.flex).sum();
51    let free = (inner_main - basis_sum).max(0.0);
52
53    let mut rects = Vec::with_capacity(n);
54    let mut cursor = main_origin + pad;
55    for (i, c) in children.iter().enumerate() {
56        let extra = if flex_sum > 0.0 {
57            free * (c.flex / flex_sum)
58        } else {
59            0.0
60        };
61        let size = c.basis + extra;
62        let r = if is_row {
63            Rect {
64                x: cursor,
65                y: cross_origin + pad,
66                width: size,
67                height: inner_cross,
68            }
69        } else {
70            Rect {
71                x: cross_origin + pad,
72                y: cursor,
73                width: inner_cross,
74                height: size,
75            }
76        };
77        rects.push(r);
78        cursor += size;
79        if i + 1 < n {
80            cursor += gap;
81        }
82    }
83    rects
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn empty_is_empty() {
92        let parent = Rect::new(0.0, 0.0, 100.0, 40.0);
93        assert!(flex_solve(parent, FlexDir::Row, 0.0, 0.0, &[]).is_empty());
94    }
95
96    #[test]
97    fn row_distributes_free_space_by_flex_and_keeps_the_gap() {
98        let parent = Rect::new(10.0, 5.0, 100.0, 40.0);
99        let kids = [
100            FlexChild {
101                basis: 20.0,
102                flex: 1.0,
103            },
104            FlexChild {
105                basis: 20.0,
106                flex: 3.0,
107            },
108        ];
109        // inner main = 100 - 8 - 4 = 88; free = 88 - 40 = 48; extras 12 and 36.
110        let rects = flex_solve(parent, FlexDir::Row, 4.0, 4.0, &kids);
111        assert_eq!(rects.len(), 2);
112        assert!((rects[0].x - 14.0).abs() < 1e-9);
113        assert!((rects[0].width - 32.0).abs() < 1e-9);
114        assert!((rects[1].x - 50.0).abs() < 1e-9);
115        assert!((rects[1].width - 56.0).abs() < 1e-9);
116        assert!((rects[0].y - 9.0).abs() < 1e-9);
117        assert!((rects[0].height - 32.0).abs() < 1e-9);
118    }
119
120    #[test]
121    fn overflow_does_not_shrink_basis() {
122        let parent = Rect::new(0.0, 0.0, 10.0, 10.0);
123        let kids = [
124            FlexChild {
125                basis: 40.0,
126                flex: 1.0,
127            },
128            FlexChild {
129                basis: 40.0,
130                flex: 1.0,
131            },
132        ];
133        let rects = flex_solve(parent, FlexDir::Col, 0.0, 0.0, &kids);
134        assert!((rects[0].height - 40.0).abs() < 1e-9);
135        assert!((rects[1].y - 40.0).abs() < 1e-9);
136    }
137}