✨ Carry grid cell overrides between identical variant grids

A switch carries a copy's overrides when the old and new mains agree on the
original value, but grid cells never carried: a copy that moved a child to
another cell lost the move on every switch, even between variants with the
same grid.

Carry a switched grid's touched cells when the old main and the new copy
have the same tracks and the same cells holding corresponding children
(compared by child path). Children and cells take their ids in the new
copy; the cells group stays touched. A different grid keeps the new
variant's cells. The switched board joins the boards whose layout updates.

The two regressions fail before the change and pass after it: a move to row
2, column 2 survives a switch to an identical grid, and a switch to a
three-column grid takes that variant's cells. Full common passes lint, JVM,
JavaScript and formatting: JVM 2076 tests / 33907 assertions; JavaScript
2012 / 32502. All six component properties pass 40 programs each at seed
712 in both runtimes.

Compare lower-quantile milliseconds with the saved baseline; keep that
baseline unchanged. The machine/JVM warning makes ratios rough. No world
is flagged.

World                      Baseline       Now    Ratio
Main into 10 copies           1.790     1.003     0.56
Main into 100 copies         15.415     9.979     0.65
Main into 1000 copies       153.086   102.913     0.67
Three nesting levels         35.215    21.423     0.61
Dependency chain             68.931    36.376     0.53
Library update               83.083    46.632     0.56
Many plain shapes             4.837     2.303     0.48
Variant switch               32.052    21.650     0.68

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-10-31 13:00:22 +01:00
parent 15f654aa1d
commit e94b3c19bf
2 changed files with 148 additions and 1 deletions

View File

