Skip to content

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

Selection tool: with a frame's content selected, a drag on it moves the content

moppeeercf2f01b6fe8a
diff --git a/crates/tools/src/select.rs b/crates/tools/src/select.rs
index 2aa7a7bbd..891f1d4ab 100644
--- a/crates/tools/src/select.rs
+++ b/crates/tools/src/select.rs
@@ -318,6 +318,18 @@ pub fn grabber_at(cx: &ToolContext, p: Point) -> Option<ItemId> {
grabbers(cx, Some(p)).into_iter().find(|(_, c)| (*c - p).hypot() <= r).map(|(id, _)| id)
}
+/// Is `p` on the selected content (a frame double-clicked into, or its grabber pressed)? A press
+/// there with the Selection tool moves the content, as in InDesign, where the pointer turns into a
+/// hand over it.
+fn on_selected_content(cx: &ToolContext, p: Point) -> bool {
+ cx.selection.content
+ && cx.hit(p).is_some_and(|(_, id)| {
+ let id = cx.doc.top_level_of(id).unwrap_or(id);
+ cx.selection.contains(id) && cx.doc.item(id).is_some_and(holds_content)
+ })
+ && content_bounds(cx).is_some_and(|b| b.contains(p))
+}
+
/// Canvas union of the content of the selected frames (an item without content counts whole).
fn content_bounds(cx: &ToolContext) -> Option<Rect> {
let mut acc: Option<Rect> = None;
@@ -777,7 +789,8 @@ impl Tool for SelectionTool {
} else if !cx.selection.contains(id) || (self.direct && !cx.selection.content) {
out.push(Action::Exec("selection.set".into(), json!({"ids": [id.0], "content": self.direct})));
}
- self.drag = Drag::Pending { start: p, hit: true, content: self.direct };
+ let content = self.direct || on_selected_content(cx, p);
+ self.drag = Drag::Pending { start: p, hit: true, content };
let _ = sr;
out
}
@@ -1201,7 +1214,7 @@ impl Tool for SelectionTool {
match handle_at(cx, p) {
Some(h) => handle_cursor(h),
None if rotate_zone(cx, p).is_some() => Cursor::Rotate,
- None if grabber_at(cx, p).is_some() => Cursor::Hand,
+ None if grabber_at(cx, p).is_some() || on_selected_content(cx, p) => Cursor::Hand,
None => Cursor::Arrow,
}
}
diff --git a/crates/tools/src/tests.rs b/crates/tools/src/tests.rs
index 25441bcc3..ddbb070e0 100644
--- a/crates/tools/src/tests.rs
+++ b/crates/tools/src/tests.rs
@@ -1137,7 +1137,7 @@ fn direct_selection_drags_content_inside_its_frame() {
}
#[test]
-fn selection_tool_grabber_moves_content_elsewhere_moves_the_frame() {
+fn selection_tool_moves_selected_content_and_the_frame_otherwise() {
let mut d = Document::new(&NewDocument::default());
let f = graphic_frame(&mut d);
let (c, l) = (Cache::new(), CanvasLayout::new(&d, false));
@@ -1156,15 +1156,28 @@ fn selection_tool_grabber_moves_content_elsewhere_moves_the_frame() {
let p = preview_params(&a);
assert_eq!((p["dx"].as_f64(), p["dy"].as_f64(), &p["content"]), (Some(-20.0), Some(10.0), &serde_json::json!(true)));
assert_eq!(t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Up, centre.x - 20.0, centre.y + 10.0)), vec![Action::Commit]);
- // A press elsewhere in the frame keeps the content selection; dragging selects and moves the frame.
+ // With the content selected (the grabber, or a double-click), the pointer is a hand over it and
+ // a drag anywhere on it moves it, as in InDesign (Dansky's Exclude Overlap tutorial, 7:13).
let edge = spread_pt(&l, 115.0, 185.0);
+ assert_eq!(t.cursor(&ctx(&d, &content, &c, &l), edge, Mods::default()), Cursor::Hand);
assert!(t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Down, edge.x, edge.y)).is_empty());
let a = t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Drag, edge.x + 30.0, edge.y));
- assert_eq!(a[0], Action::Exec("selection.set".into(), serde_json::json!({"ids": [f.0]})));
- assert_eq!(a[1], Action::Begin("Move".into()));
+ assert_eq!(a[0], Action::Begin("Move Content".into()));
let p = preview_params(&a);
- assert_eq!((p["dx"].as_f64(), p.get("content"), p.get("ids")), (Some(30.0), None, None));
- t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Up, edge.x + 30.0, edge.y));
+ assert_eq!((p["dx"].as_f64(), &p["content"]), (Some(30.0), &serde_json::json!(true)));
+ t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Up, edge.x + 30.0, edge.y));
+ // Shift keeps it on one axis; the content stays selected.
+ let shift = Mods { shift: true, ..Default::default() };
+ t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Down, edge.x, edge.y).with_mods(shift));
+ let a = t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Drag, edge.x + 30.0, edge.y + 4.0).with_mods(shift));
+ let p = preview_params(&a);
+ assert_eq!((p["dx"].as_f64(), p["dy"].as_f64(), &p["content"]), (Some(30.0), Some(0.0), &serde_json::json!(true)));
+ assert_eq!(
+ t.pointer(&ctx(&d, &content, &c, &l), &PointerEvent::new(PointerKind::Up, edge.x + 30.0, edge.y).with_mods(shift)),
+ vec![Action::Commit]
+ );
+ // Off the content (the graphic overhangs the frame's sides only), a press selects as before.
+ assert_eq!(t.cursor(&ctx(&d, &content, &c, &l), spread_pt(&l, 150.0, 250.0), Mods::default()), Cursor::Arrow);
// With the frame selected, a drag away from the grabber moves the frame.
t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Down, edge.x, edge.y));
let a = t.pointer(&ctx(&d, &frame, &c, &l), &PointerEvent::new(PointerKind::Drag, edge.x, edge.y + 30.0));