too/views/
aligned.rs

1use crate::{
2    layout::Align2,
3    math::{Size, Space},
4    view::{Builder, Layout, View},
5};
6
7pub const fn aligned(align: Align2) -> Aligned {
8    Aligned { align }
9}
10
11#[derive(Debug, Copy, Clone)]
12#[must_use = "a view does nothing unless `show()` or `show_children()` is called"]
13pub struct Aligned {
14    align: Align2,
15}
16
17impl<'v> Builder<'v> for Aligned {
18    type View = Self;
19}
20
21impl View for Aligned {
22    type Args<'v> = Self;
23    type Response = ();
24
25    fn create(this: Self::Args<'_>) -> Self {
26        this
27    }
28
29    fn layout(&mut self, mut layout: Layout, space: Space) -> Size {
30        let mut size = space.size();
31
32        let child_space = space.loosen();
33        let node = layout.nodes.get_current();
34        for &child in &node.children {
35            let next = layout.compute(child, child_space);
36            size = size.max(next);
37
38            let pos = size * self.align - next * self.align;
39            layout.set_position(child, pos);
40        }
41
42        size.max(space.min.finite_or_zero())
43            .max(space.max.finite_or_zero())
44    }
45}