Objects on Path: the Select/Move widget chooses an object and drags it to another place along the path

With one Objects on Path group selected, the Selection and Objects on Path
tools show a ring on each object's attachment point. A click chooses that
object while the group stays selected; dragging a ring along the path moves
its object to the nearest object's place, and the objects between move one
place toward where it was, in one undo step. Hidden and locked objects have
no ring and keep their places. On a closed path the drag goes on round
across the first anchor, and Escape cancels it.

- object.objectsOnPath.selectMember {member} chooses an object.
- object.objectsOnPath.moveMember {member, target, group?} moves an object
  to another object's place; only the order along the path changes.

Docs: docs/mcp.md, docs/target-app-parity.md. Not yet: Detach.
This commit is contained in:
dotCooCoo
2026-11-12 01:41:55 -08:00
parent 61650e44a2
commit 2ee2ac6245
11 changed files with 899 additions and 19 deletions
+89 -2
View File
@@ -9,6 +9,10 @@
//! from the tool's options (Objects on Path Tool Options). `object.objectsOnPath.options` sets the
//! group's Pivot, Rotate and Space (the distance between the objects along the path, and where the
//! first one attaches), and `object.objectsOnPath.expand` makes it a plain group.
//! `object.objectsOnPath.selectMember` chooses one object of a selected group (its Select/Move
//! widget), and `object.objectsOnPath.moveMember` moves one object to another's place along the
//! path, the objects between it and that place each moving one place toward where it was; hidden
//! and locked objects keep their places.
//!
//! After every edit, [`refresh`] puts the objects of each Objects on Path group that changed back
//! on its path: editing the path moves them, an object moved on its own goes back to its place,
@@ -19,7 +23,7 @@
//! Object › Objects on Path › Attach; the order on the path, the attachment point, the distribution,
//! the objects squared to the path; Pivot and Rotate; path edits realign the objects; Expand).
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use serde_json::{Value, json};
@@ -28,7 +32,7 @@ use vectorcraft_doc::{Document, Node, NodeId, NodeKind};
use vectorcraft_geom::Rect;
use super::edit::{roots_of, selected_roots};
use super::{CommandSpec, EngineError, Result, Session, bad, check_numbers, checked_id, checked_ids_param, cmd, has_selection};
use super::{CommandSpec, EngineError, Result, Session, bad, check_numbers, checked_id, checked_ids_param, cmd, has_doc, has_selection};
/// The id of the Objects on Path tool.
pub const TOOL: &str = "objectsOnPath";
@@ -53,6 +57,24 @@ pub fn specs() -> Vec<CommandSpec> {
has_objects_on_path,
options
),
cmd!(
"object.objectsOnPath.selectMember",
"Select Object on Path",
[],
None,
"{member: id of an object of an Objects on Path group} select the group alone with that object chosen, as a click on the object's Select/Move widget does; any other selection change clears the choice → {group, member}",
has_doc,
select_member
),
cmd!(
"object.objectsOnPath.moveMember",
"Move Object on Path",
[],
None,
"{member: id of an object of an Objects on Path group, target: id of another object of the same group, group?: the group's id (when given, it must be the group of both); neither object nor the group may be hidden or locked} move the object to the target's place along the path; the objects between them each move one place toward where it was, except hidden and locked objects, which keep their places. Only the order along the path changes: Space, the start, Pivot, Rotate and the order in the Layers panel stay. A target that is the object itself changes nothing → {group, member, index}",
has_doc,
move_member
),
cmd!(
"object.objectsOnPath.expand",
"Expand",
@@ -296,6 +318,71 @@ fn options(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({ "ids": groups.iter().map(|g| g.0).collect::<Vec<_>>() }))
}
// ---------- Select/Move ----------
/// The Objects on Path group whose object `key` of `p` names, checked: an object listed in the
/// group's record (not its path), in a group that is not hidden or locked.
fn member_param(s: &Session, p: &Value, key: &str, cmd: &str) -> Result<(NodeId, NodeId)> {
let v = p.get(key).filter(|v| !v.is_null()).ok_or_else(|| bad(cmd, format!("missing `{key}`")))?;
let id = checked_id(s, v, cmd)?;
let d = &s.doc()?.doc;
let not_on_path = || bad(cmd, format!("`{key}` must name an object attached to the path of an Objects on Path group"));
let g =
d.ancestry(id).unwrap_or_default().into_iter().rev().nth(1).filter(|g| d.node(*g).is_some_and(is_objects_on_path)).ok_or_else(not_on_path)?;
let listed = d.node(g).and_then(|n| n.extras().objects_on_path.as_deref()).is_some_and(|rec| rec.members.iter().any(|m| m.id == id));
if !listed {
return Err(not_on_path());
}
if !d.is_editable(g) {
return Err(bad(cmd, "the Objects on Path group is hidden or locked"));
}
if !d.is_editable(id) {
return Err(bad(cmd, format!("`{key}` names an object that is hidden or locked")));
}
Ok((g, id))
}
fn select_member(s: &mut Session, p: &Value) -> Result<Value> {
let (g, member) = member_param(s, p, "member", "object.objectsOnPath.selectMember")?;
s.select(|_, sel| sel.set_on_path_member(g, member))?;
Ok(json!({ "group": g.0, "member": member.0 }))
}
fn move_member(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "object.objectsOnPath.moveMember";
let (g, member) = member_param(s, p, "member", C)?;
let (target_group, target) = member_param(s, p, "target", C)?;
if target_group != g {
return Err(bad(C, "`target` must be an object of the same Objects on Path group as `member`"));
}
if let Some(v) = p.get("group").filter(|v| !v.is_null())
&& checked_id(s, v, C)? != g
{
return Err(bad(C, "`group` must be the Objects on Path group of `member` and `target`"));
}
let at = |s: &Session| -> Result<usize> {
let rec = s.doc()?.doc.node(g).and_then(|n| n.extras().objects_on_path.as_deref().cloned()).ok_or(EngineError::NoNode(g))?;
rec.members.iter().position(|m| m.id == member).ok_or(EngineError::NoNode(member))
};
// Onto its own place: nothing changes, and no undo step is recorded.
if target == member {
return Ok(json!({ "group": g.0, "member": member.0, "index": at(s)? }));
}
// Hidden and locked objects between them keep their places.
let index = s.edit("Move Object on Path", |d, _| {
let mut rec = d.node(g).and_then(|n| n.extras().objects_on_path.as_deref().cloned()).ok_or(EngineError::NoNode(g))?;
let fixed: HashSet<NodeId> = {
let editable = d.editable();
rec.members.iter().map(|m| m.id).filter(|&id| !editable(id)).collect()
};
let index = rec.move_member(member, target, |id| fixed.contains(&id)).ok_or(EngineError::NoNode(member))?;
let n = d.node_mut(g).ok_or(EngineError::NoNode(g))?;
n.extras_mut().objects_on_path = Some(Box::new(rec));
Ok(index)
})?;
Ok(json!({ "group": g.0, "member": member.0, "index": index }))
}
// ---------- Expand ----------
fn expand(s: &mut Session, p: &Value) -> Result<Value> {
+133
View File
@@ -531,6 +531,139 @@ fn a_space_that_is_not_usable_is_refused_and_one_too_large_is_kept_to_the_path()
assert_eq!(space(&s, g), (Some(100.0), 0.0));
}
/// The ids of group `g`'s objects in their order along the path, and of its children in the
/// Layers panel's order.
fn orders(s: &Session, g: NodeId) -> (Vec<NodeId>, Vec<NodeId>) {
let n = s.doc().unwrap().doc.node(g).unwrap();
let path = n.extras().objects_on_path.as_deref().unwrap().members.iter().map(|m| m.id).collect();
(path, n.children().unwrap().iter().map(|c| c.id).collect())
}
/// Three objects of different widths (20, 40 and 10 pt) attached to a 300 pt line from (50, 300),
/// in that order, with a Space of 100 pt from 0, Pivot 4 and Rotate 0.
fn three_on_a_line(s: &mut Session) -> (NodeId, [NodeId; 3], NodeId) {
let a = rect(s, 0.0, 0.0, 20.0, 20.0);
let b = rect(s, 100.0, 0.0, 40.0, 20.0);
let c = rect(s, 200.0, 0.0, 10.0, 20.0);
let path = line(s, (50.0, 300.0), (350.0, 300.0));
let g = attach(s, path, &[a, b, c], json!({}));
s.execute("object.objectsOnPath.options", &json!({"spacing": 100})).unwrap();
(g, [a, b, c], path)
}
#[test]
fn move_member_takes_the_target_s_place_and_shifts_the_objects_between() {
let mut s = session();
let (g, [a, b, c], path) = three_on_a_line(&mut s);
let (_, layers) = orders(&s, g);
let before = s.doc().unwrap().doc.node(g).unwrap().extras().objects_on_path.as_deref().cloned().unwrap();
let steps = s.doc().unwrap().history.undo.len();
let v = s.execute("object.objectsOnPath.moveMember", &json!({"member": a.0, "target": c.0})).unwrap();
assert_eq!(v, json!({"group": g.0, "member": a.0, "index": 2}));
assert_eq!(orders(&s, g), (vec![b, c, a], layers.clone()), "the order along the path changes, the Layers order does not");
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "one undo step");
assert_eq!(center(&s, b), Point::new(50.0, 300.0));
assert_eq!(center(&s, c), Point::new(150.0, 300.0));
assert_eq!(center(&s, a), Point::new(250.0, 300.0));
let after = s.doc().unwrap().doc.node(g).unwrap().extras().objects_on_path.as_deref().cloned().unwrap();
assert_eq!(
(after.path, after.pivot, after.rotate, after.spacing, after.start),
(path, before.pivot, before.rotate, before.spacing, before.start)
);
let w = bounds(&s, a);
assert!((w.width() - 20.0).abs() < 1e-9, "the object keeps its art: {w:?}");
// The last one back to the first place, with its group named.
let v = s.execute("object.objectsOnPath.moveMember", &json!({"group": g.0, "member": a.0, "target": b.0})).unwrap();
assert_eq!(v["index"], 0);
assert_eq!(orders(&s, g).0, [a, b, c]);
assert_eq!(center(&s, a), Point::new(50.0, 300.0));
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(orders(&s, g).0, [b, c, a]);
assert_eq!(center(&s, a), Point::new(250.0, 300.0));
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(orders(&s, g).0, [a, b, c]);
assert_eq!(center(&s, c), Point::new(250.0, 300.0));
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(orders(&s, g).0, [b, c, a]);
// Onto its own place: nothing changes and no step is recorded.
let steps = s.doc().unwrap().history.undo.len();
let v = s.execute("object.objectsOnPath.moveMember", &json!({"member": c.0, "target": c.0})).unwrap();
assert_eq!(v["index"], 1);
assert_eq!(s.doc().unwrap().history.undo.len(), steps);
// The order saves and reopens.
let data = s.execute("document.export", &json!({"format": "vectorcraft"})).unwrap()["dataBase64"].clone();
let mut o = Session::new();
o.execute("document.open", &json!({"name": "objects.vectorcraft", "dataBase64": data})).unwrap();
assert_eq!(orders(&o, g), (vec![b, c, a], layers));
assert_eq!(center(&o, a), Point::new(250.0, 300.0));
}
#[test]
fn move_member_refuses_what_is_not_an_object_on_the_same_path() {
let mut s = session();
let (g, [a, b, c], path) = three_on_a_line(&mut s);
let loose = rect(&mut s, 300.0, 20.0, 10.0, 10.0);
let x = rect(&mut s, 0.0, 200.0, 10.0, 10.0);
let other_line = line(&mut s, (50.0, 380.0), (350.0, 380.0));
let h = attach(&mut s, other_line, &[x], json!({}));
let err = |s: &mut Session, p: Value| s.execute("object.objectsOnPath.moveMember", &p).unwrap_err().to_string();
assert!(err(&mut s, json!({"member": path.0, "target": a.0})).contains("`member` must name an object attached"), "the path");
assert!(err(&mut s, json!({"member": a.0, "target": path.0})).contains("`target` must name"), "the path as the target");
assert!(err(&mut s, json!({"member": loose.0, "target": a.0})).contains("`member` must name"), "an object on no path");
assert!(err(&mut s, json!({"member": g.0, "target": a.0})).contains("`member` must name"), "the group itself");
assert!(err(&mut s, json!({"member": a.0, "target": x.0})).contains("same Objects on Path group"), "another group's object");
assert!(err(&mut s, json!({"member": a.0, "target": b.0, "group": h.0})).contains("`group` must be"), "another group named");
assert!(err(&mut s, json!({"target": b.0})).contains("missing `member`"));
assert!(err(&mut s, json!({"member": a.0})).contains("missing `target`"));
assert!(err(&mut s, json!({"member": 99999, "target": b.0})).contains("no such object"));
assert!(err(&mut s, json!({"member": "a", "target": b.0})).contains("invalid object id"));
// A locked object neither moves nor gives its place, and is not chosen.
select(&mut s, &[c]);
s.execute("object.lock", &json!({})).unwrap();
assert!(err(&mut s, json!({"member": c.0, "target": a.0})).contains("`member` names an object that is hidden or locked"));
assert!(err(&mut s, json!({"member": a.0, "target": c.0})).contains("`target` names an object that is hidden or locked"));
assert!(s.execute("object.objectsOnPath.selectMember", &json!({"member": c.0})).is_err());
s.execute("object.unlockAll", &json!({})).unwrap();
// A locked object between the two keeps its place, and the others pass over it.
select(&mut s, &[b]);
s.execute("object.lock", &json!({})).unwrap();
let v = s.execute("object.objectsOnPath.moveMember", &json!({"member": a.0, "target": c.0})).unwrap();
assert_eq!(v["index"], 2);
assert_eq!(orders(&s, g).0, [c, b, a]);
assert_eq!(center(&s, b), Point::new(150.0, 300.0), "the locked object stays where it was");
s.execute("edit.undo", &json!({})).unwrap();
s.execute("object.unlockAll", &json!({})).unwrap();
select(&mut s, &[g]);
s.execute("object.lock", &json!({})).unwrap();
assert!(s.doc().unwrap().doc.node(g).unwrap().locked);
let e = err(&mut s, json!({"member": a.0, "target": b.0}));
assert!(e.contains("hidden or locked"), "a locked group: {e}");
assert!(s.execute("object.objectsOnPath.selectMember", &json!({"member": a.0})).is_err(), "a locked group's object is not chosen");
assert_eq!(orders(&s, g).0, [a, b, c], "nothing refused changed the order");
}
#[test]
fn select_member_selects_the_group_with_the_object_chosen() {
let mut s = session();
let (g, [a, b, _], path) = three_on_a_line(&mut s);
let v = s.execute("object.objectsOnPath.selectMember", &json!({"member": b.0})).unwrap();
assert_eq!(v, json!({"group": g.0, "member": b.0}));
let sel = &s.doc().unwrap().selection;
assert_eq!((sel.objects.as_slice(), sel.on_path_member), (&[g][..], Some(b)));
// Moving the chosen object keeps it chosen.
s.execute("object.objectsOnPath.moveMember", &json!({"member": b.0, "target": a.0})).unwrap();
assert_eq!(s.doc().unwrap().selection.on_path_member, Some(b));
assert!(s.execute("object.objectsOnPath.selectMember", &json!({"member": path.0})).is_err());
select(&mut s, &[g]);
assert_eq!(s.doc().unwrap().selection.on_path_member, None, "another selection clears it");
// Both take their objects without a selection.
select(&mut s, &[]);
s.execute("object.objectsOnPath.moveMember", &json!({"member": a.0, "target": b.0})).unwrap();
assert_eq!(orders(&s, g).0[..2], [a, b]);
s.execute("object.objectsOnPath.selectMember", &json!({"member": a.0})).unwrap();
assert_eq!(s.doc().unwrap().selection.on_path_member, Some(a));
}
#[test]
fn the_space_saves_and_reopens_and_goes_with_a_copy() {
let mut s = session();