Merge #18508 into the merge queue batch

Disable Transform Again until a transform exists (#18502)
This commit is contained in:
David Mann committed 2026-12-01 10:55:46 +01:00
commit 496f3fec33
2 files changed
+51 -3

No files matched your search

+20 -3
View File
@@ -931,11 +931,24 @@ fn transform_again_copy(s: &mut Session) -> Result<Value> {
}
}
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<Value> {
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()) {
@@ -1269,7 +1282,7 @@ pub fn specs() -> Vec<CommandSpec> {
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",
@@ -1329,3 +1342,7 @@ mod tests;
#[cfg(test)]
#[path = "layer_via_cut_locked_group_tests.rs"]
mod layer_via_cut_locked_group_tests;
#[cfg(test)]
#[path = "transform_again_needs_prior_tests.rs"]
mod transform_again_needs_prior_tests;
@@ -0,0 +1,31 @@
use photocraft_geom::Rect;
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
.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();
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");
}