diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs index 4852f5ce9..31e97a7e8 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs @@ -132,7 +132,8 @@ impl Outcome for ParamDialogOutput { /// buttons and a footer note. /// /// - Keys: Tab walks the fields, Enter is OK unless a field owns it, Escape cancels. Holding Alt turns Cancel into -/// Reset, which restores the values from when the dialog opened (or `reset_to`) and keeps the dialog open. +/// Reset, which restores the values from when the dialog opened, with `reset_to`'s over them, and keeps the dialog +/// open. Keys added since the open (a host's own data without a row) go. /// - Values: the `values` binding holds them; without one the dialog keeps its own between passes. The first pass /// fills the parameters' defaults and, with `remember`, the values saved by [`ParamResult::remember`]. /// - Preview: a dialog that `previews` has the Preview check box; the host renders from the reported values while @@ -181,8 +182,8 @@ pub struct ParamDialog<'a> { /// Whether the body is one collection that owns the scroll (a browse list): the dialog's own body scroll is off. #[prop] collection_body: bool, - /// The values Reset sets, instead of the values from when the dialog opened. Keys the target lacks keep their - /// value. + /// The values Reset sets over the values from when the dialog opened: keys it lacks take their opening value, and + /// keys added since the open go. #[prop] reset_to: Option, /// Where the preview pane sits. @@ -455,14 +456,16 @@ impl<'a> ParamDialog<'a> { .collection_body(collection_body) .closable(closable) .accept_stays_open(hold) - .answers(answers) - .reset_with(move |current, opened| { - let mut values = current.clone(); - for (name, value) in reset_to.as_ref().unwrap_or(opened).iter() { + .answers(answers); + if let Some(reset_to) = reset_to { + form = form.reset_with(move |_, opened| { + let mut values = opened.clone(); + for (name, value) in reset_to.iter() { values.set(name, value.clone()); } values }); + } if let Some(reason) = ok_reason { form = form.ok_reason(reason); } diff --git a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs index 2ad66a6cc..ee1778dba 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs @@ -414,9 +414,10 @@ fn enter_on_a_focused_cancel_cancels_the_form() { assert!(matches!(closed, Some((reason, false)) if !matches!(reason, CloseReason::Activated | CloseReason::Enter)), "Enter cancels: {closed:?}"); } -// Alt turns Cancel into Reset, which sets the `reset_to` keys and leaves the keys `reset_to` lacks as they are. +// Alt turns Cancel into Reset, which restores the opening values with the `reset_to` ones over them: a key `reset_to` +// lacks returns to its opening value, and a key without a row added since the open goes. #[trial] -fn alt_reset_sets_the_reset_to_values_and_keeps_the_other_keys() { +fn alt_reset_restores_the_opening_values_with_the_reset_to_values_over_them() { let values = Rc::new(RefCell::new(DialogFields::new())); let shown = values.clone(); let mut fixture = Fixture::new(move |ui| { @@ -426,16 +427,20 @@ fn alt_reset_sets_the_reset_to_values_and_keeps_the_other_keys() { .show(ui); }); fixture.settle(); + let opening = values.borrow().bool("smooth"); let smooth = fixture.by_label("Smooth").expect("the smooth row has a check box"); fixture.click(&smooth).settle(); - assert_eq!(values.borrow().bool("smooth"), Some(true)); + assert_ne!(values.borrow().bool("smooth"), opening); + values.borrow_mut().set("points", FieldValue::Int(3)); + fixture.settle(); fixture.hold(Modifiers::ALT).idle(2); let reset = fixture.by_label("Reset").expect("Alt turns Cancel into Reset"); fixture.click(&reset).idle(2); fixture.hold(Modifiers::NONE).settle(); assert_eq!(values.borrow().number("radius"), Some(50.0), "Reset sets the reset_to value"); - assert_eq!(values.borrow().bool("smooth"), Some(true), "Reset keeps a key reset_to lacks"); + assert_eq!(values.borrow().bool("smooth"), opening, "a key reset_to lacks returns to its opening value"); + assert!(!values.borrow().contains("points"), "a key without a row set since the open goes"); assert!(fixture.by_label("Blur").is_some(), "Reset keeps the dialog open"); }