🐛 Center a grid child vertically with half its margins

The CLJS grid centered a child horizontally with half its margins, as
CSS and the flex layout do, but vertically it shifted the child by the
whole top and bottom margins. Both axes now center the margin box.

Found by laying out random grid boards in a WASM workspace and reflowing
the same objects with the CLJS engine.

Benchmark (lower quantile, ms):

| world                              | baseline |     now |    mean | ratio |
|------------------------------------+----------+---------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  21.021 |  21.658 |  0.60 |
| edit-through-a-chain-of-dependents |   68.931 |  41.085 |  42.996 |  0.60 |
| library-update-into-copies         |   83.083 |  51.898 |  52.585 |  0.62 |
| main-edit-among-many-shapes        |    4.837 |   2.255 |   2.395 |  0.47 |
| main-edit-into-10-copies           |    1.79  |   1.031 |   1.069 |  0.58 |
| main-edit-into-100-copies          |   15.415 |   9.877 |  10.192 |  0.64 |
| main-edit-into-1000-copies         |  153.086 | 100.151 | 105.021 |  0.65 |
| variant-switch-into-copies         |   32.052 |  22.340 |  23.371 |  0.70 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-13 21:17:47 +01:00
parent 05df95d042
commit 4b5b391c14
2 changed files with 34 additions and 3 deletions

View File

@ -185,8 +185,8 @@
:center
[(gpt/add origin-v (vv (/ child-height 2)))
(-> (gpo/project-point cell-bounds :v (gpo/center cell-bounds))
(gpt/add (vv top-m))
(gpt/subtract (vv bottom-m)))]
(gpt/add (vv (/ top-m 2)))
(gpt/subtract (vv (/ bottom-m 2))))]
[origin-v
(gpt/add (first cell-bounds) (vv top-m))])]

View File

@ -8,12 +8,15 @@
(:require
;; Requiring modifiers triggers the side-effect that wires
;; -child-min-width / -child-min-height into grid layout-data.
[app.common.geom.modifiers]
[app.common.geom.modifiers :as gm]
[app.common.geom.rect :as grc]
[app.common.geom.shapes :as gsh]
[app.common.geom.shapes.grid-layout.layout-data :as gld]
[app.common.geom.shapes.grid-layout.params :as glparams]
[app.common.math :as mth]
[app.common.types.modifiers :as ctm]
[app.common.types.shape :as cts]
[app.common.uuid :as uuid]
[clojure.test :as t]))
;; ---------------------------------------------------------------------------
@ -458,3 +461,31 @@
(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)))))
(t/deftest grid-centers-a-child-with-half-its-margins
;; a child centered in its cell centers its margin box, as CSS does
(let [child-id (uuid/next)
frame (make-grid-frame :layout-align-items :center :layout-justify-items :center
:layout-grid-columns [{:type :fixed :value 100.0}]
:layout-grid-rows [{:type :fixed :value 30.0}]
:width 100 :height 30)
cell-id (uuid/next)
frame (assoc frame
:shapes [child-id]
:layout-grid-cells {cell-id {:id cell-id :row 1 :column 1 :row-span 1 :column-span 1
:position :manual :align-self :auto :justify-self :auto
:shapes [child-id]}})
child (cts/setup-shape {:id child-id :type :rect :x 0 :y 0 :width 40 :height 56
:parent-id (:id frame) :frame-id (:id frame)
:layout-item-margin-type :multiple
:layout-item-margin {:m1 5 :m2 3 :m3 0 :m4 7}})
objects {uuid/zero (cts/setup-shape {:id uuid/zero :type :frame :x 0 :y 0 :width 1000 :height 1000
:shapes [(:id frame)]})
(:id frame) (assoc frame :parent-id uuid/zero :frame-id uuid/zero)
child-id child}
tree (gm/set-objects-modifiers {(:id frame) {:modifiers (ctm/reflow-modifiers)}} objects)
moved (gsh/transform-shape child (get-in tree [child-id :modifiers]))
corner (first (:points moved))]
;; (100 - 40 + 7 - 3) / 2 and (30 - 56 + 5 - 0) / 2
(t/is (mth/close? 32.0 (:x corner) 0.01))
(t/is (mth/close? -10.5 (:y corner) 0.01))))