crates/tui-widgets/src/overlay.rs 3.4 KiB raw
1
//! Centered overlay widget.
2
3
use rustik_tui_core as tui_core;
4
use tui_core::{Buffer, Cell, Constraint, Rect, Size, Widget};
5
6
/// Centered child widget rendered over a filled panel area.
7
#[derive(Debug, Clone)]
8
pub struct Overlay<W> {
9
    /// Child rendered inside the overlay panel.
10
    child: W,
11
    /// Largest panel size.
12
    max_size: Size,
13
    /// Minimum parent-space margin kept around the panel.
14
    margin: Size,
15
    /// Cell used to fill the panel before rendering the child.
16
    background: Cell,
17
}
18
19
impl<W> Overlay<W> {
20
    /// Creates an overlay around a child widget.
21
    pub fn new(child: W) -> Self {
22
        Self {
23
            child,
24
            max_size: Size::MAX,
25
            margin: Size::MIN,
26
            background: Cell::blank(),
27
        }
28
    }
29
30
    /// Sets the largest panel size.
31
    pub const fn max_size(mut self, size: Size) -> Self {
32
        self.max_size = size;
33
        self
34
    }
35
36
    /// Sets the minimum parent-space margin around the panel.
37
    pub const fn margin(mut self, margin: Size) -> Self {
38
        self.margin = margin;
39
        self
40
    }
41
42
    /// Sets the cell used to fill the panel.
43
    pub fn background(mut self, cell: Cell) -> Self {
44
        self.background = cell;
45
        self
46
    }
47
48
    /// Returns the panel area inside a parent area.
49
    pub fn area(&self, area: Rect) -> Rect {
50
        overlay_area(area, self.max_size, self.margin)
51
    }
52
}
53
54
impl<W: Widget> Widget for Overlay<W> {
55
    fn size(&self, parent: Constraint) -> Size {
56
        parent.bounded_size()
57
    }
58
59
    fn render(&mut self, area: Rect, buf: &mut Buffer) {
60
        let area = self.area(area);
61
62
        if area.is_empty() {
63
            return;
64
        }
65
66
        buf.fill(area, self.background.clone());
67
        self.child.render(area, buf);
68
    }
69
}
70
71
/// Returns a centered overlay panel area inside `area`.
72
pub fn overlay_area(area: Rect, max_size: Size, margin: Size) -> Rect {
73
    let available_cols = area.cols.saturating_sub(margin.cols.saturating_mul(2));
74
    let available_rows = area.rows.saturating_sub(margin.rows.saturating_mul(2));
75
    let cols = max_size.cols.min(available_cols);
76
    let rows = max_size.rows.min(available_rows);
77
78
    Rect::new(
79
        area.x + area.cols.saturating_sub(cols) / 2,
80
        area.y + area.rows.saturating_sub(rows) / 2,
81
        cols,
82
        rows,
83
    )
84
}
85
86
#[cfg(test)]
87
mod tests {
88
    use super::*;
89
    use rustik_tui_core::{Buffer, Cell, Color, Constraint, Label, Rect, Size, Style, Widget};
90
91
    #[test]
92
    fn overlay_area_centers_with_max_size_and_margin() {
93
        let area = overlay_area(Rect::new(0, 0, 120, 40), Size::new(96, 18), Size::new(4, 4));
94
95
        assert_eq!(area, Rect::new(12, 11, 96, 18));
96
    }
97
98
    #[test]
99
    fn overlay_fills_background_and_renders_child() {
100
        let background = Cell::new('.', Style::new(Color::Blue));
101
        let mut overlay = Overlay::new(Label::new("hi"))
102
            .max_size(Size::new(6, 3))
103
            .margin(Size::new(2, 1))
104
            .background(background);
105
        let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 5));
106
107
        overlay.render(buffer.area(), &mut buffer);
108
109
        assert_eq!(overlay.size(Constraint::max(buffer.size())), buffer.size());
110
        assert_eq!(
111
            buffer.lines(),
112
            vec![
113
                "          ",
114
                "  hi....  ",
115
                "  ......  ",
116
                "  ......  ",
117
                "          "
118
            ]
119
        );
120
    }
121
}