From dc6f7246a3de8f5ff5ef89e8eed560a62aed7588 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Mon, 12 Oct 2026 08:31:34 +0200 Subject: [PATCH] Page Setup opens on the sheet's saved margins (#524) --- crates/ui-egui/src/dialogs.rs | 34 ++++++++++-------- crates/ui-egui/tests/page_setup_margins.rs | 42 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 crates/ui-egui/tests/page_setup_margins.rs diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 5e859c6..776ac82 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -370,20 +370,26 @@ impl Dialog { ], json!({"header": "", "footer": "&CPage &P of &N"}), ), - "pageSetup" => Dialog::form( - "pageSetup", - "Page Setup", - "pageLayout.margins", - vec![ - Number { key: "left", label: "Left margin (in):" }, - Number { key: "right", label: "Right margin (in):" }, - Number { key: "top", label: "Top margin (in):" }, - Number { key: "bottom", label: "Bottom margin (in):" }, - Number { key: "header", label: "Header (in):" }, - Number { key: "footer", label: "Footer (in):" }, - ], - json!({"left": 0.7, "right": 0.7, "top": 0.75, "bottom": 0.75, "header": 0.3, "footer": 0.3}), - ), + "pageSetup" => { + // Opens on the active sheet's saved margins, so OK without edits rewrites them unchanged. + let m = app.session.active().and_then(|d| d.wb.active()).map(|s| s.print.margins).unwrap_or([0.7, 0.7, 0.75, 0.75, 0.3, 0.3]); + // Round off f32 noise so the form shows 0.1, not 0.10000000149. + let [left, right, top, bottom, header, footer] = m.map(|v| (f64::from(v) * 1e4).round() / 1e4); + Dialog::form( + "pageSetup", + "Page Setup", + "pageLayout.margins", + vec![ + Number { key: "left", label: "Left margin (in):" }, + Number { key: "right", label: "Right margin (in):" }, + Number { key: "top", label: "Top margin (in):" }, + Number { key: "bottom", label: "Bottom margin (in):" }, + Number { key: "header", label: "Header (in):" }, + Number { key: "footer", label: "Footer (in):" }, + ], + json!({"left": left, "right": right, "top": top, "bottom": bottom, "header": header, "footer": footer}), + ) + } "textToColumns" => Dialog::form( "textToColumns", "Convert Text to Columns", diff --git a/crates/ui-egui/tests/page_setup_margins.rs b/crates/ui-egui/tests/page_setup_margins.rs new file mode 100644 index 0000000..c604d77 --- /dev/null +++ b/crates/ui-egui/tests/page_setup_margins.rs @@ -0,0 +1,42 @@ +//! Page Setup opens on the active sheet's saved margins, so OK without edits keeps them (issue #524). + +use gridcraft_engine::Session; +use gridcraft_ui_egui::SheetApp; +use serde_json::{Value, json}; + +fn margins(app: &SheetApp) -> [f32; 6] { + app.session.active().and_then(|d| d.wb.active()).map(|s| s.print.margins).unwrap_or_default() +} + +fn app_with_custom_margins() -> SheetApp { + let mut session = Session::new(); + session.new_workbook(); + let mut app = SheetApp::new(session, Default::default()); + app.run("pageLayout.margins", json!({"left": 2.0, "right": 2.5, "top": 3.0, "bottom": 0.25, "header": 0.1, "footer": 0.2})).unwrap(); + assert_eq!(margins(&app), [2.0, 2.5, 3.0, 0.25, 0.1, 0.2]); + app +} + +#[test] +fn page_setup_ok_without_edits_keeps_custom_margins() { + // Custom Margins / File > Page Setup open it bare; Print Titles opens it on the Sheet tab. + for params in [json!({}), json!({"tab": "Sheet"})] { + let mut app = app_with_custom_margins(); + app.open_dialog("pageSetup", params); + let d = app.dialog.take().unwrap(); + let shown = json!({"left": 2.0, "right": 2.5, "top": 3.0, "bottom": 0.25, "header": 0.1, "footer": 0.2}); + assert_eq!(Value::Object(d.values.clone()), shown); + app.run("pageLayout.margins", Value::Object(d.values)).unwrap(); + assert_eq!(margins(&app), [2.0, 2.5, 3.0, 0.25, 0.1, 0.2]); + } +} + +#[test] +fn page_setup_one_edited_field_keeps_the_other_margins() { + let mut app = app_with_custom_margins(); + app.open_dialog("pageSetup", json!({})); + let mut d = app.dialog.take().unwrap(); + d.values.insert("left".into(), json!(1.5)); + app.run("pageLayout.margins", Value::Object(d.values)).unwrap(); + assert_eq!(margins(&app), [1.5, 2.5, 3.0, 0.25, 0.1, 0.2]); +}