Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(engine): flip and rotate presets turn about the selection's bounds, as Free Transform does (#6928) (#6967)

Korg1chf04fc80267b3
diff --git a/crates/engine/src/extra_cmds.rs b/crates/engine/src/extra_cmds.rs
index c29186216..e75b2347a 100644
--- a/crates/engine/src/extra_cmds.rs
+++ b/crates/engine/src/extra_cmds.rs
@@ -335,15 +335,18 @@ fn clouds(s: &mut Session, p: &Value, difference: bool) -> Result<Value> {
const TRANSFORM_TARGET: &str =
r#"{"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target (an unlinked mask, an alpha channel or the Quick Mask turns alone)}"#;
+/// Flips and turns about the centre of the box Free Transform would open: with a selection
+/// limiting the transform it is the selection's own bounds, as in Photoshop, so Select All
+/// mirrors across the canvas (#6928); otherwise the content's bounds.
fn transform_preset(s: &mut Session, p: &Value, kind: &str) -> Result<Value> {
let d = s.active().ok_or(EngineError::NoDocument)?;
// A targeted alpha channel, the Quick Mask or an unlinked layer mask turns by itself (#2835).
let (b, mut params) = match crate::transform_cmds::lone_target(&d.doc, d.active_layer, p)? {
- Some(surf) => (crate::transform_cmds::target_bounds(&d.doc, surf), json!({"target": p.get("target")})),
+ Some(surf) => (crate::transform_cmds::target_frame(&d.doc, surf), json!({"target": p.get("target")})),
None => {
let id = d.active_layer.ok_or(EngineError::Other("no active layer".into()))?;
let l = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?;
- (crate::transform_cmds::transform_bounds(&d.doc, l), json!({"layer": id.0}))
+ (crate::transform_cmds::transform_frame(&d.doc, l), json!({"layer": id.0}))
}
};
if b.is_empty() {
@@ -1469,6 +1472,49 @@ mod tests {
assert_eq!(pixel(&s, 10, 10), vec![0.0, 1.0, 0.0, 1.0]);
}
+ /// #6928: with a selection, the presets flip and turn about the selection's own bounds, as
+ /// in Photoshop: Select All mirrors across the canvas, a marquee across its own centre.
+ /// Without a selection they keep turning about the content (above).
+ #[test]
+ fn transform_presets_turn_about_the_selection() {
+ let mut s = session(8);
+ paint_square(&mut s, Rect::new(10, 10, 20, 14), [1.0, 0.0, 0.0, 1.0]);
+ paint_square(&mut s, Rect::new(10, 10, 11, 11), [0.0, 1.0, 0.0, 1.0]);
+ let green = vec![0.0, 1.0, 0.0, 1.0];
+ // Select All on the 40 × 30 canvas: across the canvas's centre lines.
+ s.execute("select.all", json!({})).unwrap();
+ let past = s.active().unwrap().history.past_len();
+ s.execute("edit.transform.flipHorizontal", json!({})).unwrap();
+ assert_eq!(active(&s).surface().unwrap().content_bounds(), Rect::new(20, 10, 30, 14));
+ assert_eq!(pixel(&s, 29, 10), green);
+ assert_eq!(s.active().unwrap().history.past_len(), past + 1, "one undo step");
+ s.execute("edit.transform.flipVertical", json!({})).unwrap();
+ assert_eq!(active(&s).surface().unwrap().content_bounds(), Rect::new(20, 16, 30, 20));
+ assert_eq!(pixel(&s, 29, 19), green);
+ s.execute("edit.transform.rotate180", json!({})).unwrap();
+ assert_eq!(active(&s).surface().unwrap().content_bounds(), Rect::new(10, 10, 20, 14));
+ assert_eq!(pixel(&s, 10, 10), green);
+ // A marquee (0, 0)–(24, 30) holding the whole square: across x = 12.
+ s.execute("select.rect", json!({"x": 0, "y": 0, "width": 24, "height": 30})).unwrap();
+ s.execute("edit.transform.flipHorizontal", json!({})).unwrap();
+ assert_eq!(active(&s).surface().unwrap().content_bounds(), Rect::new(4, 10, 14, 14));
+ assert_eq!(pixel(&s, 13, 10), green);
+ // Undo puts it back where it was.
+ s.undo();
+ assert_eq!(active(&s).surface().unwrap().content_bounds(), Rect::new(10, 10, 20, 14));
+ // A targeted alpha channel turns about the selection's bounds too.
+ s.execute("channel.new", json!({})).unwrap();
+ s.edit("paint channel", |doc, _| {
+ doc.channels[0].surface.fill_rect(Rect::new(10, 10, 20, 20), &[1.0]);
+ Ok(())
+ })
+ .unwrap();
+ s.execute("channel.target", json!({"channel": 0})).unwrap();
+ s.execute("select.all", json!({})).unwrap();
+ s.execute("edit.transform.flipHorizontal", json!({})).unwrap();
+ assert_eq!(s.active().unwrap().doc.channels[0].surface.content_bounds(), Rect::new(20, 10, 30, 20));
+ }
+
#[test]
fn reselect_restores_the_last_selection() {
let mut s = session(8);