cranpose_ui_layout/
alignment.rs1use crate::round_to_px;
4
5pub fn bias_offset(bias: f32, available: f32, child: f32, density: f32) -> f32 {
10 round_to_px((available - child) / 2.0 * (1.0 + bias), density)
11}
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub struct Alignment {
16 pub horizontal: HorizontalAlignment,
18 pub vertical: VerticalAlignment,
20}
21
22impl Alignment {
23 pub const fn new(horizontal: HorizontalAlignment, vertical: VerticalAlignment) -> Self {
25 Self {
26 horizontal,
27 vertical,
28 }
29 }
30
31 pub const TOP_START: Self = Self::new(HorizontalAlignment::Start, VerticalAlignment::Top);
33
34 pub const CENTER: Self = Self::new(
36 HorizontalAlignment::CenterHorizontally,
37 VerticalAlignment::CenterVertically,
38 );
39
40 pub const BOTTOM_END: Self = Self::new(HorizontalAlignment::End, VerticalAlignment::Bottom);
42}
43
44#[derive(Clone, Copy, Debug, PartialEq, Eq)]
46pub enum HorizontalAlignment {
47 Start,
49 CenterHorizontally,
51 End,
53}
54
55impl HorizontalAlignment {
56 pub fn bias(&self) -> f32 {
58 match self {
59 HorizontalAlignment::Start => -1.0,
60 HorizontalAlignment::CenterHorizontally => 0.0,
61 HorizontalAlignment::End => 1.0,
62 }
63 }
64
65 pub fn align(&self, available: f32, child: f32, density: f32) -> f32 {
68 bias_offset(self.bias(), available, child, density)
69 }
70}
71
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
74pub enum VerticalAlignment {
75 Top,
77 CenterVertically,
79 Bottom,
81}
82
83impl VerticalAlignment {
84 pub fn bias(&self) -> f32 {
86 match self {
87 VerticalAlignment::Top => -1.0,
88 VerticalAlignment::CenterVertically => 0.0,
89 VerticalAlignment::Bottom => 1.0,
90 }
91 }
92
93 pub fn align(&self, available: f32, child: f32, density: f32) -> f32 {
96 bias_offset(self.bias(), available, child, density)
97 }
98}
99
100#[cfg(test)]
101#[path = "tests/alignment_tests.rs"]
102mod tests;