tsu_editor/window.rs
1use crate::editor::Point;
2
3#[derive(Debug, Clone, Copy)]
4pub enum Direction {
5 Up,
6 Down,
7 Left,
8 Right,
9}
10
11#[derive(Debug, Clone)]
12pub struct Window {
13 pub buffer_index: usize,
14 pub position: Point,
15 pub size: (usize, usize),
16 pub vtop: usize,
17 pub vleft: usize,
18 pub cursor_x: usize,
19 pub cursor_y: usize,
20 pub active: bool,
21 pub vx: usize,
22}
23
24impl Window {
25 #[must_use]
26 /// Constructs a new window with a buffer index, a position, and a size
27 ///
28 /// # Arguments
29 /// - `buffer_index`: The index of the buffer that the window will encapsulate
30 /// - `position`: The position of the window
31 /// - `size`: The size of the window
32 ///
33 /// # Returns
34 /// - A newly constructed window
35 pub fn new(buffer_index: usize, position: Point, size: (usize, usize)) -> Self {
36 Self {
37 buffer_index,
38 position,
39 size,
40 vtop: 0,
41 vleft: 0,
42 cursor_x: 0,
43 cursor_y: 0,
44 active: false,
45 vx: 0,
46 }
47 }
48
49 #[must_use]
50 /// Retrieves the interior width of the window
51 ///
52 /// # Returns
53 /// - The interior width of the window
54 pub fn inner_width(&self) -> usize {
55 self.size.0
56 }
57
58 #[must_use]
59 /// Retrieves the interior height of the window
60 ///
61 /// # Returns
62 /// - The interior height of the window
63 pub fn inner_height(&self) -> usize {
64 self.size.1
65 }
66
67 #[must_use]
68 /// Checks if the provided position is within the current window
69 ///
70 /// # Arguments
71 /// - `x`: The x coordinate of the position
72 /// - `y`: The y coordinate of the position
73 ///
74 /// # Returns
75 /// - `true` if the position is within the window, `false` otherwise
76 pub fn contains_position(&self, x: usize, y: usize) -> bool {
77 x >= self.position.x
78 && x < self.position.x + self.size.0
79 && y >= self.position.y
80 && y < self.position.y + self.size.1
81 }
82
83 #[must_use]
84 /// Transforms terminal coordinates to local coordinates, i.e., position relative to the window
85 ///
86 /// # Arguments
87 /// - `terminal_x`: The x coordinate relative to the terminal
88 /// - `terminal_y`: The y coordinate relative to the terminal
89 ///
90 /// # Returns
91 /// - Local coordinates if the terminal coordinates provided are contained within the window
92 pub fn terminal_to_local_transform(
93 &self,
94 terminal_x: usize,
95 terminal_y: usize,
96 ) -> Option<(usize, usize)> {
97 if self.contains_position(terminal_x, terminal_y) {
98 Some((terminal_x - self.position.x, terminal_y - self.position.y))
99 } else {
100 None
101 }
102 }
103
104 #[must_use]
105 /// Transforms local coordinates to terminal coordinates, i.e., position relative to the terminal
106 ///
107 /// # Arguments
108 /// - `local_x`: The x coordinate relative to the window
109 /// - `local_y`: The y coordinate relative to the window
110 ///
111 /// # Returns
112 /// - A tuple of terminal coordinates in the format (x, y)
113 pub fn local_to_terminal_transform(&self, local_x: usize, local_y: usize) -> (usize, usize) {
114 (self.position.x + local_x, self.position.y + local_y)
115 }
116}