Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Preferences: unapplied changes no longer reach the settings file (#9730) (#9831)

Dahak Builds7228e218bfc4
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 1a64929a71..771ee59a02 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -975,7 +975,7 @@ impl SolveApp {
/// Preferences kept between runs (JSON), for the host to store.
pub fn prefs(&self) -> String {
- json!({
+ let mut v = json!({
"dark": self.ui.dark,
"grid": self.ui.show_grid,
"navigation_bar": self.ui.show_navigation_bar,
@@ -998,8 +998,19 @@ impl SolveApp {
"agent_cursor": self.ui.agent_cursor,
"preferences": self.preferences,
"gpu_path_setup_seconds": self.render_workspace.setup_seconds,
- })
- .to_string()
+ });
+ // While the Preferences dialog is open the file holds what Cancel would restore: a change
+ // that was never applied must not reach disk through the periodic save, a quit or a
+ // window close (#9730). One change covers every writer — desktop save and on_exit, the
+ // web app's save, and the control channel.
+ if let Some(held) = prefs::held_overlay(self)
+ && let Some(obj) = v.as_object_mut()
+ {
+ for (key, value) in held {
+ obj.insert(key, value);
+ }
+ }
+ v.to_string()
}
/// Restore preferences saved with [`SolveApp::prefs`].
diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs
index a4c38755c8..f4a3142a30 100644
--- a/crates/ui-egui/src/prefs.rs
+++ b/crates/ui-egui/src/prefs.rs
@@ -1660,6 +1660,53 @@ fn apply_pending(app: &mut SolveApp) -> bool {
app.prefs_window.snapshot.as_ref().is_some_and(|s| !s.matches(app))
}
+/// The saved-settings keys whose values the snapshot holds, as [`SolveApp::prefs`] writes them.
+/// While the dialog is open every writer of the settings file saves these instead of the live
+/// values, so a change that was never applied can't reach disk through the periodic save, a
+/// quit or a window close (#9730). Keys the dialog can't change keep their live values.
+pub(crate) fn held_overlay(app: &SolveApp) -> Option<serde_json::Map<String, serde_json::Value>> {
+ let s = app.prefs_window.snapshot.as_ref()?;
+ let mut prefs = s.prefs.clone();
+ // Same bookkeeping rule as `restore`: a check that finished while the dialog was open stays.
+ prefs.update.clone_from(&app.preferences.update);
+ let mut shortcut_box = app.sbox.prefs();
+ if let Some(o) = shortcut_box.as_object_mut() {
+ o.insert("pinned".into(), serde_json::json!(s.favourites));
+ }
+ let mut profiles = app.settings_profiles.prefs();
+ if let Some(o) = profiles.as_object_mut() {
+ o.insert("active".into(), serde_json::json!(s.profile));
+ }
+ let mut shortcuts = serde_json::Map::new();
+ for (id, keys) in &s.keys {
+ shortcuts.insert(id.clone(), crate::keymap::keys_value(keys));
+ }
+ // Bound outside the `json!` macro: the ui-containers guard reads `.shadow` inside a macro
+ // as egui's popup shorthand.
+ let shadow = s.shadow;
+ let mut m = serde_json::Map::new();
+ for (key, value) in [
+ ("dark", serde_json::json!(s.dark)),
+ ("grid", serde_json::json!(s.grid)),
+ ("navigation_bar", serde_json::json!(s.navigation_bar)),
+ ("perspective", serde_json::json!(s.perspective)),
+ ("pick_bodies", serde_json::json!(s.pick_bodies)),
+ ("auto_project", serde_json::json!(s.auto_project)),
+ ("visual_style", serde_json::json!(s.style)),
+ ("shortcut_box", shortcut_box),
+ ("shortcuts", serde_json::Value::Object(shortcuts)),
+ ("toolbar", s.toolbar.clone()),
+ ("settings_profiles", profiles),
+ ("autosave_minutes", serde_json::json!(s.autosave)),
+ ("ground_shadow", serde_json::json!(shadow)),
+ ("agent_cursor", serde_json::json!(s.agent)),
+ ("preferences", serde_json::to_value(&prefs).unwrap_or_default()),
+ ] {
+ m.insert(key.to_string(), value);
+ }
+ Some(m)
+}
+
/// The colour theme (#2255): Dark, Light, System, the built-in theme files and the imported
/// ones, with Import…, Export… (the theme on now as a complete file, to start a new one from)
/// and Remove (an imported theme). Like the other preferences, a choice applies at once.
@@ -3674,7 +3721,7 @@ mod tests {
}
let mut restored = crate::SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert_eq!(restored.preferences.navigation_bar, before.navigation_bar);
+ assert_eq!(restored.preferences.navigation_bar, NavigationBar::default(), "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert_eq!(h.app.preferences.navigation_bar, NavigationBar::default());
h.call("ui.prefs", json!({"open":"Interface","navigation_bar":{"slots":[]}}));
@@ -4489,6 +4536,7 @@ mod tests {
scroll_to_and_click(&mut h, "dialog:button::Restore defaults");
scroll_to_and_click(&mut h, "dialog:button::Restore defaults");
assert!(!h.app.space_mouse.restore_pending && section_is_at_defaults(&h.app));
+ h.call("ui.prefs", json!({"button":"apply"}));
let saved = h.app.prefs();
let mut next = crate::scenario::Harness::new();
next.app.preferences.space_mouse_speed = 2.5;
@@ -6037,6 +6085,79 @@ mod tests {
std::fs::remove_dir_all(dir).unwrap();
}
+ /// The settings `SolveApp::prefs` would write right now, parsed.
+ fn saved_prefs(app: &SolveApp) -> serde_json::Value {
+ serde_json::from_str(&app.prefs()).unwrap()
+ }
+
+ #[test]
+ fn unapplied_changes_are_not_saved_while_the_dialog_is_open() {
+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());
+ assert_eq!(saved_prefs(&app)["preferences"]["theme"], "dark");
+ assert_eq!(saved_prefs(&app)["preferences"]["sketch_tab_always"], false);
+ // Open the dialog: the snapshot is taken as `show` and the control channel take it.
+ app.prefs_window.open = true;
+ app.prefs_window.snapshot = Some(Snapshot::take(&app));
+ // The Light tile and the Sketch tab box, clicked without Apply.
+ set(&mut app, &json!({"theme": "light", "sketch_tab_always": true})).unwrap();
+ assert_eq!(app.preferences.theme, "light", "the live app has the change");
+ assert!(!app.ui.dark);
+ let held = saved_prefs(&app);
+ assert_eq!(held["preferences"]["theme"], "dark", "a change that was not applied must not be saved");
+ assert_eq!(held["preferences"]["sketch_tab_always"], false);
+ assert_eq!(held["dark"], true, "the Light tile's dark flag is not saved either");
+ // Apply makes the change the baseline: from now on it is saved.
+ app.prefs_window.snapshot = Some(Snapshot::take(&app));
+ let applied = saved_prefs(&app);
+ assert_eq!(applied["preferences"]["theme"], "light");
+ assert_eq!(applied["dark"], false);
+ // A second unapplied change is held back too, and Cancel puts it back.
+ set(&mut app, &json!({"theme": "dark"})).unwrap();
+ assert_eq!(saved_prefs(&app)["preferences"]["theme"], "light", "the second change waits for Apply too");
+ close(&mut app, true);
+ let after = saved_prefs(&app);
+ assert_eq!(after["preferences"]["theme"], "light", "Cancel restores the applied state");
+ assert_eq!(after["dark"], false);
+ assert_eq!(after["preferences"]["sketch_tab_always"], true, "Apply's change stays");
+ }
+
+ #[test]
+ fn settings_the_dialog_cannot_change_keep_saving_while_it_is_open() {
+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());
+ app.prefs_window.open = true;
+ app.prefs_window.snapshot = Some(Snapshot::take(&app));
+ // A recent file joins while the dialog is open: not a dialog setting, so it is saved.
+ app.home.recent.insert(0, "bracket.solid".to_string());
+ assert_eq!(saved_prefs(&app)["recent"][0], "bracket.solid");
+ }
+
+ #[test]
+ fn shortcut_changes_are_not_saved_while_the_dialog_is_open() {
+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());
+ app.keymap.custom.insert("file.new".into(), vec!["Ctrl+N".into()]);
+ app.prefs_window.open = true;
+ app.prefs_window.snapshot = Some(Snapshot::take(&app));
+ app.keymap.custom.insert("file.save".into(), vec!["Ctrl+S".into()]);
+ let saved = saved_prefs(&app);
+ assert_eq!(saved["shortcuts"]["file.new"], "Ctrl+N", "the shortcut from before the dialog is saved");
+ assert!(saved["shortcuts"].get("file.save").is_none(), "a shortcut rebound without Apply is not saved");
+ }
+
+ #[test]
+ fn control_channel_holds_unapplied_changes_out_of_the_saved_settings() {
+ let mut h = crate::scenario::Harness::new();
+ h.call("ui.prefs", json!({"open": "Interface"}));
+ h.call("ui.prefs", json!({"sketch_tab_always": true}));
+ let saved = saved_prefs(&h.app);
+ assert!(h.app.preferences.sketch_tab_always);
+ assert_eq!(saved["preferences"]["sketch_tab_always"], false, "the control channel's change waits for Apply");
+ h.call("ui.prefs", json!({"button": "apply"}));
+ assert_eq!(saved_prefs(&h.app)["preferences"]["sketch_tab_always"], true, "Apply makes it the saved state");
+ h.call("ui.prefs", json!({"sketch_tab_always": false}));
+ h.call("ui.prefs", json!({"button": "cancel"}));
+ assert_eq!(saved_prefs(&h.app)["preferences"]["sketch_tab_always"], true, "Cancel puts back what Apply left");
+ }
+
#[test]
fn startup_msaa_check_keeps_the_default_unless_fallback_is_confirmed() {
let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());
@@ -6166,7 +6287,7 @@ mod tests {
assert_eq!(h.app.session.doc.to_json(), design);
let mut restored = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert_eq!(restored.preferences.navigator_style, NavigatorStyle::Cube);
+ assert_eq!(restored.preferences.navigator_style, NavigatorStyle::Axes, "a change that was not applied is not saved (#9730)");
let before = h.app.preferences.clone();
assert!(control(&mut h.app, &json!({"navigator_style":"unknown", "nav":"solidworks"})).is_err());
assert_eq!(h.app.preferences, before, "invalid settings must fail atomically");
@@ -6431,7 +6552,7 @@ mod tests {
assert!(h.app.preferences.free_orbit);
let mut restored = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(restored.preferences.free_orbit);
+ assert!(!restored.preferences.free_orbit, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(!h.app.preferences.free_orbit);
h.call("ui.prefs", json!({"open":"Navigation","free_orbit":true,"button":"apply"}));
@@ -6460,7 +6581,7 @@ mod tests {
assert!(h.app.preferences.radial_context_menu, "the actual checkbox enables radial menus");
let mut restored = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(restored.preferences.radial_context_menu, "enabled preference survives save/load");
+ assert!(!restored.preferences.radial_context_menu, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(!h.app.preferences.radial_context_menu, "Cancel restores the default-off setting");
h.call("ui.prefs", json!({"open":"Navigation","radial_context_menu":true}));
@@ -6521,7 +6642,7 @@ mod tests {
assert_eq!(h.call("ui.prefs", json!({}))["result"]["persistent_ribbon_pressed"], false);
let mut restored = SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(!restored.preferences.persistent_ribbon_pressed);
+ assert!(restored.preferences.persistent_ribbon_pressed, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(h.app.preferences.persistent_ribbon_pressed);
h.call("ui.prefs", json!({"open":"Interface","persistent_ribbon_pressed":false,"button":"apply"}));
@@ -6553,7 +6674,7 @@ mod tests {
assert_eq!(h.call("ui.prefs", json!({}))["result"]["viewport_analysis_status"], false);
let mut restored = SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(!restored.preferences.viewport_analysis_status);
+ assert!(restored.preferences.viewport_analysis_status, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(h.app.preferences.viewport_analysis_status);
h.call("ui.prefs", json!({"open":"Interface","viewport_analysis_status":false,"button":"apply"}));
@@ -6606,7 +6727,7 @@ mod tests {
assert_eq!(h.call("ui.prefs", json!({}))["result"]["viewport_section_status"], false);
let mut restored = SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(!restored.preferences.viewport_section_status);
+ assert!(restored.preferences.viewport_section_status, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(h.app.preferences.viewport_section_status);
h.call("ui.prefs", json!({"open":"Interface","viewport_section_status":false,"button":"apply"}));
@@ -6638,7 +6759,7 @@ mod tests {
assert_eq!(h.call("ui.prefs", json!({}))["result"]["viewport_status_details"], false);
let mut restored = SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(!restored.preferences.viewport_status_details);
+ assert!(restored.preferences.viewport_status_details, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(h.app.preferences.viewport_status_details);
h.call("ui.prefs", json!({"open":"Interface","viewport_status_details":false,"button":"apply"}));
@@ -6670,7 +6791,7 @@ mod tests {
assert_eq!(h.call("ui.prefs", json!({}))["result"]["viewport_status_summary"], false);
let mut restored = SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(!restored.preferences.viewport_status_summary);
+ assert!(restored.preferences.viewport_status_summary, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(h.app.preferences.viewport_status_summary);
h.call("ui.prefs", json!({"open":"Interface","viewport_status_summary":false,"button":"apply"}));
@@ -6726,8 +6847,8 @@ mod tests {
assert!(!h.app.session.single_surface_analysis());
let mut restored = SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(!restored.preferences.single_surface_analysis);
- assert!(!restored.session.single_surface_analysis(), "loaded settings apply before any command");
+ assert!(restored.preferences.single_surface_analysis, "a change that was not applied is not saved (#9730)");
+ assert!(restored.session.single_surface_analysis(), "loaded settings apply before any command");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(h.app.preferences.single_surface_analysis && h.app.session.single_surface_analysis());
h.call("ui.prefs", json!({"open":"Graphics","single_surface_analysis":false,"button":"apply"}));
@@ -6820,7 +6941,7 @@ mod tests {
assert!(h.app.preferences.cosmetic_threads, "the actual checkbox enables the optional markings");
let mut restored = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(restored.preferences.cosmetic_threads, "the preference survives saved application settings");
+ assert!(!restored.preferences.cosmetic_threads, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(!h.app.preferences.cosmetic_threads);
h.call("ui.prefs", json!({"open":"Graphics","cosmetic_threads":true,"button":"apply"}));
@@ -7501,7 +7622,7 @@ mod ambient_occlusion_tests {
assert!(h.app.preferences.ambient_occlusion);
let mut restored = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(restored.preferences.ambient_occlusion);
+ assert!(!restored.preferences.ambient_occlusion, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert!(!h.app.preferences.ambient_occlusion);
h.call("ui.prefs", json!({"open":"Graphics", "ambient_occlusion":true}));
@@ -7565,7 +7686,7 @@ mod graphics_preset_tests {
assert!(h.app.preferences.ambient_occlusion);
let mut restored = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
restored.load_prefs(&h.app.prefs());
- assert!(restored.preferences.ambient_occlusion);
+ assert!(!restored.preferences.ambient_occlusion, "a change that was not applied is not saved (#9730)");
h.call("ui.prefs", json!({"button":"cancel"}));
assert_eq!(graphics_preset(&h.app.preferences), "performance");
assert!(!h.app.preferences.ambient_occlusion);
@@ -7583,6 +7704,9 @@ mod graphics_preset_tests {
assert_eq!(h.app.preferences.display_quality, "fine");
assert_eq!(h.app.preferences.supersampling, 2.0);
assert!(h.app.preferences.supersampling_adaptive);
+ // The dialog is closed now: what is saved is the live state, quality included (#9730).
+ let mut restored = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
+ restored.load_prefs(&h.app.prefs());
for edit in [
json!({"display_quality":"ultra"}),
json!({"supersampling":1.5}),