Lines
100 %
Functions
90 %
Branches
//! The drop-down list box of a select: a framed list under its field, with a shadow.
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::{Block, Clear, List, ListState, StatefulWidget, Widget};
use crate::frame::{list_box, shadow};
use crate::{Role, RowWindow, Theme, render_scrollbar};
/// The rows in the list colours with the selected one in the `list-selected` role.
#[must_use]
pub(crate) fn list(theme: &Theme, items: &[String]) -> List<'static> {
let rows = items.iter().map(|item| format!(" {item}"));
List::new(rows)
.style(theme.style(Role::List))
.highlight_style(theme.style(Role::ListSelected))
}
/// The box of `rows` list rows with its border, `anchor` wide and left-aligned with it: on the
/// row below `anchor`, or above it when there is no room below and more above, cut to fit the
/// side chosen. Moved left to stay inside `screen`.
pub fn dropdown_area(anchor: Rect, rows: u16, screen: Rect) -> Rect {
let height = rows.saturating_add(2);
let width = anchor.width.min(screen.width);
let x = anchor
.x
.min(screen.right().saturating_sub(width))
.max(screen.x);
let below = screen.bottom().saturating_sub(anchor.bottom());
let above = anchor.y.saturating_sub(screen.y);
if height <= below || below >= above {
return Rect::new(x, anchor.bottom(), width, height.min(below));
let height = height.min(above);
Rect::new(x, anchor.y - height, width, height)
/// A framed `list` with a shadow and a scrollbar when the items exceed the box, rendered in
/// the area [`dropdown_area`] gives. The caller owns the [`ListState`]: the window stays put
/// while the selection moves inside it and scrolls by the minimum past its edges.
pub struct DropDown<'a> {
theme: &'a Theme,
items: &'a [String],
impl<'a> DropDown<'a> {
/// The width of the box that shows the widest of `items` whole.
pub fn width(items: &[String]) -> u16 {
let widest = items.iter().map(|item| item.chars().count()).max();
u16::try_from(widest.unwrap_or(0) + 3).unwrap_or(u16::MAX)
/// The rows of the box drawn in `area`, inside its border.
pub fn rows(area: Rect) -> Rect {
Block::bordered().inner(area)
/// The area of each option row on screen in the box drawn in `area`, with the option's index
/// among `len` options when the list is scrolled to `top`.
pub fn option_rows(area: Rect, top: usize, len: usize) -> Vec<(usize, Rect)> {
let rows = Self::rows(area);
let shown = len.saturating_sub(top).min(usize::from(rows.height));
(0..shown)
.filter_map(|n| {
let y = rows.y.checked_add(u16::try_from(n).ok()?)?;
Some((top.saturating_add(n), Rect::new(rows.x, y, rows.width, 1)))
})
.collect()
pub fn new(theme: &'a Theme, items: &'a [String]) -> Self {
DropDown { theme, items }
impl StatefulWidget for DropDown<'_> {
type State = ListState;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut ListState) {
let outer = area.intersection(buf.area);
Clear.render(outer, buf);
let inner = Self::rows(outer);
list_box(self.theme).render(outer, buf);
let view = usize::from(inner.height);
*state.offset_mut() = state
.offset()
.min(RowWindow::last_top(self.items.len(), view));
StatefulWidget::render(list(self.theme, self.items), inner, buf, state);
let top = state.offset();
render_scrollbar(self.theme, outer, buf, (self.items.len(), view, top));
shadow(buf, outer, self.theme);
#[cfg(test)]
mod tests;