🐛 Place a head switched back in the parent a layout moved

Switching a swapped head back to the variant its main holds syncs it from
the copy's root, which misses a move the copy's layout gave its parent:
a grid copy with its own gap moves its child boards, and the restored
head landed where the board would be without that gap. The restored head
now takes the place its main shape has in its actual parent, unless it
authors its geometry or a layout places it.

Benchmark (lower quantile, ms):

| world                              | baseline |     now |    mean | ratio |
|------------------------------------+----------+---------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  23.030 |  27.413 |  0.65 |
| edit-through-a-chain-of-dependents |   68.931 |  42.850 |  57.976 |  0.62 |
| library-update-into-copies         |   83.083 |  60.905 |  63.778 |  0.73 |
| main-edit-among-many-shapes        |    4.837 |   3.209 |   4.225 |  0.66 |
| main-edit-into-10-copies           |    1.79  |   1.162 |   1.511 |  0.65 |
| main-edit-into-100-copies          |   15.415 |  11.296 |  12.963 |  0.73 |
| main-edit-into-1000-copies         |  153.086 | 103.929 | 116.557 |  0.68 |
| variant-switch-into-copies         |   32.052 |  22.613 |  25.676 |  0.71 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-13 21:48:29 +01:00
parent a9cce26fe0
commit 92ab1a2486
2 changed files with 37 additions and 1 deletions

View File

@ -659,6 +659,32 @@
(when-let [parent (get objects parent-id)]
(or (d/index-of (:shapes parent) shape-id) 0)))
(defn- place-restored-in-parent
"Move the swapped-back copy shape `shape-id`, with its subtree, to where
its position sits in the shape's parent. Sync places it from the copy's
root, which misses the move a layout gave a parent the sync leaves; its
own authored geometry keeps it where it is."
[changes page libraries shape-id]
(let [objects (pcb/get-objects changes)
shape (get objects shape-id)
parent (get objects (:parent-id shape))
;; a layout places the shape itself
position (when-not (or (ctk/touched-group? shape :geometry-group)
(and (ctsl/any-layout-immediate-child? objects shape)
(not (ctsl/position-absolute? shape))))
(ctf/find-ref-shape nil (assoc page :objects objects) libraries shape
:include-deleted? true :with-context? true))
position-objects (:objects (:container (meta position)))
position-parent (get position-objects (:parent-id position))
placed (when (and position-parent (= :frame (:type parent)) (= :frame (:type position-parent)))
(get (cll/placed-like position-objects position-parent parent nil) (:id position)))
delta (when placed
(gpt/subtract (first (:points placed)) (first (:points shape))))]
(cond-> changes
(and delta (not (gpt/almost-zero? delta)))
(pcb/update-shapes (cfh/get-children-ids-with-self objects shape-id) #(gsh/move % delta)
{:ignore-touched true}))))
(defn- sync-restored-position
"A swap back is an ordinary copy of its position, including that
position's content overrides. Sync it through the head that owns its
@ -674,7 +700,9 @@
head (d/seek #(= (:id component) (:component-id %))
(ctn/get-parent-copy-heads objects parent))]
(if head
(cll/generate-sync-shape-direct changes nil libraries container (:id head) false shape-id)
(-> changes
(cll/generate-sync-shape-direct nil libraries container (:id head) false shape-id)
(place-restored-in-parent page libraries shape-id))
changes))
changes)))

View File

@ -4278,3 +4278,11 @@
(t/is (map? (cg/run {:world [2 0 {:tokens false :variants true :rich-variants true :text true :layout :flex
:layout-copies false}]
:ops [[:rotate 3] [:nest 7] [:update-main] [:paste 9]]}))))
(t/deftest generated-a-head-switched-back-sits-in-the-parent-a-grid-moved
;; a swapped head switched back to its main's variant is placed in its
;; parent, which the grid of its copy moved by the gap it overrides
(t/is (map? (cg/run {:world [1 2 {:variants true :layout :grid :layout-item-margins false :grid-square false
:rich-variants true :layout-children false :grid-empty false
:layout-copies true :text true}]
:ops [[:style 14 11 1] [:text-style 14 0 1] [:nest 7] [:switch 9] [:switch 9]]}))))