diff --git a/crates/ui-panels/src/docking/dock_column.rs b/crates/ui-panels/src/docking/dock_column.rs index 5747b69c5f..2629923a03 100644 --- a/crates/ui-panels/src/docking/dock_column.rs +++ b/crates/ui-panels/src/docking/dock_column.rs @@ -15,7 +15,7 @@ use crate::contract::{ use crate::menu::GroupItem; use crate::runtime::env::Label; use crate::runtime::focus::KeyUse; -use crate::style::{ComponentStyle, Corners, StyleCx, SurfaceEdge, SurfaceKind, SurfaceStyle, UiTokens}; +use crate::style::{ComponentStyle, Corners, FocusLook, StyleCx, SurfaceEdge, SurfaceKind, SurfaceStyle, UiTokens}; use crate::tabs::PanelTabLook; use craft_behavior::divider::{self as push, Segment}; use craft_behavior::dock::{Column, DockEdge, Group}; @@ -160,6 +160,9 @@ impl

Outcome for DockColumnOutput

{ pub struct DockColumnStyle { #[style(metrics)] metrics: Metrics, + /// The ring around the chevron or the divider that has keyboard focus. + #[style(focus)] + focus: FocusLook, sizes: DockSizes, /// The card of a column shown as one ([`card`](DockColumn::card)). card: SurfaceStyle, @@ -469,7 +472,7 @@ impl Component for DockColumn<'_, P> { } fn style(cx: &StyleCx<'_>, _: DockColumnStyleKey

) -> DockColumnStyle { - DockColumnStyle { metrics: Metrics::new(), sizes: DockSizes::style(cx), card: card_style(cx) } + DockColumnStyle { metrics: Metrics::new(), focus: cx.focus_look(), sizes: DockSizes::style(cx), card: card_style(cx) } } fn sense(&self) -> Sense { diff --git a/crates/ui-panels/src/docking/floating.rs b/crates/ui-panels/src/docking/floating.rs index 7377e39240..9279bdc6d2 100644 --- a/crates/ui-panels/src/docking/floating.rs +++ b/crates/ui-panels/src/docking/floating.rs @@ -25,7 +25,7 @@ use crate::runtime::env::{Env, Label}; use crate::runtime::overlay::CloseReason; use crate::runtime::windows::{ResizeEdge, WindowSpec}; use crate::sections::{HeaderAction, HeaderStrength, MenuRows, TitleRow}; -use crate::style::{ComponentStyle, StyleCx, SurfaceKind, UiTokens}; +use crate::style::{ComponentStyle, FocusLook, StyleCx, SurfaceKind, UiTokens}; use craft_behavior::dock::DockEdge; use craft_behavior::{GesturePhase, IconId, Metrics, Sense}; use craft_ui_widgets::popover::WholeWindow; @@ -45,12 +45,15 @@ pub(crate) fn title_row(tokens: &UiTokens) -> f32 { /// title bar has beside its double arrow; the menu opens from the band. const ACTIONS: [HeaderAction; 1] = [HeaderAction::Close]; -/// The style of a floating group: the surface and padding come from the container. -#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)] +/// The style of a floating group: the surface and padding come from the container, and the ring from the theme. +#[derive(Clone, Debug, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct FloatingGroupStyle { #[style(metrics)] metrics: Metrics, + /// The ring around the chevron while it has keyboard focus. + #[style(focus)] + focus: FocusLook, } /// The column a dock's floating window holds. The window's title row is the column's bar: it shows the column's @@ -511,8 +514,8 @@ impl Component for FloatingGroup<'_> { type Style = FloatingGroupStyle; type Output = FloatingGroupOutput; - fn style(_: &StyleCx<'_>, _: FloatingGroupStyleKey) -> FloatingGroupStyle { - FloatingGroupStyle::default() + fn style(cx: &StyleCx<'_>, _: FloatingGroupStyleKey) -> FloatingGroupStyle { + FloatingGroupStyle { metrics: Metrics::new(), focus: cx.focus_look() } } fn sense(&self) -> Sense { diff --git a/crates/ui-panels/src/docking/group.rs b/crates/ui-panels/src/docking/group.rs index 29c277e8ac..1883782f46 100644 --- a/crates/ui-panels/src/docking/group.rs +++ b/crates/ui-panels/src/docking/group.rs @@ -21,7 +21,7 @@ use crate::runtime::env::Label; use crate::runtime::focus::{self, FocusRequest, FocusTarget}; use crate::runtime::memory::sub; use crate::sections::FooterBar; -use crate::style::{ComponentStyle, Corners, RuleLook, RuleWidth, StyleColor, StyleCx, SurfaceKind, TypeRole}; +use crate::style::{ComponentStyle, Corners, FocusLook, RuleLook, RuleWidth, StyleColor, StyleCx, SurfaceKind, TypeRole}; use crate::tabs::{PanelTabLook, Tab, TabKind, TabStrip, TornOff}; use craft_behavior::{CursorIcon, CursorPriority, DockSide, IconId, Metrics, PartKey, PressPhase, Sense, keyed}; use craft_theme::{ControlSize, GripMark, GripShape, Tokens}; @@ -37,6 +37,9 @@ use std::sync::Arc; pub struct GroupGripStyle { #[style(metrics)] metrics: Metrics, + /// The ring the grip shows while it has keyboard focus. + #[style(focus)] + focus: FocusLook, /// The marks a grip with [`ticks`](GroupGrip::ticks) shows. marks: GripMarks, } @@ -166,7 +169,7 @@ impl Component for GroupGrip

{ type Output = GroupGripOutput; fn style(cx: &StyleCx<'_>, _: GroupGripStyleKey

) -> GroupGripStyle { - GroupGripStyle { metrics: Metrics::new(), marks: GripMarks::style(cx) } + GroupGripStyle { metrics: Metrics::new(), focus: cx.focus_look(), marks: GripMarks::style(cx) } } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { @@ -320,6 +323,9 @@ impl Outcome for DockGroupOutput { pub struct DockGroupStyle { #[style(metrics)] metrics: Metrics, + /// The ring around the drag band or the panel menu button while it has keyboard focus. + #[style(focus)] + focus: FocusLook, header: f32, sizes: DockSizes, } @@ -506,7 +512,7 @@ impl Component for fn style(cx: &StyleCx<'_>, _: DockGroupStyleKey

) -> DockGroupStyle { let sizes = DockSizes::style(cx); - DockGroupStyle { metrics: Metrics::new(), header: sizes.strip, sizes } + DockGroupStyle { metrics: Metrics::new(), focus: cx.focus_look(), header: sizes.strip, sizes } } fn sense(&self) -> Sense { diff --git a/crates/ui-panels/src/docking/iconic.rs b/crates/ui-panels/src/docking/iconic.rs index f33dc4557b..3dceece46a 100644 --- a/crates/ui-panels/src/docking/iconic.rs +++ b/crates/ui-panels/src/docking/iconic.rs @@ -18,7 +18,7 @@ use crate::menu::GroupItem; use crate::primitives::{IconSpec, TextRun}; use crate::runtime::env::{Env, EnvValue, Label, with_env}; use crate::runtime::focus::KeyUse; -use crate::style::{ComponentStyle, Corners, GlyphRole, RuleKind, StyleColor, StyleCx, SurfaceKind, SurfaceStyle}; +use crate::style::{ComponentStyle, Corners, FocusLook, GlyphRole, RuleKind, StyleColor, StyleCx, SurfaceKind, SurfaceStyle}; use crate::tabs::PanelTabLook; use craft_behavior::dock::{Column, DockEdge, Group}; use craft_behavior::overlay::Side; @@ -177,6 +177,9 @@ impl Outcome for IconicColumnOutput

{ pub struct IconicColumnStyle { #[style(metrics)] metrics: Metrics, + /// The ring around the chevron, the icon cell or the edge divider that has keyboard focus. + #[style(focus)] + focus: FocusLook, sizes: IconicSizes, /// The ink of a panel's name in a wide column. name: StyleColor, @@ -549,6 +552,7 @@ impl Component for IconicColumn<'_, P> { let colors = &cx.tokens.color; IconicColumnStyle { metrics: Metrics::new(), + focus: cx.focus_look(), sizes: IconicSizes::style(cx), name: cx.color(colors.text.normal), name_open: cx.color(colors.accent.text), diff --git a/crates/ui-panels/src/docking/mod.rs b/crates/ui-panels/src/docking/mod.rs index cf6341c92c..eb3e4cb42b 100644 --- a/crates/ui-panels/src/docking/mod.rs +++ b/crates/ui-panels/src/docking/mod.rs @@ -75,7 +75,7 @@ use crate::runtime::commands::CommandRef; use crate::runtime::env::Label; use crate::runtime::focus::KeyUse; use crate::runtime::memory::sub; -use crate::style::{ComponentStyle, StyleCx}; +use crate::style::{ComponentStyle, FocusLook, StyleCx}; use craft_behavior::dock::{DockEdge, Layout, Node, SplitSize}; use craft_behavior::{Axis, EditEvents, Flow, Metrics, PartKey, Sense}; use divider::{Divider, Drag}; @@ -247,6 +247,9 @@ pub struct PanelDrag { pub struct SplitAreaStyle { #[style(metrics)] metrics: Metrics, + /// The ring around the handle that has keyboard focus. + #[style(focus)] + focus: FocusLook, /// The docking sizes: the dividers' gap, reach and key steps, and the groups' least extent and strip. sizes: DockSizes, } @@ -416,7 +419,7 @@ impl Component for SplitAr type Output = SplitAreaOutput

; fn style(cx: &StyleCx<'_>, _: SplitAreaStyleKey

) -> SplitAreaStyle { - SplitAreaStyle { metrics: Metrics::new(), sizes: DockSizes::style(cx) } + SplitAreaStyle { metrics: Metrics::new(), focus: cx.focus_look(), sizes: DockSizes::style(cx) } } fn sense(&self) -> Sense { diff --git a/crates/ui-panels/src/docking/stack.rs b/crates/ui-panels/src/docking/stack.rs index 7837c814ca..5be2dfa929 100644 --- a/crates/ui-panels/src/docking/stack.rs +++ b/crates/ui-panels/src/docking/stack.rs @@ -7,7 +7,7 @@ use crate::contract::{ Access, Common, Component, Container, ContainerProps, Cx, NoValue, Outcome, Pad, PaintUi, PartsCx, Role, Sizing, WordSetCx as _, }; use crate::runtime::focus::KeyUse; -use crate::style::{ComponentStyle, StyleCx}; +use crate::style::{ComponentStyle, FocusLook, StyleCx}; use craft_behavior::divider::{self as push, Segment}; use craft_behavior::{Axis, Metrics, PartKey, Sense}; use egui::{Rect, Response}; @@ -99,6 +99,9 @@ pub fn section_heights(sections: &[StackSection], total: f32, header: f32) -> Ve pub struct StackSectionsStyle { #[style(metrics)] metrics: Metrics, + /// The ring around the divider that has keyboard focus. + #[style(focus)] + focus: FocusLook, sizes: DockSizes, } @@ -189,7 +192,7 @@ impl Component for StackSections { type Output = StackSectionsOutput; fn style(cx: &StyleCx<'_>, _: StackSectionsStyleKey) -> StackSectionsStyle { - StackSectionsStyle { metrics: Metrics::new(), sizes: DockSizes::style(cx) } + StackSectionsStyle { metrics: Metrics::new(), focus: cx.focus_look(), sizes: DockSizes::style(cx) } } fn sense(&self) -> Sense { diff --git a/crates/ui-panels/src/sections/title_row.rs b/crates/ui-panels/src/sections/title_row.rs index 4ec7e6297c..855da1723a 100644 --- a/crates/ui-panels/src/sections/title_row.rs +++ b/crates/ui-panels/src/sections/title_row.rs @@ -14,7 +14,7 @@ use crate::menu::Menu; use crate::primitives::{IconSpec, Span, TextRun}; use crate::runtime::commands::CommandRef; use crate::runtime::env::{EnvValue, Label}; -use crate::style::{ComponentStyle, IconLook, Ink, StyleCx, TextLook, TypeRole}; +use crate::style::{ComponentStyle, FocusLook, IconLook, Ink, StyleCx, TextLook, TypeRole}; use craft_behavior::{CursorIcon, Flow, IconId, Metrics, Mixed, NavShape, PartKey, Roving, Sense}; use craft_theme::variant::Tone; use craft_theme::{ControlSize, Stateful, States}; @@ -186,6 +186,9 @@ pub struct TitleRowStyle { icon: IconLook, /// The kind at the band's end: caption text in the faint text colour. subtitle: TextLook, + /// The ring around the lead, command or action part that has keyboard focus. + #[style(focus)] + focus: FocusLook, /// The side of an action part: the control height, which is below a tall header's height. action: f32, /// Whether the row is a quiet title's text row (`QuietTitleRow::Text`), as tall as it must be to hold its title @@ -397,6 +400,7 @@ impl Component for TitleRow<'_> { let faint = cx.color(text.faint); TextLook::new(TypeRole::Caption, Stateful::new(faint).off(States::new(faint, faint, faint, disabled))) }, + focus: cx.focus_look(), action: cx.control().height, text_row, } diff --git a/crates/ui-panels/tests/it/focus/mod.rs b/crates/ui-panels/tests/it/focus/mod.rs index fd2552ac98..01b1ae5b24 100644 --- a/crates/ui-panels/tests/it/focus/mod.rs +++ b/crates/ui-panels/tests/it/focus/mod.rs @@ -3,6 +3,7 @@ pub(crate) mod dialog_tab; pub(crate) mod keys; pub(crate) mod press; +pub(crate) mod rings; pub(crate) mod roving; pub(crate) mod scopes; diff --git a/crates/ui-panels/tests/it/focus/rings.rs b/crates/ui-panels/tests/it/focus/rings.rs new file mode 100644 index 0000000000..ab8d4b073b --- /dev/null +++ b/crates/ui-panels/tests/it/focus/rings.rs @@ -0,0 +1,160 @@ +//! The focus ring of the panel family's focusable parts: Tab to each one and the paint step rings it, as it rings a +//! button. + +use craft_behavior::dock::{Column, DockEdge, Group}; +use craft_behavior::{Axis, PartKey}; +use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::docking::stack::{StackSection, StackSections}; +use craft_ui::docking::{ + DockColumn, DockGroup, DragUnit, FloatingGroup, GroupGrip, IconicColumn, PanelKey, PanelSpec, PanelTitle, SplitArea, Titles, WindowColumn, +}; +use craft_ui::menu::PanelRow; +use craft_ui::sections::{HeaderAction, Panel, TitleRow}; +use craft_ui::testing::memory::global; +use craft_ui::testing::{Fixture, trial}; +use egui::{Key, Modifiers, Pos2, Rect, Shape, Vec2}; + +/// Presses Tab until a stop that `wanted` accepts holds the focus, and returns the rect of the focused part, or of +/// the focused component when the stop is a whole component. Panics when Tab never reaches one. +fn tab_to(fixture: &mut Fixture, wanted: impl Fn(Option) -> bool) -> Rect { + for _ in 0..16 { + fixture.key(Key::Tab, Modifiers::NONE).settle(); + let Some((owner, part)) = fixture.focused_stop() else { continue }; + if !wanted(part.map(|part| part.key)) { + continue; + } + let rect = match part { + Some(part) => fixture.part_rect(owner, part), + None => fixture.node_of(owner).map(|target| target.rect), + }; + return rect.unwrap_or_else(|| panic!("the focused stop {owner:?} {part:?} has a rect")); + } + panic!("Tab never reached the wanted stop"); +} + +/// Tab to the part `key`. +fn tab_to_part(fixture: &mut Fixture, key: &'static str) -> Rect { + tab_to(fixture, |part| part == Some(PartKey::new(key))) +} + +/// Whether the latest frame paints a focus ring (a rectangle stroke at least two points wide) around `target`. +fn ringed(fixture: &mut Fixture, target: Rect) -> bool { + fixture.frame_shapes().iter().any(|shape| match shape { + Shape::Rect(rect) if rect.stroke.width >= 2.0 => rect.rect.expand(1.0).contains_rect(target), + _ => false, + }) +} + +fn spec(panel: &u8) -> PanelSpec { + PanelSpec::new(["Brushes", "Swatches", "History"].get(usize::from(*panel)).copied().unwrap_or("Panel")) +} + +/// A title row's disclosure lead and its action buttons are rung when Tab reaches them. +#[trial] +fn a_title_rows_focused_lead_and_action_show_the_focus_ring() { + let mut fixture = Fixture::new(|ui| { + let mut open = true; + TitleRow::new("Layers").disclosure(Bind::bound(&mut open)).show(ui); + }); + fixture.settle(); + let lead = tab_to_part(&mut fixture, "lead"); + assert!(ringed(&mut fixture, lead), "the focused disclosure lead {lead:?} is ringed"); + let mut fixture = Fixture::new(|ui| { + TitleRow::new("Layers").actions([HeaderAction::Close]).show(ui); + }); + fixture.settle(); + let action = tab_to_part(&mut fixture, "action"); + assert!(ringed(&mut fixture, action), "the focused action {action:?} is ringed"); +} + +/// A group grip and a docked group's drag band and panel menu button are rung when Tab reaches them. +#[trial] +fn a_focused_group_grip_drag_band_and_panel_menu_button_show_the_focus_ring() { + let mut fixture = Fixture::new(|ui| { + let strip = Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(60.0, 16.0)); + GroupGrip::new(DragUnit::Group(1usize), PanelKey::new(1)).label("Move tab group").show_at(ui, strip); + }); + fixture.settle(); + let grip = tab_to(&mut fixture, |part| part.is_none()); + assert!(ringed(&mut fixture, grip), "the focused grip {grip:?} is ringed"); + let group = || { + let panels = vec![PanelTitle::new(PanelKey::new(1), "Layers")]; + Fixture::new(move |ui| { + let group = DockGroup::new(1usize, Titles::from_content(panels.clone()), panels[0].key).menu(Vec::::new); + let _ = group.show_at(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(300.0, 240.0)), |_, _| ()); + }) + }; + for part in ["grip", "panel_menu"] { + let mut fixture = group(); + fixture.settle(); + let rect = tab_to_part(&mut fixture, part); + assert!(ringed(&mut fixture, rect), "the group's focused {part} part {rect:?} is ringed"); + } +} + +/// A docked column's chevron and the divider between its groups are rung when Tab reaches them. +#[trial] +fn a_dock_columns_focused_chevron_and_divider_show_the_focus_ring() { + for part in ["chevron", "divider"] { + let mut fixture = Fixture::new(|ui| { + let column = Column::new(vec![Group::new(vec![0, 1], 0), Group::new(vec![2], 0)]); + DockColumn::new("Panels", global("dock column", "focus ring trial"), &column, &spec).show_groups(ui, |_, rect, ui, _| { + Panel::new("Panel").show_at(ui, rect, |_, _| ()); + }); + }); + fixture.room(Vec2::new(240.0, 400.0)).settle(); + let rect = tab_to_part(&mut fixture, part); + assert!(ringed(&mut fixture, rect), "the column's focused {part} part {rect:?} is ringed"); + } +} + +/// A dock window's chevron is rung when Tab reaches it. +#[trial] +fn a_dock_windows_focused_chevron_shows_the_focus_ring() { + let mut fixture = Fixture::new(|ui| { + let rect = Rect::from_min_size(Pos2::new(40.0, 40.0), Vec2::new(240.0, 180.0)); + FloatingGroup::new("Layers", rect).column(WindowColumn::expanded(DockEdge::Start)).show_group(ui, |_, _| ()); + }); + fixture.settle(); + let chevron = tab_to_part(&mut fixture, "chevron"); + assert!(ringed(&mut fixture, chevron), "the window's focused chevron {chevron:?} is ringed"); +} + +/// A collapsed column's chevron and icon cells are rung when Tab reaches them. +#[trial] +fn an_iconic_columns_focused_chevron_and_cell_show_the_focus_ring() { + for part in ["chevron", "cell"] { + let mut fixture = Fixture::new(|ui| { + let column = Column::new(vec![Group::new(vec![0, 1], 0)]); + let mut open = None; + IconicColumn::new("Panels", global("iconic column", "focus ring trial"), &column, Bind::bound(&mut open), &spec) + .show_flyouts(ui, |_, _, _, _| None); + }); + fixture.room(Vec2::new(48.0, 300.0)).settle(); + let rect = tab_to_part(&mut fixture, part); + assert!(ringed(&mut fixture, rect), "the column's focused {part} part {rect:?} is ringed"); + } +} + +/// The handle between two docked areas and the divider between two stacked sections are rung when Tab reaches them. +#[trial] +fn a_focused_split_handle_and_stack_divider_show_the_focus_ring() { + use craft_behavior::dock::{Layout, Node, SplitSize}; + use craft_ui::contract::SizeLimits; + use craft_ui::docking::tree::FrameRules; + let mut fixture = Fixture::new(|ui| { + let root = Node::split(Axis::X, SplitSize::Ratio(0.5), Node::tabs(vec![0usize], 0), Node::tabs(vec![1usize], 0)); + let mut layout = Layout::new(Some(root), Vec::new()); + let limits = |_: &usize| SizeLimits::default(); + SplitArea::new("Panels", Bind::bound(&mut layout), FrameRules::new(&limits)).show(ui, |_, _| ()); + }); + fixture.room(Vec2::new(400.0, 300.0)).settle(); + let handle = tab_to_part(&mut fixture, "handle"); + assert!(ringed(&mut fixture, handle), "the focused split handle {handle:?} is ringed"); + let mut fixture = Fixture::new(|ui| { + StackSections::new(vec![StackSection::new(true), StackSection::new(true)]).label("Sections").show(ui, |_, _| ()); + }); + fixture.room(Vec2::new(240.0, 300.0)).settle(); + let divider = tab_to_part(&mut fixture, "divider"); + assert!(ringed(&mut fixture, divider), "the focused stack divider {divider:?} is ringed"); +}