🐛 Keep a copy child's layout item overrides through a variant switch

A switch carries the overrides of a copy's children into the new
variant where both variants agree on the attribute, but it never carried
their layout item attributes: margins, sizing, limits, absolute position,
z-index and alignment. The switch walked the attributes a sync updates,
which leave those out, and its list of attributes a switch may keep did
not have them either, so a copy child lost its own margin or its
absolute position, and the override with it. A switched child now
carries them under the same rule; the switched head keeps its own
placement as before.

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

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  21.753 |  0.62 |
| edit-through-a-chain-of-dependents |   68.931 |  42.985 |  0.62 |
| library-update-into-copies         |   83.083 |  51.623 |  0.62 |
| main-edit-among-many-shapes        |    4.837 |   1.988 |  0.41 |
| main-edit-into-10-copies           |    1.790 |   0.971 |  0.54 |
| main-edit-into-100-copies          |   15.415 |   9.418 |  0.61 |
| main-edit-into-1000-copies         |  153.086 | 102.474 |  0.67 |
| variant-switch-into-copies         |   32.052 |  19.863 |  0.62 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-08 02:50:49 +01:00
parent e1decc4812
commit fb00559613
5 changed files with 70 additions and 13 deletions

View File

@ -1515,17 +1515,19 @@
(defn attr-operations
"The redo and undo `:set` operations, as `[roperations uoperations]`, that
copy attributes into `dest-shape`. For each of `updatable-attrs`,
`attr-ops` gives the `[attr value]` pairs to set, in order: none to
leave it alone, and possibly some derived attributes before it."
[dest-shape attr-ops]
(reduce (fn [operations attr]
(reduce (fn [[roperations uoperations] [attr value]]
(add-update-attr-operations attr dest-shape roperations uoperations value))
operations
(attr-ops attr)))
[[] '()]
updatable-attrs))
copy attributes into `dest-shape`. For each of `attrs`, by default
`updatable-attrs`, `attr-ops` gives the `[attr value]` pairs to set, in
order: none to leave it alone, and possibly some derived attributes
before it."
([dest-shape attr-ops] (attr-operations dest-shape attr-ops updatable-attrs))
([dest-shape attr-ops attrs]
(reduce (fn [operations attr]
(reduce (fn [[roperations uoperations] [attr value]]
(add-update-attr-operations attr dest-shape roperations uoperations value))
operations
(attr-ops attr)))
[[] '()]
attrs)))
(defn- automatic-name
"The name the editors give a text after its `content`, or nil when it has

View File

@ -309,11 +309,16 @@
touched (get previous-shape :touched #{})
text-auto? (and (cfh/text-shape? current-shape)
(contains? #{:auto-height :auto-width} (:grow-type current-shape)))
;; a child carries its layout item overrides too; the head keeps its own placement
attrs (cond-> cll/updatable-attrs
(not= (:id current-shape) (:id current-root))
(concat (disj ctk/swap-keep-attrs :interactions)))
[roperations uoperations]
(cll/attr-operations current-shape
#(switch-attr-operations current-shape previous-shape origin-ref-shape
touched text-auto? %))
touched text-auto? %)
attrs)
updated-attrs (into #{} (map :attr) roperations)
updated-sync-groups (into #{}

View File

@ -596,7 +596,10 @@
:hidden :masked-group :fills :proportion :proportion-lock :constraints-h
:constraints-v :fixed-scroll :r1 :r2 :r3 :r4 :rotation :opacity :grids :exports
:strokes :blend-mode :interactions :shadow :blur :background-blur :grow-type :applied-tokens
:plugin-data :flip-x :flip-y})
:plugin-data :flip-x :flip-y
:layout-item-margin :layout-item-margin-type :layout-item-h-sizing :layout-item-v-sizing
:layout-item-max-h :layout-item-min-h :layout-item-max-w :layout-item-min-w
:layout-item-absolute :layout-item-z-index :layout-item-align-self})
(def ^:private allowed-shape-geom-attrs #{:x :y :width :height})
(def ^:private allowed-shape-base-attrs #{:id :name :type :selrect :points :transform

View File

@ -1749,6 +1749,13 @@
:grid-empty false :layout-copies true :text true}]
:ops [[:swap 0 0 0 0] [:library-add-variant 0 0 0 0] [:library-sync] [:switch 0]]}))))
(t/deftest switching-a-copy-keeps-the-layout-item-overrides-of-its-children
(t/is (map? (cg/run {:world [3 2 {:variants true :text-styles true :layout :grid :variant-diff false
:layout-item-margins false :grid-square false :rich-variants true
:layout-children true :grid-empty true :layout-copies true :text true}]
:ops [[:make-variant 2 0 0 0] [:make-variant 14 0 0 0] [:make-variant 14 0 0 0]
[:style 13 27 1] [:make-variant 0] [:switch 18]]}))))
(t/deftest random-variant-compositions
(let [coverage (atom {})
failure (atom nil)]

View File

@ -2079,6 +2079,46 @@
;; The later sync must not overwrite the protected overrides
(check-layout-item-attrs-kept-partial copy01'' "after main sync")))
(def ^:private child-layout-item-overrides
;; Overrides on a copy child that leave its geometry alone.
{:layout-item-margin {:m1 10 :m2 10 :m3 10 :m4 10}
:layout-item-margin-type :multiple
:layout-item-max-h 500
:layout-item-absolute true
:layout-item-z-index 5
:layout-item-align-self :start})
(t/deftest test-switch-keeps-the-layout-item-overrides-of-a-copy-child
;; The children of both variants agree on their layout item values, so
;; the copy child's overrides carry with their touched groups.
(let [;; ==== Setup
child-params (select-keys layout-item-main-values (keys child-layout-item-overrides))
file (-> (thf/sample-file :file1)
(thv/add-variant-with-child :v01 :c01 :m01 :c02 :m02 :r01 :r02
{:child1-params child-params
:child2-params child-params})
(tho/change-shape :m01 #(assoc % :layout :flex :layout-flex-dir :row))
(tho/change-shape :m02 #(assoc % :layout :flex :layout-flex-dir :row))
(thc/instantiate-component :c01 :copy01 :children-labels [:copy-r01]))
page (thf/current-page file)
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
#{(thi/id :copy-r01)}
#(merge % child-layout-item-overrides)
(:objects page)
{})
file (thf/apply-changes file changes)
;; ==== Action
file' (tho/swap-component-in-shape file :copy01 :c02 {:new-shape-label :copy02 :keep-touched? true})
copy02' (ths/get-shape file' :copy02)
child' (get-in (thf/current-page file') [:objects (first (:shapes copy02'))])]
(t/is (= (thi/id :r02) (:shape-ref child')))
(t/is (= child-layout-item-overrides (select-keys child' (keys child-layout-item-overrides))))
(t/is (= #{:layout-item-margin :layout-item-max-h :layout-item-absolute
:layout-item-z-index :layout-item-align-self}
(:touched child')))))
;; ============================================================
;; LAYOUT ITEM SIZING - VERTICAL (fix, auto, fill, none)
;; ============================================================