From deca5dd355e91badf779f6d055b09e3bdd1ca687 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 20 Nov 2026 13:33:37 -0500 Subject: [PATCH] Retry live preferences after custom edits --- .../src/patterns/preferences/body.rs | 3 +- .../src/patterns/preferences/document.rs | 12 ++-- .../patterns/preferences/document/model.rs | 10 +-- .../src/patterns/preferences/model.rs | 8 +-- .../src/patterns/preferences/rows.rs | 5 +- .../src/patterns/preferences/window.rs | 2 +- .../patterns/preferences/window/stories.rs | 8 ++- .../ui-patterns/tests/it/preferences/live.rs | 63 ++++++++++++++++++- 8 files changed, 91 insertions(+), 20 deletions(-) diff --git a/crates/ui-patterns/src/patterns/preferences/body.rs b/crates/ui-patterns/src/patterns/preferences/body.rs index 698ff5b84..7bca75294 100644 --- a/crates/ui-patterns/src/patterns/preferences/body.rs +++ b/crates/ui-patterns/src/patterns/preferences/body.rs @@ -87,6 +87,7 @@ fn shown_page(matcher: &Matcher, listed: &Listed, page: &PageSpec<'_>) -> ShownP pub(super) struct BodyOut { pub rows: RowsOut, pub reset: bool, + pub custom_changed: bool, } impl BodyOut { @@ -178,7 +179,7 @@ fn show_page( ) { Text::new(page.title.clone()).role(TypeRole::Title).show(ui); if page.custom { - Stack::new().show(ui, |ui, _| host.custom_page(ui, &page.id)); + Stack::new().show(ui, |ui, _| out.custom_changed |= host.custom_page(ui, &page.id)); } else if page.groups.is_empty() && !searching { Notice::new(Tone::Info, words.empty_page.clone()).layout(NoticeLayout::Centred).show(ui); } diff --git a/crates/ui-patterns/src/patterns/preferences/document.rs b/crates/ui-patterns/src/patterns/preferences/document.rs index 6a40f75e7..1695ece02 100644 --- a/crates/ui-patterns/src/patterns/preferences/document.rs +++ b/crates/ui-patterns/src/patterns/preferences/document.rs @@ -29,10 +29,14 @@ pub use texts::setting_label; /// live outside the struct (the appearance page writes the suite's theme selection). Every method has a default, so `()` /// is a document without any. pub trait CustomSettings { - /// Draws a [`RuleKind::Custom`] row's control; edits go into `draft`. - fn row(&mut self, _ui: &mut Ui, _key: &str, _draft: &mut SettingsDraft) {} - /// Draws a page listed with [`SettingsDocument::custom_page`]. - fn page(&mut self, _ui: &mut Ui, _page: &str, _draft: &mut SettingsDraft) {} + /// Draws a [`RuleKind::Custom`] row's control; return whether its own external state changed. + fn row(&mut self, _ui: &mut Ui, _key: &str, _draft: &mut SettingsDraft) -> bool { + false + } + /// Draws a custom page; return whether its own external state changed. + fn page(&mut self, _ui: &mut Ui, _page: &str, _draft: &mut SettingsDraft) -> bool { + false + } /// Dialogs opened from a custom row (a colour picker), drawn last in the window's viewport. A dialog's answer goes /// into `draft`. fn nested(&mut self, _ctx: &Context, _draft: &mut SettingsDraft) {} diff --git a/crates/ui-patterns/src/patterns/preferences/document/model.rs b/crates/ui-patterns/src/patterns/preferences/document/model.rs index a1bad68ef..7efbf03ad 100644 --- a/crates/ui-patterns/src/patterns/preferences/document/model.rs +++ b/crates/ui-patterns/src/patterns/preferences/document/model.rs @@ -108,12 +108,14 @@ impl bool { + let before = self.draft.clone(); + self.custom.row(ui, key, &mut self.draft) || self.draft != before } - fn custom_page(&mut self, ui: &mut Ui, page: &str) { - self.custom.page(ui, page, &mut self.draft); + fn custom_page(&mut self, ui: &mut Ui, page: &str) -> bool { + let before = self.draft.clone(); + self.custom.page(ui, page, &mut self.draft) || self.draft != before } fn differs(&self) -> bool { diff --git a/crates/ui-patterns/src/patterns/preferences/model.rs b/crates/ui-patterns/src/patterns/preferences/model.rs index 32e7a2b48..879d6dca3 100644 --- a/crates/ui-patterns/src/patterns/preferences/model.rs +++ b/crates/ui-patterns/src/patterns/preferences/model.rs @@ -303,10 +303,10 @@ pub trait PreferencesModel { fn set_color(&mut self, key: &str, color: Srgba) { self.set(key, PrefValue::Color(color.into())); } - /// Draws a [`RowKind::Custom`] row's control. - fn custom_row(&mut self, ui: &mut Ui, key: &str); - /// Draws a [`PageSpec::custom`] page. - fn custom_page(&mut self, ui: &mut Ui, page: &str); + /// Draws a [`RowKind::Custom`] row's control, returning whether its draft changed this pass. + fn custom_row(&mut self, ui: &mut Ui, key: &str) -> bool; + /// Draws a [`PageSpec::custom`] page, returning whether its draft changed this pass. + fn custom_page(&mut self, ui: &mut Ui, page: &str) -> bool; /// Whether the draft differs from the saved settings: enables Apply. fn differs(&self) -> bool; /// Whether OK may close the window. diff --git a/crates/ui-patterns/src/patterns/preferences/rows.rs b/crates/ui-patterns/src/patterns/preferences/rows.rs index 03ff8929a..f2359f5bb 100644 --- a/crates/ui-patterns/src/patterns/preferences/rows.rs +++ b/crates/ui-patterns/src/patterns/preferences/rows.rs @@ -75,6 +75,7 @@ impl ShownRow { #[derive(Default)] pub(super) struct RowsOut { pub edits: Vec<(String, PrefValue)>, + pub custom_changed: bool, /// The request of the swatch pressed this pass, keyed by the row. pub pick_color: Option, } @@ -177,11 +178,11 @@ pub(super) fn show_row(ui: &mut Ui, host: &mut H, } } Kind::Custom if label.as_str().is_empty() => { - Stack::new().id_salt(key).show(ui, |ui, _| host.custom_row(ui, key)); + Stack::new().id_salt(key).show(ui, |ui, _| out.custom_changed |= host.custom_row(ui, key)); } Kind::Custom => { field().show(ui, |ui, _| { - Stack::new().show(ui, |ui, _| host.custom_row(ui, key)); + Stack::new().show(ui, |ui, _| out.custom_changed |= host.custom_row(ui, key)); }); } } diff --git a/crates/ui-patterns/src/patterns/preferences/window.rs b/crates/ui-patterns/src/patterns/preferences/window.rs index 862432b31..7f4c13791 100644 --- a/crates/ui-patterns/src/patterns/preferences/window.rs +++ b/crates/ui-patterns/src/patterns/preferences/window.rs @@ -225,7 +225,7 @@ impl<'a> PreferencesWindow<'a> { pick_color = Some(request); } let edits = out.edits(); - if !edits.is_empty() || out.reset { + if !edits.is_empty() || out.reset || out.custom_changed || out.rows.custom_changed { session.work.error = None; } for (key, value) in edits { diff --git a/crates/ui-patterns/src/patterns/preferences/window/stories.rs b/crates/ui-patterns/src/patterns/preferences/window/stories.rs index 9cc136859..5cf5d277a 100644 --- a/crates/ui-patterns/src/patterns/preferences/window/stories.rs +++ b/crates/ui-patterns/src/patterns/preferences/window/stories.rs @@ -94,9 +94,13 @@ impl PreferencesModel for Demo { } } - fn custom_row(&mut self, _: &mut Ui, _: &str) {} + fn custom_row(&mut self, _: &mut Ui, _: &str) -> bool { + false + } - fn custom_page(&mut self, _: &mut Ui, _: &str) {} + fn custom_page(&mut self, _: &mut Ui, _: &str) -> bool { + false + } fn differs(&self) -> bool { self.draft != self.saved diff --git a/crates/ui-patterns/tests/it/preferences/live.rs b/crates/ui-patterns/tests/it/preferences/live.rs index 8d8052d27..73b499c74 100644 --- a/crates/ui-patterns/tests/it/preferences/live.rs +++ b/crates/ui-patterns/tests/it/preferences/live.rs @@ -25,8 +25,12 @@ impl PreferencesModel for Model { self.draft = value; } } - fn custom_row(&mut self, _: &mut Ui, _: &str) {} - fn custom_page(&mut self, _: &mut Ui, _: &str) {} + fn custom_row(&mut self, _: &mut Ui, _: &str) -> bool { + false + } + fn custom_page(&mut self, _: &mut Ui, _: &str) -> bool { + false + } fn differs(&self) -> bool { self.draft != self.saved } @@ -60,3 +64,58 @@ fn an_edit_applies_before_ok() { fixture.click(&ok).settle(); assert_eq!(model.borrow().applies, 1, "OK only closes"); } + +#[derive(Default)] +struct CustomModel { + draft: u8, + saved: u8, +} + +impl PreferencesModel for CustomModel { + fn pages(&self) -> Vec> { + vec![PageSpec::new("general", "General", vec![RowSpec::new("custom", "", RowKind::Custom)])] + } + fn set(&mut self, _: &str, _: PrefValue) {} + fn custom_row(&mut self, ui: &mut Ui, _: &str) -> bool { + let before = self.draft; + if ui.button("Invalid").clicked() { + self.draft = 1; + } + if ui.button("Correct").clicked() { + self.draft = 2; + } + self.draft != before + } + fn custom_page(&mut self, _: &mut Ui, _: &str) -> bool { + false + } + fn differs(&self) -> bool { + self.draft != self.saved + } + fn apply(&mut self) -> Result<(), String> { + if self.draft == 1 { + return Err("Invalid value".into()); + } + self.saved = self.draft; + Ok(()) + } + fn cancel(&mut self) {} + fn reset_page(&mut self, _: &str) {} +} + +#[trial] +fn corrected_custom_row_applies_after_an_error() { + let model = Rc::new(RefCell::new(CustomModel::default())); + let shown = model.clone(); + let mut state = PreferencesState::default(); + let mut fixture = Fixture::new(move |ui| { + let _ = PreferencesWindow::new(Bind::bound(&mut state)).mode(PreferencesMode::Live).embedded().show(ui, &mut *shown.borrow_mut()); + }); + fixture.settle(); + let invalid = fixture.by_label("Invalid").expect("invalid choice"); + fixture.click(&invalid).settle(); + assert_eq!(model.borrow().saved, 0); + let correct = fixture.by_label("Correct").expect("correct choice"); + fixture.click(&correct).settle(); + assert_eq!(model.borrow().saved, 2, "a correction applies without closing the window"); +}