From 506a28d798d2c6f4509918f4e4b995518937e00f Mon Sep 17 00:00:00 2001 From: DingusMcRinkyDinky <127614779+DingusMcRinkyDinky@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:08:47 +0100 Subject: [PATCH] Smart objects: Reset Transform on an untransformed object changes nothing (#18286) At 100 %, unrotated, unskewed and without Distort, Perspective or Warp, the reference keeps the item enabled and adds no History state; ours recorded a step and marked the document changed. The step's existing refusals (an unsupported smart filter, a layer locked in place) come first and stay. A Warp alone still counts as a transform. Tests in smart_cmds/reset_transform_noop_tests.rs. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EZbgszHyuEuWAzxaamteuU --- crates/engine-core/src/cmd/smart_cmds.rs | 2 + .../engine-core/src/cmd/smart_cmds/placing.rs | 12 ++ .../smart_cmds/reset_transform_noop_tests.rs | 134 ++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 crates/engine-core/src/cmd/smart_cmds/reset_transform_noop_tests.rs diff --git a/crates/engine-core/src/cmd/smart_cmds.rs b/crates/engine-core/src/cmd/smart_cmds.rs index bc881935d4f..c69f7e05cfe 100644 --- a/crates/engine-core/src/cmd/smart_cmds.rs +++ b/crates/engine-core/src/cmd/smart_cmds.rs @@ -183,6 +183,8 @@ mod contents; mod filters; mod placing; mod rendering; +#[cfg(test)] +mod reset_transform_noop_tests; mod source; pub(crate) use contents::on_close; diff --git a/crates/engine-core/src/cmd/smart_cmds/placing.rs b/crates/engine-core/src/cmd/smart_cmds/placing.rs index f08d35a7b75..eb2f1e0ec51 100644 --- a/crates/engine-core/src/cmd/smart_cmds/placing.rs +++ b/crates/engine-core/src/cmd/smart_cmds/placing.rs @@ -82,6 +82,18 @@ pub(super) fn reset_transform(s: &mut Session, p: &Value) -> Result { if let Some(name) = stuck.first() { return Err(other(format!("the {name} smart filter is not supported, so the smart object's transform can't be reset"))); } + // Already at 100 %, unrotated, unskewed, without Distort, Perspective or Warp (a whole-pixel move + // is no transform: the reset keeps the object where it sits): the reference application leaves + // the item enabled and changes nothing, adding no History state (#18286), so the document's + // changed flag stays as it was too. Checked after the refusals the step has always made (a filter + // it can't apply, a layer locked in place), which stay as they were. + if sm.warp.is_none() && sm.perspective.is_none() && sm.transform.m[..4] == [1.0, 0.0, 0.0, 1.0] { + if crate::transform_cmds::locked_for_transform(&st.doc, id) { + let name = st.doc.layer(id).map_or("", |l| l.name.as_str()); + return Err(other(format!("layer \"{name}\" is locked"))); + } + return Ok(json!({"layer": id.0, "transform": sm.transform.m})); + } // The contents' own box in source pixels: 100 % maps it one-to-one onto the document. let [x0, y0, x1, y1] = source_box(&st.doc, sm).ok_or_else(|| other("the smart object's contents are unavailable"))?; let (cx, cy) = ((x0 + x1) / 2.0, (y0 + y1) / 2.0); diff --git a/crates/engine-core/src/cmd/smart_cmds/reset_transform_noop_tests.rs b/crates/engine-core/src/cmd/smart_cmds/reset_transform_noop_tests.rs new file mode 100644 index 00000000000..ee39d9d7cc3 --- /dev/null +++ b/crates/engine-core/src/cmd/smart_cmds/reset_transform_noop_tests.rs @@ -0,0 +1,134 @@ +//! Reset Transform on a smart object that has no transform changes nothing: no History state, and +//! the document's changed flag stays as it was (#18286). Measured in the reference application: the +//! menu item stays enabled on such an object, and choosing it adds no state; on a transformed +//! object it adds exactly one. + +use super::*; + +const RESET: &str = "layer.smartObjects.resetTransform"; + +/// A document with one painted layer converted to a smart object; the smart object's id. +fn smart_session() -> (Session, u64) { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 64, "height": 48})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + 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(); + let id = s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap()["layer"].as_u64().unwrap(); + (s, id) +} + +fn state_count(s: &Session) -> usize { + s.active().unwrap().history.entries().len() +} + +fn dirty(s: &Session) -> bool { + s.active().unwrap().is_dirty() +} + +/// Marks the document saved, as File ▸ Save would. +fn mark_saved(s: &mut Session) { + let st = s.active_mut().unwrap(); + st.saved_revision = st.revision; +} + +fn layer(s: &Session, id: u64) -> Layer { + s.active().unwrap().doc.layer(LayerId(id)).unwrap().clone() +} + +/// The issue's steps: Convert to Smart Object, then Reset Transform. Enabled, and nothing happens: +/// the layer, the History and a saved document's clean state all stay. +#[test] +fn an_untransformed_smart_object_resets_without_a_state_and_stays_clean() { + let (mut s, id) = smart_session(); + assert!(s.is_enabled(RESET), "the item stays enabled on an untransformed smart object"); + mark_saved(&mut s); + let (states, before) = (state_count(&s), layer(&s, id)); + s.execute(RESET, json!({})).unwrap(); + assert_eq!(state_count(&s), states, "no history state"); + assert!(!dirty(&s), "a saved document stays unmodified"); + assert_eq!(layer(&s, id), before, "the layer is unchanged"); + assert!(s.is_enabled(RESET), "still enabled afterwards"); +} + +/// A document that already had changes keeps them marked: the no-op neither adds a step nor +/// clears the flag. +#[test] +fn a_modified_document_stays_modified() { + let (mut s, id) = smart_session(); + assert!(dirty(&s), "the fixture's edits mark the document changed"); + let states = state_count(&s); + s.execute(RESET, json!({"layer": id})).unwrap(); + assert_eq!(state_count(&s), states, "no history state for a named layer either"); + assert!(dirty(&s), "still changed"); +} + +/// Moving by whole pixels is no transform: the placement is still 100 %, unrotated and unskewed, +/// and Reset Transform keeps the object where it is, so it changes nothing. +#[test] +fn a_moved_smart_object_is_untransformed() { + let (mut s, id) = smart_session(); + s.execute("layer.translate", json!({"dx": 7, "dy": -3})).unwrap(); + let (states, before) = (state_count(&s), layer(&s, id)); + s.execute(RESET, json!({})).unwrap(); + assert_eq!(state_count(&s), states, "no history state"); + assert_eq!(layer(&s, id), before, "the layer stays where it was moved to"); +} + +/// On a transformed object the reset is still one state, and a second reset (now untransformed) +/// adds none. +#[test] +fn a_transformed_smart_object_still_adds_exactly_one_state() { + let (mut s, id) = smart_session(); + s.execute("edit.transform", json!({"layer": id, "scaleX": 50, "scaleY": 30, "rotate": 30})).unwrap(); + mark_saved(&mut s); + let states = state_count(&s); + s.execute(RESET, json!({})).unwrap(); + assert_eq!(state_count(&s), states + 1, "one history state"); + assert!(dirty(&s), "a real reset changes the document"); + s.execute(RESET, json!({})).unwrap(); + assert_eq!(state_count(&s), states + 1, "the second reset finds nothing to do"); +} + +/// Only a Warp, no other transform: still transformed (unmeasured in the reference; the issue keeps +/// today's behaviour), so the reset takes the Warp off in one state. +#[test] +fn a_warp_alone_counts_as_a_transform() { + let (mut s, _) = smart_session(); + s.execute("edit.transform.warp", json!({"style": "arc", "bend": 30})).unwrap(); + let states = state_count(&s); + s.execute(RESET, json!({})).unwrap(); + assert_eq!(state_count(&s), states + 1, "one history state"); + let d = s.active().unwrap(); + let LayerContent::Smart(sm) = &d.doc.layer(d.active_layer.unwrap()).unwrap().content else { panic!("a smart object") }; + assert!(sm.warp.is_none(), "the Warp is gone"); +} + +/// Distort leaves a projective placement whose affine part can read as 100 %: still transformed. +#[test] +fn a_distorted_smart_object_counts_as_transformed() { + let (mut s, id) = smart_session(); + let r = layer(&s, id).surface().unwrap().content_bounds(); + let (x0, y0, x1, y1) = (f64::from(r.x0), f64::from(r.y0), f64::from(r.x1), f64::from(r.y1)); + s.execute("edit.transform", json!({"layer": id, "rect": [x0, y0, x1, y1], "quad": [[x0, y0], [x1, y0], [x1 - 6.0, y1 - 4.0], [x0, y1]]})).unwrap(); + let states = state_count(&s); + s.execute(RESET, json!({})).unwrap(); + assert_eq!(state_count(&s), states + 1, "one history state"); +} + +/// A position-locked object was refused before and still is, transformed or not (unmeasured in the +/// reference; the issue keeps today's behaviour): nothing changes and no state is added. +#[test] +fn a_position_locked_untransformed_object_is_still_refused() { + let (mut s, id) = smart_session(); + s.execute("layer.lockLayers", json!({"position": true})).unwrap(); + let (states, before) = (state_count(&s), layer(&s, id)); + let err = s.execute(RESET, json!({})).unwrap_err().to_string(); + assert!(err.contains("locked"), "{err}"); + assert_eq!(state_count(&s), states, "no history state"); + assert_eq!(layer(&s, id), before); +}