From 6cf52800385164141ed09b7edd27d644589304fb Mon Sep 17 00:00:00 2001 From: Zach Berry <1112351+zberry@users.noreply.github.com> Date: Sun, 18 Oct 2026 18:26:52 -0500 Subject: [PATCH] shape.connect takes the direction between the shapes' box centres, not the means of their sites The mean of a shape's connection sites is off its centre when the sites are not symmetric (a triangle's lie mostly along its base), which pulled the dominant direction sideways and glued an arrow to a box's side when the box was mostly below. The centres come from xfrm_of, so placeholders and tables are measured as drawn. Co-Authored-By: Claude Fable 5.1 --- crates/engine/src/cmd/shape/freeform.rs | 9 ++++----- crates/engine/src/tests.rs | 7 +++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/shape/freeform.rs b/crates/engine/src/cmd/shape/freeform.rs index b068f86ac..b85e9b52f 100644 --- a/crates/engine/src/cmd/shape/freeform.rs +++ b/crates/engine/src/cmd/shape/freeform.rs @@ -27,11 +27,10 @@ pub(super) fn connect(s: &mut Session, p: &Value) -> Result { // A site left unsaid is the one facing the other shape along the dominant direction between // the two (a box mostly below is left from the bottom), which is where a person glues an // arrow; "the closest pair" chose a side site for a diagonal neighbour and crossed boxes. - let centre = |v: &[deckcraft_geom::Point]| { - let n = v.len().max(1) as f64; - deckcraft_geom::Point::new(v.iter().map(|q| q.x).sum::() / n, v.iter().map(|q| q.y).sum::() / n) - }; - let (ca, cb) = (centre(&a), centre(&b)); + // The direction runs between the shapes' box centres: the mean of the sites is off-centre + // for a shape whose sites are not symmetric (a triangle's lie mostly along its base). + let centre = |id| st.shape(id).map(|sh| xfrm_of(&st.doc, &st.selection, sh).center()).unwrap_or_default(); + let (ca, cb) = (centre(from), centre(to)); let facing = |v: &[deckcraft_geom::Point], c: deckcraft_geom::Point, dx: f64, dy: f64| { let len = dx.hypot(dy).max(1e-9); v.iter() diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index af36218e0..135d83df5 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6762,6 +6762,13 @@ fn connector_default_sites_follow_the_dominant_direction() { assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(2), Some(0)), "mostly below: bottom → top, not right → left: {r}"); let r = s.execute("shape.connect", &json!({"from": diag, "to": a})).unwrap(); assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(0), Some(2)), "and back: top → bottom: {r}"); + // The direction runs between the shapes' centres, not between the means of their sites: a + // triangle's sites lie mostly along its base, which pulled the direction sideways and chose + // the box's left site for a box that is mostly below. + let tri = s.execute("shape.insert", &json!({"preset": "triangle", "rect": [400, 0, 100, 100]})).unwrap()["id"].as_u64().unwrap(); + let below = s.execute("shape.insert", &json!({"preset": "rect", "rect": [520, 155, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + let r = s.execute("shape.connect", &json!({"from": tri, "to": below})).unwrap(); + assert_eq!(r["toSite"].as_u64(), Some(0), "centre to centre is mostly down: the box's top, not its left: {r}"); } /// A size (or colour, bold…) set on a shape before it has any text survives the text that is