🐛 Move a child that sits on the start of its squeezed parent

A flex layout can squeeze a fill frame to almost no width and move it onto
its child at the frame's right edge. The left constraint keeps the vector
from the child to its parent's left side, but after the move the child
sits on that side, so the vector has no length and no direction. The
displacement scaled a zero unit vector and the child did not move. The
displacement now takes the direction from the parent's side, pointing as
the vector did before.

Tests: start-constraint-moves-a-child-onto-the-start-of-its-squeezed-
parent and nesting-a-copy-that-squeezes-a-fill-frame-moves-the-child-at-
its-end (the variant gate program of round 11, seed 212451915).
Common suite: 2435 tests, 0 failures.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):
|                             :world | :baseline |   :now |  :mean | :ratio |
|------------------------------------+-----------+--------+--------+--------|
|  deepest-edit-through-three-levels |    35.215 | 20.995 | 22.097 |    0.6 |
| edit-through-a-chain-of-dependents |    68.931 | 46.757 | 48.547 |   0.68 |
|         library-update-into-copies |    83.083 | 47.501 | 55.402 |   0.57 |
|        main-edit-among-many-shapes |     4.837 |  2.493 |  2.793 |   0.52 |
|           main-edit-into-10-copies |      1.79 |  1.051 |  1.177 |   0.59 |
|          main-edit-into-100-copies |    15.415 | 10.322 | 11.091 |   0.67 |
|         main-edit-into-1000-copies |   153.086 | 91.123 | 97.288 |    0.6 |
|         variant-switch-into-copies |    32.052 | 20.829 | 21.942 |   0.65 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-11 09:49:36 +01:00
parent 160bb17f76
commit ed24eaa1b6
3 changed files with 51 additions and 1 deletions

View File

@ -148,6 +148,9 @@
((if (= :x axis) center-horizontal-vector center-vertical-vector) child-points parent-points))
(defn displacement
"The move that gives a child the vector `before-v` to its parent's side
again, where it now has `after-v`. A child on the side has no direction
to it, so the parent's side gives the one `before-v` had."
[before-v after-v before-parent-side-v after-parent-side-v]
(let [before-angl (gpt/angle-with-other before-v before-parent-side-v)
@ -155,8 +158,17 @@
sign (if (mth/close? before-angl after-angl) 1 -1)
length (* sign (gpt/length before-v))]
(if (mth/almost-zero? length)
(cond
(mth/almost-zero? length)
after-v
(mth/almost-zero? (gpt/length after-v))
(let [;; the parent's side, pointing as `before-v` did along it
direction (cond-> (gpt/unit after-parent-side-v)
(not (mth/close? before-angl 0)) (gpt/negate))]
(gpt/subtract after-v (gpt/scale direction (gpt/length before-v))))
:else
(gpt/subtract after-v (gpt/scale (gpt/unit after-v) length)))))
(defn side-vector

View File

@ -6,6 +6,7 @@
(ns common-tests.geom-shapes-constraints-test
(:require
[app.common.geom.point :as gpt]
[app.common.geom.shapes.constraints :as gsc]
[app.common.types.modifiers :as ctm]
[clojure.test :as t]))
@ -23,3 +24,30 @@
(t/testing ":default method can be added to modifiers"
(let [result (gsc/constraint-modifier :scale :y nil nil nil nil)]
(t/is (ctm/empty? (ctm/add-modifiers (ctm/empty) result))))))
;; ---- constraint-modifier :start ----
(defn- rect-points
[x y w h]
[(gpt/point x y) (gpt/point (+ x w) y) (gpt/point (+ x w) (+ y h)) (gpt/point x (+ y h))])
(defn- move-vector
[modifiers]
(reduce gpt/add (gpt/point 0 0)
(keep #(when (= :move (:type %)) (:vector %)) (:geometry-child modifiers))))
(t/deftest start-constraint-moves-a-child-onto-the-start-of-its-squeezed-parent
(t/testing "a child at the end of its parent keeps its start offset when the
parent is squeezed to zero and moved onto it"
(let [child (rect-points 100 0 100 100)
before (rect-points 0 0 100 100)
after (rect-points 100 0 0.01 100)
result (gsc/constraint-modifier :start :x child before child after)]
(t/is (gpt/close? (gpt/point 100 0) (move-vector result)))))
(t/testing "a child the moved parent passes over keeps its start offset"
(let [child (rect-points 50 0 100 100)
before (rect-points 0 0 100 100)
after (rect-points 100 0 0.01 100)
result (gsc/constraint-modifier :start :x child before child after)]
(t/is (gpt/close? (gpt/point 100 0) (move-vector result))))))

View File

@ -2209,6 +2209,16 @@
[:combine 18] [:make-variant] [:switch 18] [:combine] [:rotate 8]
[:update-main]]}))))
(t/deftest nesting-a-copy-that-squeezes-a-fill-frame-moves-the-child-at-its-end
(t/is (map? (cg/run {:world [3 2 {:variants true :text-styles false :layout :flex :variant-diff false
:layout-item-margins false :grid-square false :rich-variants true
:layout-children false :grid-empty false :layout-copies true :text true}]
:ops [[:add-variant] [:delete 10] [:restore] [:delete 10] [:restore] [:combine 12]
[:combine 9] [:combine] [:style 0 29 1] [:add-variant] [:add-variant]
[:make-variant] [:combine] [:add-variant] [:add-variant] [:add-variant]
[:add-variant] [:combine] [:add-variant] [:combine] [:update-main]
[:nest 13]]}))))
(t/deftest pushing-a-grid-direction-orders-the-copies-like-the-reflowed-main
(t/is (map? (cg/run {:world [3 2 {:variants true :text-styles false :layout :grid :variant-diff false
:layout-item-margins false :grid-square true :rich-variants false