cranpose_ui_layout/
alignment.rs1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub struct Alignment {
6 pub horizontal: HorizontalAlignment,
8 pub vertical: VerticalAlignment,
10}
11
12impl Alignment {
13 pub const fn new(horizontal: HorizontalAlignment, vertical: VerticalAlignment) -> Self {
15 Self {
16 horizontal,
17 vertical,
18 }
19 }
20
21 pub const TOP_START: Self = Self::new(HorizontalAlignment::Start, VerticalAlignment::Top);
23
24 pub const CENTER: Self = Self::new(
26 HorizontalAlignment::CenterHorizontally,
27 VerticalAlignment::CenterVertically,
28 );
29
30 pub const BOTTOM_END: Self = Self::new(HorizontalAlignment::End, VerticalAlignment::Bottom);
32}
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
36pub enum HorizontalAlignment {
37 Start,
39 CenterHorizontally,
41 End,
43}
44
45impl HorizontalAlignment {
46 pub fn align(&self, available: f32, child: f32) -> f32 {
48 match self {
49 HorizontalAlignment::Start => 0.0,
50 HorizontalAlignment::CenterHorizontally => ((available - child) / 2.0).max(0.0),
51 HorizontalAlignment::End => (available - child).max(0.0),
52 }
53 }
54}
55
56#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58pub enum VerticalAlignment {
59 Top,
61 CenterVertically,
63 Bottom,
65}
66
67impl VerticalAlignment {
68 pub fn align(&self, available: f32, child: f32) -> f32 {
70 match self {
71 VerticalAlignment::Top => 0.0,
72 VerticalAlignment::CenterVertically => ((available - child) / 2.0).max(0.0),
73 VerticalAlignment::Bottom => (available - child).max(0.0),
74 }
75 }
76}