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