From 4bb8e364291a0686833613acfdd8d8cfe1d83082 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 03:22:08 -0800 Subject: [PATCH] ui-widgets: a focused menu bar title shows the focus ring Tab, F10 and the arrow keys move the keyboard to a menu bar title, a part of the bar, but no ring marked it. The paint step rings a focused part with its component's focus look, and MenuBarStyle had none; the focus look on TitleFaceStyle belongs to the face, which never takes focus. MenuBarStyle now carries the theme's focus look. --- crates/ui-widgets/src/menu/bar.rs | 9 ++++--- crates/ui-widgets/tests/it/menu/bar.rs | 34 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/crates/ui-widgets/src/menu/bar.rs b/crates/ui-widgets/src/menu/bar.rs index 99e4e86c20..d583f51d85 100644 --- a/crates/ui-widgets/src/menu/bar.rs +++ b/crates/ui-widgets/src/menu/bar.rs @@ -179,12 +179,15 @@ impl Component for TitleFace { } } -/// The style of a menu bar: the titles wear row faces, which underline their mnemonics. +/// The style of a menu bar: the titles wear row faces, which underline their mnemonics, and the title the keyboard is +/// on is ringed with the focus look. #[derive(Clone, Debug, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct MenuBarStyle { #[style(metrics)] metrics: Metrics, + #[style(focus)] + focus: FocusLook, } /// A title's text without its marker, and its mnemonic: the letter and its character index in the text, none for a @@ -475,8 +478,8 @@ impl Component for MenuBar<'_> { type Style = MenuBarStyle; type Output = MenuBarOutput; - fn style(_: &StyleCx<'_>, _: MenuBarStyleKey) -> MenuBarStyle { - MenuBarStyle { metrics: Metrics::new() } + fn style(cx: &StyleCx<'_>, _: MenuBarStyleKey) -> MenuBarStyle { + MenuBarStyle { metrics: Metrics::new(), focus: cx.focus_look() } } /// Every title's width; at its least (`Proposal::Min`) the width of the "…" title alone. A row that has less room diff --git a/crates/ui-widgets/tests/it/menu/bar.rs b/crates/ui-widgets/tests/it/menu/bar.rs index 0c5f90c5d0..f16fde3cd9 100644 --- a/crates/ui-widgets/tests/it/menu/bar.rs +++ b/crates/ui-widgets/tests/it/menu/bar.rs @@ -601,3 +601,37 @@ fn a_fields_selection_holds_while_the_menu_bar_has_the_keyboard() { let copy = fixture.by_label("Copy").expect("the Edit menu is open"); assert!(!fixture.tree().is_disabled_key(copy.node), "Copy applies to the field's selection"); } + +/// Whether the latest frame paints a focus ring (a stroke at least two points wide) close around `target`, not around +/// the whole bar. +fn ringed(fixture: &mut Fixture, target: egui::Rect) -> bool { + let around = |ring: egui::Rect| ring.expand(1.0).contains_rect(target) && target.expand(4.0).contains_rect(ring); + fixture.frame_shapes().iter().any(|shape| match shape { + egui::Shape::Rect(rect) if rect.stroke.width >= 2.0 => around(rect.rect), + egui::Shape::Path(path) if path.stroke.width >= 2.0 => around(egui::Rect::from_points(&path.points)), + _ => false, + }) +} + +/// The title that Tab, F10 or an arrow key moves the keyboard to is ringed, as a focused button is, and the title the +/// keyboard left is not. +#[trial] +fn the_focused_title_shows_the_focus_ring() { + let mut rig = rig(Direction::Ltr); + let fixture = &mut rig.fixture; + fixture.key(Key::Tab, Modifiers::NONE).settle(); + let (bar, file) = fixture.focused_stop().and_then(|(bar, part)| Some((bar, part?))).expect("Tab focuses a title"); + let file_rect = fixture.part_rect(bar, file).expect("the focused title's rect"); + assert!(ringed(fixture, file_rect), "Tab rings the title it focuses"); + fixture.key(Key::ArrowRight, Modifiers::NONE).settle(); + let edit = fixture.focused_stop().and_then(|(_, part)| part).expect("Right focuses the next title"); + assert_ne!(edit, file, "Right moves to another title"); + let edit_rect = fixture.part_rect(bar, edit).expect("the next title's rect"); + assert!(ringed(fixture, edit_rect), "Right rings the title it moves to"); + assert!(!ringed(fixture, file_rect), "the title the keyboard left loses its ring"); + fixture.key(Key::Escape, Modifiers::NONE).settle(); + fixture.key(Key::F10, Modifiers::NONE).settle(); + let first = fixture.focused_stop().and_then(|(_, part)| part).expect("F10 focuses the first title"); + let first_rect = fixture.part_rect(bar, first).expect("the first title's rect"); + assert!(ringed(fixture, first_rect), "F10 rings the title it focuses"); +}