diff --git a/crates/ui-panels/src/rails/flyout.rs b/crates/ui-panels/src/rails/flyout.rs index 0207a1c2b..1d2506139 100644 --- a/crates/ui-panels/src/rails/flyout.rs +++ b/crates/ui-panels/src/rails/flyout.rs @@ -14,7 +14,7 @@ use crate::menu::{self, Menu, MenuItem}; use crate::primitives::{IconSpec, PaintShape, StrokeWidth}; use crate::runtime::commands::{CommandRef, FireSource, ResolvedCommand}; use crate::runtime::env::Label; -use crate::style::{ComponentStyle, GlyphRole, StyleColor, StyleCx}; +use crate::style::{ComponentStyle, FocusLook, GlyphRole, StyleColor, StyleCx}; use craft_behavior::overlay::{Align, Side}; use craft_behavior::{CursorIcon, HitLevel, HitPriority, IconId, Metrics, PartKey, PointerButton, PressPhase, Sense, Value}; use egui::{Pos2, Rect, Response, Vec2}; @@ -30,12 +30,16 @@ const MARK: PartKey = PartKey::new("mark"); const BAR: PartKey = PartKey::new("bar"); /// The style of a flyout button: it paints through its face, so it holds no look of its own besides its side, the rail -/// button's, and the size of the corner mark's press area, which is the size and the inset of the mark the face paints. -#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)] +/// button's, its focus ring, and the size of the corner mark's press area, which is the size and the inset of the mark +/// the face paints. +#[derive(Clone, Debug, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct FlyoutButtonStyle { #[style(metrics)] metrics: Metrics, + /// The ring the button shows while it has keyboard focus: a face paints none. + #[style(focus)] + focus: FocusLook, /// The side of the corner mark, which is also its inset from the face's corner. mark: f32, /// The button's glyph role, which its face shows the icon in. @@ -239,7 +243,12 @@ impl Component for FlyoutButton<'_, T> { fn style(cx: &StyleCx<'_>, _: FlyoutButtonStyleKey) -> FlyoutButtonStyle { let side = cx.env::().button(cx.tokens); - FlyoutButtonStyle { metrics: Metrics::new().min_size(Vec2::splat(side)), mark: cx.tokens.space.gap.tight, glyphs: cx.glyph_role() } + FlyoutButtonStyle { + metrics: Metrics::new().min_size(Vec2::splat(side)), + focus: cx.focus_look(), + mark: cx.tokens.space.gap.tight, + glyphs: cx.glyph_role(), + } } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { diff --git a/crates/ui-panels/tests/it/rails/mod.rs b/crates/ui-panels/tests/it/rails/mod.rs index 24d4dcca3..9494807ae 100644 --- a/crates/ui-panels/tests/it/rails/mod.rs +++ b/crates/ui-panels/tests/it/rails/mod.rs @@ -149,6 +149,29 @@ fn the_current_tools_button_reports_pressed_and_the_others_not() { assert_eq!(pressed("Cut"), Some(Mixed::Same(false)), "another tool's button reports that it is not"); } +/// A rail button with keyboard focus is ringed as a button is: a focus stroke of two points around its own rect. +#[trial] +fn a_focused_rail_button_shows_the_focus_ring_around_it() { + let all = tools(&[UNDO, REDO]); + let mut fixture = Fixture::new(move |ui| { + FlyoutButton::new(&all, &UNDO).show(ui); + }); + fixture.settle(); + let ring = |fixture: &mut Fixture| { + fixture.frame_shapes().iter().find_map(|shape| match shape { + egui::Shape::Rect(rect) if rect.stroke.width >= 2.0 => Some(rect.rect), + _ => None, + }) + }; + assert!(ring(&mut fixture).is_none(), "an unfocused rail button has no ring"); + fixture.key(Key::Tab, Modifiers::NONE); + fixture.settle(); + assert!(fixture.focused().is_some(), "Tab focused the rail button"); + let target = fixture.by_role(Role::BUTTON).into_iter().next().expect("the rail button shows"); + let ring = ring(&mut fixture).expect("the focused rail button paints a focus ring"); + assert!(ring.expand(1.0).contains_rect(target.rect), "the ring {ring:?} goes around the button {:?}", target.rect); +} + /// With a click command a click, Enter or Space fires it and selects nothing; a hold, a drag away and a right click /// list the tools, and the release that ends a hold fires nothing. #[trial] diff --git a/crates/ui-widgets/src/controls/disclosure.rs b/crates/ui-widgets/src/controls/disclosure.rs index 6785bf03a..16ff31972 100644 --- a/crates/ui-widgets/src/controls/disclosure.rs +++ b/crates/ui-widgets/src/controls/disclosure.rs @@ -5,7 +5,7 @@ use crate::contract::{ Animate as _, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, PaintUi, Pointer as _, Role, Styled as _, spec, }; use crate::primitives::{Glyph, PaintShape, StrokeWidth}; -use crate::style::{ComponentStyle, IconLook, StyleCx, UiTokens, ink_color, rgba_color}; +use crate::style::{ComponentStyle, FocusLook, IconLook, StyleCx, UiTokens, ink_color, rgba_color}; use craft_behavior::{Metrics, Sense}; use craft_i18n::Direction; use egui::emath::GuiRounding as _; @@ -190,6 +190,8 @@ impl Glyph for DisclosureGlyph { pub struct DisclosureStyle { #[style(metrics)] metrics: Metrics, + #[style(focus)] + focus: FocusLook, /// The triangle. #[style(icon)] glyph: IconLook, @@ -223,7 +225,7 @@ impl Component for Disclosure<'_> { fn style(cx: &StyleCx<'_>, _: DisclosureStyleKey) -> DisclosureStyle { let side = cx.control().icon; - DisclosureStyle { metrics: Metrics::new().min_size(Vec2::splat(side)), glyph: IconLook::new(side, cx.quiet_ink()) } + DisclosureStyle { metrics: Metrics::new().min_size(Vec2::splat(side)), focus: cx.focus_look(), glyph: IconLook::new(side, cx.quiet_ink()) } } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { diff --git a/crates/ui-widgets/tests/it/focus_ring.rs b/crates/ui-widgets/tests/it/focus_ring.rs index 0c45a5514..c09225d91 100644 --- a/crates/ui-widgets/tests/it/focus_ring.rs +++ b/crates/ui-widgets/tests/it/focus_ring.rs @@ -31,3 +31,25 @@ fn the_focus_ring_is_two_points_thick() { let width = widest_stroke(&mut fixture); assert!((width - 2.0).abs() < 0.01, "the ring is the theme's focus stroke of two points: {width}"); } + +/// A disclosure with keyboard focus is ringed as a button is: a focus stroke around its own rect. +#[trial] +fn a_focused_disclosure_shows_the_focus_ring_around_it() { + use craft_ui::contract::{Bind, Props as _, Role}; + use craft_ui::controls::disclosure::Disclosure; + let mut fixture = Fixture::new(|ui| { + let _ = Disclosure::new(Bind::given(false)).label("Details").show(ui); + }); + fixture.settle(); + assert!(widest_stroke(&mut fixture) < 2.0, "an unfocused disclosure has no thick line"); + fixture.key(Key::Tab, Modifiers::NONE); + fixture.settle(); + assert!(fixture.focused().is_some(), "Tab focused the disclosure"); + let target = fixture.by_role(Role::BUTTON).into_iter().next().expect("the disclosure shows"); + let ring = fixture.frame_shapes().iter().find_map(|shape| match shape { + Shape::Rect(rect) if rect.stroke.width >= 2.0 => Some(rect.rect), + _ => None, + }); + let ring = ring.expect("the focused disclosure paints a focus ring"); + assert!(ring.expand(1.0).contains_rect(target.rect), "the ring {ring:?} goes around the disclosure {:?}", target.rect); +}