New Document: a new layout keeps the columns a preset's rows showed

A preset made with file.new's rows under gridByColumn shows the column
count those rows give the current number of artboards. Changing the
Layout field removed the rows and kept the preset's stored columns, so
Create laid out a different number of columns than the dialog showed.
The layout change now writes the shown count into columns, for rows
stored as integers or floats. Under gridByRow the dialog shows the
stored columns and file.new rejects rows, so a layout change from it
keeps the stored columns, as it does for rows file.new would reject.
This commit is contained in:
dotCooCoo
2026-11-21 19:52:56 -08:00
parent abe8b303de
commit 71aaa752a0
+37 -4
View File
@@ -253,11 +253,16 @@ fn layout(d: &Dialog) -> (ArtboardLayout, u64, f64, bool) {
}
/// Sets one artboard layout field. A new layout or column count replaces the rows of a preset
/// made with `file.new`'s `rows`.
/// made with `file.new`'s `rows`; a new layout keeps the column count those rows showed.
fn set_layout(d: &mut Dialog, key: &str, v: Value) {
let (lay, shown, ..) = layout(d);
let mut l = d.fields.get("artboardLayout").cloned().filter(Value::is_object).unwrap_or_else(|| json!({}));
if let ("layout" | "columns", Some(o)) = (key, l.as_object_mut()) {
o.remove("rows");
if let ("layout" | "columns", Some(o)) = (key, l.as_object_mut())
&& o.remove("rows").and_then(|r| r.as_f64()).is_some_and(|r| r.is_finite() && r >= 1.0)
&& key == "layout"
&& lay == ArtboardLayout::GridByColumn
{
o.insert("columns".into(), json!(shown));
}
l[key] = v;
d.fields.insert("artboardLayout".into(), l);
@@ -914,7 +919,35 @@ mod tests {
assert_eq!(field(&app, "artboardLayout"), json!({"layout": "gridByColumn", "columns": 3, "spacing": 5.0}));
set(&mut app, "artboardLayout", json!({"layout": "gridByColumn", "rows": 4}));
super::set_layout(app.ui.dialog.as_mut().unwrap(), "layout", json!("gridByRow"));
assert_eq!(field(&app, "artboardLayout"), json!({"layout": "gridByRow"}));
assert_eq!(field(&app, "artboardLayout"), json!({"layout": "gridByRow", "columns": 1}));
}
/// A new layout keeps the columns a preset's rows showed, so Create lays out what the dialog
/// showed. Under a grid by row the dialog shows the stored columns, and they stay.
#[test]
fn a_new_layout_keeps_the_columns_a_presets_rows_showed() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.newDialog", json!({})).unwrap();
let columns = |app: &VectorcraftApp| super::layout(app.ui.dialog.as_ref().unwrap()).1;
set(&mut app, "artboards", json!(9));
for to in ["gridByRow", "gridByColumn", "row", "column"] {
for rows in [json!(4), json!(4.0), json!(3.6)] {
set(&mut app, "artboardLayout", json!({"layout": "gridByColumn", "columns": 2, "rows": rows}));
assert_eq!(columns(&app), 3, "9 artboards in rows {rows}");
super::set_layout(app.ui.dialog.as_mut().unwrap(), "layout", json!(to));
assert_eq!(field(&app, "artboardLayout"), json!({"layout": to, "columns": 3}), "{to}, rows {rows}");
}
}
set(&mut app, "artboardLayout", json!({"layout": "gridByRow", "columns": 2, "rows": 4}));
assert_eq!(columns(&app), 2);
super::set_layout(app.ui.dialog.as_mut().unwrap(), "layout", json!("gridByColumn"));
assert_eq!(field(&app, "artboardLayout"), json!({"layout": "gridByColumn", "columns": 2}));
assert_eq!(columns(&app), 2);
// Rows file.new would reject leave the stored columns the dialog showed.
set(&mut app, "artboardLayout", json!({"layout": "gridByColumn", "columns": 2, "rows": 0}));
assert_eq!(columns(&app), 2);
super::set_layout(app.ui.dialog.as_mut().unwrap(), "layout", json!("gridByRow"));
assert_eq!(field(&app, "artboardLayout"), json!({"layout": "gridByRow", "columns": 2}));
}
/// Columns shows what a preset's rows give the current number of artboards.