diff --git a/crates/automation/src/dialogs.rs b/crates/automation/src/dialogs.rs index af05bd59e..398c917c5 100644 --- a/crates/automation/src/dialogs.rs +++ b/crates/automation/src/dialogs.rs @@ -33,14 +33,15 @@ pub fn inspect(host: &DialogHost) -> Value { /// Runs a `ui.dialog.*` method on `host`; `None` when `method` is not one of them. /// -/// - `ui.dialog.set {dialog, field, value}` sets one field ([`DialogHost::set_json_field`]). +/// - `ui.dialog.set {dialog, field, value}` sets one field ([`DialogHost::set_json_field`]); `{dialog, fields: [{field, +/// value?}]}` sets a list of at most [`MAX_DIALOG_FIELDS`], all or none ([`DialogHost::set_json_fields`]). /// - `ui.dialog.confirm {dialog, choice?}` answers OK, as the default button would, after setting the `choice` field /// when one is given (which button of a prompt). A dialog that holds its OK files it for the app, which answers it. /// - `ui.dialog.cancel {dialog}` closes the dialog as Escape would: a standard prompt with a button for its Escape /// answer gives that answer, with its tag; any other dialog closes with its children with Cancel. pub fn dialog_method(host: &mut DialogHost, ctx: &Context, method: &str, params: &Value) -> Option> { let fields: &[&str] = match method { - "ui.dialog.set" => &["dialog", "field", "value"], + "ui.dialog.set" => &["dialog", "field", "value", "fields"], "ui.dialog.confirm" => &["dialog", "choice"], "ui.dialog.cancel" => &["dialog"], _ => return None, @@ -53,10 +54,18 @@ fn run(host: &mut DialogHost, ctx: &Context, method: &str, params: &Value, field let read = Params(¶ms); let id = dialog_id(host, ¶ms)?; match method { - "ui.dialog.set" => { - let field = read.str_field("field")?.ok_or("missing `field`")?; - host.set_json_field(ctx, id, field, params.get("value").cloned().unwrap_or(Value::Null))?; - } + "ui.dialog.set" => match params.get("fields") { + Some(list) => { + if params.get("field").is_some() || params.get("value").is_some() { + return Err("`fields` takes the place of `field` and `value`".to_owned()); + } + host.set_json_fields(ctx, id, &field_list(list)?)?; + } + None => { + let field = read.str_field("field")?.ok_or("missing `field`")?; + host.set_json_field(ctx, id, field, params.get("value").cloned().unwrap_or(Value::Null))?; + } + }, "ui.dialog.confirm" => { if let Some(choice) = params.get("choice") { host.set_json_field(ctx, id, "choice", choice.clone())?; @@ -84,6 +93,21 @@ fn run(host: &mut DialogHost, ctx: &Context, method: &str, params: &Value, field Ok(Value::Null) } +/// The `(field, value)` pairs of a `fields` list, checked whole before anything is set; a missing `value` is `null`. +fn field_list(list: &Value) -> Result, String> { + let items = list.as_array().ok_or("`fields` is not a list")?; + if items.len() > MAX_DIALOG_FIELDS { + return Err(format!("`fields` holds {} items; at most {MAX_DIALOG_FIELDS}", items.len())); + } + items + .iter() + .map(|item| { + let field = item.get("field").and_then(Value::as_str).ok_or("an item of `fields` has no string `field`")?; + Ok((field.to_owned(), item.get("value").cloned().unwrap_or(Value::Null))) + }) + .collect() +} + /// The open dialog `params.dialog` names: the id `inspect` lists, as a decimal string or a number, or `"top"` for the /// top dialog of the stack. pub fn dialog_id(host: &DialogHost, params: &Value) -> Result { diff --git a/crates/automation/tests/it/dialogs.rs b/crates/automation/tests/it/dialogs.rs index 1763a90f7..ad6986f09 100644 --- a/crates/automation/tests/it/dialogs.rs +++ b/crates/automation/tests/it/dialogs.rs @@ -1,6 +1,6 @@ //! `ui.dialog.*` on typed dialogs through one generic path. -use craft_automation::dialogs::{dialog_method, inspect}; +use craft_automation::dialogs::{MAX_DIALOG_FIELDS, dialog_method, inspect}; use craft_ui::contract::{Bind, SaveKey}; use craft_ui::dialog::{DialogHost, DialogResult, PromptChoice, PromptKind, StandardPrompt}; use craft_ui::patterns::form_dialogs::{DialogFields, HostedParamDialog, Param, ParamDialog, ParamKind}; @@ -124,6 +124,41 @@ fn param_dialog_automation_follows_its_rows() { assert_eq!((reopened.fields.number("radius"), reopened.fields.text("method")), (Some(7.0), Some("zoom"))); } +/// A `fields` list sets every field or none: a refused field in the middle leaves the dialog as it was and gives that +/// refusal; a list over the limit, an item without a string `field` and `field` beside `fields` are refused unread. +#[trial] +fn a_field_list_sets_every_field_or_none_and_has_a_limit() { + let ctx = Context::default(); + let mut host = DialogHost::new(); + host.open(HostedParamDialog::new(&ctx, "blur", DialogFields::new(), |fields| { + ParamDialog::new("blur", "Blur", Param::parse_list(r#"{"radius":0.1..1000=1,"method":"spin|zoom"}"#)).values(Bind::bound(fields)) + })); + let id = inspect(&host)[0]["id"].clone(); + let fields = |host: &DialogHost| inspect(host)[0]["fields"].clone(); + let set = |host: &mut DialogHost, params: Value| call(host, &ctx, "ui.dialog.set", params); + + let listed = json!([{"field": "radius", "value": 4.5}, {"field": "method", "value": "zoom"}]); + assert_eq!(set(&mut host, json!({"dialog": id, "fields": listed})), Ok(Value::Null)); + let settled = fields(&host); + assert_eq!((settled["radius"].as_f64(), &settled["method"]), (Some(4.5), &json!("zoom"))); + + let refused = json!([{"field": "radius", "value": 9}, {"field": "radius", "value": 5000}, {"field": "method", "value": "spin"}]); + assert!(set(&mut host, json!({"dialog": id, "fields": refused})).is_err_and(|e| e.contains("radius"))); + assert_eq!(fields(&host), settled, "a refusal in the middle sets nothing"); + + let too_many: Vec = (0..=MAX_DIALOG_FIELDS).map(|_| json!({"field": "radius", "value": 2})).collect(); + assert!(set(&mut host, json!({"dialog": id, "fields": too_many})).is_err()); + assert_eq!(fields(&host), settled, "a list over the limit sets nothing"); + + let unnamed = json!([{"field": "radius", "value": 9}, {"value": 1}]); + assert!(set(&mut host, json!({"dialog": id, "fields": unnamed})).is_err()); + assert_eq!(fields(&host), settled, "an item without a field sets nothing"); + + let both = json!({"dialog": id, "field": "radius", "value": 9, "fields": [{"field": "radius", "value": 8}]}); + assert!(set(&mut host, both).is_err()); + assert_eq!(fields(&host), settled, "`field` beside `fields` sets nothing"); +} + /// `"top"` names the top dialog of the stack: a child over its parent takes the field, the parent keeps its own; with /// nothing open the request says so. #[trial] diff --git a/crates/ui-patterns/src/patterns/form_dialogs/about.rs b/crates/ui-patterns/src/patterns/form_dialogs/about.rs index b5ed87f97..ffb038e4f 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/about.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/about.rs @@ -17,10 +17,10 @@ use crate::contract::{ ShowCollection as _, ShowContainer as _, clicked, }; use crate::dialog::{Dialog, DialogId, DialogOutput, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id}; -use crate::display::{Divider, Shade, Text}; +use crate::display::{Divider, Picture, PictureSize, Shade, Text}; use crate::layout::RowAlign; use crate::layout_components::{Row, Stack}; -use crate::list::ListRow; +use crate::list::{ListRow, ThumbSize}; use crate::pickers::Select; use crate::runtime::env::{Env, Label}; use crate::runtime::overlay::CloseReason; @@ -275,6 +275,9 @@ impl<'a> AboutDialog<'a> { } } +/// The texture key of the app's mark; the dialog shows one mark. +const ICON_KEY: u64 = 0xAB07_1C0E; + /// What the tabs read while the dialog shows them. struct Shown<'s> { words: &'s AboutWords, @@ -312,6 +315,13 @@ impl Shown<'_> { fn about(&self, ui: &mut Ui, link: &RefCell>) { let (words, info) = (self.words, self.info); Stack::new().show(ui, |ui, _| { + Row::new().align(RowAlign::Center).show(ui, |ui, _| { + if let Some(icon) = &info.icon { + let image = || (**icon).clone(); + Picture::new(info.app_name.clone(), ICON_KEY, &image).extent(PictureSize::Thumb(ThumbSize::Medium)).show(ui); + } + Text::new(info.app_name.clone()).role(TypeRole::Title).show(ui); + }); Text::new(info.blurb.clone()).show(ui); Text::new(words.version.fill(&[("version", info.version.as_str())])).show(ui); Row::new().align(RowAlign::Center).show(ui, |ui, _| { diff --git a/crates/ui-patterns/src/patterns/form_dialogs/about/credits.rs b/crates/ui-patterns/src/patterns/form_dialogs/about/credits.rs index a0fc9845f..7e4a84a07 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/about/credits.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/about/credits.rs @@ -79,9 +79,9 @@ impl AboutInfo { /// The application's mark, shown at the start of the About tab's header row. #[must_use] - pub fn icon(self, image: Arc) -> Self { - let _ = (self, image); - todo!("sh.K26") + pub fn icon(mut self, image: Arc) -> Self { + self.icon = Some(image); + self } } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/about/stories.rs b/crates/ui-patterns/src/patterns/form_dialogs/about/stories.rs index c4decf593..73e4077da 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/about/stories.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/about/stories.rs @@ -3,14 +3,27 @@ use super::{AboutDialog, AboutInfo, AboutLink, AboutTab, Contributor, Credits, HostedAbout, ModelCredit}; use crate::dialog::stories::hosted; use crate::testing::{Drive, StoryData, StoryOutput, story}; -use egui::Ui; -use std::sync::LazyLock; +use egui::{Color32, ColorImage, Ui}; +use std::sync::{Arc, LazyLock}; -fn info(community: bool) -> AboutInfo { - let info = AboutInfo::new("PhotoCraft", "An open-source, native image editor written in Rust.", "0.42.1 (a1b2c3d, 2026-10-30)") +/// A diagonal gradient standing for the app's mark. +fn mark() -> Arc { + const SIDE: usize = 32; + let pixels = (0..SIDE * SIDE).map(|at| Color32::from_rgb((at % SIDE * 255 / SIDE) as u8, (at / SIDE * 255 / SIDE) as u8, 160)).collect(); + Arc::new(ColorImage::new([SIDE, SIDE], pixels)) +} + +fn info(community: bool, with_mark: bool) -> AboutInfo { + let mut info = AboutInfo::new("PhotoCraft", "An open-source, native image editor written in Rust.", "0.42.1 (a1b2c3d, 2026-10-30)") .links([AboutLink::new("Website", "https://example.org"), AboutLink::new("Source", "https://example.org/source")]) .built_with("egui ยท wgpu"); - if community { info.community(AboutLink::new("Join us on Discord", "https://discord.example/join")) } else { info } + if community { + info = info.community(AboutLink::new("Join us on Discord", "https://discord.example/join")); + } + if with_mark { + info = info.icon(mark()); + } + info } const TABS: [(&str, AboutTab); 3] = [("About", AboutTab::About), ("Contributors", AboutTab::Contributors), ("Models", AboutTab::Models)]; @@ -36,12 +49,13 @@ static CREDITS: LazyLock = LazyLock::new(|| { fn about_dialog(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let tab = *data.choice("tab", AboutTab::About, &TABS); let community = *data.value("community_button", true); - hosted(ui, data, (tab, community), || HostedAbout::new(info(community), CREDITS.clone()).tab(tab)) + let mark = *data.value("mark", true); + hosted(ui, data, (tab, community, mark), || HostedAbout::new(info(community, mark), CREDITS.clone()).tab(tab)) } #[story(AboutDialog<'static>, covers = [], drive = Drive::Never)] fn about_dialog_contributors(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let tab = *data.choice("tab", AboutTab::Contributors, &TABS); let community = *data.value("community_button", true); - hosted(ui, data, (tab, community), || HostedAbout::new(info(community), CREDITS.clone()).tab(tab)) + hosted(ui, data, (tab, community), || HostedAbout::new(info(community, false), CREDITS.clone()).tab(tab)) } diff --git a/crates/ui-patterns/src/patterns/preferences/document/model.rs b/crates/ui-patterns/src/patterns/preferences/document/model.rs index e295a0bae..80db08cfd 100644 --- a/crates/ui-patterns/src/patterns/preferences/document/model.rs +++ b/crates/ui-patterns/src/patterns/preferences/document/model.rs @@ -161,7 +161,7 @@ impl bool { diff --git a/crates/ui-patterns/src/patterns/preferences/document/texts.rs b/crates/ui-patterns/src/patterns/preferences/document/texts.rs index 4442ae44b..7eb4b1644 100644 --- a/crates/ui-patterns/src/patterns/preferences/document/texts.rs +++ b/crates/ui-patterns/src/patterns/preferences/document/texts.rs @@ -223,10 +223,8 @@ impl RowKind::Bool(*on), - // P3(shell-w5): sh.K32 (Shape::Int { range, unit }, Some(Value::Integer(n))) => RowKind::Int { value: *n, range: range.clone(), unit: *unit, unit_text: None }, (Shape::Float { range, decimals, unit }, Some(Value::Float(x))) => { - // P3(shell-w5): sh.K32 RowKind::Float { value: *x, range: range.clone(), decimals: *decimals, unit: *unit, unit_text: None } } (Shape::Text, Some(Value::String(text))) => RowKind::Text(text), diff --git a/crates/ui-patterns/src/patterns/preferences/model.rs b/crates/ui-patterns/src/patterns/preferences/model.rs index 8116523b6..d0d0301ab 100644 --- a/crates/ui-patterns/src/patterns/preferences/model.rs +++ b/crates/ui-patterns/src/patterns/preferences/model.rs @@ -246,9 +246,11 @@ impl<'a> RowKind<'a> { /// A translated unit shown after an integer or number field, for quantities with no `UnitKind`; other kinds /// ignore it. #[must_use] - pub fn unit_text(self, text: &'a str) -> Self { - let _ = (self, text); - todo!("sh.K32") + pub fn unit_text(mut self, text: &'a str) -> Self { + if let RowKind::Int { unit_text, .. } | RowKind::Float { unit_text, .. } = &mut self { + *unit_text = Some(text); + } + self } /// What an integer or number field counts, shown after it, or what the ids of a choice count (the choice labels its diff --git a/crates/ui-patterns/src/patterns/preferences/rows.rs b/crates/ui-patterns/src/patterns/preferences/rows.rs index c97416d03..80165a647 100644 --- a/crates/ui-patterns/src/patterns/preferences/rows.rs +++ b/crates/ui-patterns/src/patterns/preferences/rows.rs @@ -7,7 +7,7 @@ use crate::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowConta use crate::form::{Field, Help}; use crate::layout_components::{Row, Stack}; use crate::pickers::Select; -use crate::runtime::env::{Label, UnitKind, Units}; +use crate::runtime::env::{Label, LengthUnits, UnitKind, Units}; use crate::runtime::requests::ColorRequest; use crate::text::{HexField, NumberField, TextField}; use craft_behavior::NumberCodec; @@ -31,8 +31,9 @@ pub(super) enum Kind { Bool(bool), Choice { value: String, options: Vec>, unit: Option }, Color(Rgba), - Int { value: i64, range: RangeInclusive, unit: Option }, - Float { value: f64, range: RangeInclusive, decimals: u8, step: Option, unit: Option }, + Int { value: i64, range: RangeInclusive, unit: Option, unit_text: Option