Skip to main content

bhoo/systems/material/
layout.rs

1use crate::*;
2
3impl Frame<'_> {
4    /// Allocate a styled rectangular region and lay out children inside its padding.
5    pub fn boxed(
6        &mut self,
7        height: f32,
8        radius: f32,
9        padding: f32,
10        background: Color,
11        build: impl FnOnce(&mut Self),
12    ) {
13        let bounds = self.reserve(height.max(0.0));
14        self.paint_rect(bounds, radius.max(0.0), background);
15        self.within(bounds.inset(padding.max(0.0)), build);
16    }
17    /// Center a child region up to a maximum width, keeping layout in caller control.
18    pub fn container(&mut self, max_width: f32, height: f32, build: impl FnOnce(&mut Self)) {
19        let bounds = self.reserve(height);
20        let width = max_width.max(0.0).min(bounds.width);
21        self.within(
22            Rect {
23                x: bounds.x + (bounds.width - width) / 2.0,
24                width,
25                ..bounds
26            },
27            build,
28        );
29    }
30    /// Arrange children vertically using the given shared spacing.
31    pub fn stack(&mut self, count: usize, spacing: f32, mut build: impl FnMut(&mut Self, usize)) {
32        self.with_gap(spacing, |f| {
33            for index in 0..count {
34                build(f, index);
35            }
36        });
37    }
38    /// Lay out a bounded collection in equal-width columns with a shared gap.
39    pub fn grid(
40        &mut self,
41        count: usize,
42        columns: usize,
43        row_height: f32,
44        gap: f32,
45        mut build: impl FnMut(&mut Self, usize),
46    ) {
47        if count == 0 || columns == 0 {
48            return;
49        }
50        self.with_gap(gap, |f| {
51            let rows = count.div_ceil(columns);
52            for row in 0..rows {
53                f.row(row_height, columns, |f, column| {
54                    let index = row * columns + column;
55                    if index < count {
56                        build(f, index);
57                    }
58                });
59            }
60        });
61    }
62    pub fn reserve(&mut self, height: f32) -> Rect {
63        self.allocate(height)
64    }
65    pub fn paragraph(
66        &mut self,
67        bounds: Rect,
68        text: &str,
69        color: Color,
70        style: TextStyle,
71        line_height: f32,
72    ) {
73        let metrics = self.ui.text_metrics;
74        let mut index = 0;
75        metrics.lines(text, bounds.width, style, |line| {
76            let y = bounds.y + index as f32 * line_height;
77            if y < bounds.y + bounds.height {
78                self.styled_text(
79                    Rect {
80                        y,
81                        height: line_height,
82                        ..bounds
83                    },
84                    line,
85                    color,
86                    style,
87                );
88            }
89            index += 1;
90        });
91    }
92    pub fn with_gap(&mut self, gap: f32, build: impl FnOnce(&mut Self)) {
93        let previous = self.gap;
94        self.gap = gap.max(0.0);
95        build(self);
96        self.gap = previous;
97    }
98    pub fn row(&mut self, height: f32, columns: usize, mut build: impl FnMut(&mut Self, usize)) {
99        if columns == 0 {
100            return;
101        }
102        let b = self.allocate(height);
103        let gap = self.gap.min(b.width / columns as f32);
104        let width = ((b.width - gap * (columns - 1) as f32) / columns as f32).max(0.0);
105        for i in 0..columns {
106            self.within(
107                Rect {
108                    x: b.x + i as f32 * (width + gap),
109                    width,
110                    ..b
111                },
112                |f| build(f, i),
113            );
114        }
115    }
116}
117
118#[cfg(test)]
119#[path = "layout.spec.rs"]
120mod tests;