Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Disable Convert to Smart Object for layers that are already smart objects

NetterSilver2466f5c3a74b
diff --git a/crates/automation/tests/mcp.rs b/crates/automation/tests/mcp.rs
index 7386f3d58..6236c5acd 100644
--- a/crates/automation/tests/mcp.rs
+++ b/crates/automation/tests/mcp.rs
@@ -731,11 +731,12 @@ async fn embedded_smart_objects_can_be_edited_saved_and_unpacked_without_file_au
let client = connect(PhotocraftMcp::headless()).await;
let run = async |id: &str, params: Value| json_of(&call(&client, "command_run", json!({"id": id, "params": params})).await);
json_of(&call(&client, "doc_new", json!({"width": 16, "height": 12, "background": "white"})).await);
- run("layer.smartObjects.convertToSmartObject", json!({})).await;
let outer = run("layer.smartObjects.convertToSmartObject", json!({})).await["layer"].clone();
// An explicit target works even with a regular pixel layer selected.
run("layer.new.layer", json!({"name": "Other layer"})).await;
assert_eq!(run("layer.smartObjects.editContents", json!({"layer": outer})).await["document"], 1);
+ // A smart object cannot be converted again, so nest one inside the contents instead.
+ run("layer.smartObjects.convertToSmartObject", json!({})).await;
assert_eq!(run("layer.smartObjects.editContents", json!({})).await["document"], 2);
run("layer.setProps", json!({"name": "Edited photo", "opacity": 0.5})).await;
assert_eq!(run("layer.smartObjects.saveContents", json!({})).await["updated"], true);
diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rs
index 6ffc89e67..9f99d3792 100644
--- a/crates/engine/src/smart_cmds.rs
+++ b/crates/engine/src/smart_cmds.rs
@@ -1177,6 +1177,13 @@ fn not_smart(s: &Session) -> std::result::Result<(), String> {
if active_smart(s).is_ok() { Err("the layer is already a smart object".into()) } else { Ok(()) }
}
+/// Convert to Smart Object: refused only when the single targeted layer is already a smart
+/// object. A multi-layer selection can still be combined (nesting any smart object in it).
+fn convertible_to_smart(s: &Session) -> std::result::Result<(), String> {
+ convertible(s)?;
+ if crate::layer_multi_cmds::selected(s).len() <= 1 { not_smart(s) } else { Ok(()) }
+}
+
macro_rules! spec {
($id:literal, $label:literal, $menu:expr, $params:literal, $en:expr, $run:expr) => {
CommandSpec { id: $id, label: $label, menu: $menu, shortcut: None, params: $params, enabled: $en, run: $run, journal: true }
@@ -1193,7 +1200,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Convert to Smart Object",
SO,
r##"{"layer":id?} (default: combine selected layers)"##,
- convertible,
+ convertible_to_smart,
convert
),
spec!(
diff --git a/crates/engine/src/smart_cmds/tests.rs b/crates/engine/src/smart_cmds/tests.rs
index 1caaff6ce..f1929166c 100644
--- a/crates/engine/src/smart_cmds/tests.rs
+++ b/crates/engine/src/smart_cmds/tests.rs
@@ -149,6 +149,26 @@ fn empty_layer_cannot_be_converted() {
assert!(s.execute("layer.smartObjects.convertToSmartObject", json!({})).is_err());
}
+#[test]
+fn convert_to_smart_object_is_disabled_for_a_layer_that_is_already_smart() {
+ let mut s = session(8);
+ paint(&mut s);
+ assert!(s.is_enabled("layer.smartObjects.convertToSmartObject"));
+ convert(&mut s);
+ assert!(!s.is_enabled("layer.smartObjects.convertToSmartObject"));
+}
+
+#[test]
+fn convert_to_smart_object_stays_enabled_for_a_selection_that_includes_a_smart_object() {
+ let mut s = session(8);
+ paint(&mut s);
+ let smart_id = LayerId(convert(&mut s));
+ s.execute("layer.new.layer", json!({})).unwrap();
+ let other_id = s.active().unwrap().active_layer.unwrap();
+ crate::layer_multi_cmds::set_selection(&mut s, vec![smart_id, other_id], Some(other_id), None).unwrap();
+ assert!(s.is_enabled("layer.smartObjects.convertToSmartObject"));
+}
+
#[test]
fn smart_filters_are_re_editable() {
for depth in [8, 16] {