From 99c37518d2ed601f2d60f5256be6ccb14a2d145c Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 21:33:22 -0800 Subject: [PATCH] Preferences: prefs.reset with Preferences open keeps the reset on OK A prefs.reset run while the Preferences dialog is open now puts the reset values in the dialog: the UI-only rows and the preferences of the categories it reset, and the preference groups when it resets every category. OK applies a reset still pending from outside the command path before it reads the dialog. OK then applies the reset values instead of the dialog's earlier ones, and leaves the open document's units alone. --- crates/ui-egui/src/prefs_dialog.rs | 22 +++++++++++++ crates/ui-egui/src/prefs_dialog/tests.rs | 42 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/crates/ui-egui/src/prefs_dialog.rs b/crates/ui-egui/src/prefs_dialog.rs index 0f777a9dca..d3e0f9641d 100644 --- a/crates/ui-egui/src/prefs_dialog.rs +++ b/crates/ui-egui/src/prefs_dialog.rs @@ -220,6 +220,8 @@ pub(crate) fn note_set(d: &mut Dialog, field: &str, value: &Value) { /// OK: validate and apply every preference in the dialog (nothing changes on error). pub fn confirm(app: &mut VectorcraftApp) -> Result { + // A `prefs.reset` run since the last frame shows in the dialog before OK reads it. + apply_prefs_resets(app); let Some(d) = app.ui.dialog.clone() else { return Err("no dialog open".into()) }; let values: Map = d.fields.iter().filter(|(k, _)| !k.starts_with("__")).map(|(k, v)| (k.clone(), v.clone())).collect(); let touch_ws = values.get("touchWorkspace").and_then(Value::as_bool); @@ -525,6 +527,7 @@ pub(crate) fn apply_prefs_resets(app: &mut VectorcraftApp) -> bool { return false; } let in_reset = |category: &str| resets.iter().any(|r| r.is_none_or(|c| c == category)); + let prefs = app.session.prefs.to_json(); for (key, value) in default_ui_fields(&app.ui) { let reset = UI_FIELDS.iter().any(|&(k, c, _)| k == key && in_reset(c)); match key { @@ -533,6 +536,25 @@ pub(crate) fn apply_prefs_resets(app: &mut VectorcraftApp) -> bool { "__taskBar" if reset => app.ui.task_bar = value, _ => {} } + // An open Preferences dialog shows the reset rows, so OK keeps them. + if reset && let Some(d) = app.ui.dialog.as_mut().filter(|d| d.kind == "preferences") { + d.fields.insert(key.into(), json!(value)); + } + } + // And the reset preferences, the groups with a reset of every category; Units › General then + // leaves the open document's units alone on OK. + use vectorcraft_engine::cmd::prefscmds::{PREF_GROUPS, spec}; + let every = resets.contains(&None); + if let Some(d) = app.ui.dialog.as_mut().filter(|d| d.kind == "preferences") { + for (k, v) in prefs.as_object().into_iter().flatten() { + let reset = spec(k).map_or(every && PREF_GROUPS.contains(&k.as_str()), |s| in_reset(s.category)); + if d.fields.contains_key(k) && reset { + d.fields.insert(k.clone(), v.clone()); + if k == "unitsGeneral" { + d.fields.insert(RESET_UNITS.into(), v.clone()); + } + } + } } // The Edit Original row shows the app's Edit Original choice. if vectorcraft_engine::cmd::prefscmds::spec("editOriginalSystemDefaults").is_some_and(|s| in_reset(s.category)) { diff --git a/crates/ui-egui/src/prefs_dialog/tests.rs b/crates/ui-egui/src/prefs_dialog/tests.rs index 05720a2e26..d3838a574f 100644 --- a/crates/ui-egui/src/prefs_dialog/tests.rs +++ b/crates/ui-egui/src/prefs_dialog/tests.rs @@ -557,6 +557,48 @@ fn prefs_reset_resets_the_ui_only_rows_of_its_category() { assert_eq!(rows(&a), default, "every category"); } +/// A `prefs.reset` run while Preferences is open shows the reset values in the dialog, for the +/// categories it reset, so OK keeps the reset; rows of other categories keep the dialog's values. +#[test] +fn prefs_reset_while_preferences_is_open_shows_the_reset_values() { + let fresh = app(); + let rows = |a: &VectorcraftApp| (a.ui.view.smart_guides, a.ui.view.snap_to_grid, a.ui.task_bar); + let default = rows(&fresh); + let mut a = app(); + (a.ui.view.smart_guides, a.ui.view.snap_to_grid, a.ui.task_bar) = (!default.0, !default.1, !default.2); + a.run("prefs.set", json!({"key": "keyboardIncrement", "value": 7})).unwrap(); + open(&mut a, Some("General")); + a.run("prefs.reset", json!({"category": "General"})).unwrap(); + let d = a.ui.dialog.as_ref().unwrap(); + assert_eq!(d.fields["__taskBar"], json!(default.2), "the dialog shows the reset row"); + assert_eq!(d.fields["__smartGuides"], json!(!default.0), "a row of another category keeps its value"); + confirm(&mut a).unwrap(); + assert_eq!(rows(&a), (!default.0, !default.1, default.2), "OK keeps the reset"); + assert_eq!(a.session.prefs.keyboard_increment, fresh.session.prefs.keyboard_increment, "OK keeps the reset preference"); + + // Every category, the preference groups included. + let group = fresh.session.prefs.to_json()["pixelSnap"].clone(); + let mut changed = group.clone(); + let flag = changed.as_object_mut().and_then(|o| o.values_mut().find(|v| v.is_boolean())).expect("a pixelSnap option is a boolean"); + *flag = json!(!flag.as_bool().unwrap_or_default()); + a.run("prefs.set", json!({"key": "keyboardIncrement", "value": 7})).unwrap(); + a.run("prefs.set", json!({"key": "pixelSnap", "value": changed})).unwrap(); + open(&mut a, Some("General")); + assert_eq!(a.ui.dialog.as_ref().unwrap().fields["pixelSnap"], changed); + a.run("prefs.reset", json!({})).unwrap(); + confirm(&mut a).unwrap(); + assert_eq!(rows(&a), default, "every category"); + assert_eq!(a.session.prefs.keyboard_increment, fresh.session.prefs.keyboard_increment); + assert_eq!(a.session.prefs.to_json()["pixelSnap"], group, "OK keeps the reset group"); + + // Run straight on the session, with OK before the next frame. + a.run("prefs.set", json!({"key": "keyboardIncrement", "value": 7})).unwrap(); + open(&mut a, Some("General")); + a.session.execute("prefs.reset", &json!({"category": "General"})).unwrap(); + confirm(&mut a).unwrap(); + assert_eq!(a.session.prefs.keyboard_increment, fresh.session.prefs.keyboard_increment, "OK keeps a reset outside run"); +} + /// Reset Preferences and `prefs.reset` give Enable Contextual Task Bar the value the current /// workspace was saved with, which for a workspace saved with the bar off differs from the default. #[test]