Menu: a submenu opens as soon as the pointer is on its row; a row the pointer crosses on its way to an open submenu takes over after a 0.3 s rest

This commit is contained in:
aiongg
2026-11-12 14:32:46 +00:00
parent 67e30b245a
commit 1b18aa6a93
3 changed files with 70 additions and 11 deletions
+13 -11
View File
@@ -30,8 +30,9 @@ const MARKS: ColumnKey = ColumnKey::new("menu_marks");
/// The column of a level's row icons, and the gap after it, after the mark column.
const ICONS: ColumnKey = ColumnKey::new("menu_icons");
/// The seconds the pointer rests on a row before the row opens its submenu, or closes the open submenu.
const DWELL: f64 = 0.3;
/// The seconds the pointer rests on a row it crossed on its way to another row's open submenu before the row takes
/// over: opens its own submenu, or closes the open one. A row the pointer does not cross that way takes over at once.
const AIM_REST: f64 = 0.3;
/// `label` with a `&` marker before the first character that is `letter`, so the level's assignment honours a row's
/// own mnemonic; a label without that letter is left as it is.
@@ -394,22 +395,23 @@ impl Component for MenuItem<'_> {
let moving = ctx.input(|input| input.pointer.delta() != Vec2::ZERO);
let hovered = cx.response.hovered();
let focused = cx.is_focused();
let aimed = moving && level.aimed(cx.id);
// The pointer's last move headed for the open submenu of another row: this row is one it crosses on the way.
let heading = level.aimed(cx.id);
let keyboard = level.keyboard();
let (waited, focus_gained, key_started) = {
let (rested, focus_gained, key_started) = {
let rest = cx.memory::<Rest>();
let gained = focused && !rest.focused;
rest.focused = focused;
let started = keyboard && !rest.keyboard;
rest.keyboard = keyboard;
// The pointer heading for the open submenu does not count as resting on this row.
if hovered && !aimed {
if hovered && !moving {
(now - *rest.since.get_or_insert(now), gained, started)
} else {
rest.since = None;
(0.0, gained, started)
}
};
let takes_over = hovered && (!heading || rested >= AIM_REST);
// The focused row of a menu that the pointer opened is not active: the keyboard makes it so when focus lands on
// it or the keyboard starts driving the menu.
if enabled && ((hovered && moving) || (focused && keyboard && (focus_gained || key_started))) && level.activate_row(cx.id) {
@@ -417,14 +419,14 @@ impl Component for MenuItem<'_> {
}
// A disabled row is never active.
self.active = enabled && level.is_active(cx.id);
if hovered && waited < DWELL {
// A crossed row waits for the rest to end.
if hovered && heading && rested < AIM_REST {
frame::request_repaint(&ctx);
}
let typed = enabled && level.take_run(cx.id);
let asked = enabled && (typed || cx.activation().is_some());
if self.submenu {
let rested = enabled && hovered && waited >= DWELL;
self.opens = asked || (enabled && forward) || rested;
self.opens = asked || (enabled && forward) || (enabled && takes_over);
if self.opens && level.open_submenu(cx.id) {
frame::request_repaint(&ctx);
}
@@ -435,7 +437,7 @@ impl Component for MenuItem<'_> {
}
return;
}
if hovered && waited >= DWELL && level.clear_submenu() {
if takes_over && level.clear_submenu() {
frame::request_repaint(&ctx);
}
// The run closes the menus: focus leaves them first, so the command reaches the stop focus returns to.
@@ -554,7 +556,7 @@ impl Component for MenuItem<'_> {
}
}
/// How long the pointer has rested on a row, and whether the row had the focus in the last pass.
/// Since when the pointer has rested on a row without moving, and whether the row had the focus in the last pass.
#[derive(Clone, Default)]
struct Rest {
since: Option<f64>,
+2
View File
@@ -10,6 +10,8 @@ mod openers;
mod pick_focus;
mod submenus;
use craft_behavior::IconId;
use craft_ui::contract::{Show as _, ShowContainer as _};
use craft_ui::layout_components::Stack;
@@ -0,0 +1,55 @@
//! Submenus: a row opens its submenu as soon as the pointer is on it, and an open submenu stays open while the
//! pointer heads for it across other rows.
use craft_ui::buttons::Button;
use craft_ui::contract::{Show as _, ShowContainer as _, Trigger};
use craft_ui::menu::{Menu, MenuItem};
use craft_ui::testing::{Fixture, pointer, trial};
/// A menu with two submenu rows, Recent and Templates, the first with a submenu taller than the rows below it.
fn open_menu() -> Fixture {
let mut fixture = Fixture::new(|ui| {
let opener = Button::new("Open").show(ui);
Menu::new("Main").opened_by(&opener, Trigger::Click).show(ui, |ui, _| {
MenuItem::new("New").show(ui);
Menu::submenu_row(ui, "Recent", |ui, _| {
for name in ["Report.txt", "Notes.txt", "Plan.txt", "Budget.txt", "Draft.txt"] {
MenuItem::new(name).show(ui);
}
});
Menu::submenu_row(ui, "Templates", |ui, _| {
MenuItem::new("Blank").show(ui);
});
});
});
fixture.settle();
let opener = fixture.by_label("Open").expect("the opener");
fixture.click(&opener);
fixture.settle();
fixture
}
#[trial]
fn a_submenu_opens_as_soon_as_the_pointer_is_on_its_row() {
let mut fixture = open_menu();
let recent = fixture.by_label("Recent").expect("the menu is open").center();
fixture.frame(vec![pointer::moved(recent)]).idle(1);
assert!(fixture.by_label("Report.txt").is_some(), "the submenu opens without the pointer resting on its row");
}
#[trial]
fn an_open_submenu_stays_open_while_the_pointer_heads_for_it_across_another_row() {
let mut fixture = open_menu();
let recent = fixture.by_label("Recent").expect("the menu is open").center();
fixture.frame(vec![pointer::moved(recent)]).idle(1);
let submenu = fixture.by_label("Report.txt").expect("the submenu is open").rect;
let templates = fixture.by_label("Templates").expect("the second submenu row").rect;
// A point on Templates, near the submenu's edge and level with its rows: the pointer heads for the submenu.
let crossing = egui::pos2(templates.max.x - 2.0, templates.center().y);
assert!(crossing.y < submenu.max.y, "the submenu reaches below the crossing point");
fixture.frame(vec![pointer::moved(crossing)]).idle(1);
assert!(fixture.by_label("Report.txt").is_some(), "the submenu the pointer heads for stays open");
assert!(fixture.by_label("Blank").is_none(), "the crossed row does not open its submenu");
fixture.advance(0.4).idle(2);
assert!(fixture.by_label("Blank").is_some(), "the crossed row takes over once the pointer rests on it");
}