diff --git a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs index 41aff4c56..9fc8a985c 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs @@ -933,8 +933,13 @@ fn a_typed_value_past_a_rows_range_is_clamped_and_the_host_takes_one_range_alert let mut fixture = host_fixture(&host, &twice); host.borrow_mut().open(HostedParamDialog::new(fixture.ctx(), "light", DialogFields::new(), light)); fixture.idle(6); + // The number field named `label`: a range row's slider, before it on the line, carries the same name. let enter = |fixture: &mut Fixture, label: &str, text: &str| { - let field = fixture.by_label(label).unwrap_or_else(|| panic!("the {label} field")); + let field = fixture + .by_role(Role::SPIN_BUTTON) + .into_iter() + .find(|target| fixture.tree().name_of(target.node) == Some(label)) + .unwrap_or_else(|| panic!("the {label} field")); fixture.click(&field).settle(); fixture.key(Key::A, Modifiers::COMMAND).type_text(text); fixture.key(Key::Tab, Modifiers::NONE).settle(); diff --git a/crates/ui-widgets/src/form/slider_field.rs b/crates/ui-widgets/src/form/slider_field.rs index 9ee3a8969..d7c739b21 100644 --- a/crates/ui-widgets/src/form/slider_field.rs +++ b/crates/ui-widgets/src/form/slider_field.rs @@ -53,7 +53,8 @@ pub struct SliderFieldStyle { /// The step moves the slider's keys and wheel and the field's keys, wheel and scrub; typed values snap to it unless /// `typed_decimals` sets their independent precision. Without a step the field steps by a hundredth on a range 10 wide /// or less and by 1 on a longer one, and by default rounds typed values to the places that step needs. The field and -/// then the slider are Tab stops. A log or power scale curves the +/// the slider are Tab stops in the order they are read: the field and then the slider when stacked, the slider and then +/// the field when inline. A log or power scale curves the /// slider's track only: the field, typing and steps stay linear. #[derive(crate::contract::Component, crate::contract::Props)] pub struct SliderField<'a> { @@ -61,8 +62,8 @@ pub struct SliderField<'a> { common: Common, value: Bind<'a, f64>, range: RangeInclusive, - /// Whether the caption and the number field share a line over the slider, or the number field comes first on the - /// slider's line for a row labelled by an enclosing `Field`. + /// Whether the caption and the number field share a line over the slider, or the slider and then the number field + /// share one line for a row labelled by an enclosing `Field`. #[prop] layout: SliderFieldLayout, /// How positions along the slider's track map to values. @@ -404,27 +405,35 @@ impl Component for SliderField<'_> { }; let flow = Flow::new(cx.rect, cx.env.direction().is_rtl()); let place = |logical: Rect| flow.to_physical(logical.translate(cx.rect.min.to_vec2())); - // The field is shown first, so it is the first Tab stop. + // The parts are shown in the order they are read, which is their Tab order: the field's line comes first when + // stacked, and the slider comes before the field on the inline line. // The group has no stop, so its tooltip, and with it the reason it is disabled, is carried by the first stop. let face = self.track_face.take(); let bind = self.value.reborrow(); let field_view = look.field(&name, shown, Some(bind.check_of())); - let field_view = match cx.resolved.tooltip { - Some(tooltip) => field_view.tooltip(tooltip.clone()), - None => field_view, - }; - let typed = match self.layout { - // The label step puts the caption at the line's start and the field in the number cell at its end. - SliderFieldLayout::Stacked => { - ui.labelled("field", place(field), name.clone(), SideLabel::new().layout(FieldLayout::Line).scrub(true), field_view) - } - SliderFieldLayout::Inline => ui.child("field", place(field), field_view), - }; - self.out_of_range = typed.out_of_range.clone(); // A read-only field's track is read-only too: no press on it edits the value, whatever `track_press` is, and an // inert composite arms no first-Down gesture in its child for a later writable release. let slider = look.slider(&name, shown, Some(bind.check_of()), face).read_only(cx.resolved.read_only); - let dragged = ui.child("slider", place(track), slider); + let (typed, dragged) = match self.layout { + // The label step puts the caption at the line's start and the field in the number cell at its end. + SliderFieldLayout::Stacked => { + let field_view = match cx.resolved.tooltip { + Some(tooltip) => field_view.tooltip(tooltip.clone()), + None => field_view, + }; + let typed = ui.labelled("field", place(field), name.clone(), SideLabel::new().layout(FieldLayout::Line).scrub(true), field_view); + (typed, ui.child("slider", place(track), slider)) + } + SliderFieldLayout::Inline => { + let slider = match cx.resolved.tooltip { + Some(tooltip) => slider.tooltip(tooltip.clone()), + None => slider, + }; + let dragged = ui.child("slider", place(track), slider); + (ui.child("field", place(field), field_view), dragged) + } + }; + self.out_of_range = typed.out_of_range.clone(); let start = *self.range.start(); cx.adopt(&typed, &start, |value, mine| { if let Some(value) = value { diff --git a/crates/ui-widgets/tests/it/form.rs b/crates/ui-widgets/tests/it/form.rs new file mode 100644 index 000000000..945dadd61 --- /dev/null +++ b/crates/ui-widgets/tests/it/form.rs @@ -0,0 +1,96 @@ +//! `SliderField`: Tab visits its slider and its number field in the order a reader meets them on the row. + +use craft_ui::contract::{Bind, Props as _, Role, Show as _, ShowContainer as _, TooltipSpec}; +use craft_ui::form::{SliderField, SliderFieldLayout}; +use craft_ui::layout_components::Row; +use craft_ui::runtime::env::Direction; +use craft_ui::testing::{Fixture, trial}; +use craft_ui::text::NumberField; +use egui::{Key, Modifiers}; + +/// A focused control: its role and its name. +type Stop = (Option, Option); + +/// The control keyboard focus is on. +fn focused(fixture: &Fixture) -> Stop { + let tree = fixture.tree(); + let key = tree.focused_key(); + (tree.role_of(key), tree.name_of(key).map(str::to_owned)) +} + +/// A row that holds a number field, a slider field laid out as `layout` and another number field, under `direction`. +/// A disabled slider field carries a reason, so its first part stays a Tab stop. +fn row(layout: SliderFieldLayout, direction: Direction, enabled: bool) -> Fixture { + let mut fixture = Fixture::new(move |ui| { + Row::new().show(ui, |ui, _| { + let mut before = Some(1.0); + let mut size = 20.0; + let mut after = Some(2.0); + NumberField::new("Before", Bind::bound(&mut before)).range(0.0..=10.0).show(ui); + SliderField::new("Size", Bind::bound(&mut size), 0.0..=100.0) + .layout(layout) + .enabled(enabled) + .tooltip(TooltipSpec::new("The brush size").disabled_text("Pick a brush first")) + .show(ui); + NumberField::new("After", Bind::bound(&mut after)).range(0.0..=10.0).show(ui); + }); + }); + fixture.direction(direction).settle(); + fixture +} + +/// The controls `count` presses of Tab reach from the start of `fixture`, then those `count - 1` presses of Shift-Tab +/// reach back from the last one. +fn tabs(fixture: &mut Fixture, count: usize) -> (Vec, Vec) { + let mut forward = Vec::new(); + for _ in 0..count { + fixture.key(Key::Tab, Modifiers::NONE).settle(); + forward.push(focused(fixture)); + } + let mut back = vec![focused(fixture)]; + for _ in 1..count { + fixture.key(Key::Tab, Modifiers::SHIFT).settle(); + back.push(focused(fixture)); + } + (forward, back) +} + +/// A part of the slider field. +fn size(role: Role) -> Stop { + (Some(role), Some("Size".to_owned())) +} + +/// The number field `name` beside the slider field. +fn other(name: &str) -> Stop { + (Some(Role::SPIN_BUTTON), Some(name.to_owned())) +} + +/// In both directions, Tab goes from the field before the slider field to its slider, then its number field, then the +/// field after it; Shift-Tab goes back the same way. In the inline layout the slider comes first on the line; in the +/// stacked one the number field is on the caption line above the slider. +#[trial] +fn tab_visits_a_slider_fields_parts_in_reading_order() { + for (layout, [first, second]) in [ + (SliderFieldLayout::Inline, [size(Role::SLIDER), size(Role::SPIN_BUTTON)]), + (SliderFieldLayout::Stacked, [size(Role::SPIN_BUTTON), size(Role::SLIDER)]), + ] { + let order = vec![other("Before"), first, second, other("After")]; + let reversed: Vec = order.iter().rev().cloned().collect(); + for direction in [Direction::Ltr, Direction::Rtl] { + let (forward, back) = tabs(&mut row(layout, direction, true), order.len()); + assert_eq!(forward, order, "Tab order of a {layout:?} slider field under {direction:?}"); + assert_eq!(back, reversed, "Shift-Tab order of a {layout:?} slider field under {direction:?}"); + } + } +} + +/// A disabled slider field's reason is carried by the part read first, which stays the one Tab stop of the field: the +/// slider on the inline line, the number field when stacked. +#[trial] +fn a_disabled_slider_fields_reason_is_on_the_part_read_first() { + for (layout, first) in [(SliderFieldLayout::Inline, size(Role::SLIDER)), (SliderFieldLayout::Stacked, size(Role::SPIN_BUTTON))] { + let order = vec![other("Before"), first, other("After")]; + let (forward, _) = tabs(&mut row(layout, Direction::Ltr, false), order.len()); + assert_eq!(forward, order, "Tab order of a disabled {layout:?} slider field"); + } +} diff --git a/crates/ui-widgets/tests/it/main.rs b/crates/ui-widgets/tests/it/main.rs index c9fd20c1d..ba8cebcd6 100644 --- a/crates/ui-widgets/tests/it/main.rs +++ b/crates/ui-widgets/tests/it/main.rs @@ -13,6 +13,7 @@ pub(crate) mod contract; pub(crate) mod disabled_fade; pub(crate) mod field_text_centred; pub(crate) mod focus_ring; +pub(crate) mod form; pub(crate) mod keys; pub(crate) mod label; pub(crate) mod layer; diff --git a/crates/ui-widgets/tests/it/value_drag/primary_down.rs b/crates/ui-widgets/tests/it/value_drag/primary_down.rs index c38fdf316..e49fe3ea6 100644 --- a/crates/ui-widgets/tests/it/value_drag/primary_down.rs +++ b/crates/ui-widgets/tests/it/value_drag/primary_down.rs @@ -140,7 +140,8 @@ impl Rig { Self { fixture, state, fields } } fn geometry(&mut self, row: usize) -> Geometry { - for _ in 0..((row + 1) * if self.fields { 2 } else { 1 }) { + // An inline slider field's slider is its first stop, before its number field. + for _ in 0..(if self.fields { row * 2 + 1 } else { row + 1 }) { self.fixture.key(Key::Tab, Modifiers::NONE).settle(); } let owner = self.fixture.focused_stop().expect("naturally focused real Slider child").0;