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

            
4
use ratatui::buffer::{Buffer, Cell};
5
use ratatui::layout::{Position, Rect};
6
use ratatui::text::Span;
7

            
8
use super::MIN_SIZE;
9

            
10
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11
pub struct Place {
12
    pub x: i32,
13
    pub y: i32,
14
    pub width: u16,
15
    pub height: u16,
16
}
17

            
18
impl From<Rect> for Place {
19
343983
    fn from(rect: Rect) -> Self {
20
343983
        Self {
21
343983
            x: i32::from(rect.x),
22
343983
            y: i32::from(rect.y),
23
343983
            width: rect.width,
24
343983
            height: rect.height,
25
343983
        }
26
343983
    }
27
}
28

            
29
/// TV's `range`: `value` kept in `low..=high`, `low` winning when they cross.
30
130420
pub(super) fn within(value: i32, low: i32, high: i32) -> i32 {
31
130420
    value.min(high).max(low)
32
130420
}
33

            
34
23153220
pub(super) fn cell(value: i32) -> u16 {
35
23153220
    u16::try_from(value.max(0)).unwrap_or(u16::MAX)
36
23153220
}
37

            
38
impl Place {
39
522656
    fn right(self) -> i32 {
40
522656
        self.x + i32::from(self.width)
41
522656
    }
42

            
43
516289
    fn bottom(self) -> i32 {
44
516289
        self.y + i32::from(self.height)
45
516289
    }
46

            
47
    /// The cells of the window as it draws them, from its own corner.
48
    #[must_use]
49
17289
    pub fn local(self) -> Rect {
50
17289
        Rect::new(0, 0, self.width, self.height)
51
17289
    }
52

            
53
    /// The cells inside `area` that show the window; empty when it is all outside.
54
    #[must_use]
55
516289
    pub fn visible_in(self, area: Rect) -> Rect {
56
516289
        let left = self.x.max(i32::from(area.x));
57
516289
        let top = self.y.max(i32::from(area.y));
58
516289
        let right = self.right().min(i32::from(area.right()));
59
516289
        let bottom = self.bottom().min(i32::from(area.bottom()));
60
516289
        match right > left && bottom > top {
61
515566
            true => Rect::new(
62
515566
                cell(left),
63
515566
                cell(top),
64
515566
                cell(right - left),
65
515566
                cell(bottom - top),
66
515566
            ),
67
723
            false => Rect::default(),
68
        }
69
516289
    }
70

            
71
    /// Where `local`, a rect in the window's own cells, shows on the screen inside `area`.
72
    #[must_use]
73
88437
    pub fn shown(self, local: Rect, area: Rect) -> Rect {
74
88437
        let moved = Self {
75
88437
            x: self.x + i32::from(local.x),
76
88437
            y: self.y + i32::from(local.y),
77
88437
            width: local.width,
78
88437
            height: local.height,
79
88437
        };
80
88437
        moved.visible_in(self.visible_in(area))
81
88437
    }
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
64000
    pub(super) fn limited(self, area: Rect) -> Self {
87
64000
        let width = self.width.min(area.width);
88
64000
        let height = self.height.min(area.height);
89
64000
        Self {
90
64000
            x: within(
91
64000
                self.x,
92
64000
                i32::from(area.x) - i32::from(width) + 1,
93
64000
                i32::from(area.right()) - 1,
94
64000
            ),
95
64000
            y: within(self.y, i32::from(area.y), i32::from(area.bottom()) - 1),
96
64000
            width,
97
64000
            height,
98
64000
        }
99
64000
    }
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
403
    pub(super) fn scaled(self, old: Rect, new: Rect) -> Self {
105
403
        let (x, width) = grow(
106
403
            self.x - i32::from(old.x),
107
403
            self.width,
108
403
            (old.width, new.width),
109
403
            MIN_SIZE.0,
110
403
        );
111
403
        let (y, height) = grow(
112
403
            self.y - i32::from(old.y),
113
403
            self.height,
114
403
            (old.height, new.height),
115
403
            MIN_SIZE.1,
116
403
        );
117
403
        Self {
118
403
            x: x + i32::from(new.x),
119
403
            y: y + i32::from(new.y),
120
403
            width,
121
403
            height,
122
403
        }
123
403
        .limited(new)
124
403
    }
125
}
126

            
127
/// One axis of `scaled`: the start and length scaled from `old` to `new` cells, rounded as TV does.
128
806
fn grow(start: i32, len: u16, (old, new): (u16, u16), min: u16) -> (i32, u16) {
129
806
    let old = i64::from(old).max(1);
130
1612
    let scale = |edge: i32| {
131
1612
        let scaled = (i64::from(edge) * i64::from(new) + old / 2).div_euclid(old);
132
1612
        i32::try_from(scaled).unwrap_or_default()
133
1612
    };
134
806
    let low = scale(start);
135
806
    let high = scale(start + i32::from(len));
136
806
    let len = within(high - low, i32::from(min.min(new)), i32::from(new));
137
806
    (low, cell(len))
138
806
}
139

            
140
10664
fn is_wide(cell: &Cell) -> bool {
141
10664
    cell.symbol()
142
10664
        .chars()
143
10664
        .next()
144
10664
        .is_some_and(|glyph| Span::raw(glyph.to_string()).width() > 1)
145
10664
}
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.
150
6367
pub fn copy_visible(window: &Buffer, place: Place, visible: Rect, screen: &mut Buffer) {
151
6367
    if let Some(left) = visible.x.checked_sub(1) {
152
805
        for y in visible.top()..visible.bottom() {
153
9142
            if let Some(cell) = screen.cell_mut((left, y)).filter(|cell| is_wide(cell)) {
154
1
                cell.set_symbol(" ");
155
9141
            }
156
        }
157
5562
    }
158
6367
    let cut = place.right() > i32::from(visible.right());
159
6367
    for position in visible.intersection(screen.area).positions() {
160
10544298
        let own = Position::new(
161
10544298
            cell(i32::from(position.x) - place.x),
162
10544298
            cell(i32::from(position.y) - place.y),
163
10544298
        );
164
10544298
        let (Some(from), Some(to)) = (window.cell(own), screen.cell_mut(position)) else {
165
            continue;
166
        };
167
10544298
        to.clone_from(from);
168
10544298
        if cut && position.x + 1 == visible.right() && is_wide(from) {
169
1
            to.set_symbol(" ");
170
10544297
        }
171
    }
172
6367
}
173

            
174
#[cfg(test)]
175
mod tests;