Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge remote-tracking branch 'fork/codex/transform-content' into codex/content-handles

moppeeer37769986ab23
diff --cc crates/engine/src/cmd/object.rs
index 01aa9af5f,e6c61c062..96d51bb14
--- a/crates/engine/src/cmd/object.rs
+++ b/crates/engine/src/cmd/object.rs
@@@ -3379,74 -3431,82 +3482,154 @@@ mod spread_target_tests
}
}
+#[cfg(test)]
+mod content_move_tests {
+ use super::*;
+
+ /// A session with a 40×20 image placed in a frame at (100, 100)–(300, 300).
+ fn placed() -> (Session, ItemId) {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let png = designcraft_render::Rendered { width: 40, height: 20, pixels: vec![200; 40 * 20 * 4] }.to_png();
+ let f = s.execute("frame.create", &json!({"rect": [100, 100, 300, 300], "content": "graphic"})).unwrap()["id"].as_u64().unwrap();
+ s.execute("place.load", &json!({"base64": super::super::base64_encode(&png)})).unwrap();
+ s.execute("place.drop", &json!({"frame": f})).unwrap();
+ (s, ItemId(f))
+ }
+
+ /// Spread bounds of the frame and of its graphic.
+ fn boxes(s: &Session, id: ItemId) -> (Rect, Rect) {
+ let it = s.doc().unwrap().doc.item(id).unwrap().clone();
+ let g = it.graphic().unwrap();
+ (it.bounds(), (it.xf * g.xf).transform_rect_bbox(Rect::new(0.0, 0.0, g.size.0, g.size.1)))
+ }
+
+ fn near(a: Rect, b: Rect) -> bool {
+ [a.x0 - b.x0, a.y0 - b.y0, a.x1 - b.x1, a.y1 - b.y1].iter().all(|d| d.abs() < 1e-6)
+ }
+
+ #[test]
+ fn content_moves_inside_the_frame_in_one_undo_step() {
+ let (mut s, f) = placed();
+ let (frame0, g0) = boxes(&s, f);
+ s.execute("selection.set", &json!({"ids": [f.0], "content": true})).unwrap();
+ s.execute("transform.move", &json!({"dx": 10, "dy": -5})).unwrap();
+ let (frame1, g1) = boxes(&s, f);
+ assert!(near(frame1, frame0), "the frame stays");
+ assert!(near(g1, g0 + Vec2::new(10.0, -5.0)), "{g1:?}");
+ s.execute("edit.undo", &json!({})).unwrap();
+ assert!(near(boxes(&s, f).1, g0));
+ // Explicit ids with `content` act on the content whatever the selection is.
+ s.execute("selection.set", &json!({"ids": [f.0]})).unwrap();
+ s.execute("transform.move", &json!({"ids": [f.0], "content": true, "dx": 3})).unwrap();
+ assert!(near(boxes(&s, f).0, frame0) && near(boxes(&s, f).1, g0 + Vec2::new(3.0, 0.0)));
+ // A frame selection moves frame and content together.
+ s.execute("transform.move", &json!({"dx": 4})).unwrap();
+ assert!(near(boxes(&s, f).0, frame0 + Vec2::new(4.0, 0.0)) && near(boxes(&s, f).1, g0 + Vec2::new(7.0, 0.0)));
+ }
+
+ #[test]
+ fn content_moves_on_the_page_axes_in_a_rotated_frame_and_pasted_items_move_too() {
+ let (mut s, f) = placed();
+ s.execute("transform.rotate", &json!({"ids": [f.0], "angle": 90})).unwrap();
+ let (frame0, g0) = boxes(&s, f);
+ s.execute("transform.move", &json!({"ids": [f.0], "content": true, "dx": 10})).unwrap();
+ let (frame1, g1) = boxes(&s, f);
+ assert!(near(frame1, frame0) && near(g1, g0 + Vec2::new(10.0, 0.0)), "{g0:?} → {g1:?}");
+ // Items pasted into a frame are its content.
+ let a = s.execute("frame.create", &json!({"rect": [400, 100, 420, 120], "content": "unassigned"})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": [a]})).unwrap();
+ s.execute("edit.copy", &json!({})).unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [400, 300, 500, 400], "content": "unassigned"})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": [b]})).unwrap();
+ s.execute("edit.pasteInto", &json!({})).unwrap();
+ let kids = |s: &Session| {
+ let it = s.doc().unwrap().doc.item(ItemId(b)).unwrap().clone();
+ (it.bounds(), it.children().iter().map(|c| it.xf.transform_rect_bbox(c.bounds())).collect::<Vec<_>>())
+ };
+ let (fb0, k0) = kids(&s);
+ s.execute("transform.move", &json!({"ids": [b], "content": true, "dx": 5, "dy": 6})).unwrap();
+ let (fb1, k1) = kids(&s);
+ assert!(near(fb1, fb0) && near(k1[0], k0[0] + Vec2::new(5.0, 6.0)), "{k0:?} → {k1:?}");
+ }
+}
++
+ #[cfg(test)]
+ mod content_transform_tests {
+ use serde_json::json;
+
+ use crate::Session;
+
+ /// A rotated graphic frame holding a 200 x 100 image, its content selected.
+ fn rotated_frame_with_image() -> (Session, u64) {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let f = s.execute("frame.create", &json!({"rect": [100, 100, 300, 300], "content": "graphic"})).unwrap()["id"].as_u64().unwrap();
+ let png = designcraft_render::Rendered { width: 200, height: 100, pixels: vec![200u8; 200 * 100 * 4] }.to_png();
+ s.execute("file.place", &json!({"base64": crate::cmd::file::base64_encode(&png), "name": "a.png", "frame": f})).unwrap();
+ s.execute("transform.rotate", &json!({"ids": [f], "angle": -45})).unwrap();
+ s.execute("selection.set", &json!({"ids": [f], "content": true})).unwrap();
+ (s, f)
+ }
+
+ #[test]
+ fn rotating_the_content_leaves_the_frame_as_it_is() {
+ let (mut s, f) = rotated_frame_with_image();
+ let before = s.execute("transform.info", &json!({"ids": [f]})).unwrap();
+ assert!((before["rotation"].as_f64().unwrap() + 45.0).abs() < 1e-6 && (before["content"]["rotation"].as_f64().unwrap() + 45.0).abs() < 1e-6);
+ // The tutorial's step: the image straightened inside its tilted frame.
+ s.execute("transform.set", &json!({"rotation": 0})).unwrap();
+ let after = s.execute("transform.info", &json!({"ids": [f]})).unwrap();
+ assert!((after["rotation"].as_f64().unwrap() + 45.0).abs() < 1e-6, "the frame stays at -45: {after}");
+ assert!(after["content"]["rotation"].as_f64().unwrap().abs() < 1e-6, "the image is upright: {after}");
+ // Scale and position act on the image too.
+ s.execute("transform.set", &json!({"scaleX": 50, "scaleY": 50})).unwrap();
+ let info = s.execute("transform.info", &json!({"ids": [f]})).unwrap();
+ assert!((info["content"]["scaleX"].as_f64().unwrap() - 50.0).abs() < 1e-6, "{info}");
+ assert!((info["rotation"].as_f64().unwrap() + 45.0).abs() < 1e-6);
+ // One undo step each.
+ s.execute("edit.undo", &json!({})).unwrap();
+ s.execute("edit.undo", &json!({})).unwrap();
+ let undone = s.execute("transform.info", &json!({"ids": [f]})).unwrap();
+ assert!((undone["content"]["rotation"].as_f64().unwrap() + 45.0).abs() < 1e-6, "{undone}");
+ }
+
+ #[test]
+ fn with_the_graphic_selected_transform_info_reports_the_graphic() {
+ let (mut s, f) = rotated_frame_with_image();
+ s.execute("transform.set", &json!({"rotation": 0})).unwrap();
+ // What the Control bar and Properties read: the graphic's 0, not the frame's -45, so a
+ // spinner step from it gives 1, not -44.
+ let info = s.execute("transform.info", &json!({})).unwrap();
+ assert!(info["rotation"].as_f64().unwrap().abs() < 1e-6, "{info}");
+ let rot = info["rotation"].as_f64().unwrap() + 1.0;
+ s.execute("transform.set", &json!({"rotation": rot})).unwrap();
+ let after = s.execute("transform.info", &json!({"ids": [f]})).unwrap();
+ assert!((after["content"]["rotation"].as_f64().unwrap() - 1.0).abs() < 1e-6, "{after}");
+ assert!((after["rotation"].as_f64().unwrap() + 45.0).abs() < 1e-6, "the frame stays: {after}");
+ // `id` names the target explicitly, like `ids`: the frame.
+ let by_id = s.execute("transform.info", &json!({"id": f})).unwrap();
+ assert!((by_id["rotation"].as_f64().unwrap() + 45.0).abs() < 1e-6, "{by_id}");
+ }
+
+ #[test]
+ fn a_locked_frame_records_no_undo_step() {
+ let (mut s, f) = rotated_frame_with_image();
+ s.execute("object.lock", &json!({"ids": [f]})).unwrap();
+ s.execute("selection.set", &json!({"ids": [f], "content": true})).unwrap();
+ let undo = s.doc().unwrap().history.undo.len();
+ assert_eq!(s.execute("transform.set", &json!({"rotation": 0})).unwrap()["changed"], 0);
+ assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no empty undo step");
+ }
+
+ #[test]
+ fn a_frame_selection_still_transforms_the_frame_and_an_empty_frame_has_no_content() {
+ let (mut s, f) = rotated_frame_with_image();
+ // `content: false` (or a frame selection) turns frame and image together.
+ s.execute("transform.set", &json!({"rotation": 0, "content": false})).unwrap();
+ let info = s.execute("transform.info", &json!({"ids": [f]})).unwrap();
+ assert!(info["rotation"].as_f64().unwrap().abs() < 1e-6, "{info}");
+ let e = s.execute("frame.create", &json!({"rect": [400, 100, 500, 200], "content": "graphic"})).unwrap()["id"].as_u64().unwrap();
+ assert!(s.execute("transform.set", &json!({"ids": [e], "content": true, "rotation": 10})).is_err());
+ }
+ }