diff --git a/crates/engine-core/src/cmd/smart_cmds.rs b/crates/engine-core/src/cmd/smart_cmds.rs index bc881935d4..84e50aa8aa 100644 --- a/crates/engine-core/src/cmd/smart_cmds.rs +++ b/crates/engine-core/src/cmd/smart_cmds.rs @@ -95,6 +95,21 @@ fn active_smart(s: &Session) -> std::result::Result<&SmartObject, String> { fn has_smart(s: &Session) -> std::result::Result<(), String> { active_smart(s).map(|_| ()) } +/// Layer ▸ Smart Objects ▸ Reset Transform: one smart object on its own, not fully locked. Measured +/// in the reference application (#18287): greyed with Lock All on the object and with two or more +/// layers selected, whatever their kinds. Lock Position or Lock Image Pixels alone, and a smart +/// object inside a locked group, were not measured: they keep the transform's own refusal. +fn can_reset_transform(s: &Session) -> std::result::Result<(), String> { + active_smart(s)?; + let d = s.active().ok_or("no document open")?; + if d.selected_layers().len() > 1 { + return Err("several layers are selected; Reset Transform works on one smart object".into()); + } + if d.active_layer.and_then(|id| d.doc.layer(id)).is_some_and(|l| l.locks.all) { + return Err("the smart object is locked (Lock All)".into()); + } + Ok(()) +} fn has_linked(s: &Session) -> std::result::Result<(), String> { match active_smart(s)?.source { SmartSource::Linked { .. } => Ok(()), @@ -246,7 +261,7 @@ pub fn specs() -> Vec { "Reset Transform", SO, r##"{"layer":id?} (the placement returns to 100 % of the current contents' own size with rotation, skew, Distort, Perspective and the Warp cleared, centred on the average of the placement's four corners; the smart filters stay; refused with nothing changed when the object can't be re-rendered (a visible filter PhotoCraft doesn't have, or contents that can't be read); one undo step)"##, - has_smart, + can_reset_transform, reset_transform ), spec!("layer.smartObjects.saveContents", "Save Contents", &[], "{} (in an Edit Contents document)", is_smart_child, |s, _| { @@ -419,5 +434,7 @@ pub fn specs() -> Vec { mod stack_cache; mod unpack; +#[cfg(test)] +mod reset_transform_enabled_tests; #[cfg(test)] mod tests; diff --git a/crates/engine-core/src/cmd/smart_cmds/reset_transform_enabled_tests.rs b/crates/engine-core/src/cmd/smart_cmds/reset_transform_enabled_tests.rs new file mode 100644 index 0000000000..2e19128647 --- /dev/null +++ b/crates/engine-core/src/cmd/smart_cmds/reset_transform_enabled_tests.rs @@ -0,0 +1,133 @@ +//! When Layer ▸ Smart Objects ▸ Reset Transform is enabled (#18287). Measured in the reference +//! application: greyed when the selected smart object has Lock All, when two smart objects are +//! selected, and when a smart object and a pixel layer are selected; enabled for one selected +//! embedded or linked smart object; greyed for a pixel layer. Automation refuses the same cases +//! with the reason and changes nothing. Lock Position or Lock Image Pixels alone, and a smart object +//! in a locked group, were not measured and keep today's behaviour. + +use super::*; + +const RESET: &str = "layer.smartObjects.resetTransform"; + +/// A document with one painted layer converted to a smart object, scaled so a reset would change +/// it; the smart object's id. +fn smart_session() -> (Session, LayerId) { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 64, "height": 48})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + paint_active(&mut s); + let id = s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap()["layer"].as_u64().unwrap(); + s.execute("edit.transform", json!({"layer": id, "scaleX": 50, "scaleY": 50})).unwrap(); + (s, LayerId(id)) +} + +fn paint_active(s: &mut Session) { + s.edit("setup", |doc, active| { + let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + surface.fill_rect(Rect::new(8, 6, 40, 30), &[0.8, 0.3, 0.1, 1.0]); + Ok(()) + }) + .unwrap(); +} + +fn state_count(s: &Session) -> usize { + s.active().unwrap().history.entries().len() +} + +fn layer(s: &Session, id: LayerId) -> Layer { + s.active().unwrap().doc.layer(id).unwrap().clone() +} + +/// Greyed with the reason, and the command run anyway (automation, a script) is refused with that +/// reason and leaves the document and its History as they were. +#[track_caller] +fn refused(s: &mut Session, params: Value, why: &str) { + assert!(!s.is_enabled(RESET), "greyed"); + let reason = s.disabled_reason(RESET).unwrap_or_default(); + assert!(reason.contains(why), "the reason says why: {reason:?}"); + let (states, doc) = (state_count(s), s.active().unwrap().doc.clone()); + let err = s.execute(RESET, params).unwrap_err().to_string(); + assert!(err.contains(why), "the error says why: {err}"); + assert_eq!(state_count(s), states, "no history state"); + assert!(s.active().unwrap().doc == doc, "nothing changed"); +} + +#[test] +fn greyed_when_the_smart_object_has_lock_all() { + let (mut s, id) = smart_session(); + assert!(s.is_enabled(RESET), "a single unlocked smart object"); + s.execute("layer.lockLayers", json!({"all": true})).unwrap(); + assert!(layer(&s, id).locks.all, "the fixture is fully locked"); + refused(&mut s, json!({}), "locked"); + refused(&mut s, json!({"layer": id.0}), "locked"); + s.execute("layer.lockLayers", json!({"all": false})).unwrap(); + assert!(s.is_enabled(RESET), "enabled again once unlocked"); +} + +#[test] +fn greyed_with_two_smart_objects_selected() { + let (mut s, a) = smart_session(); + s.execute("layer.smartObjects.newSmartObjectViaCopy", json!({})).unwrap(); + let b = s.active().unwrap().active_layer.unwrap(); + assert_ne!(a, b, "a second smart object"); + assert!(s.is_enabled(RESET), "one selected"); + crate::layer_multi_cmds::set_selection(&mut s, vec![a, b], Some(b), None).unwrap(); + refused(&mut s, json!({}), "layers are selected"); +} + +#[test] +fn greyed_with_a_smart_object_and_a_pixel_layer_selected() { + let (mut s, smart) = smart_session(); + s.execute("layer.new.layer", json!({})).unwrap(); + paint_active(&mut s); + let pixel = s.active().unwrap().active_layer.unwrap(); + // The smart object active, as in the reference's take. + crate::layer_multi_cmds::set_selection(&mut s, vec![pixel, smart], Some(smart), None).unwrap(); + assert_eq!(s.active().unwrap().active_layer, Some(smart), "the smart object is the active layer"); + refused(&mut s, json!({}), "layers are selected"); + refused(&mut s, json!({"layer": smart.0}), "layers are selected"); + // Back to the smart object alone: enabled. + crate::layer_multi_cmds::set_selection(&mut s, vec![smart], Some(smart), None).unwrap(); + assert!(s.is_enabled(RESET)); +} + +#[test] +fn enabled_for_one_embedded_or_linked_smart_object() { + let (mut s, id) = smart_session(); + assert!(matches!(&layer(&s, id).content, LayerContent::Smart(sm) if matches!(sm.source, SmartSource::Embedded { .. }))); + assert!(s.is_enabled(RESET), "embedded"); + s.edit("link", |doc, _| { + let LayerContent::Smart(sm) = &mut doc.layer_mut(id).unwrap().content else { panic!("a smart object") }; + sm.source = SmartSource::Linked { path: "contents.png".into() }; + Ok(()) + }) + .unwrap(); + assert!(s.is_enabled(RESET), "linked"); +} + +/// Lock Position alone was not measured: the item stays enabled as before, and the transform still +/// refuses to move the locked object. +#[test] +fn lock_position_alone_keeps_todays_behaviour() { + let (mut s, id) = smart_session(); + s.execute("layer.lockLayers", json!({"position": true})).unwrap(); + assert!(s.is_enabled(RESET), "enabled as today"); + let before = layer(&s, id); + assert!(s.execute(RESET, json!({})).is_err(), "the transform refuses a position-locked layer"); + assert_eq!(layer(&s, id), before); +} + +#[test] +fn pixel_type_group_and_background_layers_stay_greyed() { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 64, "height": 48})).unwrap(); + let bg = s.active().unwrap().active_layer.unwrap(); + assert!(s.active().unwrap().doc.is_background(bg), "the fixture starts on the Background"); + assert!(!s.is_enabled(RESET), "Background"); + s.execute("layer.new.layer", json!({})).unwrap(); + assert!(!s.is_enabled(RESET), "pixel layer"); + s.execute("type.create", json!({"x": 2, "y": 20, "text": "I", "size": 12})).unwrap(); + assert!(!s.is_enabled(RESET), "type layer"); + s.execute("layer.new.group", json!({})).unwrap(); + assert!(!s.is_enabled(RESET), "group"); +}