diff --git a/crates/engine/src/artboard_cmds.rs b/crates/engine/src/artboard_cmds.rs index a00c3921a8..d09687b3ce 100644 --- a/crates/engine/src/artboard_cmds.rs +++ b/crates/engine/src/artboard_cmds.rs @@ -303,6 +303,21 @@ fn artboard_from_group(s: &mut Session, p: &Value) -> Result { Ok(json!({"layer": id.0, "rect": [rect.x0, rect.y0, rect.width(), rect.height()]})) } +/// The layers' union bounds, or the canvas when they have none: the board Artboard from Layers +/// makes when no size is given. +fn union_bounds<'a>(layers: impl IntoIterator, canvas: Rect) -> Rect { + layers.into_iter().filter_map(crate::layer_multi_cmds::layer_bounds).reduce(|a, b| a.union(&b)).unwrap_or(canvas) +} + +/// What Artboard from Layers' dialog opens with, as in the reference application: the name the +/// board would get and the selected top-level layers' size (`None` without a document). +pub fn from_layers_defaults(s: &Session) -> Option<(String, [i64; 2])> { + let d = s.active()?; + let ids = crate::layer_multi_cmds::top_level(&d.doc, &crate::layer_multi_cmds::selected(s)); + let [_, _, w, h] = xywh(union_bounds(ids.iter().filter_map(|id| d.doc.layer(*id)), d.doc.bounds())); + Some((next_artboard_name(&d.doc), [w, h])) +} + fn artboard_from_layers(s: &mut Session, p: &Value) -> Result { let sel = crate::layer_multi_cmds::selected(s); let d = s.active().ok_or(EngineError::NoDocument)?; @@ -310,6 +325,10 @@ fn artboard_from_layers(s: &mut Session, p: &Value) -> Result { let name = p.get("name").and_then(Value::as_str).filter(|n| !n.is_empty()).map(str::to_string); let cmd = "layer.new.artboardFromLayers"; let background = background_param(p, cmd)?.unwrap_or_default(); + // `int` rounds, so a small negative (-0.4) would pass as 0, "keep the bounds". + if ["width", "height"].iter().any(|k| p.get(*k).and_then(Value::as_f64).is_some_and(|v| v < 0.0)) { + return Err(bad(cmd, "the artboard width and height must not be negative")); + } // A size of the user's own (a post, a story…) from the layers' top left; 0 or none keeps // their bounds on that axis (#9087). let size = [int(p, "width").filter(|v| *v != 0), int(p, "height").filter(|v| *v != 0)]; @@ -322,8 +341,7 @@ fn artboard_from_layers(s: &mut Session, p: &Value) -> Result { for id in &ids { children.push(doc.remove(*id).ok_or(EngineError::NoLayer(*id))?); } - let bounds = children.iter().filter_map(crate::layer_multi_cmds::layer_bounds).reduce(|a, b| a.union(&b)).unwrap_or(canvas); - let [x, y, w, h] = xywh(bounds); + let [x, y, w, h] = xywh(union_bounds(&children, canvas)); let rect = board_rect(cmd, [x, y, size[0].unwrap_or(w), size[1].unwrap_or(h)])?; let mut g = Layer::group(name.unwrap_or_else(|| next_artboard_name(doc)), children); set_artboard(&mut g, Artboard { rect, background, preset: String::new() }); diff --git a/crates/engine/src/artboard_cmds/tests.rs b/crates/engine/src/artboard_cmds/tests.rs index 14b3ee4f7d..6c2afe16a4 100644 --- a/crates/engine/src/artboard_cmds/tests.rs +++ b/crates/engine/src/artboard_cmds/tests.rs @@ -679,5 +679,25 @@ fn artboard_from_layers_takes_a_custom_size() { let mut s = session(8); add_square(&mut s, Rect::new(10, 10, 30, 30), [1.0, 0.0, 0.0, 1.0]); assert!(s.execute("layer.new.artboardFromLayers", json!({"width": -5})).is_err()); + // `int` rounds -0.4 to 0, which would otherwise keep the bounds. + assert!(s.execute("layer.new.artboardFromLayers", json!({"height": -0.4})).is_err()); assert!(doc(&s).artboards().is_empty(), "a refused size changes nothing"); + assert_eq!(s.execute("layer.new.artboardFromLayers", json!({"name": ""})).unwrap()["rect"], json!([10, 10, 20, 20])); + assert_eq!(doc(&s).artboards()[0].1, "Artboard 1", "an empty name gives Artboard N"); +} + +/// The dialog's defaults (#9087): the selected layers' size and the board's name, as the command +/// makes it with no size; the canvas when the layers have no pixels. +#[test] +fn from_layers_defaults_are_the_selected_layers_size() { + let mut s = session(8); + add_square(&mut s, Rect::new(10, 10, 30, 40), [1.0, 0.0, 0.0, 1.0]); + assert_eq!(from_layers_defaults(&s), Some(("Artboard 1".to_string(), [20, 30]))); + s.execute("layer.new.artboardFromLayers", json!({})).unwrap(); + add_square(&mut s, Rect::new(50, 5, 60, 10), [1.0, 0.0, 0.0, 1.0]); + assert_eq!(from_layers_defaults(&s), Some(("Artboard 2".to_string(), [10, 5]))); + let mut s = session(8); + s.execute("layer.new.layer", json!({})).unwrap(); + assert_eq!(from_layers_defaults(&s), Some(("Artboard 1".to_string(), [100, 80])), "an empty layer: the canvas"); + assert_eq!(from_layers_defaults(&Session::new()), None); } diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rs index b8b7d758f2..57c0faa1cb 100644 --- a/crates/ui-egui/src/filter_dialog.rs +++ b/crates/ui-egui/src/filter_dialog.rs @@ -405,6 +405,17 @@ fn apply_command_defaults(app: &PhotocraftApp, command: &str, fields: &mut Map