PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Smart Objects: stacked layers sit at the contents top level (#2657) (#2676)
Valentin Kleindfa4a2e51ee5
diff --git a/crates/engine/src/automate_cmds.rs b/crates/engine/src/automate_cmds.rsindex 0ae4d5ee5..6827c1424 100644--- a/crates/engine/src/automate_cmds.rs+++ b/crates/engine/src/automate_cmds.rs@@ -379,9 +379,8 @@ fn statistics(s: &mut Session, p: &Value) -> Result<Value> {s.execute("edit.autoAlignLayers", json!({"projection": "auto"}))?;let name = s.active().map(|d| d.doc.name.clone()).unwrap_or_default();s.edit("Convert to Smart Object", |doc, active| {- let children = std::mem::take(&mut doc.layers);- let group = Layer::new(name.clone(), LayerContent::Group(photocraft_doc::Group { children, expanded: true, artboard: None }));- let smart = crate::smart_cmds::layer_to_smart(doc, &group)?;+ let layers = std::mem::take(&mut doc.layers);+ let smart = crate::smart_cmds::layers_to_smart(doc, &name, layers)?;*active = Some(smart.id);doc.layers = vec![smart];Ok(())diff --git a/crates/engine/src/automate_cmds/tests.rs b/crates/engine/src/automate_cmds/tests.rsindex 9a51f6808..f6dff2317 100644--- a/crates/engine/src/automate_cmds/tests.rs+++ b/crates/engine/src/automate_cmds/tests.rs@@ -130,6 +130,43 @@ fn statistics_makes_a_stack_mode_smart_object() {assert!(s.execute("file.scripts.statistics", json!({"mode": "average", "input": dir})).is_err());}+#[test]+fn stacked_smart_object_holds_the_loaded_layers_at_its_top_level() {+ // Like Photoshop, Load Files into Stack › Create Smart Object (Statistics without alignment) and+ // Statistics with alignment embed one layer per file, bottom to top, with no wrapper group.+ let dir = tmp("stack-top");+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 96, "height": 72, "background": "#606060"})).unwrap();+ s.edit("texture", |doc, active| {+ let surf = doc.layer_mut(active.ok_or(EngineError::NoDocument)?).and_then(Layer::surface_mut).ok_or(EngineError::NoDocument)?;+ for k in 0..40 {+ let (x, y, v) = ((k * 37) % 88, (k * 23) % 66, (k % 7) as f32 / 6.0);+ surf.fill_rect(photocraft_geom::Rect::new(x, y, x + 3 + k % 5, y + 2 + k % 4), &[v, 1.0 - v, 0.5, 1.0]);+ }+ Ok(())+ })+ .unwrap();+ let source = photocraft_compose::flatten(&s.active().unwrap().doc);+ let files: Vec<String> = (0..3)+ .map(|i| {+ let path = format!("{dir}/img{i}.png");+ crate::file_cmds::save_doc(&s.active().unwrap().doc, &path, None).unwrap();+ path+ })+ .collect();+ for align in [false, true] {+ let mut s = Session::new();+ s.execute("file.scripts.statistics", json!({"mode": "median", "input": files, "align": align})).unwrap();+ let d = &s.active().unwrap().doc;+ let LayerContent::Smart(so) = &d.layers[0].content else { panic!("not a smart object") };+ let photocraft_doc::SmartSource::Embedded { file_name, bytes } = &so.source else { panic!("not embedded") };+ let inner = crate::smart_cmds::decode_source(file_name, bytes).unwrap();+ let names: Vec<&str> = inner.layers.iter().map(|l| l.name.as_str()).collect();+ assert_eq!(names, ["img0.png", "img1.png", "img2.png"], "align {align}");+ assert_eq!(photocraft_compose::flatten(d).px, source.px, "align {align}");+ }+}+#[test]fn contact_sheet_places_thumbnails_with_captions() {let dir = tmp("contact");diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rsindex 103a1f6f3..8c12a56a8 100644--- a/crates/engine/src/file_cmds.rs+++ b/crates/engine/src/file_cmds.rs@@ -840,10 +840,10 @@ fn load_files_into_stack(s: &mut Session, p: &Value) -> Result<Value> {let n = stack.layers.len();if p.get("createSmartObject").and_then(Value::as_bool).unwrap_or(false) {// "Create Smart Object after Loading Layers": the layers go inside one smart object, ready- // for Layer › Smart Objects › Stack Mode.- let children = std::mem::take(&mut stack.layers);- let group = Layer::new(stem(&paths[0]), LayerContent::Group(photocraft_doc::Group { children, expanded: true, artboard: None }));- let smart = crate::smart_cmds::layer_to_smart(&stack, &group)?;+ // for Layer › Smart Objects › Stack Mode. Like Photoshop, they sit at the top level of the+ // contents, with no wrapper group.+ let layers = std::mem::take(&mut stack.layers);+ let smart = crate::smart_cmds::layers_to_smart(&stack, &stem(&paths[0]), layers)?;stack.layers = vec![smart];}let i = s.add_document(stack, None);