mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Integration: Artboard from Layers dialog opens at the layers' size; refuse -0.4 (#9262)
The dialog remembered the last Width/Height: make a 1080x1350 board from one
layer, then open it for a 20x30 layer and press OK, and that layer got a
1080x1350 board too. It also opened at 0 and an empty Name. It now starts at
the selected layers' size and the next "Artboard N" every time, as in the
reference application. `{"height": -0.4}` rounded to 0 and kept the bounds;
negative sizes are now refused before rounding.
Co-authored-by: DuBearte <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
b13274419f
commit
9462fa130c
3 files changed
+75
-2
No files matched your search
@@ -303,6 +303,21 @@ fn artboard_from_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
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<Item = &'a Layer>, 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<Value> {
|
||||
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<Value> {
|
||||
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<Value> {
|
||||
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() });
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -405,6 +405,17 @@ fn apply_command_defaults(app: &PhotocraftApp, command: &str, fields: &mut Map<S
|
||||
if command == "edit.definePattern" {
|
||||
fields.insert("name".into(), json!(photocraft_engine::pattern_cmds::default_name(&app.session)));
|
||||
}
|
||||
// The reference application's Artboard from Layers opens at the next Artboard N and the
|
||||
// selected layers' size every time, never a remembered size; past the field's 30000 px
|
||||
// range, 0 keeps the bounds.
|
||||
if command == "layer.new.artboardFromLayers"
|
||||
&& let Some((name, [w, h])) = photocraft_engine::artboard_cmds::from_layers_defaults(&app.session)
|
||||
{
|
||||
let fits = w <= 30_000 && h <= 30_000;
|
||||
fields.insert("name".into(), json!(name));
|
||||
fields.insert("width".into(), json!(if fits { w } else { 0 }));
|
||||
fields.insert("height".into(), json!(if fits { h } else { 0 }));
|
||||
}
|
||||
if command == "layer.layerStyle.globalLight" {
|
||||
global_light_defaults(app, fields);
|
||||
}
|
||||
@@ -2690,6 +2701,30 @@ mod tests {
|
||||
assert!(cmyk.iter().any(|o| o == "coated-cmyk") && !cmyk.iter().any(|o| o == "srgb"), "a CMYK document gets CMYK profiles");
|
||||
}
|
||||
|
||||
/// #9087: Artboard from Layers opens at the selected layers' size and the next Artboard N, as
|
||||
/// in the reference application, never at a size remembered from other layers.
|
||||
#[test]
|
||||
fn artboard_from_layers_opens_at_the_selected_layers_size() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 100, "height": 80})).unwrap();
|
||||
app.run("layer.new.layer", json!({})).unwrap();
|
||||
app.run("select.rect", json!({"x": 10, "y": 5, "width": 20, "height": 30})).unwrap();
|
||||
app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
app.session.prefs.edit(|prefs| {
|
||||
prefs.dialogs.insert("layer.new.artboardFromLayers".into(), json!({"width": 1080, "height": 1350}));
|
||||
});
|
||||
let id = open(&mut app, "layer.new.artboardFromLayers").unwrap();
|
||||
let fields = &app.ui.dialog_mut(id).unwrap().fields;
|
||||
assert_eq!((&fields["width"], &fields["height"]), (&json!(20), &json!(30)), "not the remembered 1080 x 1350");
|
||||
assert_eq!(fields["name"], json!("Artboard 1"));
|
||||
crate::dialogs::confirm(&mut app, id).unwrap();
|
||||
let doc = &app.session.active().unwrap().doc;
|
||||
let boards = doc.artboards();
|
||||
assert_eq!(boards.len(), 1);
|
||||
assert_eq!((boards[0].1, boards[0].2.rect), ("Artboard 1", photocraft_geom::Rect::new(10, 5, 30, 35)));
|
||||
}
|
||||
|
||||
/// #4951: a profile remembered from a document of another colour mode isn't preselected.
|
||||
#[test]
|
||||
fn assign_profile_drops_a_remembered_profile_that_does_not_fit() {
|
||||
|
||||
Reference in new issue
Block a user