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