PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Integration: keep an untouched cylinder on Warp mode's Enter (#5769)
David Mann63958b07abb7
diff --git a/crates/engine/src/warp_cmds.rs b/crates/engine/src/warp_cmds.rsindex 3d44a8c34..242897671 100644--- a/crates/engine/src/warp_cmds.rs+++ b/crates/engine/src/warp_cmds.rs@@ -145,6 +145,22 @@ fn cylinder_in_source(w: &Warp, inv: &Homography, sb: [f64; 4]) -> Option<Warp>Some(Warp::custom(mesh, sb))}+/// Whether the custom warp `w` is exactly what Warp mode shows for `layer`'s stored cylinder+/// ([`smart_warp_doc_space`]: its mesh over the same box), so committing it untouched changes+/// nothing. Stored as a custom warp instead, the cylinder would be lost, as a PSD keeps it only+/// as `warpCylinder` (#4112).+fn is_stored_cylinder(layer: &Layer, w: &Warp) -> bool {+ let LayerContent::Smart(sm) = &layer.content else { return false };+ if w.style != WarpStyle::Custom || sm.warp.as_ref().is_none_or(|s| s.style != WarpStyle::Cylinder) {+ return false;+ }+ let (Some(shown), Some(m)) = (smart_warp_doc_space(layer), w.mesh.as_ref()) else { return false };+ let Some(o) = shown.mesh.as_ref() else { return false };+ let tol = 1e-6 * (1.0 + o.points.iter().flatten().chain(&shown.bounds).fold(0.0f64, |a, v| a.max(v.abs())));+ let near = |a: &[f64], b: &[f64]| a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() <= tol);+ o.us == m.us && o.vs == m.vs && near(&shown.bounds, &w.bounds) && near(o.points.as_flattened(), m.points.as_flattened())+}+/// A Background is turned into a normal layer by the caller first/// ([`crate::extra_cmds::background_to_layer`]).fn warp_layer(doc_sel: Option<&Surface>, group: Locks, l: &mut Layer, w: &Warp, rect: Rect, interp: Interp, frames: Option<&Frames>) -> Result<()> {@@ -385,6 +401,10 @@ fn apply(s: &mut Session, p: &Value, cmd: &str) -> Result<Value> {if w.is_identity() && !is_smart {return Ok(json!({"layer": id.0, "changed": false}));}+ // An untouched cylinder (Warp mode, then Enter) stays as it is, with no history step.+ if p.get("mesh").is_none() && s.active().and_then(|d| d.doc.layer(id)).is_some_and(|l| is_stored_cylinder(l, &w)) {+ return Ok(json!({"layer": id.0, "changed": false}));+ }let interp = Interp::parse(p.get("interpolation").and_then(Value::as_str).unwrap_or("bicubic"));let content_knots = p.get("mesh").is_some() && p.get("warp").is_none();s.edit("Warp", |doc, _| {@@ -800,6 +820,52 @@ mod tests {assert_eq!(w.mesh.as_ref().map(|m| (m.nx(), m.ny())), Some((13, 4)));}+ /// Warp mode shows a stored cylinder as its mesh, and Enter commits that mesh untouched: the+ /// smart object keeps its cylinder (else the next PSD save writes no warp), while a moved+ /// point still makes it a custom warp.+ #[test]+ fn untouched_cylinder_committed_from_warp_mode_stays_a_cylinder() {+ for scale in [100, 150] {+ let mut s = session(8);+ s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();+ s.execute("edit.transform", json!({"scaleX": scale, "scaleY": 100})).unwrap();+ let id = s.active().unwrap().active_layer.unwrap();+ let rect = warp_bounds(&s.active().unwrap().doc, s.active().unwrap().doc.layer(id).unwrap());+ let [x0, x1] = [f64::from(rect.x0), f64::from(rect.x1)];+ let values = [x0, 40.0, x1, 10.0, 0.3, 0.3, 0.5];+ s.execute("layer.smartObjects.warp", json!({"style": "cylinder", "values": values, "rect": [rect.x0, 10, rect.x1, 40]})).unwrap();+ let stored = |s: &Session| {+ let st = s.active().unwrap();+ let LayerContent::Smart(sm) = &st.doc.layer(id).unwrap().content else { panic!("smart") };+ sm.warp.clone().unwrap()+ };+ let cylinder = stored(&s);+ assert_eq!(cylinder.style, WarpStyle::Cylinder, "{scale}");+ let shown = smart_warp_doc_space(s.active().unwrap().doc.layer(id).unwrap()).unwrap();+ assert_eq!(shown.style, WarpStyle::Custom);+ s.execute("select.all", json!({})).unwrap();+ let (sel, steps) = (s.active().unwrap().doc.selection.clone(), s.active().unwrap().history.past_len());+ let r = s.execute("edit.transform.warp", json!({"layer": id.0, "rect": shown.bounds, "warp": shown, "interpolation": "bicubic"})).unwrap();+ assert_eq!(r["changed"], false, "{scale}");+ assert_eq!(stored(&s), cylinder, "{scale}: untouched");+ let st = s.active().unwrap();+ assert!(st.doc.selection == sel && st.history.past_len() == steps, "{scale}: no step, the selection stays");+ // The same mesh over another box is another warp.+ let mut wider = shown.clone();+ wider.bounds[2] += 10.0;+ s.execute("edit.transform.warp", json!({"layer": id.0, "rect": wider.bounds, "warp": wider})).unwrap();+ assert_eq!(stored(&s).style, WarpStyle::Custom, "{scale}: other box");+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(stored(&s), cylinder);+ let mut moved = shown.clone();+ if let Some(m) = moved.mesh.as_mut() {+ m.points[20][1] += 5.0;+ }+ s.execute("edit.transform.warp", json!({"layer": id.0, "rect": moved.bounds, "warp": moved})).unwrap();+ assert_eq!(stored(&s).style, WarpStyle::Custom, "{scale}: edited");+ }+ }+#[test]fn identity_and_bend_zero_are_no_ops_at_every_depth() {for depth in [8u64, 16, 32] {diff --git a/crates/io/tests/cylinder_warp.rs b/crates/io/tests/cylinder_warp.rsindex be75e3150..8ed1caa91 100644--- a/crates/io/tests/cylinder_warp.rs+++ b/crates/io/tests/cylinder_warp.rs@@ -66,7 +66,7 @@ fn round_trip(doc: &Document) -> (PsdFile, Document) {/// Opaque pixels of the render that are mostly `channel` (0 red, 1 green, 2 blue).fn count(s: &Surface, channel: usize) -> usize {let r = s.content_bounds();- s.read_region(r).chunks_exact(4).filter(|p| p[3] > 0.5 && p[channel] > 0.5 && (0..3).all(|c| c == channel || p[c] < 0.5)).count()+ s.read_region(r).as_chunks::<4>().0.iter().filter(|p| p[3] > 0.5 && p[channel] > 0.5 && (0..3).all(|c| c == channel || p[c] < 0.5)).count()}/// Before #4112 a cylinder was read as no warp: re-rendering the contents drew them flat, thediff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex 7ae223029..a0bd3520c 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -1093,6 +1093,9 @@ pub fn commit(app: &mut PhotocraftApp) {return;}match r {+ // Nothing changed (an untouched cylinder or Warp Text): no step to fold a copy into, so+ // it stays its own step, as for an untouched box.+ Ok(v) if v.get("changed") == Some(&json!(false)) => {}Ok(_) => finish(app, document, LayerId(t.layer), made, true),Err(e) => keep_open(app, t, preview, e),}@@ -4124,6 +4127,35 @@ mod tests {assert_eq!(app.session.active().unwrap().doc.layers.len(), layers);}+ /// #4112: ⌥⌘T on a cylinder smart object, Warp mode, Enter with nothing touched: the copy+ /// keeps the cylinder and stays its own step (no warp step to fold into), so one undo takes+ /// back the copy and nothing before it.+ #[test]+ fn untouched_warp_of_a_cylinder_copy_keeps_its_own_step() {+ let ctx = egui::Context::default();+ let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));+ app.run("layer.smartObjects.convertToSmartObject", json!({})).unwrap();+ app.run("layer.smartObjects.warp", json!({"style": "cylinder", "values": [8, 24, 24, 8, 0.3, 0.3, 0.5], "rect": [8, 8, 24, 24]})).unwrap();+ let warp = |app: &PhotocraftApp, i: usize| match &app.session.active().unwrap().doc.layers[i].content {+ photocraft_doc::LayerContent::Smart(sm) => sm.warp.as_ref().map(|w| w.style),+ _ => None,+ };+ let (layers, steps) = (app.session.active().unwrap().doc.layers.len(), app.session.active().unwrap().history.past_len());+ assert_eq!(warp(&app, layers - 1), Some(WarpStyle::Cylinder));+ crate::menus::invoke(&mut app, &ctx, "edit.freeTransformCopy", json!({})).unwrap();+ enter_warp(&mut app, &ctx);+ assert!(app.ui.transform.as_ref().unwrap().warp.is_some(), "Warp mode shows the cylinder");+ commit(&mut app);+ assert!(!app.ui.status_error, "{}", app.ui.status);+ let st = app.session.active().unwrap();+ assert_eq!((st.doc.layers.len(), st.history.past_len()), (layers + 1, steps + 1));+ assert_eq!(warp(&app, layers), Some(WarpStyle::Cylinder), "the copy is still a cylinder");+ app.session.undo();+ let st = app.session.active().unwrap();+ assert_eq!((st.doc.layers.len(), st.history.past_len()), (layers, steps), "one undo takes back the copy only");+ assert_eq!(warp(&app, layers - 1), Some(WarpStyle::Cylinder));+ }+/// #1099: cancel, frame cleanup and a commit before the next frame all belong to the/// original document, including after tab reordering and with shared layer IDs.#[test]