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