1
//! What a dialog draws on: the frame, or a copy of the screen it may hang off.
2

            
3
use ratatui::Frame;
4
use ratatui::buffer::Buffer;
5
use ratatui::layout::{Position, Rect};
6
use ratatui::widgets::{StatefulWidget, Widget};
7
use tvui::{Place, copy_visible};
8

            
9
use crate::hits::Hits;
10

            
11
/// A copy of the screen made wide enough for a dialog that hangs off its right and bottom, and
12
/// shifted right by the columns the dialog hangs off the left.
13
pub(crate) struct Offscreen {
14
    buf: Buffer,
15
    shift: u16,
16
    host: Rect,
17
    cursor: Option<Position>,
18
}
19

            
20
impl Offscreen {
21
    /// The cells a popup may open in.
22
78
    pub(crate) fn area(&self) -> Rect {
23
78
        Rect {
24
78
            x: self.host.x + self.shift,
25
78
            ..self.host
26
78
        }
27
78
    }
28

            
29
1389
    pub(crate) fn buffer_mut(&mut self) -> &mut Buffer {
30
1389
        &mut self.buf
31
1389
    }
32

            
33
332
    pub(crate) fn set_cursor_position(&mut self, position: Position) {
34
332
        self.cursor = Some(position);
35
332
    }
36

            
37
5036
    pub(crate) fn render_widget<W: Widget>(&mut self, widget: W, area: Rect) {
38
5036
        widget.render(area, &mut self.buf);
39
5036
    }
40

            
41
2837
    pub(crate) fn render_stateful_widget<W: StatefulWidget>(
42
2837
        &mut self,
43
2837
        widget: W,
44
2837
        area: Rect,
45
2837
        state: &mut W::State,
46
2837
    ) {
47
2837
        widget.render(area, &mut self.buf, state);
48
2837
    }
49
}
50

            
51
3530
fn cells(value: i32) -> u16 {
52
3530
    u16::try_from(value.max(0)).unwrap_or(u16::MAX)
53
3530
}
54

            
55
/// Draws a dialog at `place`, which may hang off the left, right and bottom of `host`: `draw` gets
56
/// the canvas, the full-size rect of the dialog on it and the hits to fill. What `host` shows of the
57
/// canvas goes back to the frame, and so do the hits and the cursor if it is on a visible cell.
58
706
pub(crate) fn draw_placed(
59
706
    frame: &mut Frame,
60
706
    place: Place,
61
706
    host: Rect,
62
706
    hits: &mut Hits,
63
706
    draw: impl FnOnce(&mut Offscreen, Rect, &mut Hits),
64
706
) {
65
706
    let screen = frame.area();
66
706
    let shift = cells(-place.x);
67
706
    let wide = cells(i32::from(shift) + place.x + i32::from(place.width));
68
706
    let tall = cells(place.y + i32::from(place.height));
69
706
    let size = Rect::new(
70
706
        0,
71
706
        0,
72
706
        screen.right().saturating_add(shift).max(wide),
73
706
        screen.bottom().max(tall),
74
706
    );
75
706
    let mut canvas = Offscreen {
76
706
        buf: Buffer::empty(size),
77
706
        shift,
78
706
        host,
79
706
        cursor: None,
80
706
    };
81
706
    for position in screen.positions() {
82
1457150
        let (Some(from), Some(to)) = (
83
1457150
            frame.buffer_mut().cell(position),
84
1457150
            canvas.buf.cell_mut((position.x + shift, position.y)),
85
1457150
        ) else {
86
            continue;
87
        };
88
1457150
        to.clone_from(from);
89
    }
90
706
    let area = Rect::new(
91
706
        cells(place.x + i32::from(shift)),
92
706
        cells(place.y),
93
706
        place.width,
94
706
        place.height,
95
706
    );
96
706
    let mut local = Hits::default();
97
706
    draw(&mut canvas, area, &mut local);
98
706
    let whole = Place {
99
706
        x: -i32::from(shift),
100
706
        y: 0,
101
706
        width: size.width,
102
706
        height: size.height,
103
706
    };
104
706
    copy_visible(&canvas.buf, whole, host, frame.buffer_mut());
105
706
    hits.extend_shifted(local, shift, host);
106
706
    let shown = canvas
107
706
        .cursor
108
332
        .and_then(|at| at.x.checked_sub(shift).map(|x| Position::new(x, at.y)))
109
330
        .filter(|at| host.contains(*at));
110
706
    if let Some(at) = shown {
111
322
        frame.set_cursor_position(at);
112
384
    }
113
706
}