Skip to main content

blit_widget/layout/
rect.rs

1use blit::{Axis, Context, Fit, LayoutCx, Rect, Scalar};
2
3/// places children in exact supplied local rectangles
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub struct Layout;
6
7pub fn new() -> Layout {
8    Layout
9}
10
11impl<C: Context> blit::Layout<C> for Layout {
12    type Item = Rect<C::Scalar>;
13
14    fn fit(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis) -> Fit<C::Scalar> {
15        let mut natural = C::Scalar::ZERO;
16        for child in cx.children() {
17            let rect = *cx.item(child);
18            let origin = match axis {
19                Axis::Horizontal => rect.x,
20                Axis::Vertical => rect.y,
21            };
22            let extent = cx
23                .fixed_extent(child, axis)
24                .unwrap_or_else(|| axis.extent(rect.size()).max(C::Scalar::ZERO));
25            natural = natural.max(origin + extent);
26        }
27        Fit::exact(natural)
28    }
29
30    fn distribute(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis, _extent: C::Scalar) {
31        for child in cx.children() {
32            let rect = *cx.item(child);
33            let origin = match axis {
34                Axis::Horizontal => rect.x,
35                Axis::Vertical => rect.y,
36            };
37            cx.set_extent(child, axis.extent(rect.size()).max(C::Scalar::ZERO));
38            cx.set_offset(child, origin);
39        }
40    }
41}