Lines
100 %
Functions
Branches
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::Widget;
use super::styles;
use crate::markup::{plain, spans};
use crate::{Role, Theme};
/// One row of menu titles, each padded by a space, the first one column in. The selected
/// title, if any, is drawn in the `menu-selected` roles.
pub struct MenuBar<'a> {
theme: &'a Theme,
titles: &'a [&'a str],
selected: Option<usize>,
}
impl<'a> MenuBar<'a> {
#[must_use]
pub fn new(theme: &'a Theme, titles: &'a [&'a str], selected: Option<usize>) -> Self {
MenuBar {
theme,
titles,
selected,
/// The cells of each title, padding included, clipped to `area`.
pub fn title_areas(area: Rect, titles: &[&str]) -> Vec<Rect> {
let mut x = area.x.saturating_add(1);
let mut areas = Vec::new();
for title in titles {
let width = u16::try_from(plain(title).chars().count() + 2).unwrap_or(u16::MAX);
let cell = Rect::new(x, area.y, width, 1).intersection(area);
areas.push(cell);
x = x.saturating_add(width);
areas
impl Widget for MenuBar<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
buf.set_style(area, self.theme.style(Role::Menu));
let areas = Self::title_areas(area, self.titles);
for (index, (title, cell)) in self.titles.iter().zip(areas).enumerate() {
let (base, hot) = styles(self.theme, self.selected == Some(index), true);
let mut line = Vec::from([Span::styled(" ", base)]);
line.extend(spans(title, base, hot));
line.push(Span::styled(" ", base));
buf.set_style(cell, base);
Line::from(line).render(cell, buf);