🐛 Carry no size a copy's layout gave it over a variant switch

A nested copy that fills its flex parent while its main is turned gets
its size from the layout. Switching it to a variant without that layout
carried the size as an override, as both mains agree on their size and
its copy chain authors a turn, so it kept the layout's size for good.

A dimension the layout of the copy gave it before the switch is no
override: the copy takes the new variant's.

Test: a-switch-carries-no-size-the-old-layout-gave-a-child.
Switch, swap, sync and golden master tests pass (215); variant programs
pass (seed 9007, 300).

Benchmark (alone):

|                             :world | :baseline |   :now |   :mean | :ratio |
|------------------------------------+-----------+--------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 | 20.013 |  20.948 |   0.57 |
| edit-through-a-chain-of-dependents |    68.931 | 39.641 |  40.924 |   0.58 |
|         library-update-into-copies |    83.083 | 50.865 |  52.074 |   0.61 |
|        main-edit-among-many-shapes |     4.837 |  2.536 |   3.259 |   0.52 |
|           main-edit-into-10-copies |      1.79 |   1.04 |   1.067 |   0.58 |
|          main-edit-into-100-copies |    15.415 |  9.946 |  10.414 |   0.65 |
|         main-edit-into-1000-copies |   153.086 | 99.227 | 101.039 |   0.65 |
|         variant-switch-into-copies |    32.052 | 21.451 |  24.531 |   0.67 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-13 15:32:09 +01:00
parent 6f7a77504f
commit e0e5966b7e
3 changed files with 19 additions and 7 deletions

View File

@ -2141,7 +2141,7 @@
(= (boolean (:flip-x shape)) (flip? :flip-x))
(= (boolean (:flip-y shape)) (flip? :flip-y))))))
(defn- layout-sized
(defn layout-sized
"The dimensions a layout gives `shape`: those it fills in its layout
parent or hugs as a layout itself, and both once it is turned in a
layout parent, which resizes it along its own axes."

View File

@ -93,14 +93,16 @@
[prev-shape ; The shape before the switch
current-shape ; The shape after the switch (a clean copy)
origin-shape ; The original shape
owned ; The dimensions the layout of the shape before gave it
attr]
;; Each dimension keeps the copy's size when both variants agree on it,
;; and takes the new variant's otherwise (selrect and points follow)
;; and takes the new variant's otherwise (selrect and points follow); a
;; size the copy's layout gave it is no override
(let [size (fn [dimension]
(let [prev (get prev-shape dimension)
current (get current-shape dimension)
origin (get origin-shape dimension)]
(if (mth/close? origin current) prev current)))
(if (and (mth/close? origin current) (not (contains? owned dimension))) prev current)))
width (size :width)
height (size :height)
;; the turn and flips the switch carries, when both variants agree on them
@ -194,7 +196,7 @@
where `origin-ref-shape`, the main shape before the switch, and the new
copy agree on the attribute (decision (a) of the component contract);
geometry carries the override's size at the new position."
[current-shape previous-shape origin-ref-shape touched text-auto? attr]
[current-shape previous-shape origin-ref-shape touched text-auto? owned attr]
(let [sync-group
(ctk/resolve-sync-group (:type previous-shape) attr)
@ -297,7 +299,7 @@
;; Both scalar dimensions and composite geometry must describe
;; the same result when only one dimension carries an override.
(contains? #{:points :selrect :width :height} attr)
(switch-size-change-value previous-shape current-shape origin-ref-shape attr)
(switch-size-change-value previous-shape current-shape origin-ref-shape owned attr)
:else
(get previous-shape attr)))]
@ -361,7 +363,9 @@
;; and it's the source shape that may have touched elements, and we only want
;; to copy those touched elements.
[changes current-shape previous-shape current-root prev-root origin-ref-shape container]
(let [;; We need to sync only the position relative to the origin of the component.
(let [;; the dimensions the layout of the copy gave it before the switch
owned (cll/layout-sized (:objects container) previous-shape)
;; We need to sync only the position relative to the origin of the component.
;; (see cll/update-attrs for a full explanation)
previous-shape (-> previous-shape
(cll/resize-with-parent (get (:objects container) (:parent-id previous-shape))
@ -378,7 +382,7 @@
[roperations uoperations]
(-> (cll/attr-operations current-shape
#(switch-attr-operations current-shape previous-shape origin-ref-shape
touched text-auto? %)
touched text-auto? owned %)
attrs)
(reoriented-operations current-shape previous-shape))

View File

@ -2604,6 +2604,14 @@
:layout-copies false :text false}]
:ops [[:duplicate] [:swap] [:resize] [:resize 2] [:style 0 9 1] [:edit 2 0 0 1] [:update-main 2]]}))))
(t/deftest a-switch-carries-no-size-the-old-layout-gave-a-child
(t/is (map? (cg/run {:world [2 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 [[:combine 20] [:switch 0] [:add-variant 0] [:switch 0] [:combine 16 0 0 0]
[:rotate 23 0 0 0] [:style 26 29 1] [:combine 25 0 0 0] [:variant-property 0 0 0 0]
[:switch 16]]}))))
(t/deftest settling-a-bool-in-a-grid-copy-twice-keeps-its-shapes-in-place
(t/is (map? (cg/run {:world [3 0 {:variants true :rich-variants true :text true :layout :grid
:layout-copies true :layout-children true}]