use super::*;
use crate::edit::Edit;
use crate::style::Sides;
use crate::tree::{Behavior, ContextItem, ContextMenuResponse, Jump, Position, Ui};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Pick {
Cut,
Copy,
Locked,
Small,
Large,
Panel,
}
const SIZES: [ContextItem<'static, Pick>; 2] = [
ContextItem::action("Small", Pick::Small),
ContextItem::action("Large", Pick::Large),
];
const PANEL: [ContextItem<'static, Pick>; 1] = [ContextItem::action("Panel", Pick::Panel)];
fn items() -> [ContextItem<'static, Pick>; 5] {
[
ContextItem::action("Cut", Pick::Cut).with_shortcut(CUT_KEY),
ContextItem::action("Copy", Pick::Copy),
ContextItem::action("Locked", Pick::Locked)
.with_shortcut(CUT_KEY)
.enabled(false),
ContextItem::Separator,
ContextItem::submenu("Size", &SIZES),
]
}
const CUT_KEY: &str = "Ctrl+X";
const TARGET: Vec2 = Vec2::new(100.0, 30.0);
struct Page {
target: NodeKey,
corner: NodeKey,
other: NodeKey,
panel: NodeKey,
menus: [ContextMenuResponse<Pick>; 3],
}
fn page(ui: &mut Ui, pointer: PointerInput, nav: NavInput) -> Page {
frame(ui, pointer, nav, |ui| {
let size = [
Declaration::Width(LengthAuto::Px(TARGET.x)),
Declaration::Height(LengthAuto::Px(TARGET.y)),
Declaration::FlexShrink(0.0),
];
let fill = [
Declaration::FlexGrow(1.0),
Declaration::FlexDirection(FlexDirection::Column),
];
let mut keys = None;
let panel = ui.block("#panel", &fill, |ui| {
let target = ui
.block_with("#target", &size, Behavior::BUTTON, |_| {})
.key;
let target_menu = ui.context_menu(target, &items());
let other = ui.block_with("#other", &size, Behavior::BUTTON, |_| {}).key;
let corner_at = [
Declaration::Position(Position::Absolute),
Declaration::Inset(Sides::Left, LengthAuto::Px(PAGE - TARGET.x)),
Declaration::Inset(Sides::Top, LengthAuto::Px(PAGE - TARGET.y)),
];
let corner = ui
.block_with(
"#corner",
&[&size[..], &corner_at[..]].concat(),
Behavior::BUTTON,
|_| {},
)
.key;
let corner_menu = ui.context_menu(corner, &items());
keys = Some((target, other, corner, target_menu, corner_menu));
});
let panel_menu = ui.context_menu(panel.key, &PANEL);
let (target, other, corner, target_menu, corner_menu) = keys.expect("built");
Page {
target,
corner,
other,
panel: panel.key,
menus: [target_menu, corner_menu, panel_menu],
}
})
}
fn secondary(pos: Vec2) -> PointerInput {
PointerInput {
secondary_pressed: true,
..PointerInput::hovering(pos)
}
}
fn item(ui: &Ui, anchor: NodeKey, label: &str) -> NodeKey {
children_of(ui, Ui::popup_key(anchor))
.into_iter()
.find(|&item| {
children_of(ui, item)
.first()
.is_some_and(|&span| ui.text(span) == Some(label))
})
.unwrap_or_else(|| panic!("the menu holds no {label:?}"))
}
fn inside(ui: &Ui, key: NodeKey) -> Vec2 {
rect(ui, key).0 + Vec2::splat(5.0)
}
fn opened(ui: &mut Ui) -> (Page, Vec2) {
let built = page(ui, idle(), NavInput::default());
let at = inside(ui, built.target);
(page(ui, secondary(at), NavInput::default()), at)
}
#[test]
fn a_secondary_press_opens_the_menu_at_the_pointer() {
let mut ui = Ui::new();
let (built, at) = opened(&mut ui);
assert!(built.menus[0].opened, "the press did not report opening");
assert!(!built.menus[2].opened, "the panel's menu opened too");
assert!(ui.is_popup_open(built.target));
assert_eq!(rect(&ui, Ui::popup_key(built.target)).0, at);
let next = page(&mut ui, idle(), NavInput::default());
assert!(!next.menus[0].opened, "opening was reported twice");
assert!(ui.is_popup_open(next.target), "the menu did not stay open");
let again = rect(&ui, next.target).0 + Vec2::splat(2.0);
let reopened = page(&mut ui, secondary(again), NavInput::default());
assert!(
reopened.menus[0].opened,
"the second press did not reopen it"
);
assert_eq!(rect(&ui, Ui::popup_key(next.target)).0, again);
let beside = Vec2::splat(PAGE * 0.5);
let (menu_min, menu_max) = rect(&ui, Ui::popup_key(next.target));
assert!(!(beside.cmpge(menu_min).all() && beside.cmplt(menu_max).all()));
let moved = page(&mut ui, secondary(beside), NavInput::default());
assert!(moved.menus[2].opened, "the panel's menu did not open");
assert!(!ui.is_popup_open(moved.target), "the target's stayed open");
assert_eq!(rect(&ui, Ui::popup_key(moved.panel)).0, beside);
}
#[test]
fn a_disabled_widgets_menu_never_opens() {
let build = |ui: &mut Ui, pointer: PointerInput, enabled: bool| {
frame(ui, pointer, NavInput::default(), |ui| {
let size = [
Declaration::Width(LengthAuto::Px(TARGET.x)),
Declaration::Height(LengthAuto::Px(TARGET.y)),
];
let mut key = None;
ui.enabled(enabled, |ui| {
let target = ui
.block_with("#target", &size, Behavior::BUTTON, |_| {})
.key;
ui.context_menu(target, &items());
key = Some(target);
});
key.expect("built")
})
};
let mut ui = Ui::new();
let target = build(&mut ui, idle(), false);
let at = inside(&ui, target);
build(&mut ui, secondary(at), false);
assert!(!ui.is_popup_open(target), "a disabled widget's menu opened");
build(&mut ui, idle(), true);
build(&mut ui, secondary(at), true);
assert!(
ui.is_popup_open(target),
"the enabled widget's menu did not open"
);
build(&mut ui, idle(), false);
assert!(!ui.is_popup_open(target), "disabling it left the menu open");
}
#[test]
fn ui_menu_again_over_a_menu_focus_cannot_enter_reports_nothing() {
let disabled = [ContextItem::action("Locked", Pick::Locked).enabled(false)];
let build = |ui: &mut Ui, nav: NavInput| {
frame(ui, idle(), nav, |ui| {
let size = [
Declaration::Width(LengthAuto::Px(TARGET.x)),
Declaration::Height(LengthAuto::Px(TARGET.y)),
];
let target = ui
.block_with("#target", &size, Behavior::BUTTON, |_| {})
.key;
(target, ui.context_menu(target, &disabled))
})
};
let mut ui = Ui::new();
build(&mut ui, NavInput::default());
let (target, _) = build(&mut ui, NavInput::NEXT);
let (_, first) = build(&mut ui, NavInput::MENU);
assert!(first.opened);
build(&mut ui, NavInput::NAVIGATION);
assert_eq!(
ui.focused(),
Some(target),
"focus entered a menu of nothing"
);
let (_, again) = build(&mut ui, NavInput::MENU);
assert!(!again.opened, "an open menu was reported opened again");
assert!(ui.is_popup_open(target));
}
#[test]
fn a_primary_press_does_not_open_the_menu() {
let mut ui = Ui::new();
let built = page(&mut ui, idle(), NavInput::default());
let at = inside(&ui, built.target);
let pressed = page(&mut ui, press(at), NavInput::default());
let released = page(&mut ui, release(at), NavInput::default());
assert!(!pressed.menus[0].opened && !released.menus[0].opened);
assert!(!ui.is_popup_open(built.target), "a primary press opened it");
}
#[test]
fn at_the_bottom_right_the_menu_flips_to_end_at_the_pointer() {
let mut ui = Ui::new();
let built = page(&mut ui, idle(), NavInput::default());
let (_, corner_max) = rect(&ui, built.corner);
let at = corner_max - Vec2::splat(5.0);
page(&mut ui, secondary(at), NavInput::default());
let (min, max) = rect(&ui, Ui::popup_key(built.corner));
assert_eq!(max, at, "the menu did not flip to end at the pointer");
assert!(min.cmpge(Vec2::ZERO).all(), "it left the viewport");
}
#[test]
fn ui_menu_opens_the_focused_widgets_menu_below_it() {
let mut ui = Ui::new();
page(&mut ui, idle(), NavInput::default());
let built = page(&mut ui, idle(), NavInput::NEXT);
assert_eq!(ui.focused(), Some(built.target));
let menu = page(&mut ui, idle(), NavInput::MENU);
assert!(
menu.menus[0].opened,
"ui_menu did not open the target's menu"
);
let (target_min, target_max) = rect(&ui, built.target);
assert_eq!(
rect(&ui, Ui::popup_key(built.target)).0,
Vec2::new(target_min.x, target_max.y)
);
let mut ui = Ui::new();
page(&mut ui, idle(), NavInput::default());
page(&mut ui, idle(), NavInput::NEXT);
let other = page(&mut ui, idle(), NavInput::NEXT);
assert_eq!(ui.focused(), Some(other.other));
let menu = page(&mut ui, idle(), NavInput::MENU);
assert!(
menu.menus[2].opened,
"ui_menu on a widget with no menu did not reach the panel around it"
);
assert!(!ui.is_popup_open(other.target));
}
#[test]
fn moves_and_accept_pick_an_item_once_and_close_the_menu() {
let mut ui = Ui::new();
let (built, _) = opened(&mut ui);
let mut picks = Vec::new();
let mut step = |ui: &mut Ui, nav: NavInput| picks.extend(page(ui, idle(), nav).menus[0].picked);
step(&mut ui, NavInput::NAVIGATION);
assert_eq!(ui.focused(), Some(item(&ui, built.target, "Cut")));
step(&mut ui, DOWN);
assert_eq!(ui.focused(), Some(item(&ui, built.target, "Copy")));
step(&mut ui, NavInput::ACCEPT);
assert!(!ui.is_popup_open(built.target), "the pick left it open");
step(&mut ui, NavInput::NAVIGATION);
step(&mut ui, NavInput::ACCEPT);
assert_eq!(picks, [Pick::Copy], "one pick was not reported once");
assert_eq!(ui.focused(), Some(built.target), "focus did not return");
}
#[test]
fn a_disabled_item_is_neither_picked_nor_focused() {
let mut ui = Ui::new();
let (built, _) = opened(&mut ui);
let on = centre(&ui, item(&ui, built.target, "Locked"));
page(&mut ui, press(on), NavInput::default());
let released = page(&mut ui, release(on), NavInput::default());
assert_eq!(
released.menus[0].picked, None,
"the disabled item was picked"
);
assert!(ui.is_popup_open(built.target), "the click closed the menu");
page(&mut ui, idle(), NavInput::NAVIGATION);
page(&mut ui, idle(), DOWN);
assert_eq!(ui.focused(), Some(item(&ui, built.target, "Copy")));
page(&mut ui, idle(), DOWN);
assert_eq!(
ui.focused(),
Some(item(&ui, built.target, "Size")),
"a move stopped on the disabled item or the separator"
);
}
#[test]
fn a_submenu_opens_beside_its_item_and_its_pick_closes_the_chain() {
let mut ui = Ui::new();
let (built, _) = opened(&mut ui);
let size = item(&ui, built.target, "Size");
let on = centre(&ui, size);
page(&mut ui, press(on), NavInput::default());
page(&mut ui, release(on), NavInput::default());
assert!(ui.is_popup_open(size), "the click did not open the submenu");
let (item_min, item_max) = rect(&ui, size);
assert_eq!(
rect(&ui, Ui::popup_key(size)).0,
Vec2::new(item_max.x, item_min.y),
"the submenu is not beside its item"
);
let on = centre(&ui, item(&ui, size, "Large"));
page(&mut ui, press(on), NavInput::default());
let picked = page(&mut ui, release(on), NavInput::default());
assert_eq!(picked.menus[0].picked, Some(Pick::Large));
assert!(!ui.is_popup_open(size), "the submenu stayed open");
assert!(!ui.is_popup_open(built.target), "the menu stayed open");
}
#[test]
fn a_submenu_flips_left_at_the_right_edge() {
let mut ui = Ui::new();
let built = page(&mut ui, idle(), NavInput::default());
let at = inside(&ui, built.corner);
page(&mut ui, secondary(at), NavInput::default());
page(&mut ui, idle(), NavInput::NAVIGATION);
for _ in 0..2 {
page(&mut ui, idle(), DOWN);
}
let size = item(&ui, built.corner, "Size");
assert_eq!(ui.focused(), Some(size));
page(&mut ui, idle(), RIGHT);
assert!(ui.is_popup_open(size), "the right arrow did not open it");
let (item_min, _) = rect(&ui, size);
let (_, sub_max) = rect(&ui, Ui::popup_key(size));
assert_eq!(
sub_max.x, item_min.x,
"the submenu did not flip to the left"
);
}
#[test]
fn back_closes_the_top_submenu_first_then_the_menu() {
let mut ui = Ui::new();
let (built, _) = opened(&mut ui);
page(&mut ui, idle(), NavInput::NAVIGATION);
page(&mut ui, idle(), DOWN);
page(&mut ui, idle(), DOWN);
let size = item(&ui, built.target, "Size");
page(&mut ui, idle(), RIGHT);
page(&mut ui, idle(), NavInput::NAVIGATION);
assert_eq!(
ui.focused(),
Some(item(&ui, size, "Small")),
"focus did not move into the submenu"
);
page(&mut ui, idle(), NavInput::BACK);
assert!(!ui.is_popup_open(size), "back left the submenu open");
assert!(ui.is_popup_open(built.target), "back closed the menu too");
assert_eq!(ui.focused(), Some(size));
page(&mut ui, idle(), RIGHT);
page(&mut ui, idle(), NavInput::NAVIGATION);
page(&mut ui, idle(), LEFT);
assert!(!ui.is_popup_open(size), "left left the submenu open");
assert!(ui.is_popup_open(built.target), "left closed the menu");
page(&mut ui, idle(), NavInput::NAVIGATION);
assert_eq!(
ui.focused(),
Some(size),
"left did not put focus on the item"
);
page(&mut ui, idle(), NavInput::BACK);
assert!(!ui.is_popup_open(built.target), "back left the menu open");
assert_eq!(ui.focused(), Some(built.target));
assert!(
!ui.back_requested(),
"a back spent on the menu was reported"
);
}
#[test]
fn a_press_outside_closes_the_menu_and_is_swallowed() {
let mut ui = Ui::new();
let (built, _) = opened(&mut ui);
let on = centre(&ui, built.corner);
let (menu_min, menu_max) = rect(&ui, Ui::popup_key(built.target));
assert!(
!(on.cmpge(menu_min).all() && on.cmplt(menu_max).all()),
"the fixture's press lands on the menu"
);
let interaction = |ui: &Ui| ui.interaction_of(built.corner);
page(&mut ui, press(on), NavInput::default());
assert!(!ui.is_popup_open(built.target), "the press left it open");
assert!(!interaction(&ui).pressed, "the dismissing press pressed it");
let released = page(&mut ui, release(on), NavInput::default());
assert_eq!(released.menus[0].picked, None);
assert!(!interaction(&ui).clicked, "the dismissing press clicked it");
}
fn menu_page(
ui: &mut Ui,
nav: NavInput,
text: TextInput,
items: &[ContextItem<'_, Pick>],
) -> NodeKey {
frame_with_text(ui, idle(), nav, text, |ui| {
let size = [
Declaration::Width(LengthAuto::Px(TARGET.x)),
Declaration::Height(LengthAuto::Px(TARGET.y)),
Declaration::FlexShrink(0.0),
];
let target = ui
.block_with("#target", &size, Behavior::BUTTON, |_| {})
.key;
ui.context_menu(target, items);
target
})
}
fn typing(text: &str) -> TextInput {
TextInput {
edits: vec![Edit::Insert(text.to_owned())],
..TextInput::default()
}
}
fn opened_by_key(ui: &mut Ui, items: &[ContextItem<'_, Pick>]) -> NodeKey {
menu_page(ui, NavInput::default(), TextInput::default(), items);
let target = menu_page(ui, NavInput::NEXT, TextInput::default(), items);
menu_page(ui, NavInput::MENU, TextInput::default(), items);
menu_page(ui, NavInput::NAVIGATION, TextInput::default(), items);
assert!(ui.is_popup_open(target), "ui_menu did not open the menu");
target
}
#[test]
fn home_and_end_skip_disabled_items() {
let items = [
ContextItem::action("Locked", Pick::Locked).enabled(false),
ContextItem::action("Cut", Pick::Cut),
ContextItem::action("Copy", Pick::Copy),
ContextItem::Separator,
ContextItem::submenu("Size", &SIZES),
ContextItem::action("Panel", Pick::Panel).enabled(false),
];
let mut ui = Ui::new();
let target = opened_by_key(&mut ui, &items);
menu_page(&mut ui, DOWN, TextInput::default(), &items);
assert_eq!(ui.focused(), Some(item(&ui, target, "Copy")));
let end = NavInput::jumping(Jump::Last);
menu_page(&mut ui, end, TextInput::default(), &items);
assert_eq!(ui.focused(), Some(item(&ui, target, "Size")), "End");
let home = NavInput::jumping(Jump::First);
menu_page(&mut ui, home, TextInput::default(), &items);
assert_eq!(ui.focused(), Some(item(&ui, target, "Cut")), "Home");
assert!(ui.is_popup_open(target), "a jump closed the menu");
}
#[test]
fn the_arrows_wrap_at_the_menus_ends() {
let mut ui = Ui::new();
let target = opened_by_key(&mut ui, &items());
assert_eq!(ui.focused(), Some(item(&ui, target, "Cut")));
menu_page(&mut ui, UP, TextInput::default(), &items());
assert_eq!(
ui.focused(),
Some(item(&ui, target, "Size")),
"up did not wrap to the last item"
);
menu_page(&mut ui, DOWN, TextInput::default(), &items());
assert_eq!(
ui.focused(),
Some(item(&ui, target, "Cut")),
"down did not wrap to the first item"
);
}
#[test]
fn a_long_menu_wraps_to_its_first_item_and_scrolls_it_into_view() {
let labels: Vec<String> = (0..40).map(|index| format!("item {index:02}")).collect();
let items: Vec<ContextItem<'_, Pick>> = labels
.iter()
.map(|label| ContextItem::action(label.as_str(), Pick::Cut))
.collect();
let mut ui = Ui::new();
let target = opened_by_key(&mut ui, &items);
let menu = Ui::popup_key(target);
let (menu_min, menu_max) = rect(&ui, menu);
assert!(menu_max.y - menu_min.y < PAGE, "the menu was not capped");
menu_page(
&mut ui,
NavInput::jumping(Jump::Last),
TextInput::default(),
&items,
);
let last = item(&ui, target, "item 39");
assert_eq!(ui.focused(), Some(last));
assert!(ui.scroll_offset_of(menu).y > 0.0, "End did not scroll");
menu_page(&mut ui, DOWN, TextInput::default(), &items);
let first = item(&ui, target, "item 00");
assert_eq!(
ui.focused(),
Some(first),
"down wrapped to an item other than the first"
);
assert_eq!(
ui.scroll_offset_of(menu).y,
0.0,
"the first item is not in view"
);
}
#[test]
fn typeahead_skips_disabled_items() {
let items = [
ContextItem::action("Cut", Pick::Cut),
ContextItem::action("Copy", Pick::Copy).enabled(false),
ContextItem::action("Crop", Pick::Small),
];
let mut ui = Ui::new();
let target = opened_by_key(&mut ui, &items);
assert_eq!(ui.focused(), Some(item(&ui, target, "Cut")));
menu_page(&mut ui, NavInput::NAVIGATION, typing("c"), &items);
menu_page(&mut ui, NavInput::NAVIGATION, TextInput::default(), &items);
assert_eq!(ui.focused(), Some(item(&ui, target, "Crop")));
}
#[test]
fn a_submenu_takes_typeahead_and_still_picks() {
let mut ui = Ui::new();
let target = opened_by_key(&mut ui, &items());
menu_page(
&mut ui,
NavInput::jumping(Jump::Last),
TextInput::default(),
&items(),
);
let size = item(&ui, target, "Size");
menu_page(&mut ui, RIGHT, TextInput::default(), &items());
menu_page(
&mut ui,
NavInput::NAVIGATION,
TextInput::default(),
&items(),
);
assert_eq!(ui.focused(), Some(item(&ui, size, "Small")));
assert!(ui.popup_list_open(), "the submenu does not take the keys");
menu_page(&mut ui, NavInput::NAVIGATION, typing("l"), &items());
menu_page(
&mut ui,
NavInput::NAVIGATION,
TextInput::default(),
&items(),
);
let large = item(&ui, size, "Large");
assert_eq!(ui.focused(), Some(large), "the submenu did not take the l");
menu_page(&mut ui, NavInput::NAVIGATION, typing("c"), &items());
menu_page(
&mut ui,
NavInput::NAVIGATION,
TextInput::default(),
&items(),
);
assert_eq!(ui.focused(), Some(large), "the menu under it took the c");
let picked = frame(&mut ui, idle(), NavInput::ACCEPT, |ui| {
let size = [
Declaration::Width(LengthAuto::Px(TARGET.x)),
Declaration::Height(LengthAuto::Px(TARGET.y)),
Declaration::FlexShrink(0.0),
];
let target = ui
.block_with("#target", &size, Behavior::BUTTON, |_| {})
.key;
ui.context_menu(target, &items()).picked
});
assert_eq!(picked, Some(Pick::Large));
assert!(!ui.is_popup_open(target), "the pick left the menu open");
}
#[test]
fn a_shortcut_is_shown_after_the_label() {
let mut ui = Ui::new();
let (built, _) = opened(&mut ui);
let cut = item(&ui, built.target, "Cut");
let [label, shortcut] = children_of(&ui, cut)[..] else {
panic!("Cut holds no label and shortcut");
};
assert_eq!(ui.text(shortcut), Some(CUT_KEY));
let (label_min, label_max) = rect(&ui, label);
let (shortcut_min, shortcut_max) = rect(&ui, shortcut);
assert!(
shortcut_min.x >= label_max.x,
"the shortcut is not after the label"
);
let copy = item(&ui, built.target, "Copy");
let (_, copy_max) = rect(&ui, copy);
assert!(
copy_max.x - shortcut_max.x < shortcut_min.x - label_min.x,
"the shortcut ends at {shortcut_max}, short of the item's end at {copy_max}"
);
assert_eq!(children_of(&ui, copy).len(), 1, "Copy shows a shortcut");
let locked = item(&ui, built.target, "Locked");
assert_eq!(
children_of(&ui, locked).len(),
2,
"disabling an item lost its shortcut"
);
}