🐛 Compare a switched child with its ref in its parent's frame

A copy that turns a nested copy turns the children of that nested copy
with it. When a resize deeper in a child's chain marks it with the
geometry group, the variant switch compares the old child with its ref
to tell whether the copy authored its geometry. It turned the ref only
into the frame of the copy root, so the turn the child inherits from its
nested parent looked authored: the switch placed the child at its main's
unturned offset and marked it touched.

`generate-keep-touched` also turns the ref into the frame of the old
child's parent, so only a turn the child authors itself carries.

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

|                             :world | :baseline |   :now |  :mean | :ratio |
|------------------------------------+-----------+--------+--------+--------|
|  deepest-edit-through-three-levels |    35.215 | 20.988 | 21.212 |    0.6 |
| edit-through-a-chain-of-dependents |    68.931 | 41.108 | 42.793 |    0.6 |
|         library-update-into-copies |    83.083 |  45.86 | 47.865 |   0.55 |
|        main-edit-among-many-shapes |     4.837 |  2.114 |  2.268 |   0.44 |
|           main-edit-into-10-copies |      1.79 |  1.004 |  1.024 |   0.56 |
|          main-edit-into-100-copies |    15.415 |  9.828 |  9.983 |   0.64 |
|         main-edit-into-1000-copies |   153.086 | 98.452 | 99.954 |   0.64 |
|         variant-switch-into-copies |    32.052 | 20.156 | 21.317 |   0.63 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-09 19:06:15 +01:00
parent f795f76545
commit 4674812f62
3 changed files with 56 additions and 1 deletions

View File

@ -544,13 +544,30 @@
(cll/orient-like new-base-ref-shape new-head
(gsh/shape->center new-base-ref-shape)))
identity)
;; `ref-shape` turned too as the copy turns the parent of `old` below
;; its root, so a turn a nested copy authors is not the child's own
nested-turn (fn [ref-shape old old-ref]
(let [ref-objects (:objects (:container (meta old-ref)))
ref-root (get ref-objects (:id orig-base-ref-shape))
ref-parent (get ref-objects (:parent-id old-ref))
old-parent (get page-objects (:parent-id old))
root-turn (when (and ref-root ref-parent)
(cll/orient-like ref-root original-shape
(gsh/shape->center ref-root)))
turned (when ref-parent
(cond-> ref-parent (some? root-turn) root-turn))
turn (when (and turned old-parent
(not= (:id old-parent) (:id original-shape)))
(cll/orient-like turned old-parent (gsh/shape->center turned)))]
(cond-> ref-shape (some? turn) turn)))
changes
(reduce (fn [changes {:keys [old old-ref new swapped?]}]
(if swapped?
(keep-swapped-item changes new old ldata page libraries)
(clsw/update-attrs-on-switch changes new old new-shape original-shape
(orient-ref old-ref) container)))
(nested-turn (orient-ref old-ref) old old-ref)
container)))
changes
pairs)

View File

@ -1982,6 +1982,14 @@
[:duplicate 1 0 0 0] [:transform 1 4 0 4] [:variant-property 0 0 0 0]
[:switch 6] [:rotate 10 0 0 0] [:rotate 1 0 0 0]]}))))
(t/deftest switching-a-copy-places-the-child-of-its-turned-nested-copy
(t/is (map? (cg/run {:world [3 2 {:variants true :text-styles true :layout nil :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 [[:nest 15 0 0 0] [:resize 9 0 0 0] [:nest 7 0 0 0] [:combine 4 0 0 0]
[:make-variant 2 0 0 0] [:make-variant 6 0 0 0] [:swap 3 0 0 0]
[:transform 14 3 0 13] [:make-variant 4] [:switch 9]]}))))
(t/deftest updating-a-main-keeps-a-pushed-swap-its-main-converged-to
(t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false
:layout-item-margins false :grid-square false :rich-variants true

View File

@ -71,6 +71,36 @@
(t/is (= (:data (tm/file before))
(:data (tm/file (r/step switched (n/undo)))))))))
(t/deftest switch-places-the-child-of-a-turned-nested-copy-by-its-parent
;; the variants hold a copy whose child a main of its chain resized, and the
;; switched copy turns that copy: the child follows its turned parent
(let [file (-> (thf/sample-file :file)
(tho/add-frame :leaf-main {:width 50 :height 50})
(thc/make-component :leaf :leaf-main)
(tho/add-frame :holder-main {:width 100 :height 100})
(thc/instantiate-component :leaf :holder-leaf :parent-label :holder-main)
(thc/make-component :holder :holder-main)
(thv/add-variant :variants :a :main-a :b :main-b
:variant1-params {:width 100 :height 100}
:variant2-params {:width 100 :height 100})
(thc/instantiate-component :holder :a-holder :parent-label :main-a)
(thc/instantiate-component :holder :b-holder :parent-label :main-b)
(thc/instantiate-component :a :copy))
nested (fn [situation] (first (:shapes (tm/resolve-shape situation :copy))))
before (-> (tm/make-situation file)
(r/step (n/change-height :holder-leaf 40)) r/settle
(r/step (n/rotate nested 45)) r/settle)
switched (-> before (r/step (n/switch-variant :copy "Value2")) r/settle)
holder (tm/shape-by-id switched (nested switched))
leaf (tm/shape-by-id switched (first (:shapes holder)))
file (tm/file switched)
full (:file (r/sync-cascade file {} (keys (get-in file [:data :components]))))]
(t/is (mth/close? 45 (:rotation holder) 0.001))
(t/is (not (ctk/touched-group? leaf :geometry-group)))
(t/is (mth/close? 45 (:rotation leaf) 0.001))
(t/is (gsh/close-attrs? :points (:points leaf)
(:points (ths/get-shape-by-id full (:id leaf))) 0.001))))
(t/deftest switching-an-untouched-rotated-copy-authors-its-retained-orientation
(let [file (-> (thf/sample-file :file)
(thv/add-variant :variant :c01 :m01 :c02 :m02