Skip to main content

tvui/desktop/
place.rs

1//! Where a window sits: TV's bounds of a view that may hang off the desktop, so the origin is
2//! signed. Every question about what of the window is on the screen is answered here.
3
4use 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
29/// TV's `range`: `value` kept in `low..=high`, `low` winning when they cross.
30pub(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    /// The cells of the window as it draws them, from its own corner.
48    #[must_use]
49    pub fn local(self) -> Rect {
50        Rect::new(0, 0, self.width, self.height)
51    }
52
53    /// The cells inside `area` that show the window; empty when it is all outside.
54    #[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    /// Where `local`, a rect in the window's own cells, shows on the screen inside `area`.
72    #[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    /// TV's `moveGrow` limits within `area`: the size at most the desktop's, one column of the
84    /// window inside it on the left and right, one row inside at the bottom, and the top edge
85    /// never above the desktop (`dmLimitLoY`).
86    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    /// The place after the desktop went from `old` to `new`, as TV's `calcBounds` does for a
102    /// `gfGrowRel` window: both edges scale with the desktop, the size stays within the minimum
103    /// and the desktop's, then the limits apply.
104    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
127/// One axis of `scaled`: the start and length scaled from `old` to `new` cells, rounded as TV does.
128fn 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
147/// Copies to `screen` the cells of `window`, drawn at its full size, that `visible` shows. A wide
148/// glyph whose second cell the right edge cuts off is blanked, and so is one on the screen that
149/// ends under the left edge, or the diff would skip the window's first cell.
150pub 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;