Merge pull request #2176 from Silvertree2010/fix/2078-when-i-click-pixels-the-unit-of

Make New Document preset cards follow the chosen units
This commit is contained in:
BFlat
2026-10-16 02:34:25 +07:00
committed by GitHub
2 changed files with 24 additions and 5 deletions
+7 -5
View File
@@ -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);
}
+17
View File
@@ -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"));
}