@ -391,6 +391,45 @@
#(update % :touched (fnil conj #{}) :geometry-group)
{:ignore-touched true}))))
(defn- grid-cells-by-path
"The cells of the grid `shape` without their ids, with each child as its
path (see `add-unique-path`)."
[shape path-of]
(into #{}
(map (fn [cell] (-> cell (dissoc :id) (update :shapes #(mapv path-of %)))))
(vals (:layout-grid-cells shape))))
(defn- carry-grid-cells
"Carry the cell overrides of the switched grid `old` into `new`, its clean
copy of the new variant, when the old main `old-ref` and `new` have
identical grids: the same tracks and the same cells holding
corresponding children. Otherwise the new variant's cells win.
`counterpart` gives the child of `new` that relates to a child of `old`."
[changes old old-ref new old-path new-path counterpart]
(let [identical? (and (= (:layout-grid-rows old-ref) (:layout-grid-rows new))
(= (:layout-grid-columns old-ref) (:layout-grid-columns new))
(= (grid-cells-by-path old-ref old-path)
(grid-cells-by-path new new-path)))
cell-id (fn [{:keys [row column]}]
(some (fn [[id cell]] (when (and (= row (:row cell)) (= column (:column cell))) id))
(:layout-grid-cells new)))
cells (when identical?
(reduce (fn [cells cell]
(let [shapes (mapv counterpart (:shapes cell))
id (or (cell-id cell) (:id cell))]
(if (some nil? shapes)
(reduced nil)
(assoc cells id (assoc cell :id id :shapes shapes)))))
{}
(vals (:layout-grid-cells old))))]
(if cells
(pcb/update-shapes changes [(:id new)]
#(-> %
(assoc :layout-grid-cells cells)
(update :touched (fnil conj #{}) :layout-grid-cells))
{:ignore-touched true})
changes)))
(defn generate-keep-touched
"This is used as part of the switch process, when you switch from
an original-shape to a new-shape. It generate changes to
@ -466,6 +505,25 @@
(clsw/update-attrs-on-switch changes new old new-shape original-shape old-ref container)))
changes
pairs)
;; Cell overrides carry only between identical grids.
new-paths (into {} (map (juxt :id :shape-path)) new-shapes-w-path)
counterpart (fn [id]
(when-let [old-child (get page-objects id)]
(let [old-ref (find-shape-ref-child-of container libraries old-child
(:id orig-base-ref-shape))]
(:id (get new-shapes-map (get o-ref-shapes-p-map (:id old-ref)))))))
grid-pairs (filter (fn [{:keys [old new swapped?]}]
(and (not swapped?)
(ctsl/grid-layout? old)
(ctsl/grid-layout? new)
(ctk/touched-group? old :layout-grid-cells)))
pairs)
changes (reduce (fn [changes {:keys [old old-ref new]}]
(carry-grid-cells changes old old-ref new
o-ref-shapes-p-map new-paths counterpart))
changes
grid-pairs)
root-geometry? (some #(and (= (:id %) (:id original-shape))
(ctk/touched-group? % :geometry-group)) orig-touched)
parent (get page-objects (:parent-id original-shape))
@ -486,7 +544,7 @@
nil libraries (assoc container :objects (pcb/get-objects changes))
(:id new-shape) false))
changes (clsw/generate-sync-swap-constraints changes page libraries (:id new-shape))]
[changes (mapv (comp :parent-id :new) pairs)]))
[changes (into (mapv (comp :parent-id :new) pairs) (map (comp :id :new)) grid-pairs)]))
(defn- shape-index
"Position of `shape-id` among the children of `parent-id`; 0 when it is not

View File

@ -6,6 +6,7 @@
(ns common-tests.logic.variants-switch-test
(:require
[app.common.data :as d]
[app.common.files.changes-builder :as pcb]
[app.common.files.helpers :as cfh]
[app.common.geom.point :as gpt]
@ -26,6 +27,7 @@
[app.common.types.component :as ctk]
[app.common.types.modifiers :as ctm]
[app.common.types.path.segment :as segment]
[app.common.types.shape.layout :as ctl]
[app.common.types.text :as txt]
[clojure.set :as set]
[clojure.string :as string]
@ -3748,3 +3750,90 @@
(t/is (= #{:geometry-group} (:touched text)))
(t/is (= #{:geometry-group} (:touched (copy-child file' :copy02))))))
(defn- add-grid-member
"Add to the variant `variant-label` the member `component-label`: a grid
board `root-label` with two rows and `columns` columns, holding Item 1,
Item 2 and Item 3 at the `[row column]` of `cells`."
[file variant-label component-label root-label value columns cells]
(let [variant-id (thi/id variant-label)
labels (mapv #(keyword (str (name root-label) "-item" %)) [1 2 3])
file (reduce (fn [file [index label]]
(ths/add-sample-shape file label :type :rect :name (str "Item " index)
:parent-label root-label))
(ths/add-sample-shape file root-label :type :frame :parent-label variant-label
:variant-id variant-id :variant-name value
:layout :grid :layout-grid-dir :row
:layout-grid-rows [] :layout-grid-columns []
:layout-grid-cells {})
(map vector [1 2 3] labels))
frame (reduce (fn [frame _] (ctl/add-grid-column frame {:type :flex :value 1}))
(-> (ths/get-shape file root-label)
(ctl/add-grid-row {:type :flex :value 1})
(ctl/add-grid-row {:type :flex :value 1}))
(range columns))
cells (reduce (fn [cells [label [row column]]]
(let [[id _] (d/seek #(and (= row (:row (val %))) (= column (:column (val %)))) cells)]
(update cells id assoc :shapes [(thi/id label)] :position :manual)))
(:layout-grid-cells frame)
(map vector labels cells))]
(-> file
(ths/update-shape root-label :layout-grid-rows (:layout-grid-rows frame))
(ths/update-shape root-label :layout-grid-columns (:layout-grid-columns frame))
(ths/update-shape root-label :layout-grid-cells cells)
(thc/make-component component-label root-label)
(thc/update-component component-label {:variant-id variant-id
:variant-properties [{:name "Style" :value value}]}))))
(defn- item-cell
"The `[row column]` of the child named `item-name` of the grid `shape`."
[file shape item-name]
(let [objects (:objects (thf/current-page file))
item (d/seek #(= item-name (:name (get objects %))) (:shapes shape))
cell (ctl/get-cell-by-shape-id shape item)]
[(:row cell) (:column cell)]))
(defn- grid-variants-with-moved-copy
"Blue and Green are identical 2x2 grids; Wide has three columns and puts
Item 3 in the first row. The copy of Blue moves Item 3 to row 2,
column 2, a cell override."
[]
(let [file (-> (thf/sample-file :file1)
(ths/add-sample-shape :v01 :type :frame :is-variant-container true)
(add-grid-member :v01 :blue :blue-root "Blue" 2 [[1 1] [1 2] [2 1]])
(add-grid-member :v01 :green :green-root "Green" 2 [[1 1] [1 2] [2 1]])
(add-grid-member :v01 :wide :wide-root "Wide" 3 [[1 1] [1 2] [1 3]])
(thc/instantiate-component :blue :copy01))
page (thf/current-page file)
copy (ths/get-shape file :copy01)
item3 (d/seek #(= "Item 3" (:name (get (:objects page) %))) (:shapes copy))
moved (fn [cells]
(-> cells
(update-vals #(update % :shapes (fn [shapes] (filterv (partial not= item3) shapes))))
(as-> cells
(let [[id _] (d/seek #(and (= 2 (:row (val %))) (= 2 (:column (val %)))) cells)]
(update cells id assoc :shapes [item3] :position :manual)))))
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) #{(:id copy)}
#(update % :layout-grid-cells moved)
(:objects page) {})]
(thf/apply-changes file changes)))
(t/deftest a-switch-between-identical-grids-keeps-the-cell-override
(let [file (grid-variants-with-moved-copy)
copy (ths/get-shape file :copy01)
file' (tho/swap-component-in-shape file :copy01 :green {:new-shape-label :copy02 :keep-touched? true})
copy' (ths/get-shape file' :copy02)]
(t/is (= [2 2] (item-cell file copy "Item 3")))
(t/is (ctk/touched-group? copy :layout-grid-cells))
(t/is (= [2 2] (item-cell file' copy' "Item 3")))
(t/is (= [1 1] (item-cell file' copy' "Item 1")))
(t/is (= [1 2] (item-cell file' copy' "Item 2")))
(t/is (ctk/touched-group? copy' :layout-grid-cells))))
(t/deftest a-switch-to-a-different-grid-takes-the-new-variants-cells
(let [file (grid-variants-with-moved-copy)
file' (tho/swap-component-in-shape file :copy01 :wide {:new-shape-label :copy02 :keep-touched? true})
copy' (ths/get-shape file' :copy02)]
(t/is (= [1 3] (item-cell file' copy' "Item 3")))
(t/is (= [1 1] (item-cell file' copy' "Item 1")))
(t/is (not (ctk/touched-group? copy' :layout-grid-cells)))))