📎 Size the generated variant containers like the app's boards

The generator built its variant containers at 0.01 by 0.01. Adding a
variant grows the container by a member's width, as the app does, and
a member constrained to scale grew with it by about twelve thousand
times, until its coordinates left the valid range. Both generated
worlds now size the container to 200 by 200, room for its members.

Harness only; the benchmark is not run.

Also in this commit:

- Keep the model of shapes an undo stores in a deleted component. Undoing an
  operation that created a component deletes it, and the deleted component keeps
  its shapes with their overrides. The model went back to its state before the
  operation, which never knew those shapes, so restoring the component expected
  none of their overrides. An undo now keeps the model of the shapes it leaves
  only in a deleted component.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-09 03:45:24 +01:00
parent 9a52566167
commit 04b78e706f
3 changed files with 45 additions and 1 deletions

View File

@ -181,6 +181,8 @@
[["a" setup/labeled-fill] ["b" "#ff0000"]]
:size 100) s)
s))
(as-> s (if (:library-variants opts)
(variants/size-container s "library-variants" 200 200) s))
(as-> s (if (and (:library-variants opts) (:rich-variants opts))
(variants/diversify s "library-variants") s))
tm/file
@ -247,6 +249,19 @@
(into {} (map (juxt (comp :id :shape) :shape)) (forest situation))
(mapcat #(vals (get-in % [:data :components])) (vals (cf/all-files situation)))))
(defn- keep-stored-entries
"The model `before` an undo, with the entries of `model` for the shapes the
undo takes off the pages of `situation` into a deleted component, which
keeps them for a restore to bring back."
[before model situation]
(let [live (into #{} (map (comp :id :shape)) (forest situation))
stored (all-objects situation)]
(reduce-kv (fn [result id groups]
(cond-> result
(and (contains? stored id) (not (contains? live id)) (not (contains? before id)))
(assoc id groups)))
before model)))
(defn- settle-model
[model situation synced?]
(let [references (when (some :layout-grid-cells (vals model))
@ -290,6 +305,7 @@
(variants/members setup/labeled-fill (:rich-variants opts)
(:variant-diff opts))
:size 100)])
s (variants/size-container s "random-variants" 200 200)
s (cond-> s (:rich-variants opts) (variants/diversify "random-variants"))]
(reduce #(tm/apply-to %2 %1) s
[(n/make-nested-component-with-variant "variant" "random-variants" "a")
@ -1052,7 +1068,7 @@
history (:undo state)
redo (:redo state)
next-model (case kind
:undo (:before (peek history))
:undo (keep-stored-entries (:before (peek history)) model after)
:redo (:after (peek redo))
(update-model model situation after resolved kind))
next-model (cond-> (or next-model model)

View File

@ -68,6 +68,19 @@
:type :rect :name "Badge" :width 12 :height 12 :parent-label root-label))))
(tm/file situation) (map-indexed vector members))))))
(defn size-container
"Size the container of set `set-name` to `width` by `height`, as the
boards the app builds around its members."
[situation set-name width height]
(let [container-id (thi/id (:container-label (n/variant-set situation set-name)))
file (tm/file situation)
page (thf/current-page file)
changes (cls/generate-update-shapes
(pcb/empty-changes nil (:id page)) [container-id]
#(gsh/transform-shape % (ctm/change-size % width height))
(:objects page) {})]
(tm/with-file situation (thf/apply-changes file changes))))
(defn nearest
"Prefer matches in earlier unchanged properties, then container order.
This predicts the contract without calling the production distance or

View File

@ -1864,6 +1864,21 @@
:ops [[:edit 3 0 0 1] [:variant-property 0 6 0 0] [:switch 0] [:make-variant 5]
[:switch 13] [:make-variant 13 0 0 0] [:add-variant 0] [:switch 10]]}))))
(t/deftest adding-variants-beside-a-scaled-member-keeps-the-file-valid
(t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :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] [:restore 0] [:style 1 8 1] [:add-variant 0] [:switch 0]
[:add-variant 0 0 0 0]]}))))
(t/deftest restoring-a-variant-an-undo-removed-keeps-its-swap
(t/is (map? (cg/run {:world [3 2 {:variants true :text-styles true :layout nil :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 [[:make-variant 10 0 0 0] [:make-variant 0] [:switch 0] [:combine 4] [:switch 12]
[:combine 15] [:switch 19] [:combine 0 0 0 0] [:make-variant 14 0 0 0]
[:undo 0 0 0 0] [:restore 0 0 0 0]]}))))
(t/deftest random-variant-compositions
(let [coverage (atom {})
failure (atom nil)]