diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index f94ead91c..2cf5ec010 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -685,6 +685,19 @@ pub(crate) fn with_hover(ctx: &Context, viewport: ViewportId, f: impl FnOnce( PerViewport::::with(ctx, viewport, |record| f(record.hover.as_ref().map(|(_, _, under)| under), record.captured)) } +/// Whether a shape the component `id` registered shows hover in the running pass, by the pass's one resolution of +/// what lies under the pointer ([`Under`]): it takes the press there, or it is the topmost shape and takes none. A +/// container reads it for the components in its body before they run. While a press is open it holds for none: only +/// the press's target hovers, and that target reads it from its press. +pub fn hovers(ctx: &Context, id: Id) -> bool { + with_hover(ctx, pass::current_viewport(ctx), |under, captured| { + !captured + && under.is_some_and(|under| { + [under.press.click, under.press.drag, under.hover_top].into_iter().flatten().any(|(winner, _)| winner.owner == id) + }) + }) +} + /// The resolutions of what lies under a point the running viewport's latest pass made. #[cfg(feature = "testing")] pub(crate) fn resolutions(ctx: &Context) -> u32 { diff --git a/crates/ui-widgets/src/menu/item.rs b/crates/ui-widgets/src/menu/item.rs index a89b29e9c..806feca88 100644 --- a/crates/ui-widgets/src/menu/item.rs +++ b/crates/ui-widgets/src/menu/item.rs @@ -89,6 +89,9 @@ pub struct MenuItemOutput { pub opens_submenu: bool, /// A submenu row's submenu is the open one of its level: show the `Menu` after the row. pub submenu_open: bool, + /// The row is its level's active row and paints with the row fill: the pointer moved onto it, or the keyboard + /// moved to it. + pub active: bool, /// The row's rect grown by the menu's padding: where a submenu hangs from, so its first row is level with this one /// and it starts at the popup's edge. pub(crate) frame: Rect, @@ -149,7 +152,8 @@ fn label_line(area: Rect, detail: f32) -> Rect { /// - [`keep_open`](MenuItem::keep_open) keeps the menus open when the row runs (a check row that toggles). /// - The mnemonic is the `&` letter of the label, else the first letter no earlier row of the level uses. The /// underline sits under that letter while Alt is held or the keyboard drives the menu, never on macOS. -/// - The pointer moves the level's active row; the row under it paints with the shared row fill. +/// - The level's active row paints with the shared row fill: the row the pointer moved onto (its `Menu` sets it +/// before the rows run), or the one the keyboard moved to. /// - A [`tint`](MenuItem::tint) (a user label colour) fills the row behind its content at a strength the theme keeps /// the text readable on (`MenuTint`); the active, open and disabled looks win over it. The row keeps its text, so the /// colour is never the only cue. @@ -453,7 +457,7 @@ impl Component for MenuItem<'_> { 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) { + if enabled && focused && keyboard && (focus_gained || key_started) && level.activate_row(cx.id) { frame::request_repaint(&ctx); } // A disabled row is never active. @@ -607,7 +611,16 @@ impl Component for MenuItem<'_> { // The frame is where a submenu hangs: the row with the popup padding, widened by `gap.tight` on both sides so a // submenu that flips to the other side keeps the gap. let frame = cx.response.rect.expand(level.pad()).expand2(egui::vec2(cx.style.submenu_gap, 0.0)); - MenuItemOutput { response: cx.response.clone(), ran: self.ran, opens_submenu: self.opens, submenu_open: self.open, frame, level, id: cx.id } + MenuItemOutput { + response: cx.response.clone(), + ran: self.ran, + opens_submenu: self.opens, + submenu_open: self.open, + active: self.active, + frame, + level, + id: cx.id, + } } } diff --git a/crates/ui-widgets/src/menu/level.rs b/crates/ui-widgets/src/menu/level.rs index 3a01df9c0..2cdaa99da 100644 --- a/crates/ui-widgets/src/menu/level.rs +++ b/crates/ui-widgets/src/menu/level.rs @@ -281,6 +281,17 @@ impl MenuLevel { }) } + /// Makes the enabled row `under` names, among the rows as the user last saw them, the active row: the row the + /// pointer moved onto. Returns whether that changed it. + pub(super) fn follow_pointer(&self, under: impl Fn(Id) -> bool) -> bool { + self.with(|state| { + let Some(row) = state.shown.iter().find(|row| row.enabled && under(row.id)).map(|row| row.id) else { return false }; + let changed = state.active != Some(row); + state.active = Some(row); + changed + }) + } + pub(crate) fn active(&self) -> Option { self.with(|state| state.active) } diff --git a/crates/ui-widgets/src/menu/popup.rs b/crates/ui-widgets/src/menu/popup.rs index dd451a818..393472a4f 100644 --- a/crates/ui-widgets/src/menu/popup.rs +++ b/crates/ui-widgets/src/menu/popup.rs @@ -298,7 +298,8 @@ impl Component for Menu<'_> { Sense::empty() } - /// A row of a submenu level that took Back closes the menu. A typed letter runs the row its mnemonic names; the + /// A pointer move onto an enabled row of the level makes it the active row, before the rows run. A row of a + /// submenu level that took Back closes the menu. A typed letter runs the row its mnemonic names; the /// container claims the letters, so a letter works whichever row is focused. A level the pointer opened with no active row has no lead: /// the first key enters it at an end. The active row is the lead, so the first key after the pointer lit a row /// moves from that row; a row lit in this pass leads from the next, so only a key in the same frame as the pointer @@ -323,6 +324,12 @@ impl Component for Menu<'_> { level.run(row); frame::request_repaint(&cx.core.ctx); } + // The rows run after this, so the pass that moved the pointer onto a row paints it, and the row it left, as the + // level now has them. + if crate::runtime::pointer::frame(&cx.core.ctx).delta != egui::Vec2::ZERO { + let ctx = cx.core.ctx.clone(); + level.follow_pointer(|row| crate::runtime::pointer::hovers(&ctx, row)); + } let keyboard = crate::runtime::focus::keyboard_in_menu(&cx.core.ctx); self.props.roving = self.props.roving.map(|roving| roving.no_lead(!keyboard && !level.has_active())); if let Some(row) = level.active() { diff --git a/crates/ui-widgets/tests/it/menu/bar.rs b/crates/ui-widgets/tests/it/menu/bar.rs index c1571c088..a01dfc38b 100644 --- a/crates/ui-widgets/tests/it/menu/bar.rs +++ b/crates/ui-widgets/tests/it/menu/bar.rs @@ -93,3 +93,53 @@ fn a_letter_runs_the_row_it_marks_in_a_menu_a_title_click_opened() { assert_eq!(saved.get(), 1, "the letter runs the row it marks"); assert!(fixture.by_label("Save").is_none(), "and the run closes the menu"); } + +/// The pass a pointer move over a menu's rows runs paints the row under the pointer as the active row and no other, +/// moving down or up, at 1x and at a fractional scale. egui runs one more frame after any frame with input; that frame +/// changes nothing and asks for none. +#[trial] +fn a_pointer_move_over_menu_rows_lights_the_row_under_it_in_that_frame() { + use craft_behavior::MotionMode; + use craft_ui::runtime::env::Scale; + use craft_ui::testing::pointer; + use std::cell::RefCell; + use std::collections::HashMap; + use std::rc::Rc; + const ROWS: usize = 8; + let names: Vec = (1..=ROWS).map(|at| format!("Item {at:02}")).collect(); + for scale in [1.0, 1.5] { + let lit: Rc>> = Rc::default(); + let seen = lit.clone(); + let rows = names.clone(); + let mut fixture = Fixture::new(move |ui| { + let seen = seen.clone(); + let rows = rows.clone(); + let titles = vec![MenuTitle::new("&File", move |ui| { + for name in &rows { + let active = MenuItem::new(name.clone()).show(ui).active; + // The last run of the frame is the one that paints. + seen.borrow_mut().insert(name.clone(), active); + } + })]; + MenuBar::new("Main menu", titles).show(ui); + }); + fixture.room(egui::vec2(480.0, 320.0)).scale(Scale::new(scale)).motion(MotionMode::Off).settle(); + let file = fixture.by_label("File").expect("the File title"); + fixture.click(&file); + fixture.settle(); + let shown: Vec<(String, egui::Pos2)> = + names.iter().map(|name| (name.clone(), fixture.by_label(name).expect("the File menu shows every row").center())).collect(); + let downward = shown.iter(); + let upward = shown.iter().rev().skip(1); + for (name, center) in downward.chain(upward) { + lit.borrow_mut().clear(); + fixture.frame(vec![pointer::moved(*center)]); + let active: Vec = lit.borrow().iter().filter(|(_, on)| **on).map(|(row, _)| row.clone()).collect(); + assert_eq!(active, vec![name.clone()], "at {scale}x the move frame onto {name} lights it and no other row"); + lit.borrow_mut().clear(); + fixture.frame(Vec::new()); + assert_eq!(lit.borrow().get(name), Some(&true), "at {scale}x {name} stays lit with the pointer at rest"); + assert!(!fixture.repaint_requested(), "at {scale}x a pointer at rest on {name} asks for no frame"); + } + } +}