From a8e9b9d86e346e2349622943746cc547c208687b Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Thu, 15 Oct 2026 09:39:03 +0000 Subject: [PATCH] Make New Document preset cards follow the chosen units Fixes #2078 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/new_document.rs | 12 +++++++----- crates/ui-egui/src/panels/tests_units.rs | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/new_document.rs b/crates/ui-egui/src/dialogs/new_document.rs index 4a57f72bb..63aac7f8a 100644 --- a/crates/ui-egui/src/dialogs/new_document.rs +++ b/crates/ui-egui/src/dialogs/new_document.rs @@ -97,8 +97,9 @@ fn sync(app: &VectorcraftApp, d: &mut Dialog) { let preset = d.str("preset"); match d.fields.get(APPLIED).and_then(Value::as_object) { Some(a) if a.get("preset").and_then(Value::as_str) == Some(preset.as_str()) => { - // Unchanged fields keep the preset (falling through would apply it again). - let edited = a.iter().any(|(k, v)| k != "preset" && d.fields.get(k) != Some(v)); + // Unchanged fields keep the preset (falling through would apply it again); the units + // only change how the sizes show. + let edited = a.iter().any(|(k, v)| k != "preset" && k != "units" && d.fields.get(k) != Some(v)); if edited { d.fields.insert("preset".into(), json!("")); d.fields.remove(APPLIED); @@ -238,10 +239,11 @@ fn presets(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { egui::ScrollArea::vertical().id_salt(("newdoc-presets", cat)).max_height(HEIGHT - 110.0).auto_shrink([false, false]).show(ui, |ui| { ui.horizontal_wrapped(|ui| { ui.spacing_mut().item_spacing = egui::vec2(12.0, 12.0); - let (w, h, preset) = (d.f64("width", 0.0), d.f64("height", 0.0), d.str("preset")); + let (w, h, preset, units) = (d.f64("width", 0.0), d.f64("height", 0.0), d.str("preset"), unit(d)); for s in &list { - // The cards show the chosen orientation (square sizes have none). - let mut s = s.clone(); + // The cards show the chosen orientation (square sizes have none) and the units chosen + // in Preset Details; choosing one keeps both. + let mut s = DocSettings { units, ..s.clone() }; if (w > h && s.height > s.width) || (h > w && s.width > s.height) { std::mem::swap(&mut s.width, &mut s.height); } diff --git a/crates/ui-egui/src/panels/tests_units.rs b/crates/ui-egui/src/panels/tests_units.rs index 06ca20633..86c55e026 100644 --- a/crates/ui-egui/src/panels/tests_units.rs +++ b/crates/ui-egui/src/panels/tests_units.rs @@ -126,3 +126,20 @@ fn ruler_labels_have_the_steps_decimals() { assert_eq!(ruler_label(-0.0000001, 0.5), "0"); assert_eq!(ruler_label(-2.5, 0.5), "-2.5"); } + +#[test] +fn new_document_cards_follow_the_chosen_units() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + general_mm(&mut app); + app.run("file.newDialog", json!({})).unwrap(); + let d = app.ui.dialog.as_mut().unwrap(); + d.fields.insert("category".into(), json!("Art & Illustration")); + d.fields.insert("preset".into(), json!("Postcard")); + let t = dialog_texts(&mut app); + assert!(shows(&t, "101.6 × 152.4 mm"), "Postcard in mm: {t:?}"); + // Choosing Pixels: the cards show pixels and Postcard stays chosen. + app.ui.dialog.as_mut().unwrap().fields.insert("units".into(), json!("Pixels")); + let t = dialog_texts(&mut app); + assert!(shows(&t, "288 × 432 px") && !shows(&t, "101.6 × 152.4 mm"), "Postcard in px: {t:?}"); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields["preset"], json!("Postcard")); +}