Skip to main content

cranpose_ui_layout/
layout_direction.rs

1//! Which way a layout reads.
2
3/// Which side of the interface is the start.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
5pub enum LayoutDirection {
6    /// Start is the left edge — Latin, Cyrillic, CJK.
7    #[default]
8    Ltr,
9    /// Start is the right edge — Arabic, Hebrew.
10    Rtl,
11}
12
13impl LayoutDirection {
14    /// Converts a distance from the start edge to a physical horizontal position.
15    pub fn place_x(self, start: f32, width: f32, item_width: f32) -> f32 {
16        if self.is_rtl() {
17            width - start - item_width
18        } else {
19            start
20        }
21    }
22    /// Whether the start edge is the right one.
23    pub fn is_rtl(self) -> bool {
24        matches!(self, Self::Rtl)
25    }
26
27    /// The direction that reads the other way.
28    pub fn reversed(self) -> Self {
29        match self {
30            Self::Ltr => Self::Rtl,
31            Self::Rtl => Self::Ltr,
32        }
33    }
34
35    /// Turns start/end values into physical left/right values.
36    pub fn resolve(self, start: f32, end: f32) -> (f32, f32) {
37        match self {
38            Self::Ltr => (start, end),
39            Self::Rtl => (end, start),
40        }
41    }
42}