mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Merge feat/scripts-api-d1a-app-documents into feat/scripts-api-d7a-variables
This commit is contained in:
commit
80399145d9
1 file changed
+37
-4
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user