Lines
100 %
Functions
Branches
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::Widget;
use super::*;
fn theme() -> Theme {
Theme::preset("dn").expect("the dn preset parses")
}
fn row(buf: &Buffer, y: u16) -> String {
(0..buf.area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect()
fn bar(selected: Option<usize>) -> Buffer {
let area = Rect::new(0, 0, 20, 1);
let mut buf = Buffer::empty(area);
MenuBar::new(&theme(), &["~M~ode", "~H~elp"], selected).render(area, &mut buf);
buf
fn items() -> Vec<MenuItem<'static>> {
Vec::from([
MenuItem {
label: "~A~ccounts",
key: "1",
marked: true,
enabled: true,
},
label: "Co~n~sole",
key: "F10",
marked: false,
enabled: false,
])
fn popup(selected: usize) -> Buffer {
let area = Rect::new(0, 0, 30, 8);
for position in area.positions() {
buf[position].set_char('x');
let items = items();
MenuPopup::new(&theme(), &items, selected).render(Rect::new(1, 0, 8, 1), &mut buf);
#[test]
fn the_bar_pads_each_title_one_column_in() {
assert_eq!(row(&bar(None), 0), " Mode Help ");
fn title_areas_cover_the_padded_titles_and_clip_to_the_row() {
let titles = ["~M~ode", "~H~elp"];
let areas = MenuBar::title_areas(Rect::new(0, 0, 20, 1), &titles);
assert_eq!(areas, [Rect::new(1, 0, 6, 1), Rect::new(7, 0, 6, 1)]);
let clipped = MenuBar::title_areas(Rect::new(0, 0, 9, 1), &titles);
assert_eq!(clipped[1], Rect::new(7, 0, 2, 1));
fn the_bar_colours_hotkeys_and_the_selected_title() {
let theme = theme();
let buf = bar(Some(0));
assert_eq!(
buf[(2, 0)].style().fg,
theme.style(Role::MenuSelectedHotkey).fg
);
assert_eq!(buf[(3, 0)].style().bg, theme.style(Role::MenuSelected).bg);
assert_eq!(buf[(1, 0)].style().bg, theme.style(Role::MenuSelected).bg);
assert_eq!(buf[(8, 0)].style().fg, theme.style(Role::MenuHotkey).fg);
assert_eq!(buf[(9, 0)].style().fg, theme.style(Role::Menu).fg);
assert_eq!(buf[(19, 0)].style().bg, theme.style(Role::Menu).bg);
fn the_popup_box_fits_the_widest_label_and_key() {
let area = MenuPopup::area(Rect::new(1, 0, 8, 1), &items());
assert_eq!(area, Rect::new(0, 1, 19, 4));
let rows = MenuPopup::item_areas(Rect::new(1, 0, 8, 1), &items());
assert_eq!(rows, [Rect::new(1, 2, 17, 1), Rect::new(1, 3, 17, 1)]);
fn the_popup_lists_labels_left_keys_right_and_a_bullet_on_the_marked() {
let buf = popup(0);
assert_eq!(row(&buf, 1), "┌─────────────────┐xxxxxxxxxxx");
assert_eq!(row(&buf, 2), "│ • Accounts 1 │xxxxxxxxxxx");
assert_eq!(row(&buf, 4), "└─────────────────┘xxxxxxxxxxx");
fn the_popup_shades_the_selected_item_and_the_hotkeys() {
let buf = popup(1);
assert_eq!(buf[(3, 2)].style().bg, theme.style(Role::Menu).bg);
assert_eq!(buf[(3, 3)].style().bg, theme.style(Role::MenuSelected).bg);
assert_eq!(buf[(4, 2)].style().fg, theme.style(Role::MenuHotkey).fg);
fn a_disabled_item_uses_the_disabled_roles_in_every_cell_of_its_row() {
let plain = popup(0);
let chosen = popup(1);
for (x, y, buf, role) in [
(5, 3, &plain, Role::MenuDisabled),
(7, 3, &plain, Role::MenuDisabled),
(5, 3, &chosen, Role::MenuSelectedDisabled),
(7, 3, &chosen, Role::MenuSelectedDisabled),
(5, 2, &plain, Role::MenuSelected),
] {
let style = buf[(x, y)].style();
assert_eq!(style.fg, theme.style(role).fg, "{x},{y} fg");
assert_eq!(style.bg, theme.style(role).bg, "{x},{y} bg");
fn the_popup_casts_a_shadow_right_and_below() {
assert_eq!(buf[(19, 2)].style().bg, theme.style(Role::Shadow).bg);
assert_eq!(buf[(20, 3)].style().bg, theme.style(Role::Shadow).bg);
assert_eq!(buf[(4, 5)].style().bg, theme.style(Role::Shadow).bg);
assert_ne!(buf[(22, 3)].style().bg, theme.style(Role::Shadow).bg);
fn a_popup_past_the_buffer_edge_is_clipped_not_a_panic() {
let mut buf = Buffer::empty(Rect::new(0, 0, 10, 3));
MenuPopup::new(&theme(), &items, 0).render(Rect::new(5, 0, 8, 1), &mut buf);
assert_eq!(buf[(4, 1)].symbol(), "┌");