🐛 Keep a copy child's geometry when the copy authors its constraint

A copy resized after its main keeps a child with top constraints at its
size. Setting that child's vertical constraint to scale in the copy
moves nothing, but the next sync resized the child through the new
constraint by the whole earlier resize: a 20 px child of a copy twice
its main's height became 40 px, at whatever later moment a sync came. A
constraint applies to the resizes after it, so a copy child whose copy
authors a constraint that ties one of its dimensions keeps the geometry
it shows, and its children follow it. The test oracle expects the same.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  20.631 |  0.59 |
| edit-through-a-chain-of-dependents |   68.931 |  39.243 |  0.57 |
| library-update-into-copies         |   83.083 |  46.415 |  0.56 |
| main-edit-among-many-shapes        |    4.837 |   2.091 |  0.43 |
| main-edit-into-10-copies           |     1.79 |   1.073 |  0.60 |
| main-edit-into-100-copies          |   15.415 |   9.951 |  0.65 |
| main-edit-into-1000-copies         |  153.086 | 100.057 |  0.65 |
| variant-switch-into-copies         |   32.052 |  20.292 |  0.63 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-02 22:02:53 +01:00
parent 0b30155159
commit 05ae99a666
2 changed files with 38 additions and 1 deletions

View File

@ -638,6 +638,17 @@
height (:height (:selrect (if height? shape-inst shape-main)))]
(gsh/transform-shape shape-main (ctm/change-size shape-main width height))))))
(defn- constraint-kept?
"Whether the copy shape `shape-inst` keeps the geometry it shows at sync:
its copy authors a constraint that ties one of its dimensions to its copy
parent. A constraint applies to the resizes after it, so sync does not
resize the shape through it again."
[container shape-inst]
(and (ctk/touched-group? shape-inst :constraints-group)
(or (contains? #{:scale :leftright} (:constraints-h shape-inst))
(contains? #{:scale :topbottom} (:constraints-v shape-inst)))
(some? (:shape-ref (ctn/get-shape container (:parent-id shape-inst))))))
(defn- sync-copy-recursive
"Sync the copy shape `shape-inst` from `shape-main`, the main shape it
pairs with, then pair their children (`compare-children`) and walk each
@ -665,7 +676,8 @@
own-flow? (and (not reset?) (ctsl/grid-layout? shape-inst)
(ctk/touched-group? shape-inst :layout-grid-cells))
placed (cond
(and (not reset?) (ctk/touched-group? shape-inst :geometry-group))
(and (not reset?) (or (ctk/touched-group? shape-inst :geometry-group)
(constraint-kept? container shape-inst)))
(in-main-frame shape-inst (:root-inst ctx) (:root-main ctx))
(and (not reset?) (= (:id shape-inst) (:id (:root-inst ctx))))

View File

@ -460,6 +460,31 @@
(t/is (mth/close? 100 (:width after)))
(t/is (not (contains? (:touched after) :geometry-group)))))
(t/deftest a-constraint-a-copy-sets-after-a-resize-moves-nothing-at-sync
;; A constraint the copy sets on a child applies to later resizes, so the
;; next sync keeps the child as the earlier resize left it.
(let [file (-> (thf/sample-file :file1)
(tho/add-simple-component :c :c-root :c-child
:root-params {:width 100 :height 100}
:child-params {:x 0 :y 0 :width 20 :height 20})
(thc/instantiate-component :c :copy :children-labels [:copy-child]))
resized (-> (tm/make-situation file)
(r/step (n/change-height :copy 200))
(r/step (n/change-property :copy-child :constraints-v :scale))
r/settle)
edited (-> resized (r/step (n/change-property :c-child :fills "#dc2626")) r/settle)
child (tm/resolve-shape edited :copy-child)]
(t/is (mth/close? 20 (:height (tm/resolve-shape resized :copy-child))))
(t/is (= "#dc2626" (get-in child [:fills 0 :fill-color])))
(t/is (mth/close? 20 (:height child)))
(t/is (not (contains? (:touched child) :geometry-group)))))
(t/deftest generated-constraint-a-copy-sets-after-a-resize-moves-nothing
;; a copy resized from its 0.01 main sets a child's vertical constraint to
;; scale; the child keeps the height the resize left it
(t/is (map? (cg/run {:world [2 0 {:text true :text-styles true}]
:ops [[:add-child 0 0 0 0] [:resize 0 0 0 0] [:delete 0 0 0 0] [:style 1 9 1]]}))))
(t/deftest a-resized-turned-main-keeps-its-copies-at-their-first-corner
;; a width change keeps a shape's first corner; a copy oriented like its
;; main keeps its own first corner, whether the turn or flip is the main's