Retry live preferences after custom edits

This commit is contained in:
Dylan M. Taylor
2026-11-20 13:33:37 -05:00
parent fcc757df4e
commit deca5dd355
8 changed files with 91 additions and 20 deletions
@@ -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<H: PreferencesModel + ?Sized>(
) {
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);
}
@@ -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<T> {
/// 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) {}
@@ -108,12 +108,14 @@ impl<T: Serialize + DeserializeOwned + Default + Clone + PartialEq, C: CustomSet
insert(&mut self.draft.table, key, new);
}
fn custom_row(&mut self, ui: &mut Ui, key: &str) {
self.custom.row(ui, key, &mut self.draft);
fn custom_row(&mut self, ui: &mut Ui, key: &str) -> 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 {
@@ -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.
@@ -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<ColorRequest>,
}
@@ -177,11 +178,11 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(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));
});
}
}
@@ -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 {
@@ -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
@@ -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<PageSpec<'_>> {
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");
}