diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 552571e3c..6d0526ffb 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -1331,6 +1331,9 @@ pub fn open_document_setup(app: &mut DesignApp) { } // The margins as shown: OK applies them to every page only when they were edited. f["marginsShown"] = json!(SETUP_EDGES.map(|e| f[format!("margin{e}")].clone())); + // The size as shown: OK sends it only when edited, so resized pages and parents keep theirs. + f["widthShown"] = f["width"].clone(); + f["heightShown"] = f["height"].clone(); let mut d = Dialog::new("documentSetup", f); d.unit = units; app.ui.dialog = Some(d); @@ -1426,17 +1429,26 @@ fn setup_measure(d: &Dialog, key: &str) -> Option { /// Document Setup's OK: `layout.documentSetup` with what was typed; a value that can't be read is /// refused before anything changes. The margins (the first page's, shown) go to every page only -/// when they were edited, so pages keep their own margins otherwise. +/// when they were edited, and so does the size, so pages keep their own margins and sizes otherwise. fn confirm_document_setup(app: &mut DesignApp, d: &Dialog) -> Result { let measure = |key: &str| setup_measure(d, key).ok_or_else(|| format!("{key}: not a measure")); let edges = |group: &str| -> Result, String> { SETUP_EDGES.iter().map(|e| measure(&format!("{group}{e}"))).collect() }; let count = |key: &str| d.n(key).filter(|n| n.is_finite() && *n >= 1.0).map(|n| n as u64).ok_or_else(|| format!("{key}: a whole number from 1")); + let size = |key: &str| -> Result, String> { + let v = measure(key)?; + Ok((d.fields.get(key) != d.fields.get(&format!("{key}Shown"))).then_some(v)) + }; let margins = edges("margin")?; let shown = d.fields.get("marginsShown").and_then(Value::as_array); let edited = SETUP_EDGES.iter().enumerate().any(|(i, e)| shown.and_then(|s| s.get(i)) != d.fields.get(&format!("margin{e}"))); let mut p = json!({"intent": d.s("intent"), "pages": count("pages")?, "startPage": count("startPage")?, "facingPages": d.b("facingPages"), - "binding": d.s("binding"), "width": measure("width")?, "height": measure("height")?, - "bleed": edges("bleed")?, "slug": edges("slug")?, "adjustLayout": d.b("adjustLayout")}); + "binding": d.s("binding"), "bleed": edges("bleed")?, "slug": edges("slug")?, "adjustLayout": d.b("adjustLayout")}); + if let Some(w) = size("width")? { + p["width"] = json!(w); + } + if let Some(h) = size("height")? { + p["height"] = json!(h); + } if edited { p["margins"] = json!({"top": margins[0], "bottom": margins[1], "inside": margins[2], "outside": margins[3]}); } diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs index b65bf0d5e..3f25ef5cd 100644 --- a/crates/ui-egui/src/dialogs/tests.rs +++ b/crates/ui-egui/src/dialogs/tests.rs @@ -336,6 +336,29 @@ fn document_setup_binding_follows_show_right_to_left_features() { assert_eq!(h.get_by_label("Right to Left").accesskit_node().toggled(), Some(egui::accesskit::Toggled::True)); } +#[test] +fn document_setup_keeps_resized_pages_unless_the_size_is_edited() { + let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"pages": 3})).unwrap(); + app.run("layout.pageSize", json!({"pages": [2], "width": 300, "height": 400})).unwrap(); + let sizes = + |app: &DesignApp| app.session.active().unwrap().doc.spreads.iter().flat_map(|s| &s.pages).map(|p| (p.width, p.height)).collect::>(); + let before = sizes(&app); + assert_eq!(before[1], (300.0, 400.0)); + // Bleed only, then margins only: no page changes size. + for edit in [json!({"bleedTop": "9 pt", "bleedBottom": "9 pt", "bleedInside": "9 pt", "bleedOutside": "9 pt"}), json!({"marginTop": "1p0"})] { + crate::menus::activate(&mut app, "layout.documentSetup", &Value::Null); + setup_fields(&mut app, edit.clone()); + confirm(&mut app).unwrap(); + assert_eq!(sizes(&app), before, "{edit}"); + } + // An edited width still goes to every page. + crate::menus::activate(&mut app, "layout.documentSetup", &Value::Null); + setup_fields(&mut app, json!({"width": "500 pt"})); + confirm(&mut app).unwrap(); + assert!(sizes(&app).iter().all(|s| s.0 == 500.0)); +} + #[test] fn document_setup_keeps_a_value_it_cannot_read_and_changes_nothing() { for (key, value) in [("width", "wide"), ("marginTop", "-"), ("pages", "0"), ("bleedTop", "x")] { diff --git a/crates/ui-egui/tests/autocraft_regression_2439.rs b/crates/ui-egui/tests/autocraft_regression_2439.rs new file mode 100644 index 000000000..99fe8e2e4 --- /dev/null +++ b/crates/ui-egui/tests/autocraft_regression_2439.rs @@ -0,0 +1,33 @@ +use designcraft_engine::Session; +use designcraft_ui_egui::dialogs::{confirm, open_document_setup}; +use designcraft_ui_egui::{DesignApp, Services}; +use serde_json::json; + +fn sizes(app: &DesignApp) -> (Vec<(f64, f64)>, Vec<(f64, f64)>) { + let doc = &app.session.active().unwrap().doc; + let pages = doc.spreads.iter().flat_map(|s| &s.pages).map(|p| (p.width, p.height)).collect(); + let parents = doc.parents.iter().flat_map(|sp| sp.pages.iter()).map(|p| (p.width, p.height)).collect(); + (pages, parents) +} + +#[test] +fn document_setup_bleed_keeps_resized_pages_and_parents() { + let mut app = DesignApp::new(Session::new(), Services::default()); + app.run("file.new", json!({"pages": 3})).unwrap(); + app.run("layout.pageSize", json!({"pages": [2], "width": 300, "height": 400})).unwrap(); + let parent = app.session.active().unwrap().doc.parents[0].id.0; + app.run("layout.parentPageSize", json!({"parent": parent, "page": 0, "width": 200, "height": 250})).unwrap(); + let before = sizes(&app); + assert_eq!(before.0[1], (300.0, 400.0)); + assert_eq!(before.1[0], (200.0, 250.0)); + + open_document_setup(&mut app); + let d = app.ui.dialog.as_mut().unwrap(); + for e in ["Top", "Bottom", "Inside", "Outside"] { + d.fields.insert(format!("bleed{e}"), json!("9 pt")); + } + confirm(&mut app).unwrap(); + + assert_eq!(app.session.active().unwrap().doc.settings.bleed, [9.0; 4]); + assert_eq!(sizes(&app), before, "bleed alone must not resize pages or parents"); +}