🐛 Leave hidden children out before deriving a layout from them

Adding a flex or grid layout derives its direction, gaps and paddings
from the board's children, leaving out the hidden ones, but it checked
for children before leaving them out. A board whose children were all
hidden then failed: flex reduced an empty list of points and grid read
the bounds of no shapes. Making a variant of a hidden main reaches it,
since the new variant board gets a flex layout around the main. Hidden
children are now left out first; with none left, the layout gets the
parameters of a board with no children.

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

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  20.267 |  0.58 |
| edit-through-a-chain-of-dependents |   68.931 |  39.871 |  0.58 |
| library-update-into-copies         |   83.083 |  45.985 |  0.55 |
| main-edit-among-many-shapes        |    4.837 |   1.943 |  0.40 |
| main-edit-into-10-copies           |    1.790 |   1.036 |  0.58 |
| main-edit-into-100-copies          |   15.415 |   9.998 |  0.65 |
| main-edit-into-1000-copies         |  153.086 |  97.836 |  0.64 |
| variant-switch-into-copies         |   32.052 |  19.658 |  0.61 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-08 03:51:04 +01:00
parent db512c2dc9
commit e0bbc50c32
5 changed files with 81 additions and 58 deletions

View File

@ -18,10 +18,9 @@
(calculate-params objects shapes nil))
([objects shapes parent]
(when (d/not-empty? shapes)
(let [shapes (->> shapes (remove :hidden))
points
;; hidden shapes take no part in the layout
(when-let [shapes (seq (remove :hidden shapes))]
(let [points
(->> shapes
(map :id)
(ctt/sort-z-index objects)

View File

@ -106,62 +106,66 @@
[params true]))]
params))
(defn- calculate-params*
"The grid parameters for the visible `shapes`."
[_objects shapes parent]
(if (empty? shapes)
(-> {:layout-grid-columns [ctl/default-track-value ctl/default-track-value]
:layout-grid-rows [ctl/default-track-value ctl/default-track-value]}
(ctl/create-cells [1 1 2 2]))
(let [all-shapes-rect (gco/shapes->rect shapes)
shapes+bounds
(->> shapes
(map #(vector % (grc/points->rect (get % :points)))))
shapes-by-x
(->> shapes+bounds
(sort-by (comp :x second)))
shapes-by-y
(->> shapes+bounds
(sort-by (comp :y second)))
cols (calculate-tracks :x shapes-by-x)
rows (calculate-tracks :y shapes-by-y)
num-cols (count cols)
num-rows (count rows)
total-cols-width (->> cols (reduce #(+ %1 (:size %2)) 0))
total-rows-height (->> rows (reduce #(+ %1 (:size %2)) 0))
column-gap
(if (= num-cols 1)
0
(/ (- (:width all-shapes-rect) total-cols-width) (dec num-cols)))
row-gap
(if (= num-rows 1)
0
(/ (- (:height all-shapes-rect) total-rows-height) (dec num-rows)))
layout-grid-rows (mapv (constantly ctl/default-track-value) rows)
layout-grid-columns (mapv (constantly ctl/default-track-value) cols)
parent-childs-vector (gpt/to-vec (gpo/origin (:points parent)) (gpt/point all-shapes-rect))
p-left (max 0 (:x parent-childs-vector))
p-top (max 0 (:y parent-childs-vector))]
(-> {:layout-grid-columns layout-grid-columns
:layout-grid-rows layout-grid-rows
:layout-gap {:row-gap row-gap
:column-gap column-gap}
:layout-padding {:p1 p-top :p2 p-left :p3 p-top :p4 p-left}
:layout-grid-dir (if (> num-cols num-rows) :row :column)}
(ctl/create-cells [1 1 num-cols num-rows])
(assign-shape-cells rows cols)))))
(defn calculate-params
"Given the shapes calculate its grid parameters (horizontal vs vertical, gaps, etc)"
([objects shapes]
(calculate-params objects shapes nil))
([_objects shapes parent]
(if (empty? shapes)
(-> {:layout-grid-columns [ctl/default-track-value ctl/default-track-value]
:layout-grid-rows [ctl/default-track-value ctl/default-track-value]}
(ctl/create-cells [1 1 2 2]))
(let [shapes (->> shapes (remove :hidden))
all-shapes-rect (gco/shapes->rect shapes)
shapes+bounds
(->> shapes
(map #(vector % (grc/points->rect (get % :points)))))
shapes-by-x
(->> shapes+bounds
(sort-by (comp :x second)))
shapes-by-y
(->> shapes+bounds
(sort-by (comp :y second)))
cols (calculate-tracks :x shapes-by-x)
rows (calculate-tracks :y shapes-by-y)
num-cols (count cols)
num-rows (count rows)
total-cols-width (->> cols (reduce #(+ %1 (:size %2)) 0))
total-rows-height (->> rows (reduce #(+ %1 (:size %2)) 0))
column-gap
(if (= num-cols 1)
0
(/ (- (:width all-shapes-rect) total-cols-width) (dec num-cols)))
row-gap
(if (= num-rows 1)
0
(/ (- (:height all-shapes-rect) total-rows-height) (dec num-rows)))
layout-grid-rows (mapv (constantly ctl/default-track-value) rows)
layout-grid-columns (mapv (constantly ctl/default-track-value) cols)
parent-childs-vector (gpt/to-vec (gpo/origin (:points parent)) (gpt/point all-shapes-rect))
p-left (max 0 (:x parent-childs-vector))
p-top (max 0 (:y parent-childs-vector))]
(-> {:layout-grid-columns layout-grid-columns
:layout-grid-rows layout-grid-rows
:layout-gap {:row-gap row-gap
:column-gap column-gap}
:layout-padding {:p1 p-top :p2 p-left :p3 p-top :p4 p-left}
:layout-grid-dir (if (> num-cols num-rows) :row :column)}
(ctl/create-cells [1 1 num-cols num-rows])
(assign-shape-cells rows cols))))))
([objects shapes parent]
(calculate-params* objects (remove :hidden shapes) parent)))

View File

@ -200,3 +200,8 @@
(t/is (= [[{:min-size 10 :max-size 10 :size 10}] 5]
(fld/add-space-to-items :size :min-size :max-size 5
[{:min-size 10 :max-size 10 :size 10}]))))
(t/deftest calculate-params-of-hidden-children-only-is-nil
;; hidden children take no part, so a board of only hidden ones has
;; nothing to derive the layout from, as a board with none
(t/is (nil? (params-for-child {:x 10 :y 20 :width 50 :height 50 :hidden true}))))

View File

@ -443,3 +443,12 @@
(t/deftest grid-calculate-params-clamps-overflowing-child-padding
(let [params (grid-params-for-children {:x -10 :y -5 :width 150 :height 150})]
(t/is (= {:p1 0 :p2 0 :p3 0 :p4 0} (:layout-padding params)))))
(t/deftest grid-calculate-params-of-hidden-children-only-is-the-empty-grid
;; hidden children take no part, as in a board with none
(let [empty (glparams/calculate-params nil [] nil)
params (grid-params-for-children {:x 10 :y 20 :width 30 :height 30 :hidden true})
shape (fn [params] (-> params
(update :layout-grid-cells #(map (fn [cell] (dissoc cell :id)) (vals %)))
(update :layout-grid-cells (partial sort-by (juxt :row :column)))))]
(t/is (= (shape empty) (shape params)))))

View File

@ -1764,6 +1764,12 @@
: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 making-a-variant-of-a-hidden-main-lays-out-its-board
(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 [[:style 0 1 1] [:make-variant 0 0 0 0]]}))))
(t/deftest random-variant-compositions
(let [coverage (atom {})
failure (atom nil)]