1
//! TurboVision frames: double-line active windows, single-line passive ones, dialogs, shadows.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::{Alignment, Rect};
5
use ratatui::style::Style;
6
use ratatui::widgets::{Block, BorderType, Borders};
7

            
8
use crate::{FrameMode, Role, Theme};
9

            
10
/// The first character of a kitty Unicode-placeholder cell, which carries an image.
11
const KITTY_PLACEHOLDER: char = '\u{10EEEE}';
12

            
13
2482
fn framed(
14
2482
    title: &str,
15
2482
    border_type: BorderType,
16
2482
    [fill, border, title_style]: [Style; 3],
17
2482
) -> Block<'static> {
18
2482
    let block = Block::new()
19
2482
        .borders(Borders::ALL)
20
2482
        .border_type(border_type)
21
2482
        .style(fill)
22
2482
        .border_style(border);
23
2482
    if title.is_empty() {
24
361
        return block;
25
2121
    }
26
2121
    block
27
2121
        .title(format!(" {title} "))
28
2121
        .title_style(title_style)
29
2121
        .title_alignment(Alignment::Center)
30
2482
}
31

            
32
/// A dialog frame: double-line, the title centred. A dialog being moved or sized has TV's
33
/// dragging frame: single-line, all in the icon colour; one that is not on top has TV's
34
/// passive frame: single-line in the frame colour.
35
#[must_use]
36
2121
pub(crate) fn dialog(theme: &Theme, title: &str, mode: FrameMode) -> Block<'static> {
37
2121
    let fill = theme.style(Role::Dialog);
38
2121
    let frame = theme.style(Role::DialogFrame);
39
2121
    match mode {
40
        FrameMode::Dragging => {
41
74
            let icon = theme.style(Role::DialogIcon);
42
74
            framed(title, BorderType::Plain, [fill, icon, icon])
43
        }
44
48
        FrameMode::Passive => framed(title, BorderType::Plain, [fill; 3]),
45
        FrameMode::Active => {
46
1999
            let styles = [fill, frame, theme.style(Role::DialogTitle)];
47
1999
            framed(title, BorderType::Double, styles)
48
        }
49
    }
50
2121
}
51

            
52
/// A menu drop-down frame: single-line, no title, in the menu colours.
53
#[must_use]
54
109
pub fn menu_box(theme: &Theme) -> Block<'static> {
55
109
    let menu = theme.style(Role::Menu);
56
109
    framed("", BorderType::Plain, [menu; 3])
57
109
}
58

            
59
/// A list box frame: single-line, no title, in the list colours.
60
#[must_use]
61
252
pub(crate) fn list_box(theme: &Theme) -> Block<'static> {
62
252
    let list = theme.style(Role::List);
63
252
    framed("", BorderType::Plain, [list; 3])
64
252
}
65

            
66
/// Darkens the two columns right of `area` and the row below it, as under a TurboVision dialog.
67
/// Cells keep their symbols; kitty placeholder cells and cells off the buffer are left alone.
68
2482
pub(crate) fn shadow(buf: &mut Buffer, area: Rect, theme: &Theme) {
69
2482
    let style = Style::reset().patch(theme.style(Role::Shadow));
70
2482
    let right = Rect::new(area.right(), area.y.saturating_add(1), 2, area.height);
71
2482
    let below = Rect::new(area.x.saturating_add(2), area.bottom(), area.width, 1);
72
2482
    for strip in [right, below] {
73
4964
        for position in strip.intersection(buf.area).positions() {
74
143407
            if let Some(cell) = buf.cell_mut(position)
75
143407
                && !cell.symbol().starts_with(KITTY_PLACEHOLDER)
76
            {
77
143406
                cell.set_style(style);
78
1
            }
79
        }
80
    }
81
2482
}
82

            
83
#[cfg(test)]
84
mod tests;