1
//! Dialog buttons: the two-row Turbo Vision button, a face and a half-block shadow.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::Rect;
5
use ratatui::style::Style;
6

            
7
use crate::markup::{plain, spans};
8
use crate::{Role, Theme};
9

            
10
/// The width of a button whose label is shorter than this less 4.
11
const MIN_WIDTH: u16 = 10;
12

            
13
/// Rows of a button: the face and the shadow under it.
14
pub(crate) const HEIGHT: u16 = 2;
15

            
16
/// A dialog button; the `default` one is the one Enter presses.
17
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18
pub struct Button<'a> {
19
    pub label: &'a str,
20
    pub default: bool,
21
}
22

            
23
/// How a button is drawn besides its own flags.
24
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
25
pub(crate) struct Look {
26
    pub focused: bool,
27
    pub pressed: bool,
28
}
29

            
30
impl Button<'_> {
31
    /// Columns, the shadow included: 10, or the label and 4 when that is more.
32
    #[must_use]
33
14962
    pub fn width(&self) -> u16 {
34
14962
        let label = u16::try_from(plain(self.label).chars().count() + 4).unwrap_or(u16::MAX);
35
14962
        label.max(MIN_WIDTH)
36
14962
    }
37

            
38
    /// The part of the button in `area` a click presses: the face row without its blank first
39
    /// column and its shadow column, as in Turbo Vision.
40
    #[must_use]
41
4013
    pub fn face(area: Rect) -> Rect {
42
4013
        Rect::new(
43
4013
            area.x.saturating_add(1),
44
4013
            area.y,
45
4013
            area.width.saturating_sub(2),
46
4013
            1,
47
4013
        )
48
4013
    }
49

            
50
    /// The face style and the style of the hotkey letter.
51
4047
    fn faces(&self, theme: &Theme, focused: bool) -> (Style, Style) {
52
4047
        let role = match (focused, self.default) {
53
620
            (true, _) => Role::ButtonFocus,
54
803
            (false, true) => Role::ButtonDefault,
55
2624
            (false, false) => Role::Button,
56
        };
57
4047
        let face = theme.style(role);
58
4047
        let hot = match theme.style(Role::ButtonHotkey).fg {
59
4047
            Some(fg) => face.fg(fg),
60
            None => face,
61
        };
62
4047
        (face, hot)
63
4047
    }
64

            
65
    /// Draws the button into `area`, `HEIGHT` rows; the pressed one has no shadow and its face
66
    /// and label sit a column to the right.
67
4048
    pub(crate) fn render(&self, area: Rect, buf: &mut Buffer, theme: &Theme, look: Look) {
68
4048
        if area.width < 3 || area.height < HEIGHT {
69
1
            return;
70
4047
        }
71
4047
        let (face, hot) = self.faces(theme, look.focused);
72
4047
        let shadow = theme.style(Role::ButtonShadow);
73
4047
        let last = area.width - 1;
74
4047
        let first = if look.pressed { 2 } else { 1 };
75
97908
        let mut put = |x: u16, y: u16, symbol: &str, style: Style| {
76
97908
            if let Some(cell) = buf.cell_mut((area.x + x, area.y + y)) {
77
97893
                cell.set_symbol(symbol);
78
97893
                cell.set_style(style);
79
15
            }
80
97908
        };
81
4047
        for x in 0..area.width {
82
40474
            let on_face = x >= first && (look.pressed || x < last);
83
40474
            put(x, 0, " ", if on_face { face } else { shadow });
84
40474
            let lower = if look.pressed || x < 2 {
85
10806
                " "
86
            } else {
87
29668
                "\u{2580}"
88
            };
89
40474
            put(x, 1, lower, shadow);
90
        }
91
4047
        if !look.pressed {
92
3708
            put(last, 0, "\u{2584}", shadow);
93
339
        }
94
4047
        let text = plain(self.label);
95
4047
        let pad = usize::from(last).saturating_sub(text.chars().count() + 1) / 2;
96
4047
        let mut x = first + u16::try_from(pad.max(1)).unwrap_or(0);
97
4047
        let end = if look.pressed { area.width } else { last };
98
4047
        for span in spans(self.label, face, hot) {
99
8127
            for symbol in span.content.chars() {
100
13253
                if x < end {
101
13252
                    put(x, 0, symbol.encode_utf8(&mut [0; 4]), span.style);
102
1
                }
103
13253
                x += 1;
104
            }
105
        }
106
4048
    }
107
}
108

            
109
#[cfg(test)]
110
mod tests;