From 377a27330cebf705dd04eaf27044e040e05227c2 Mon Sep 17 00:00:00 2001 From: dholzric Date: Mon, 30 Nov 2026 17:37:50 -0600 Subject: [PATCH] Smart Objects: name four History states as the reference does (#18466) Saving a contents document records "Update Smart Objects" in the parent, Convert to Layers "Convert Smart Object to Layers", Update Modified Content "Update Modified Smart Object" and Embed All Linked "Embed All Linked Smart Objects". Menu labels and command ids are unchanged. Closing a changed contents document runs the same commit_child as Save and takes the same name. The automatic update of a changed linked file keeps its own label (#14117). One assertion in embed_all_linked_tests.rs read the old name and follows it. Co-Authored-By: Claude Fable 5.2 --- crates/engine-core/src/cmd/smart_cmds.rs | 4 +- .../src/cmd/smart_cmds/contents.rs | 2 +- .../contents_unpack_history_names_tests.rs | 103 ++++++++++++++++++ .../engine-core/src/cmd/smart_cmds/unpack.rs | 6 +- .../engine-core/src/embed_all_linked_tests.rs | 2 +- 5 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 crates/engine-core/src/cmd/smart_cmds/contents_unpack_history_names_tests.rs diff --git a/crates/engine-core/src/cmd/smart_cmds.rs b/crates/engine-core/src/cmd/smart_cmds.rs index bc881935d4..a32a6f9f09 100644 --- a/crates/engine-core/src/cmd/smart_cmds.rs +++ b/crates/engine-core/src/cmd/smart_cmds.rs @@ -133,7 +133,7 @@ fn embed_all_linked(s: &mut Session, _p: &Value) -> Result { } let parent = doc.id; let contents: Vec = ids.iter().map(|id| smart(doc, *id).map(|sm| sm.contents_id)).collect::>()?; - s.edit("Embed All Linked", |doc, _| { + s.edit("Embed All Linked Smart Objects", |doc, _| { for (id, src) in ids.iter().zip(sources) { smart_mut(doc, *id)?.source = src; refresh_or_fail(doc, *id)?; @@ -268,7 +268,7 @@ pub fn specs() -> Vec { }), spec!("layer.smartObjects.updateModifiedContent", "Update Modified Content", SO, r##"{"layer":id?}"##, has_linked, |s, p| { let id = layer_param(s, p)?; - s.edit("Update Modified Content", |doc, _| refresh_or_fail(doc, id))?; + s.edit("Update Modified Smart Object", |doc, _| refresh_or_fail(doc, id))?; Ok(json!({"layer": id.0})) }), spec!( diff --git a/crates/engine-core/src/cmd/smart_cmds/contents.rs b/crates/engine-core/src/cmd/smart_cmds/contents.rs index 8f28fe2a26..037bf8d9ba 100644 --- a/crates/engine-core/src/cmd/smart_cmds/contents.rs +++ b/crates/engine-core/src/cmd/smart_cmds/contents.rs @@ -143,7 +143,7 @@ pub fn commit_child(s: &mut Session, index: usize) -> Result { let prev = s.active; s.active = Some(parent); s.note_active_change(); - let r = s.edit("Edit Contents", |doc, _| { + let r = s.edit("Update Smart Objects", |doc, _| { let ids = instances(doc, link.contents_id); if ids.is_empty() { return Err(other("the smart object's instances were removed")); diff --git a/crates/engine-core/src/cmd/smart_cmds/contents_unpack_history_names_tests.rs b/crates/engine-core/src/cmd/smart_cmds/contents_unpack_history_names_tests.rs new file mode 100644 index 0000000000..7f60c66514 --- /dev/null +++ b/crates/engine-core/src/cmd/smart_cmds/contents_unpack_history_names_tests.rs @@ -0,0 +1,103 @@ +//! #18466: saving a smart object's contents, Convert to Layers, Update Modified Content and Embed All Linked add +//! History states with the names measured in the reference application. The menu labels keep their own wording. + +use photocraft_geom::Rect; +use serde_json::json; + +use crate::Session; + +/// A document whose active layer is a smart object made from one painted layer. +fn smart_object() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 32, "height": 24})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + paint(&mut s, [1.0, 0.0, 0.0, 1.0]); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + s +} + +/// The same, with the smart object linked to a file in a directory of its own. Returns the directory too. +fn linked_smart_object(name: &str) -> (Session, std::path::PathBuf) { + let dir = std::env::temp_dir().join(format!("pc-so-history-names-{name}-{}", std::process::id())); + std::fs::remove_dir_all(&dir).ok(); + std::fs::create_dir_all(&dir).unwrap(); + let mut s = smart_object(); + s.execute("layer.smartObjects.convertToLinked", json!({"path": dir.join("linked.pcraft").to_str().unwrap()})).unwrap(); + (s, dir) +} + +/// Fills a corner of the active document's top layer. +fn paint(s: &mut Session, colour: [f32; 4]) { + s.edit("setup", |doc, _| { + doc.layers.last_mut().unwrap().surface_mut().unwrap().fill_rect(Rect::new(2, 2, 10, 10), &colour); + Ok(()) + }) + .unwrap(); +} + +/// The number of History states of the active document and the name of the last one. +fn history(s: &Session) -> (usize, Option) { + let history = &s.active().unwrap().history; + (history.past_len(), history.undo_label().map(str::to_string)) +} + +fn named(states: usize, name: &str) -> (usize, Option) { + (states, Some(name.to_string())) +} + +#[test] +fn saving_the_contents_adds_update_smart_objects_to_the_parent() { + let mut s = smart_object(); + let parent = s.active_index().unwrap(); + let (states, _) = history(&s); + let child = s.execute("layer.smartObjects.editContents", json!({})).unwrap()["document"].as_u64().unwrap() as usize; + paint(&mut s, [0.0, 1.0, 0.0, 1.0]); + s.execute("layer.smartObjects.saveContents", json!({})).unwrap(); + s.set_active(parent); + assert_eq!(history(&s), named(states + 1, "Update Smart Objects")); + // Closing a changed contents document saves it the same way, under the same name. + s.set_active(child); + paint(&mut s, [0.0, 0.0, 1.0, 1.0]); + s.execute("file.close", json!({})).unwrap(); + s.set_active(parent); + assert_eq!(history(&s), named(states + 2, "Update Smart Objects")); +} + +#[test] +fn convert_to_layers_adds_convert_smart_object_to_layers() { + let mut s = smart_object(); + let (states, _) = history(&s); + s.execute("layer.smartObjects.convertToLayers", json!({})).unwrap(); + assert_eq!(history(&s), named(states + 1, "Convert Smart Object to Layers")); +} + +#[test] +fn update_modified_content_adds_update_modified_smart_object() { + let (mut s, dir) = linked_smart_object("update"); + let (states, _) = history(&s); + s.execute("layer.smartObjects.updateModifiedContent", json!({})).unwrap(); + assert_eq!(history(&s), named(states + 1, "Update Modified Smart Object")); + std::fs::remove_dir_all(&dir).ok(); +} + +#[test] +fn embed_all_linked_adds_embed_all_linked_smart_objects() { + let (mut s, dir) = linked_smart_object("embed"); + let (states, _) = history(&s); + s.execute("layer.smartObjects.embedAllLinked", json!({})).unwrap(); + assert_eq!(history(&s), named(states + 1, "Embed All Linked Smart Objects")); + std::fs::remove_dir_all(&dir).ok(); +} + +#[test] +fn the_menu_entries_keep_their_labels() { + let _session = crate::session(); + for (id, label) in [ + ("layer.smartObjects.editContents", "Edit Contents"), + ("layer.smartObjects.convertToLayers", "Convert to Layers"), + ("layer.smartObjects.updateModifiedContent", "Update Modified Content"), + ("layer.smartObjects.embedAllLinked", "Embed All Linked"), + ] { + assert_eq!(crate::commands::find(id).unwrap().label, label, "{id}"); + } +} diff --git a/crates/engine-core/src/cmd/smart_cmds/unpack.rs b/crates/engine-core/src/cmd/smart_cmds/unpack.rs index a0bc6c2e73..a2dfbc61f9 100644 --- a/crates/engine-core/src/cmd/smart_cmds/unpack.rs +++ b/crates/engine-core/src/cmd/smart_cmds/unpack.rs @@ -187,7 +187,7 @@ pub(super) fn convert_to_layers(s: &mut Session, p: &Value) -> Result { let discarded = sm.smart_filters.len(); let out = unpacked(so, layers); let (new_id, group) = (out.id, out.is_group()); - s.edit("Convert to Layers", |doc, active| { + s.edit("Convert Smart Object to Layers", |doc, active| { let path = doc.path_of(id).ok_or(EngineError::NoLayer(id))?; *doc.layer_at_mut(&path).ok_or(EngineError::NoLayer(id))? = out; *active = Some(new_id); @@ -224,3 +224,7 @@ fn check_destination_shapes( } Ok(()) } + +#[cfg(test)] +#[path = "contents_unpack_history_names_tests.rs"] +mod contents_unpack_history_names_tests; diff --git a/crates/engine-core/src/embed_all_linked_tests.rs b/crates/engine-core/src/embed_all_linked_tests.rs index 11fd04dc6b..d3b5284e7f 100644 --- a/crates/engine-core/src/embed_all_linked_tests.rs +++ b/crates/engine-core/src/embed_all_linked_tests.rs @@ -99,7 +99,7 @@ fn embed_all_linked_embeds_both_linked_objects_in_one_undoable_step() { assert_eq!(embedded(&s, ids[2]), third, "the already embedded object is untouched"); let entries = s.active().unwrap().history.entries(); assert_eq!(entries.len(), before + 1, "one history step"); - assert_eq!(entries.last().map(String::as_str), Some("Embed All Linked")); + assert_eq!(entries.last().map(String::as_str), Some("Embed All Linked Smart Objects")); assert!(!s.is_enabled("layer.smartObjects.embedAllLinked"), "nothing left to embed"); s.execute("edit.undo", json!({})).unwrap();