From 80fa3a977c2c22ed979f12b3677a97027f720a56 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Mon, 30 Nov 2026 13:20:30 -0800 Subject: [PATCH 1/2] Disable Transform Again until a transform exists --- crates/engine-core/src/cmd/edit_cmds.rs | 23 +++++++++++++--- .../cmd/transform_again_needs_prior_tests.rs | 27 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 crates/engine-core/src/cmd/transform_again_needs_prior_tests.rs diff --git a/crates/engine-core/src/cmd/edit_cmds.rs b/crates/engine-core/src/cmd/edit_cmds.rs index 5849b59bfd..16f088d2fc 100644 --- a/crates/engine-core/src/cmd/edit_cmds.rs +++ b/crates/engine-core/src/cmd/edit_cmds.rs @@ -920,11 +920,24 @@ fn transform_again_copy(s: &mut Session) -> Result { } } +fn last_transform(s: &Session) -> Result<&Value> { + s.journal + .iter() + .rev() + .find(|(id, _)| id == "edit.transform") + .map(|(_, params)| params) + .ok_or_else(|| EngineError::Other("there is no transform to repeat".into())) +} + +fn can_transform_again(s: &Session) -> std::result::Result<(), String> { + has_doc(s)?; + last_transform(s).map(|_| ()).map_err(|error| error.to_string()) +} + /// The `edit.transform` params that repeat the last transform on the layers `ids` (their frame /// together is the box it starts from). fn transform_again_params(s: &Session, ids: &[LayerId]) -> Result { - let (_, last) = - s.journal.iter().rev().find(|(id, _)| id == "edit.transform").cloned().ok_or(EngineError::Other("there is no transform to repeat".into()))?; + let last = last_transform(s)?; let (rect, quad) = (last.get("rect").cloned(), last.get("quad").cloned()); let mut p = json!({"interpolation": last.get("interpolation").cloned().unwrap_or(json!("bicubic"))}); match (rect, quad, last.get("matrix").cloned()) { @@ -1258,7 +1271,7 @@ pub fn specs() -> Vec { spec!("image.autoContrast", "Auto Contrast", &["Image"], Some("Cmd+Alt+Shift+L"), "{}", has_pixels, |s, _| auto_adjust(s, "contrast")), spec!("image.autoColor", "Auto Color", &["Image"], Some("Cmd+Shift+B"), "{}", has_pixels, |s, _| auto_adjust(s, "color")), spec!("edit.toggleLastState", "Toggle Last State", &["Edit"], Some("Cmd+Alt+Z"), "{}", has_doc, |s, _| toggle_last_state(s)), - spec!("edit.transform.again", "Again", &["Edit", "Transform"], Some("Cmd+Shift+T"), "{}", has_doc, |s, _| transform_again(s)), + spec!("edit.transform.again", "Again", &["Edit", "Transform"], Some("Cmd+Shift+T"), "{}", can_transform_again, |s, _| transform_again(s)), spec!( "edit.transform.againCopy", "Transform Again on a Copy", @@ -1314,3 +1327,7 @@ pub fn specs() -> Vec { #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "transform_again_needs_prior_tests.rs"] +mod transform_again_needs_prior_tests; diff --git a/crates/engine-core/src/cmd/transform_again_needs_prior_tests.rs b/crates/engine-core/src/cmd/transform_again_needs_prior_tests.rs new file mode 100644 index 0000000000..52a9b164ab --- /dev/null +++ b/crates/engine-core/src/cmd/transform_again_needs_prior_tests.rs @@ -0,0 +1,27 @@ +use serde_json::json; + +#[test] +fn transform_again_is_enabled_only_after_a_transform_in_the_session() { + let command = "edit.transform.again"; + let mut session = crate::session(); + assert!(!session.is_enabled(command)); + + session.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); + session.execute("layer.new.layer", json!({"name": "Pixels"})).unwrap(); + session.execute("select.rect", json!({"x": 10, "y": 10, "width": 25, "height": 20})).unwrap(); + session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + session.execute("select.deselect", json!({})).unwrap(); + assert!(!session.is_enabled(command), "ordinary document edits do not enable Again"); + let history = session.active().unwrap().history.past_len(); + let error = session.execute(command, json!({})).unwrap_err().to_string(); + assert!(error.contains("there is no transform to repeat"), "{error}"); + assert_eq!(session.active().unwrap().history.past_len(), history); + + session.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 10, 0]})).unwrap(); + assert!(session.is_enabled(command)); + session.execute(command, json!({})).unwrap(); + assert_eq!(session.active().unwrap().history.past_len(), history + 2); + + session.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); + assert!(session.is_enabled(command), "the previous transform remains session-wide when opening another document"); +} From 2a6fb517384f968f7a759d54a2eafe91ab61d654 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Mon, 30 Nov 2026 14:07:03 -0800 Subject: [PATCH 2/2] Use an engine-core fixture for Transform Again --- .../src/cmd/transform_again_needs_prior_tests.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/engine-core/src/cmd/transform_again_needs_prior_tests.rs b/crates/engine-core/src/cmd/transform_again_needs_prior_tests.rs index 52a9b164ab..dd34796639 100644 --- a/crates/engine-core/src/cmd/transform_again_needs_prior_tests.rs +++ b/crates/engine-core/src/cmd/transform_again_needs_prior_tests.rs @@ -1,3 +1,4 @@ +use photocraft_geom::Rect; use serde_json::json; #[test] @@ -8,9 +9,12 @@ fn transform_again_is_enabled_only_after_a_transform_in_the_session() { session.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); session.execute("layer.new.layer", json!({"name": "Pixels"})).unwrap(); - session.execute("select.rect", json!({"x": 10, "y": 10, "width": 25, "height": 20})).unwrap(); - session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - session.execute("select.deselect", json!({})).unwrap(); + session + .edit("Paint fixture", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 10, 35, 30), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); assert!(!session.is_enabled(command), "ordinary document edits do not enable Again"); let history = session.active().unwrap().history.past_len(); let error = session.execute(command, json!({})).unwrap_err().to_string();