🐛 Carry no position of a child across a variant switch

A switch carried a child's :x or :y when the old main's child and the new
copy's child shared the coordinate, though one lies in main space and the
other in copy space. A turned copy whose child inherits a resize could
then switch to a child whose :y disagreed with its selrect and points,
touched for a place it never authored. The switch now keeps the new
variant's position and carries only a size.

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

|                             :world | :baseline |   :now |  :mean | :ratio |
|------------------------------------+-----------+--------+--------+--------|
|  deepest-edit-through-three-levels |    35.215 | 19.545 | 20.219 |   0.56 |
| edit-through-a-chain-of-dependents |    68.931 | 37.535 | 39.465 |   0.54 |
|         library-update-into-copies |    83.083 | 52.071 | 53.432 |   0.63 |
|        main-edit-among-many-shapes |     4.837 |  2.116 |  2.202 |   0.44 |
|           main-edit-into-10-copies |      1.79 |  1.055 |  1.079 |   0.59 |
|          main-edit-into-100-copies |    15.415 |  9.013 |  9.777 |   0.58 |
|         main-edit-into-1000-copies |   153.086 | 94.616 | 97.597 |   0.62 |
|         variant-switch-into-copies |    32.052 | 23.127 | 23.774 |   0.72 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-08 17:15:04 +01:00
parent bef82a1a45
commit ec59d15021
3 changed files with 30 additions and 0 deletions

View File

@ -175,6 +175,9 @@
;; If the attribute is not valid for the destiny, don't copy it
(not (cts/is-allowed-switch-keep-attr? attr (:type current-shape)))
;; The position is the new variant's; geometry carries only a size
(contains? #{:x :y} attr)
;; If the values are already equal, don't copy them; geometry
;; within its precision
(if (= sync-group :geometry-group)

View File

@ -1841,6 +1841,14 @@
:ops [[:nest 17 0 0 0] [:duplicate 9 0 0 0] [:make-variant 21 0 0 0]
[:transform 6 16 0 18] [:rotate 15 0 0 0] [:switch 14 0 0 0]]}))))
(t/deftest switching-a-turned-copy-back-keeps-its-child-in-place
(t/is (map? (cg/run {:world [3 2 {:variants true :text-styles true :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 [[:delete 0 0 0 0] [:variant-property 0 1 0 0] [:switch 0] [:delete 4 0 0 0]
[:delete 7 0 0 0] [:make-variant 8 0 0 0] [:transform 13 3 7 0] [:combine 7]
[:switch 7] [:resize 10 0 0 0] [:combine 1] [:switch 10]]}))))
(t/deftest random-variant-compositions
(let [coverage (atom {})
failure (atom nil)]

View File

@ -3715,6 +3715,25 @@
(t/is (mth/close? width (:width points)))
(t/is (mth/close? height (:height points)))))
(t/deftest test-switch-places-a-moved-child-where-the-new-variant-does
;; the copy lies where its main does, so the old main's child and the new
;; copy's share their coordinates; a switch still carries no place
(let [file (-> (thf/sample-file :file1)
(thv/add-variant-with-child
:v01 :c01 :m01 :c02 :m02 :r01 :r02
{:child1-params {:width 100 :height 50 :type :rect}
:child2-params {:width 100 :height 50 :type :rect}})
(thc/instantiate-component :c01 :copy01 :children-labels [:copy-r01])
(tho/move-shape-tree :copy01 (gpt/point -100 -100) :ignore-touched true)
(tho/move-shape-tree :copy-r01 (gpt/point 0 20)))
rect (copy-child file :copy01)
file' (tho/swap-component-in-shape file :copy01 :c02 {:new-shape-label :copy02 :keep-touched? true})
rect' (copy-child file' :copy02)]
(t/is (contains? (:touched rect) :geometry-group))
(t/is (mth/close? (- (:y (ths/get-shape file' :r02)) (:y (ths/get-shape file' :m02)))
(- (:y rect') (:y (ths/get-shape file' :copy02)))))
(t/is (mth/close? (-> rect' :selrect :y) (:y rect')))))
(t/deftest test-switch-keeps-a-resized-child-of-same-size-variants
(let [file (-> (thf/sample-file :file1)
(thv/add-variant-with-child