mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Preferences: host-labelled rows show their rule's help and follow its enabled state
This commit is contained in:
@@ -179,7 +179,13 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(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, _| {
|
||||
|
||||
@@ -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<PageSpec<'_>> {
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user