Skip to main content

tvui/
frame.rs

1//! TurboVision frames: double-line active windows, single-line passive ones, dialogs, shadows.
2
3use ratatui::buffer::Buffer;
4use ratatui::layout::{Alignment, Rect};
5use ratatui::style::Style;
6use ratatui::widgets::{Block, BorderType, Borders};
7
8use crate::{FrameMode, Role, Theme};
9
10/// The first character of a kitty Unicode-placeholder cell, which carries an image.
11const KITTY_PLACEHOLDER: char = '\u{10EEEE}';
12
13fn framed(
14    title: &str,
15    border_type: BorderType,
16    [fill, border, title_style]: [Style; 3],
17) -> Block<'static> {
18    let block = Block::new()
19        .borders(Borders::ALL)
20        .border_type(border_type)
21        .style(fill)
22        .border_style(border);
23    if title.is_empty() {
24        return block;
25    }
26    block
27        .title(format!(" {title} "))
28        .title_style(title_style)
29        .title_alignment(Alignment::Center)
30}
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]
36pub(crate) fn dialog(theme: &Theme, title: &str, mode: FrameMode) -> Block<'static> {
37    let fill = theme.style(Role::Dialog);
38    let frame = theme.style(Role::DialogFrame);
39    match mode {
40        FrameMode::Dragging => {
41            let icon = theme.style(Role::DialogIcon);
42            framed(title, BorderType::Plain, [fill, icon, icon])
43        }
44        FrameMode::Passive => framed(title, BorderType::Plain, [fill; 3]),
45        FrameMode::Active => {
46            let styles = [fill, frame, theme.style(Role::DialogTitle)];
47            framed(title, BorderType::Double, styles)
48        }
49    }
50}
51
52/// A menu drop-down frame: single-line, no title, in the menu colours.
53#[must_use]
54pub fn menu_box(theme: &Theme) -> Block<'static> {
55    let menu = theme.style(Role::Menu);
56    framed("", BorderType::Plain, [menu; 3])
57}
58
59/// A list box frame: single-line, no title, in the list colours.
60#[must_use]
61pub(crate) fn list_box(theme: &Theme) -> Block<'static> {
62    let list = theme.style(Role::List);
63    framed("", BorderType::Plain, [list; 3])
64}
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.
68pub(crate) fn shadow(buf: &mut Buffer, area: Rect, theme: &Theme) {
69    let style = Style::reset().patch(theme.style(Role::Shadow));
70    let right = Rect::new(area.right(), area.y.saturating_add(1), 2, area.height);
71    let below = Rect::new(area.x.saturating_add(2), area.bottom(), area.width, 1);
72    for strip in [right, below] {
73        for position in strip.intersection(buf.area).positions() {
74            if let Some(cell) = buf.cell_mut(position)
75                && !cell.symbol().starts_with(KITTY_PLACEHOLDER)
76            {
77                cell.set_style(style);
78            }
79        }
80    }
81}
82
83#[cfg(test)]
84mod tests;