1
//! The drop-down list box of a select: a framed list under its field, with a shadow.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::Rect;
5
use ratatui::widgets::{Block, Clear, List, ListState, StatefulWidget, Widget};
6

            
7
use crate::frame::{list_box, shadow};
8
use crate::{Role, RowWindow, Theme, render_scrollbar};
9

            
10
/// The rows in the list colours with the selected one in the `list-selected` role.
11
#[must_use]
12
252
pub(crate) fn list(theme: &Theme, items: &[String]) -> List<'static> {
13
3245
    let rows = items.iter().map(|item| format!(" {item}"));
14
252
    List::new(rows)
15
252
        .style(theme.style(Role::List))
16
252
        .highlight_style(theme.style(Role::ListSelected))
17
252
}
18

            
19
/// The box of `rows` list rows with its border, `anchor` wide and left-aligned with it: on the
20
/// row below `anchor`, or above it when there is no room below and more above, cut to fit the
21
/// side chosen. Moved left to stay inside `screen`.
22
#[must_use]
23
241
pub fn dropdown_area(anchor: Rect, rows: u16, screen: Rect) -> Rect {
24
241
    let height = rows.saturating_add(2);
25
241
    let width = anchor.width.min(screen.width);
26
241
    let x = anchor
27
241
        .x
28
241
        .min(screen.right().saturating_sub(width))
29
241
        .max(screen.x);
30
241
    let below = screen.bottom().saturating_sub(anchor.bottom());
31
241
    let above = anchor.y.saturating_sub(screen.y);
32
241
    if height <= below || below >= above {
33
166
        return Rect::new(x, anchor.bottom(), width, height.min(below));
34
75
    }
35
75
    let height = height.min(above);
36
75
    Rect::new(x, anchor.y - height, width, height)
37
241
}
38

            
39
/// A framed `list` with a shadow and a scrollbar when the items exceed the box, rendered in
40
/// the area [`dropdown_area`] gives. The caller owns the [`ListState`]: the window stays put
41
/// while the selection moves inside it and scrolls by the minimum past its edges.
42
pub struct DropDown<'a> {
43
    theme: &'a Theme,
44
    items: &'a [String],
45
}
46

            
47
impl<'a> DropDown<'a> {
48
    /// The width of the box that shows the widest of `items` whole.
49
    #[must_use]
50
234
    pub fn width(items: &[String]) -> u16 {
51
2979
        let widest = items.iter().map(|item| item.chars().count()).max();
52
234
        u16::try_from(widest.unwrap_or(0) + 3).unwrap_or(u16::MAX)
53
234
    }
54

            
55
    /// The rows of the box drawn in `area`, inside its border.
56
    #[must_use]
57
726
    pub fn rows(area: Rect) -> Rect {
58
726
        Block::bordered().inner(area)
59
726
    }
60

            
61
    /// The area of each option row on screen in the box drawn in `area`, with the option's index
62
    /// among `len` options when the list is scrolled to `top`.
63
    #[must_use]
64
239
    pub fn option_rows(area: Rect, top: usize, len: usize) -> Vec<(usize, Rect)> {
65
239
        let rows = Self::rows(area);
66
239
        let shown = len.saturating_sub(top).min(usize::from(rows.height));
67
239
        (0..shown)
68
1144
            .filter_map(|n| {
69
1144
                let y = rows.y.checked_add(u16::try_from(n).ok()?)?;
70
1144
                Some((top.saturating_add(n), Rect::new(rows.x, y, rows.width, 1)))
71
1144
            })
72
            .collect()
73
239
    }
74

            
75
    #[must_use]
76
252
    pub fn new(theme: &'a Theme, items: &'a [String]) -> Self {
77
252
        DropDown { theme, items }
78
252
    }
79
}
80

            
81
impl StatefulWidget for DropDown<'_> {
82
    type State = ListState;
83

            
84
252
    fn render(self, area: Rect, buf: &mut Buffer, state: &mut ListState) {
85
252
        let outer = area.intersection(buf.area);
86
252
        Clear.render(outer, buf);
87
252
        let inner = Self::rows(outer);
88
252
        list_box(self.theme).render(outer, buf);
89
252
        let view = usize::from(inner.height);
90
252
        *state.offset_mut() = state
91
252
            .offset()
92
252
            .min(RowWindow::last_top(self.items.len(), view));
93
252
        StatefulWidget::render(list(self.theme, self.items), inner, buf, state);
94
252
        let top = state.offset();
95
252
        render_scrollbar(self.theme, outer, buf, (self.items.len(), view, top));
96
252
        shadow(buf, outer, self.theme);
97
252
    }
98
}
99

            
100
#[cfg(test)]
101
mod tests;