diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/align.rs b/crates/engine-core/src/cmd/layer_multi_cmds/align.rs index 07fcdf4f031..d3835c136aa 100644 --- a/crates/engine-core/src/cmd/layer_multi_cmds/align.rs +++ b/crates/engine-core/src/cmd/layer_multi_cmds/align.rs @@ -53,7 +53,16 @@ pub(super) fn align(s: &mut Session, p: &Value, kind: &str) -> Result { let sel = selected(s); let to = p.get("to").and_then(Value::as_str).unwrap_or("auto").to_string(); let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone(); - let moved = s.edit("Align", |doc, _| { + // The History state is named after the entry (#18283). + let name = match edge { + Edge::Top => "Align Top Edges", + Edge::VCenter => "Align Vertical Centers", + Edge::Bottom => "Align Bottom Edges", + Edge::Left => "Align Left Edges", + Edge::HCenter => "Align Horizontal Centers", + Edge::Right => "Align Right Edges", + }; + let moved = s.edit(name, |doc, _| { let items = bounded_selection(doc, &sel); if items.is_empty() { return Err(EngineError::Other("the selected layers have no content to align".into())); @@ -160,3 +169,7 @@ pub(super) fn distribute(s: &mut Session, kind: &str) -> Result { })?; Ok(json!({"moved": moved})) } + +#[cfg(test)] +#[path = "align_history_names_tests.rs"] +mod align_history_names_tests; diff --git a/crates/engine-core/src/cmd/layer_multi_cmds/align_history_names_tests.rs b/crates/engine-core/src/cmd/layer_multi_cmds/align_history_names_tests.rs new file mode 100644 index 00000000000..f6c762c433d --- /dev/null +++ b/crates/engine-core/src/cmd/layer_multi_cmds/align_history_names_tests.rs @@ -0,0 +1,103 @@ +//! #18283: each Layer › Align entry leaves one History state named after the entry, such as "Align Left Edges", +//! with and without a pixel selection, instead of "Align" for all six. A run that a position-locked layer refuses +//! records no state. The Distribute entries were not measured and keep their name. + +use photocraft_doc::LayerId; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::Session; + +/// The command's last id segment and the History state the reference application records for it. +const NAMES: [(&str, &str); 6] = [ + ("topEdges", "Align Top Edges"), + ("verticalCenters", "Align Vertical Centers"), + ("bottomEdges", "Align Bottom Edges"), + ("leftEdges", "Align Left Edges"), + ("horizontalCenters", "Align Horizontal Centers"), + ("rightEdges", "Align Right Edges"), +]; + +fn session() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 100, "height": 80})).unwrap(); + s +} + +/// A new pixel layer with an opaque rectangle. +fn rect_layer(s: &mut Session, r: Rect) -> LayerId { + let id = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); + s.edit("paint", |doc, _| { + doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(r, &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + id +} + +fn select(s: &mut Session, ids: &[LayerId]) { + s.execute("layer.select", json!({"layer": ids[0].0})).unwrap(); + for id in &ids[1..] { + s.execute("layer.select", json!({"layer": id.0, "mode": "toggle"})).unwrap(); + } +} + +/// Two layers that share no edge and no centre, so every Align entry moves one of them. +fn two_layers(s: &mut Session) -> [LayerId; 2] { + [rect_layer(s, Rect::new(10, 5, 20, 15)), rect_layer(s, Rect::new(40, 30, 70, 60))] +} + +/// How many History states there are, and the name of the newest. +fn history(s: &Session) -> (usize, Option) { + let history = &s.active().unwrap().history; + (history.past_len(), history.undo_label().map(String::from)) +} + +#[test] +fn each_align_entry_names_its_one_history_state() { + for (kind, name) in NAMES { + // Several layers, aligned to each other. + let mut s = session(); + let layers = two_layers(&mut s); + select(&mut s, &layers); + let states = history(&s).0; + s.execute(&format!("layer.align.{kind}"), json!({})).unwrap(); + assert_eq!(history(&s), (states + 1, Some(name.to_string())), "{kind}"); + // One layer, aligned to a pixel selection. + let mut s = session(); + rect_layer(&mut s, Rect::new(10, 5, 20, 15)); + s.execute("select.rect", json!({"x": 50, "y": 40, "width": 30, "height": 30})).unwrap(); + let states = history(&s).0; + s.execute(&format!("layer.align.{kind}"), json!({})).unwrap(); + assert_eq!(history(&s), (states + 1, Some(name.to_string())), "{kind} to a selection"); + } +} + +#[test] +fn a_run_refused_by_a_position_locked_layer_records_no_state() { + for all in [false, true] { + let mut s = session(); + let layers = two_layers(&mut s); + // Top Edges has to move the lower rectangle, which is locked: Lock position, then Lock all. + s.edit("lock", |doc, _| { + let locks = &mut doc.layer_mut(layers[1]).unwrap().locks; + (locks.position, locks.all) = (true, all); + Ok(()) + }) + .unwrap(); + select(&mut s, &layers); + let before = history(&s); + assert!(s.execute("layer.align.topEdges", json!({})).is_err(), "lock all: {all}"); + assert_eq!(history(&s), before, "lock all: {all}"); + } +} + +/// Not measured, so it keeps the name it had. +#[test] +fn distribute_keeps_its_name() { + let mut s = session(); + let layers = [rect_layer(&mut s, Rect::new(0, 0, 10, 10)), rect_layer(&mut s, Rect::new(20, 30, 30, 40)), rect_layer(&mut s, Rect::new(80, 60, 90, 70))]; + select(&mut s, &layers); + s.execute("layer.distribute.leftEdges", json!({})).unwrap(); + assert_eq!(history(&s).1.as_deref(), Some("Distribute")); +}