From f846294322ecaeba4c41e463fdb9cd147edca1b9 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 06:52:28 +0700 Subject: [PATCH] ui-contract: a scope's initial focus on a roving group arrives at the group's arrival member, as Tab does; SwatchPair makes its active well the arrival, so opening a panel on the pair activates no other well --- crates/color-ui/src/ui/pair.rs | 2 ++ crates/color-ui/tests/it/pair.rs | 35 +++++++++++++++++++-- crates/ui-contract/src/runtime/focus/nav.rs | 14 +++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/crates/color-ui/src/ui/pair.rs b/crates/color-ui/src/ui/pair.rs index d181d6c53e..43395b8e0c 100644 --- a/crates/color-ui/src/ui/pair.rs +++ b/crates/color-ui/src/ui/pair.rs @@ -442,6 +442,8 @@ impl Component for SwatchPair<'_, P> { let shape = if cx.style.round { HitShape::circle(at.center(), at.width() / 2.0) } else { HitShape::Rect(at) }; let part = cx.part(WELL.at(i as u64), shape).sense(sense).priority(HitPriority::new(HitLevel::Body, u8::from(front))).flags(flags).focusable(); + // Focus arrives at the active well, so arriving makes no other well active. + let part = if well == active { part.selected() } else { part }; if self.popover { part.pointer_only("Enter opens the picker"); } diff --git a/crates/color-ui/tests/it/pair.rs b/crates/color-ui/tests/it/pair.rs index 199a20aad9..17de531e2c 100644 --- a/crates/color-ui/tests/it/pair.rs +++ b/crates/color-ui/tests/it/pair.rs @@ -1,4 +1,5 @@ -//! `SwatchPair` wells take the theme's well shape (`shape.wells`): a round well is round to the pointer too. +//! `SwatchPair` wells take the theme's well shape (`shape.wells`): a round well is round to the pointer too. Focus +//! arrives at the active well, so a panel opening with the pair first activates no other well. use std::cell::{Cell, RefCell}; use std::rc::Rc; @@ -6,7 +7,7 @@ use std::rc::Rc; use craft_behavior::Mixed; use craft_color_ui::ui::{PairWell, SwatchPair, SwatchPairOutput, Well}; use craft_theme::{Srgba, ThemeId}; -use craft_ui::contract::{Bind, Role, Show as _}; +use craft_ui::contract::{Bind, Role, Show as _, ShowContainer as _}; use craft_ui::testing::{Fixture, trial}; use egui::{Rect, Ui, Vec2, vec2}; @@ -115,3 +116,33 @@ fn the_well_corner_follows_the_theme_well_shape() { assert_eq!(clicks("craft.adwaita"), [Some(Well::Primary), None], "round wells"); assert_eq!(clicks("craft.pro"), [Some(Well::Primary), Some(Well::Primary)], "square wells"); } + +/// A popover that opens with the pair as its first stop puts the focus on the active well, as Tab into the pair does: +/// the arrival makes no other well active (a Color panel opened over the Stroke chip leaves Stroke active). +#[trial] +fn a_popover_opening_on_the_pair_focuses_the_active_well_and_activates_none() { + use craft_ui::buttons::Button; + use craft_ui::popover::Popover; + let activated = Rc::new(Cell::new(false)); + let seen = activated.clone(); + let mut open = false; + let mut fixture = Fixture::new(move |ui| { + let button = Button::new("Colours").show(ui).response; + if button.clicked() { + open = true; + } + let _ = Popover::new("Colours", Bind::bound(&mut open)).opened_by(&button).show(ui, |ui, _| { + let wells = [PairWell::new("Fill", Srgba::rgb(0.1, 0.3, 0.8)), PairWell::new("Stroke", Srgba::rgb(0.0, 0.0, 0.0))]; + let shown = SwatchPair::<()>::new("Fill and stroke", wells, Bind::given(Well::Secondary)).show(ui); + seen.set(seen.get() || !shown.choice.events.is_empty()); + }); + }); + fixture.room(vec2(400.0, 300.0)).settle(); + let opener = fixture.by_label("Colours").expect("the opener"); + fixture.click(&opener).settle(); + let tree = fixture.tree(); + let focused = craft_ui::testing::access::focused_key(tree); + assert_eq!(tree.role_of(focused), Some(Role::RADIO_BUTTON), "a well takes the focus"); + assert_eq!(tree.toggled_of(focused), Some(Mixed::Same(true)), "the focus arrives at the active well"); + assert!(!activated.get(), "the arrival activates no well"); +} diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index db1c30fe68..c38e1c7e92 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -649,9 +649,19 @@ pub(super) fn initial_focus(state: &mut State, frame: u64) -> Vec { return Vec::new(); } let stops = &state.stops[first.min(state.stops.len())..]; + // A first stop in a roving group arrives at the group's arrival member, as Tab does. + let lists = state.this(); + let entry = stops.first().map(|rec| { + rec.stop + .group + .and_then(|group| lists.group(group)) + .filter(|group| !group.row) + .and_then(|group| lists.arrival(&group)) + .unwrap_or_else(|| rec.clone()) + }); let pick = match policy.initial { - Initial::First => stops.first(), - Initial::Default => stops.iter().find(|rec| rec.stop.default).or_else(|| stops.first()), + Initial::First => entry.as_ref(), + Initial::Default => stops.iter().find(|rec| rec.stop.default).or(entry.as_ref()), Initial::FirstField => stops.iter().find(|rec| rec.stop.text).or_else(|| stops.iter().find(|rec| rec.stop.default)), Initial::Key(name) => stops.iter().find(|rec| rec.stop.name == Some(name)), _ => None,