Lines
100 %
Functions
66.67 %
Branches
//! The menu bar and the open drop-down.
use ratatui::Frame;
use ratatui::layout::Rect;
use tvui::{MenuBar, MenuItem, MenuPopup};
use crate::app::{App, MenuId};
use crate::hits::{Hits, Target};
/// The rows under the popup box: its border, its shadow and the status line.
const BELOW_POPUP: u16 = 3;
const TITLES: [&str; MenuId::ALL.len()] = [
MenuId::View.title(),
MenuId::Window.title(),
MenuId::Options.title(),
];
pub(super) fn draw_menu(frame: &mut Frame, bar: Rect, app: &App, hits: &mut Hits) {
let selected = app.menu_title().map(MenuId::index);
let titles = MenuBar::title_areas(bar, &TITLES);
frame.render_widget(MenuBar::new(&app.theme, &TITLES, selected), bar);
if let (Some(menu), Some(at)) = (app.menu_title(), app.menu_item()) {
hits.push(frame.area(), Target::MenuBackdrop);
draw_popup(frame, titles[menu.index()], at, app, hits);
}
for (index, title) in titles.into_iter().enumerate() {
hits.push(title, Target::MenuTitle(index));
fn draw_popup(frame: &mut Frame, anchor: Rect, selected: usize, app: &App, hits: &mut Hits) {
let entries = app.menu_entries();
let items: Vec<MenuItem> = entries
.iter()
.map(|entry| MenuItem {
label: &entry.label,
key: &entry.key,
marked: entry.marked,
enabled: entry.enabled,
})
.collect();
let fits = usize::from(
frame
.area()
.bottom()
.saturating_sub(anchor.bottom() + BELOW_POPUP),
);
let rows = items.len().min(fits.max(1));
let top = MenuPopup::fit(&app.menu_window, items.len(), selected, rows);
let popup = MenuPopup::new(&app.theme, &items, selected).scrolled(top, rows);
hits.push(MenuPopup::area_rows(anchor, &items, rows), Target::Block);
let cells = MenuPopup::item_areas_rows(anchor, &items, rows);
for (index, item) in cells.into_iter().enumerate() {
hits.push(item, Target::MenuItem(top + index));
frame.render_widget(popup, anchor);
#[cfg(test)]
mod tests;