Skip to content

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

Merge remote-tracking branch 'origin/main' into direct-select-anchor-points

moppeeer1038ec1b3c2b
diff --cc crates/engine/src/cmd/edit.rs
index d22f816b8,a10e9ae2f..b1ac6230e
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@@ -288,8 -262,10 +288,12 @@@ pub fn specs() -> Vec<CommandSpec>
st.selection.cells = None;
if let Some(i) = st.selection.items.iter().position(|x| *x == id) {
st.selection.items.remove(i);
+ // Its points go with it: arrow keys and Delete act on the selected points.
+ st.selection.anchors.retain(|a| a.0 != id);
+ // A key object needs another object to align to it.
+ if st.selection.key == Some(id) || st.selection.items.len() < 2 {
+ st.selection.key = None;
+ }
} else {
st.selection.items.push(id);
}
diff --cc crates/engine/src/tests.rs
index a2903fc6a,b1792485d..3f0ea282a
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@@ -1652,107 -1652,107 +1652,212 @@@ fn a_line_fits_when_its_baseline_does(
assert!(overset(&mut s, 50.0), "a baseline below the frame still oversets the line");
}
+/// Direct Selection: a marquee over the top edges of two frames selects their top points, and a
+/// drag on one of them slants both frames in one undo step (InDesign's way to make a row of
+/// parallelograms).
+#[test]
+fn direct_selection_moves_the_points_of_several_frames_at_once() {
+ use designcraft_tools::{Mods, PointerEvent, PointerKind};
+ let mut s = session();
+ let v = ViewInfo::at_zoom(1.0);
+ let a = s.execute("frame.create", &json!({"rect": [100, 300, 200, 400]})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200, 300, 300, 400]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": []})).unwrap();
+ s.set_tool("directSelection");
+ s.pointer(&PointerEvent::new(PointerKind::Down, 80.0, 280.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Drag, 320.0, 320.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Up, 320.0, 320.0), v).unwrap();
+ let sel = s.doc().unwrap().selection.clone();
+ assert_eq!(sel.anchors.len(), 4, "{:?}", sel.anchors);
+ assert!(sel.items.contains(&designcraft_doc::ItemId(a)) && sel.items.contains(&designcraft_doc::ItemId(b)));
+ let shift = Mods { shift: true, ..Default::default() };
+ s.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 300.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Drag, 136.0, 302.0).with_mods(shift), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Up, 136.0, 302.0).with_mods(shift), v).unwrap();
+ let corners = |s: &Session, id: u64| -> Vec<(f64, f64)> {
+ let st = s.doc().unwrap();
+ let it = st.doc.item(designcraft_doc::ItemId(id)).unwrap();
+ it.path.subpaths[0].anchors.iter().map(|an| ((it.xf * an.p).x.round(), (it.xf * an.p).y.round())).collect()
+ };
+ // Both frames: top points 36 pt to the right, bottom points where they were.
+ assert_eq!(corners(&s, a), vec![(136.0, 300.0), (236.0, 300.0), (200.0, 400.0), (100.0, 400.0)]);
+ assert_eq!(corners(&s, b), vec![(236.0, 300.0), (336.0, 300.0), (300.0, 400.0), (200.0, 400.0)]);
+ s.execute("edit.undo", &json!({})).unwrap();
+ assert_eq!(corners(&s, a), vec![(100.0, 300.0), (200.0, 300.0), (200.0, 400.0), (100.0, 400.0)]);
+ assert_eq!(corners(&s, b), vec![(200.0, 300.0), (300.0, 300.0), (300.0, 400.0), (200.0, 400.0)]);
+}
+
+#[test]
+fn selected_points_follow_their_path() {
+ let mut s = session();
+ let a = s.execute("frame.create", &json!({"rect": [100, 300, 200, 400]})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200, 300, 300, 400]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"anchors": [[a, 0, 1], [b, 0, 2]]})).unwrap();
+ // Shift-clicking a frame off the selection drops its points too.
+ s.execute("selection.toggle", &json!({"id": a})).unwrap();
+ let id = |n| designcraft_doc::ItemId(n);
+ assert_eq!(s.doc().unwrap().selection.anchors, vec![(id(b), 0, 2)]);
+ // A point added before the first one: the selected point keeps pointing at the same anchor.
+ let l = s.execute("path.create", &json!({"anchors": [{"p": [100, 100]}, {"p": [200, 100]}, {"p": [300, 150]}]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"anchors": [[l, 0, 1]]})).unwrap();
+ s.execute("path.appendAnchor", &json!({"id": l, "anchor": {"p": [50, 120]}, "at": "start"})).unwrap();
+ let st = s.doc().unwrap();
+ let (_, si, ai) = st.selection.anchors[0];
+ let it = st.doc.item(id(l)).unwrap();
+ assert_eq!((it.xf * it.path.subpaths[si].anchors[ai].p).round(), designcraft_geom::Point::new(200.0, 100.0));
+ // `points` that isn't a list is refused, even with an `id` beside it.
+ for bad in [json!("x"), json!({"0": 1}), json!(5)] {
+ assert!(s.execute("path.moveAnchors", &json!({"points": bad, "id": l, "anchors": [[0, 0]], "dx": 5})).is_err(), "points {bad}");
+ }
+ for bad in [json!(3), json!(-1), json!("0")] {
+ assert!(s.execute("path.appendAnchor", &json!({"id": l, "anchor": {"p": [0, 0]}, "subpath": bad})).is_err(), "subpath {bad}");
+ }
+ assert_eq!(s.doc().unwrap().doc.item(id(l)).unwrap().path.subpaths[0].anchors.len(), 4, "nothing appended");
+}
+
+#[test]
+fn direct_selection_pulls_one_corner_of_an_unselected_frame() {
+ // Chris Nielsen's "The Adobe InDesign Toolbox" (10:10): with the White Arrow, click outside,
+ // then press on a frame's corner and pull it: only that corner moves (a triangle, nearly).
+ use designcraft_tools::{PointerEvent, PointerKind, SnapView};
+ let mut s = Session::new();
+ s.execute("file.new", &json!({"width": 612, "height": 792, "facingPages": false})).unwrap();
+ let id = s.execute("frame.create", &json!({"rect": [156, 219, 455, 569], "content": "graphic"})).unwrap()["id"].as_u64().unwrap();
+ s.execute("edit.deselectAll", &json!({})).unwrap();
+ let corners = |s: &Session| {
+ let d = &s.doc().unwrap().doc;
+ let it = d.item(designcraft_doc::ItemId(id)).unwrap();
+ it.path.subpaths[0].anchors.iter().map(|a| it.xf * a.p).collect::<Vec<_>>()
+ };
+ let before = corners(&s);
+ s.set_tool("directSelection");
+ let v = ViewInfo { snap: SnapView::OFF, ..ViewInfo::at_zoom(1.0) };
+ let layout = s.layout();
+ let at = |x: f64, y: f64| layout.to_canvas(designcraft_doc::SpreadRef::Doc(0), designcraft_geom::Point::new(x, y));
+ let (a, b) = (at(455.0, 219.0), at(455.0, 150.0));
+ s.pointer(&PointerEvent::new(PointerKind::Down, a.x, a.y), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Drag, a.x, a.y - 30.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Drag, b.x, b.y), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Up, b.x, b.y), v).unwrap();
+ let after = corners(&s);
+ let moved: Vec<_> = before.iter().zip(&after).filter(|(p, q)| (**p - **q).hypot() > 1e-6).collect();
+ assert_eq!(moved.len(), 1, "one corner moved: {before:?} → {after:?}");
+ assert!((*moved[0].1 - designcraft_geom::Point::new(455.0, 150.0)).hypot() < 1e-6, "{after:?}");
+ assert_eq!(s.doc().unwrap().selection.items, [designcraft_doc::ItemId(id)], "the frame is selected");
+ // One undo step puts the corner back.
+ s.execute("edit.undo", &json!({})).unwrap();
+ assert_eq!(corners(&s), before);
+ // Away from its corners, the White Arrow still drags the whole unselected frame.
+ s.execute("edit.deselectAll", &json!({})).unwrap();
+ let (c, d) = (at(300.0, 219.0), at(300.0, 269.0));
+ s.pointer(&PointerEvent::new(PointerKind::Down, c.x, c.y), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Drag, c.x, c.y + 20.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Drag, d.x, d.y), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Up, d.x, d.y), v).unwrap();
+ assert!(corners(&s).iter().zip(&before).all(|(p, q)| (p.y - q.y - 50.0).abs() < 1e-6), "{:?}", corners(&s));
+}
++
+ /// Issue #1637: with several objects selected, a click on one of them (no drag) makes it the key
+ /// object, and Align to Key Object leaves it in place. A second click on it drops the key.
+ #[test]
+ fn a_click_in_the_selection_makes_the_key_object() {
+ use designcraft_tools::{PointerEvent, PointerKind};
+ let mut s = session();
+ let a = s.execute("frame.create", &json!({"rect": [50.0, 100.0, 100.0, 150.0], "content": "unassigned"})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200.0, 120.0, 300.0, 170.0], "content": "unassigned"})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": [a, b]})).unwrap();
+ s.set_tool("selection");
+ let v = ViewInfo::at_zoom(1.0);
+ // On the part of the key object that the aligned object never covers, away from the handles.
+ let click = |s: &mut Session| {
+ s.pointer(&PointerEvent::new(PointerKind::Down, 280.0, 160.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Up, 280.0, 160.0), v).unwrap();
+ };
+ click(&mut s);
+ assert_eq!(s.doc().unwrap().selection.items.len(), 2, "the click keeps both selected");
+ // The key is the right-hand object, so aligning left edges to the selection would move it.
+ s.execute("object.align", &json!({"edge": "left", "to": "keyObject"})).unwrap();
+ let x0 = |s: &Session, id: u64| s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().bounds().x0;
+ assert!((x0(&s, b) - 200.0).abs() < 1e-6, "the key object moved to {}", x0(&s, b));
+ assert!((x0(&s, a) - 200.0).abs() < 1e-6, "the other object is at {}", x0(&s, a));
+ // The Align panel and the Control panel send no target: the key object is the default one.
+ s.execute("object.align", &json!({"edge": "top"})).unwrap();
+ let y0 = |s: &Session, id: u64| s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().bounds().y0;
+ assert!((y0(&s, b) - 120.0).abs() < 1e-6, "the key object moved to {}", y0(&s, b));
+ assert!((y0(&s, a) - 120.0).abs() < 1e-6, "the other object is at {}", y0(&s, a));
+ click(&mut s);
+ assert_eq!(s.doc().unwrap().selection.key, None, "a click on the key object drops the key");
+ }
+
+ /// Dragging one of several selected objects moves them all and makes no key object.
+ #[test]
+ fn dragging_the_selection_makes_no_key_object() {
+ use designcraft_tools::{PointerEvent, PointerKind};
+ let mut s = session();
+ let a = s.execute("frame.create", &json!({"rect": [50.0, 100.0, 100.0, 150.0], "content": "unassigned"})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200.0, 100.0, 260.0, 150.0], "content": "unassigned"})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": [a, b]})).unwrap();
+ s.set_tool("selection");
+ let v = ViewInfo::at_zoom(1.0);
+ s.pointer(&PointerEvent::new(PointerKind::Down, 230.0, 125.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Drag, 250.0, 135.0), v).unwrap();
+ s.pointer(&PointerEvent::new(PointerKind::Up, 250.0, 135.0), v).unwrap();
+ assert_eq!(s.doc().unwrap().selection.key, None);
+ }
+
+ /// Shift-clicking the key object out of the selection drops the key.
+ #[test]
+ fn deselecting_the_key_object_drops_the_key() {
+ let mut s = session();
+ let a = s.execute("frame.create", &json!({"rect": [50.0, 100.0, 100.0, 150.0]})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200.0, 100.0, 260.0, 150.0]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": [a, b]})).unwrap();
+ s.execute("selection.key", &json!({"id": b})).unwrap();
+ assert_eq!(s.doc().unwrap().selection.key, Some(designcraft_doc::ItemId(b)));
+ s.execute("selection.toggle", &json!({"id": b})).unwrap();
+ assert_eq!(s.doc().unwrap().selection.key, None);
+ }
+
+ /// A key object needs another object to align to: with one object selected there is nothing to key
+ /// against, and the stored key would turn that object into the target as soon as a second one joins.
+ #[test]
+ fn a_single_selected_object_cannot_be_the_key_object() {
+ let mut s = session();
+ let a = s.execute("frame.create", &json!({"rect": [50.0, 100.0, 100.0, 150.0]})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200.0, 120.0, 300.0, 170.0]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": [a]})).unwrap();
+ assert!(s.execute("selection.key", &json!({"id": a})).is_err());
+ assert_eq!(s.doc().unwrap().selection.key, None);
+ s.execute("selection.toggle", &json!({"id": b})).unwrap();
+ assert_eq!(s.doc().unwrap().selection.key, None, "the second object joining must not find a key already set");
+ }
+
+ /// With the selection down to the key object alone, Align no longer has a key object: a single
+ /// object aligns to the page, as before.
+ #[test]
+ fn a_lone_key_object_aligns_to_the_page() {
+ let mut s = session();
+ let a = s.execute("frame.create", &json!({"rect": [50.0, 100.0, 100.0, 150.0]})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200.0, 120.0, 300.0, 170.0]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": [a, b]})).unwrap();
+ s.execute("selection.key", &json!({"id": b})).unwrap();
+ s.execute("selection.toggle", &json!({"id": a})).unwrap();
+ assert_eq!(s.doc().unwrap().selection.key, None);
+ s.execute("object.align", &json!({"edge": "left"})).unwrap();
+ let x0 = s.doc().unwrap().doc.item(designcraft_doc::ItemId(b)).unwrap().bounds().x0;
+ assert!(x0.abs() < 1e-6, "the object is at {x0}, not at the page's left edge");
+ }
+
+ /// Aligning one object alone (`ids`) while it is the key object of a larger selection also takes
+ /// the page.
+ #[test]
+ fn aligning_the_key_object_alone_takes_the_page() {
+ let mut s = session();
+ let a = s.execute("frame.create", &json!({"rect": [50.0, 100.0, 100.0, 150.0]})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200.0, 120.0, 300.0, 170.0]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"ids": [a, b]})).unwrap();
+ s.execute("selection.key", &json!({"id": b})).unwrap();
+ s.execute("object.align", &json!({"edge": "left", "ids": [b]})).unwrap();
+ let x0 = s.doc().unwrap().doc.item(designcraft_doc::ItemId(b)).unwrap().bounds().x0;
+ assert!(x0.abs() < 1e-6, "the object is at {x0}, not at the page's left edge");
+ }
diff --cc crates/tools/src/select.rs
index 5853536aa,cba5fc317..9f2c9492d
--- a/crates/tools/src/select.rs
+++ b/crates/tools/src/select.rs
@@@ -279,8 -224,9 +280,11 @@@ pub struct SelectionTool
guides: Vec<Overlay>,
/// Selected item to toggle if this Shift press never becomes a drag.
shift_release: Option<u64>,
+ /// Selected anchor point to take away if this Shift press never becomes a drag.
+ anchor_release: Option<(ItemId, usize, usize)>,
+ /// One of several selected items to make (or stop being) the key object for Align if this
+ /// press never becomes a drag.
+ key_release: Option<u64>,
/// The loaded text cursor.
loaded: Option<Loaded>,
/// Frame whose live corners are being edited (diamonds shown).
@@@ -289,7 -235,7 +293,7 @@@
impl SelectionTool {
pub fn new(direct: bool) -> Self {
- Self { direct, drag: Drag::None, hover_handle: None, guides: vec![], shift_release: None, anchor_release: None, loaded: None, live: None }
- Self { direct, drag: Drag::None, hover_handle: None, guides: vec![], shift_release: None, key_release: None, loaded: None, live: None }
++ Self { direct, drag: Drag::None, hover_handle: None, guides: vec![], shift_release: None, key_release: None, anchor_release: None, loaded: None, live: None }
}
/// [`bracket_at`] for this tool. With Direct Selection an anchor under the pointer wins: the
@@@ -1240,14 -1133,9 +1249,15 @@@ impl Tool for SelectionTool
PointerKind::Up => {
self.guides.clear();
let release = self.shift_release.take();
+ let anchor_release = self.anchor_release.take();
+ let key = self.key_release.take();
let d = std::mem::replace(&mut self.drag, Drag::None);
match d {
+ // A Shift-click on a selected point that never became a drag takes it away.
+ Drag::Anchor { .. } if anchor_release.is_some() => {
+ let points: Vec<_> = cx.selection.anchors.iter().copied().filter(|a| Some(*a) != anchor_release).collect();
+ vec![Action::Commit, Action::Exec("selection.set".into(), anchor_selection(cx, &points))]
+ }
Drag::Move { .. }
| Drag::Resize { .. }
| Drag::ContentResize { .. }