Page Setup opens on the sheet's saved margins (#524)

This commit is contained in:
MarekSpodBiedry 2026-10-12 08:31:34 +02:00
parent 3926d21aa9
commit dc6f7246a3
2 changed files with 62 additions and 14 deletions

View File

@ -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",

View File

@ -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]);
}