Skip to main content

photon_ui/layout/
position.rs

1use std::{
2    fmt,
3    ops::{
4        Add,
5        AddAssign,
6        Sub,
7        SubAssign,
8    },
9};
10
11use super::Offset;
12use crate::tui::Rect;
13
14/// A point in the terminal coordinate system.
15#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
16pub struct Position {
17    pub x: u16,
18    pub y: u16,
19}
20
21impl Position {
22    pub const MAX: Self = Self::new(u16::MAX, u16::MAX);
23    pub const MIN: Self = Self::ORIGIN;
24    pub const ORIGIN: Self = Self::new(0, 0);
25
26    pub const fn new(x: u16, y: u16) -> Self {
27        Self { x, y }
28    }
29
30    pub fn offset(self, offset: Offset) -> Self {
31        self + offset
32    }
33}
34
35impl From<(u16, u16)> for Position {
36    fn from((x, y): (u16, u16)) -> Self {
37        Self { x, y }
38    }
39}
40
41impl From<Position> for (u16, u16) {
42    fn from(position: Position) -> Self {
43        (position.x, position.y)
44    }
45}
46
47impl From<Rect> for Position {
48    fn from(rect: Rect) -> Self {
49        Self::new(rect.x, rect.y)
50    }
51}
52
53impl fmt::Display for Position {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        write!(f, "({}, {})", self.x, self.y)
56    }
57}
58
59impl Add<Offset> for Position {
60    type Output = Self;
61
62    fn add(self, offset: Offset) -> Self::Output {
63        let max = i32::from(u16::MAX);
64        let x = i32::from(self.x)
65            .saturating_add(i32::from(offset.x))
66            .clamp(0, max) as u16;
67        let y = i32::from(self.y)
68            .saturating_add(i32::from(offset.y))
69            .clamp(0, max) as u16;
70        Self { x, y }
71    }
72}
73
74impl Sub<Offset> for Position {
75    type Output = Self;
76
77    fn sub(self, offset: Offset) -> Self::Output {
78        let max = i32::from(u16::MAX);
79        let x = i32::from(self.x)
80            .saturating_sub(i32::from(offset.x))
81            .clamp(0, max) as u16;
82        let y = i32::from(self.y)
83            .saturating_sub(i32::from(offset.y))
84            .clamp(0, max) as u16;
85        Self { x, y }
86    }
87}
88
89impl AddAssign<Offset> for Position {
90    fn add_assign(&mut self, offset: Offset) {
91        *self = *self + offset;
92    }
93}
94
95impl SubAssign<Offset> for Position {
96    fn sub_assign(&mut self, offset: Offset) {
97        *self = *self - offset;
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn position_new() {
107        let p = Position::new(5, 10);
108        assert_eq!(p.x, 5);
109        assert_eq!(p.y, 10);
110    }
111
112    #[test]
113    fn position_from_tuple() {
114        let p: Position = (3, 4).into();
115        assert_eq!(p, Position::new(3, 4));
116    }
117
118    #[test]
119    fn position_into_tuple() {
120        let p = Position::new(1, 2);
121        let (x, y): (u16, u16) = p.into();
122        assert_eq!(x, 1);
123        assert_eq!(y, 2);
124    }
125
126    #[test]
127    fn position_add_offset() {
128        let p = Position::new(5, 5);
129        let o = Offset::new(3, -2);
130        assert_eq!(p + o, Position::new(8, 3));
131    }
132
133    #[test]
134    fn position_sub_offset() {
135        let p = Position::new(5, 5);
136        let o = Offset::new(3, 2);
137        assert_eq!(p - o, Position::new(2, 3));
138    }
139
140    #[test]
141    fn position_add_clamps_at_max() {
142        let p = Position::MAX;
143        let o = Offset::new(1, 1);
144        assert_eq!(p + o, Position::MAX);
145    }
146
147    #[test]
148    fn position_sub_clamps_at_min() {
149        let p = Position::ORIGIN;
150        let o = Offset::new(1, 1);
151        assert_eq!(p - o, Position::ORIGIN);
152    }
153
154    #[test]
155    fn position_add_assign() {
156        let mut p = Position::new(1, 1);
157        p += Offset::new(2, 3);
158        assert_eq!(p, Position::new(3, 4));
159    }
160
161    #[test]
162    fn position_sub_assign() {
163        let mut p = Position::new(5, 5);
164        p -= Offset::new(2, 3);
165        assert_eq!(p, Position::new(3, 2));
166    }
167
168    #[test]
169    fn position_from_rect() {
170        let rect = Rect {
171            x: 7,
172            y: 8,
173            width: 10,
174            height: 20,
175        };
176        let p: Position = rect.into();
177        assert_eq!(p, Position::new(7, 8));
178    }
179
180    #[test]
181    fn position_display() {
182        assert_eq!(Position::new(1, 2).to_string(), "(1, 2)");
183    }
184}