diff --git a/crates/ui-egui/src/dialogs/new_document.rs b/crates/ui-egui/src/dialogs/new_document.rs index 6b9abbcb2f..d7c20c0137 100644 --- a/crates/ui-egui/src/dialogs/new_document.rs +++ b/crates/ui-egui/src/dialogs/new_document.rs @@ -234,18 +234,20 @@ fn unit(d: &Dialog) -> Unit { } /// The artboard layout fields. A grid by column with rows shows the columns those rows give the -/// current number of artboards, as `file.new` lays them out. +/// current number of artboards, as `file.new` lays them out. Rows and columns are read as +/// `file.new` reads them: a number of 1 or more, rounded and kept to [`MAX_ARTBOARDS`]. fn layout(d: &Dialog) -> (ArtboardLayout, u64, f64, bool) { let l = d.fields.get("artboardLayout"); let get = |k: &str| l.and_then(|l| l.get(k)); + let count = |k: &str| get(k).and_then(Value::as_f64).filter(|c| c.is_finite() && *c >= 1.0).map(|c| c.round().min(MAX_ARTBOARDS as f64) as u64); let lay = get("layout").and_then(Value::as_str).and_then(ArtboardLayout::parse).unwrap_or_default(); - let rows = get("rows").and_then(Value::as_u64).filter(|r| *r > 0 && lay == ArtboardLayout::GridByColumn); + let rows = count("rows").filter(|_| lay == ArtboardLayout::GridByColumn); let columns = match rows { Some(rows) => { let n = d.f64("artboards", 1.0).round().clamp(1.0, MAX_ARTBOARDS as f64) as u64; n.div_ceil(rows.min(n)) } - None => get("columns").and_then(Value::as_u64).unwrap_or(1), + None => count("columns").unwrap_or(1), }; (lay, columns, get("spacing").and_then(Value::as_f64).unwrap_or(20.0), get("rightToLeft").and_then(Value::as_bool).unwrap_or(false)) } @@ -931,6 +933,26 @@ mod tests { assert_eq!(columns(&app), 2); } + /// Rows and columns stored as floats, as `ui.dialog.set` stores them, show what `file.new` + /// makes of them: rounded, at least 1 and at most the artboard limit. + #[test] + fn columns_read_float_rows_and_columns_as_file_new_does() { + 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!(5)); + for (rows, shown) in [(json!(4.0), 2), (json!(3.5), 2), (json!(2.4), 3)] { + set(&mut app, "artboardLayout", json!({"layout": "gridByColumn", "columns": 1, "rows": rows})); + assert_eq!(columns(&app), shown, "rows {rows}"); + } + set(&mut app, "artboardLayout", json!({"layout": "gridByRow", "columns": 3.0})); + assert_eq!(columns(&app), 3); + set(&mut app, "artboardLayout", json!({"layout": "gridByRow", "columns": 1e9})); + assert_eq!(columns(&app), super::MAX_ARTBOARDS as u64); + set(&mut app, "artboardLayout", json!({"layout": "gridByRow", "columns": 0.4})); + assert_eq!(columns(&app), 1); + } + /// Recent shows each entry as it was made: a portrait and a landscape A3 are two cards of /// their own orientations, and only the chosen one is selected (#2860). #[test]