layout_engine/
align.rs

1use crate::layout::LayoutInfo;
2
3/// A struct representing horizontal/vertical alignment
4#[derive(Debug, Clone, Copy, PartialEq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum Alignment {
7    /// The object should be aligned at the begining of the axis
8    Begin,
9    /// The object should be centered on the axis
10    Center,
11    /// The object should be at the end of the axis.
12    End,
13    /// The object should take us much space as possible on the axis
14    Expand,
15}
16
17impl Alignment {
18    /// Aligns a given axis
19    pub const fn align(
20        &self,
21        mut outer: LayoutInfo,
22        size: usize,
23    ) -> LayoutInfo {
24        match self {
25            Alignment::Expand => outer,
26            Alignment::End => {
27                outer.start = outer.end.saturating_sub(size);
28                outer
29            }
30            Alignment::Begin => {
31                outer.end = outer.start + size;
32                outer
33            }
34            Alignment::Center => {
35                let centre = (outer.start + outer.end) / 2;
36                LayoutInfo {
37                    start: centre.saturating_sub(size / 2),
38                    end: centre + (size / 2) + (size % 2),
39                }
40            }
41        }
42    }
43}