Lines
100 %
Functions
88.46 %
Branches
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Clear, Widget};
use super::styles;
use crate::frame::shadow;
use crate::markup::{plain, spans};
use crate::{RowWindow, Theme, menu_box, render_scrollbar};
/// A drop-down row: the label with its `~X~` mark, the key text on the right, whether it is
/// marked and whether it can be chosen.
pub struct MenuItem<'a> {
pub label: &'a str,
pub key: &'a str,
pub marked: bool,
pub enabled: bool,
}
/// The drop-down under a menu title: a single-line box with a shadow, one item a row, the
/// selected one in the `menu-selected` roles and the marked ones with a bullet. Rendered
/// with the title's area as its anchor.
pub struct MenuPopup<'a> {
theme: &'a Theme,
items: &'a [MenuItem<'a>],
selected: usize,
top: usize,
rows: usize,
fn columns(text: &str) -> usize {
text.chars().count()
impl<'a> MenuPopup<'a> {
#[must_use]
pub fn new(theme: &'a Theme, items: &'a [MenuItem<'a>], selected: usize) -> Self {
MenuPopup {
theme,
items,
selected,
top: 0,
rows: items.len(),
/// Shows only `rows` items from item `top`, with a scrollbar when some are hidden.
pub fn scrolled(mut self, top: usize, rows: usize) -> Self {
self.top = top;
self.rows = rows;
self
/// The first item to show so that `selected` is in view among `rows` rows of `len` items;
/// `window` keeps it between frames, so the list moves only when the selection leaves it.
pub fn fit(window: &RowWindow, len: usize, selected: usize, rows: usize) -> usize {
let held = window.fit(len, rows);
let top = match selected {
at if at < held => at,
at if at >= held + rows => at + 1 - rows,
_ => held,
};
window.set_top(top);
top
/// The box with its border: left edge one column left of `anchor`, top edge on the row below it.
pub fn area(anchor: Rect, items: &[MenuItem<'_>]) -> Rect {
Self::area_rows(anchor, items, items.len())
/// The row of each item, the full width inside the border.
pub fn item_areas(anchor: Rect, items: &[MenuItem<'_>]) -> Vec<Rect> {
Self::item_areas_rows(anchor, items, items.len())
/// [`Self::area`] tall enough for `rows` items, as wide as the widest of all of them.
pub fn area_rows(anchor: Rect, items: &[MenuItem<'_>], rows: usize) -> Rect {
let label = items.iter().map(|i| columns(&plain(i.label))).max();
let key = items.iter().map(|i| columns(i.key)).max();
let inner = 4 + label.unwrap_or(0) + 2 + key.unwrap_or(0);
let width = u16::try_from(inner + 2).unwrap_or(u16::MAX);
let height = u16::try_from(rows + 2).unwrap_or(u16::MAX);
Rect::new(anchor.x.saturating_sub(1), anchor.bottom(), width, height)
/// [`Self::item_areas`] for the `rows` items shown.
pub fn item_areas_rows(anchor: Rect, items: &[MenuItem<'_>], rows: usize) -> Vec<Rect> {
let inner = Self::area_rows(anchor, items, rows).inner(ratatui::layout::Margin::new(1, 1));
(0..inner.height)
.map(|row| Rect::new(inner.x, inner.y.saturating_add(row), inner.width, 1))
.collect()
fn row(&self, item: &MenuItem<'_>, width: u16, selected: bool) -> Line<'static> {
let (base, hot) = styles(self.theme, selected, item.enabled);
let mark = if item.marked { "•" } else { " " };
let used = 4 + columns(&plain(item.label)) + columns(item.key);
let gap = usize::from(width).saturating_sub(used);
let mut spans_of = Vec::from([Span::styled(format!(" {mark} "), base)]);
spans_of.extend(spans(item.label, base, hot));
spans_of.push(Span::styled(" ".repeat(gap), base));
spans_of.push(Span::styled(format!("{} ", item.key), base));
Line::from(spans_of)
impl Widget for MenuPopup<'_> {
fn render(self, anchor: Rect, buf: &mut Buffer) {
let full = Self::area_rows(anchor, self.items, self.rows);
let outer = full.intersection(buf.area);
Clear.render(outer, buf);
menu_box(self.theme).render(outer, buf);
let cells = Self::item_areas_rows(anchor, self.items, self.rows);
let shown = self.items.iter().enumerate().skip(self.top);
for ((index, item), cell) in shown.zip(cells) {
let selected = index == self.selected;
let style = styles(self.theme, selected, item.enabled).0;
let line = self.row(item, cell.width, selected).style(style);
line.render(cell.intersection(buf.area), buf);
render_scrollbar(
self.theme,
full,
buf,
(self.items.len(), self.rows, self.top),
);
shadow(buf, outer, self.theme);