1use ratatui::buffer::{Buffer, Cell};
5use ratatui::layout::{Position, Rect};
6use ratatui::text::Span;
7
8use super::MIN_SIZE;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct Place {
12 pub x: i32,
13 pub y: i32,
14 pub width: u16,
15 pub height: u16,
16}
17
18impl From<Rect> for Place {
19 fn from(rect: Rect) -> Self {
20 Self {
21 x: i32::from(rect.x),
22 y: i32::from(rect.y),
23 width: rect.width,
24 height: rect.height,
25 }
26 }
27}
28
29pub(super) fn within(value: i32, low: i32, high: i32) -> i32 {
31 value.min(high).max(low)
32}
33
34pub(super) fn cell(value: i32) -> u16 {
35 u16::try_from(value.max(0)).unwrap_or(u16::MAX)
36}
37
38impl Place {
39 fn right(self) -> i32 {
40 self.x + i32::from(self.width)
41 }
42
43 fn bottom(self) -> i32 {
44 self.y + i32::from(self.height)
45 }
46
47 #[must_use]
49 pub fn local(self) -> Rect {
50 Rect::new(0, 0, self.width, self.height)
51 }
52
53 #[must_use]
55 pub fn visible_in(self, area: Rect) -> Rect {
56 let left = self.x.max(i32::from(area.x));
57 let top = self.y.max(i32::from(area.y));
58 let right = self.right().min(i32::from(area.right()));
59 let bottom = self.bottom().min(i32::from(area.bottom()));
60 match right > left && bottom > top {
61 true => Rect::new(
62 cell(left),
63 cell(top),
64 cell(right - left),
65 cell(bottom - top),
66 ),
67 false => Rect::default(),
68 }
69 }
70
71 #[must_use]
73 pub fn shown(self, local: Rect, area: Rect) -> Rect {
74 let moved = Self {
75 x: self.x + i32::from(local.x),
76 y: self.y + i32::from(local.y),
77 width: local.width,
78 height: local.height,
79 };
80 moved.visible_in(self.visible_in(area))
81 }
82
83 pub(super) fn limited(self, area: Rect) -> Self {
87 let width = self.width.min(area.width);
88 let height = self.height.min(area.height);
89 Self {
90 x: within(
91 self.x,
92 i32::from(area.x) - i32::from(width) + 1,
93 i32::from(area.right()) - 1,
94 ),
95 y: within(self.y, i32::from(area.y), i32::from(area.bottom()) - 1),
96 width,
97 height,
98 }
99 }
100
101 pub(super) fn scaled(self, old: Rect, new: Rect) -> Self {
105 let (x, width) = grow(
106 self.x - i32::from(old.x),
107 self.width,
108 (old.width, new.width),
109 MIN_SIZE.0,
110 );
111 let (y, height) = grow(
112 self.y - i32::from(old.y),
113 self.height,
114 (old.height, new.height),
115 MIN_SIZE.1,
116 );
117 Self {
118 x: x + i32::from(new.x),
119 y: y + i32::from(new.y),
120 width,
121 height,
122 }
123 .limited(new)
124 }
125}
126
127fn grow(start: i32, len: u16, (old, new): (u16, u16), min: u16) -> (i32, u16) {
129 let old = i64::from(old).max(1);
130 let scale = |edge: i32| {
131 let scaled = (i64::from(edge) * i64::from(new) + old / 2).div_euclid(old);
132 i32::try_from(scaled).unwrap_or_default()
133 };
134 let low = scale(start);
135 let high = scale(start + i32::from(len));
136 let len = within(high - low, i32::from(min.min(new)), i32::from(new));
137 (low, cell(len))
138}
139
140fn is_wide(cell: &Cell) -> bool {
141 cell.symbol()
142 .chars()
143 .next()
144 .is_some_and(|glyph| Span::raw(glyph.to_string()).width() > 1)
145}
146
147pub fn copy_visible(window: &Buffer, place: Place, visible: Rect, screen: &mut Buffer) {
151 if let Some(left) = visible.x.checked_sub(1) {
152 for y in visible.top()..visible.bottom() {
153 if let Some(cell) = screen.cell_mut((left, y)).filter(|cell| is_wide(cell)) {
154 cell.set_symbol(" ");
155 }
156 }
157 }
158 let cut = place.right() > i32::from(visible.right());
159 for position in visible.intersection(screen.area).positions() {
160 let own = Position::new(
161 cell(i32::from(position.x) - place.x),
162 cell(i32::from(position.y) - place.y),
163 );
164 let (Some(from), Some(to)) = (window.cell(own), screen.cell_mut(position)) else {
165 continue;
166 };
167 to.clone_from(from);
168 if cut && position.x + 1 == visible.right() && is_wide(from) {
169 to.set_symbol(" ");
170 }
171 }
172}
173
174#[cfg(test)]
175mod tests;