From 69ed953094d249f2dc769bf25a16ebe6350d0da5 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 10:18:40 +0000 Subject: [PATCH] Preferences: host-labelled rows show their rule's help and follow its enabled state --- .../src/patterns/preferences/rows.rs | 8 ++++- .../ui-patterns/tests/it/preferences/page.rs | 35 +++++++++++++++++++ crates/ui-widgets/src/form/field.rs | 18 +++++++--- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/crates/ui-patterns/src/patterns/preferences/rows.rs b/crates/ui-patterns/src/patterns/preferences/rows.rs index 46d5c5d62..bc03be521 100644 --- a/crates/ui-patterns/src/patterns/preferences/rows.rs +++ b/crates/ui-patterns/src/patterns/preferences/rows.rs @@ -179,7 +179,13 @@ pub(super) fn show_row(ui: &mut Ui, host: &mut H, } } Kind::CustomRows => { - Stack::new().id_salt(key).show(ui, |ui, _| host.custom_row(ui, key)); + // The host draws the rows and their labels; the rule's enabled state and help apply to them as to a field. + Stack::new().id_salt(key).enabled(row.enabled).show(ui, |ui, _| { + host.custom_row(ui, key); + if let Some(help) = &row.help { + Help::Text(help.clone()).show(ui); + } + }); } Kind::Custom => { field().show(ui, |ui, _| { diff --git a/crates/ui-patterns/tests/it/preferences/page.rs b/crates/ui-patterns/tests/it/preferences/page.rs index edf044554..bc3759740 100644 --- a/crates/ui-patterns/tests/it/preferences/page.rs +++ b/crates/ui-patterns/tests/it/preferences/page.rs @@ -1,5 +1,6 @@ //! `PreferencesPage` over a model with no draft: each edit reaches the model in the pass it lands in. +use craft_ui::buttons::Button; use craft_ui::contract::{Bind, Role, Show as _, ShowContainer as _}; use craft_ui::display::Text; use craft_ui::form::Field; @@ -141,3 +142,37 @@ fn a_host_labelled_custom_row_shows_its_label_once() { let fields = tree.keys().into_iter().filter(|key| tree.name_of(*key) == Some("Hosted") && tree.role_of(*key) == Some(Role::GROUP)).count(); assert_eq!(fields, 1, "the host's field is the row's only labelled group"); } + +/// A disabled host-labelled row with help: the host draws a button. +struct HelpedRows; + +impl PreferencesModel for HelpedRows { + fn pages(&self) -> Vec> { + let row = RowSpec::new("general.host", "Hosted", RowKind::CustomRows).help("Drawn with its note").enabled(false); + vec![PageSpec::new("general", "General", vec![row])] + } + + fn set(&mut self, _: &str, _: PrefValue) {} + + fn custom_row(&mut self, ui: &mut Ui, _: &str) { + Button::new("Hosted button").show(ui); + } + + fn custom_page(&mut self, _: &mut Ui, _: &str) {} + + fn reset_page(&mut self, _: &str) {} +} + +/// The rule's help shows under the rows the host draws, and a disabled rule disables them. +#[trial] +fn a_host_labelled_row_shows_its_rules_help_and_follows_its_enabled_state() { + let mut state = PreferencesState::default(); + let mut fixture = Fixture::new(move |ui| { + PreferencesPage::new(Bind::bound(&mut state)).show(ui, &mut HelpedRows); + }); + fixture.settle(); + let button = fixture.by_label("Hosted button").expect("the host's button"); + let help = fixture.texts().iter().find(|run| run.text == "Drawn with its note").map(|run| run.rect).expect("the help shows"); + assert!(help.top() >= button.rect.bottom(), "the help {help:?} sits under the host's rows {:?}", button.rect); + assert!(fixture.tree().is_disabled_key(button.node), "the disabled rule disables the host's button"); +} diff --git a/crates/ui-widgets/src/form/field.rs b/crates/ui-widgets/src/form/field.rs index 5f634c2fd..dee769d3d 100644 --- a/crates/ui-widgets/src/form/field.rs +++ b/crates/ui-widgets/src/form/field.rs @@ -21,6 +21,16 @@ pub enum Help { Status(Tone, Label), } +impl Help { + /// Shows the help line as a field shows it under its control. + pub fn show(&self, ui: &mut Ui) { + match self { + Help::Text(text) => Text::new(text.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui), + Help::Status(tone, text) => Text::new(text.clone()).role(TypeRole::Caption).tone(*tone).show(ui), + }; + } +} + /// The style of a field: no look of its own; the label step and the help line carry it. #[derive(Clone, Debug, Default, PartialEq, ComponentStyle)] #[non_exhaustive] @@ -116,10 +126,8 @@ impl Container for Field { Text::new(message.text.clone()).role(TypeRole::Caption).tone(tone).show(ui); return; } - match &self.help { - Some(Help::Text(text)) => Text::new(text.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui), - Some(Help::Status(tone, text)) => Text::new(text.clone()).role(TypeRole::Caption).tone(*tone).show(ui), - None => return, - }; + if let Some(help) = &self.help { + help.show(ui); + } } }