penpot/common/test/common_tests/geom_modifiers_test.cljc
alonso.torres e10ce2dba9 🐛 Turn and skew layout children with their layout board
A turned board that fills its flex parent skews, and so does a grid
board it holds. In the layout pass a layout board handed its modifiers
only to its absolute children, so the layout moved the others without
the board's turn or skew: they stayed rigid, while the WASM layout and a
component sync skew them. The layout pass now hands a layout board's
modifiers to all its children, which keep their size along its axes,
and the layout places them from the transformed bounds.

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

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  20.425 |  0.58 |
| edit-through-a-chain-of-dependents |   68.931 |  41.077 |  0.60 |
| library-update-into-copies         |   83.083 |  47.075 |  0.57 |
| main-edit-among-many-shapes        |    4.837 |   2.276 |  0.47 |
| main-edit-into-10-copies           |     1.79 |   0.998 |  0.56 |
| main-edit-into-100-copies          |   15.415 |  11.243 |  0.73 |
| main-edit-into-1000-copies         |  153.086 | 109.801 |  0.72 |
| variant-switch-into-copies         |   32.052 |  20.218 |  0.63 |

AI-assisted-by: claude-opus-5-5
2026-11-18 10:29:45 +01:00

229 lines
9.7 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns common-tests.geom-modifiers-test
(:require
[app.common.geom.modifiers :as gm]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.geom.shapes.transforms :as gtr]
[app.common.test-helpers.compositions :as tho]
[app.common.test-helpers.files :as thf]
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as ths]
[app.common.types.modifiers :as ctm]
[clojure.test :as t]))
(t/use-fixtures :each thi/test-fixture)
;; ---- Helpers
(defn- add-flex-frame
"Create a flex layout frame"
[file frame-label & {:keys [width height] :as params}]
(ths/add-sample-shape file frame-label
(merge {:type :frame
:name "FlexFrame"
:layout-flex-dir :row
:width (or width 200)
:height (or height 200)}
params)))
(defn- add-rect-child
"Create a rectangle child inside a parent"
[file rect-label parent-label & {:keys [width height x y] :as params}]
(ths/add-sample-shape file rect-label
(merge {:type :rect
:name "Rect"
:parent-label parent-label
:width (or width 50)
:height (or height 50)
:x (or x 0)
:y (or y 0)}
params)))
(defn- add-ghost-child-id
"Add a non-existent child ID to a frame's shapes list.
This simulates data inconsistency where a child ID is referenced
but the child shape doesn't exist in objects."
[file frame-label ghost-id]
(let [page (thf/current-page file)
frame-id (thi/id frame-label)]
(update file :data
(fn [file-data]
(update-in file-data [:pages-index (:id page) :objects frame-id :shapes]
conj ghost-id)))))
;; ---- Tests
(t/deftest flex-layout-with-normal-children
(t/testing "set-objects-modifiers processes flex layout children correctly"
(let [file (-> (thf/sample-file :file1)
(add-flex-frame :frame1)
(add-rect-child :rect1 :frame1))
page (thf/current-page file)
objects (:objects page)
rect-id (thi/id :rect1)
;; Create a move modifier for the rectangle
modif-tree {rect-id {:modifiers (ctm/move-modifiers (gpt/point 10 20))}}
;; This should not crash
result (gm/set-objects-modifiers modif-tree objects)]
(t/is (some? result))
;; The rectangle should have modifiers
(t/is (contains? result rect-id)))))
(t/deftest flex-layout-with-nonexistent-child
(t/testing "set-objects-modifiers handles flex frame with non-existent child in shapes"
(let [ghost-id (thi/next-uuid)
file (-> (thf/sample-file :file1)
(add-flex-frame :frame1)
(add-rect-child :rect1 :frame1)
;; Add a non-existent child ID to the frame's shapes
(add-ghost-child-id :frame1 ghost-id))
page (thf/current-page file)
objects (:objects page)
rect-id (thi/id :rect1)
;; Create a move modifier for the existing rectangle
modif-tree {rect-id {:modifiers (ctm/move-modifiers (gpt/point 10 20))}}
;; This should NOT crash even though the flex frame has
;; a child ID (ghost-id) that doesn't exist in objects
result (gm/set-objects-modifiers modif-tree objects)]
(t/is (some? result))
(t/is (contains? result rect-id)))))
(t/deftest flex-layout-with-all-ghost-children
(t/testing "set-objects-modifiers handles flex frame with only non-existent children"
(let [ghost1 (thi/next-uuid)
ghost2 (thi/next-uuid)
file (-> (thf/sample-file :file1)
(add-flex-frame :frame1)
;; Add only non-existent children to the frame's shapes
(add-ghost-child-id :frame1 ghost1)
(add-ghost-child-id :frame1 ghost2))
page (thf/current-page file)
objects (:objects page)
frame-id (thi/id :frame1)
;; Create a move modifier for the frame itself
modif-tree {frame-id {:modifiers (ctm/move-modifiers (gpt/point 5 5))}}
;; Should not crash even though the flex frame has
;; no existing children in its shapes list
result (gm/set-objects-modifiers modif-tree objects)]
(t/is (some? result)))))
(t/deftest grid-layout-with-nonexistent-child
(t/testing "set-objects-modifiers handles grid frame with non-existent child in shapes"
(let [ghost-id (thi/next-uuid)
file (-> (thf/sample-file :file1)
(ths/add-sample-shape :frame1
{:type :frame
:name "GridFrame"
:layout-grid-dir :row
:width 200
:height 200})
(add-rect-child :rect1 :frame1)
(add-ghost-child-id :frame1 ghost-id))
page (thf/current-page file)
objects (:objects page)
rect-id (thi/id :rect1)
modif-tree {rect-id {:modifiers (ctm/move-modifiers (gpt/point 10 20))}}
;; Should not crash for grid layout with ghost child
result (gm/set-objects-modifiers modif-tree objects)]
(t/is (some? result))
(t/is (contains? result rect-id)))))
(t/deftest flex-layout-resize-with-nonexistent-child
(t/testing "resize modifier propagation handles non-existent children"
(let [ghost-id (thi/next-uuid)
file (-> (thf/sample-file :file1)
(add-flex-frame :frame1)
(add-rect-child :rect1 :frame1)
(add-ghost-child-id :frame1 ghost-id))
page (thf/current-page file)
objects (:objects page)
frame-id (thi/id :frame1)
;; Create a resize modifier for the frame itself
modif-tree {frame-id {:modifiers (ctm/resize-modifiers
(gpt/point 2 2)
(gpt/point 0 0))}}
;; Should not crash when propagating resize through flex layout
;; that has ghost children
result (gm/set-objects-modifiers modif-tree objects)]
(t/is (some? result))
;; The frame should have modifiers
(t/is (contains? result frame-id)))))
(t/deftest nested-flex-layout-with-nonexistent-child
(t/testing "nested flex layout handles non-existent children in outer frame"
(let [ghost-id (thi/next-uuid)
file (-> (thf/sample-file :file1)
(add-flex-frame :outer-frame)
(add-flex-frame :inner-frame :parent-label :outer-frame)
(add-rect-child :rect1 :inner-frame)
(add-ghost-child-id :outer-frame ghost-id))
page (thf/current-page file)
objects (:objects page)
rect-id (thi/id :rect1)
modif-tree {rect-id {:modifiers (ctm/move-modifiers (gpt/point 5 10))}}
result (gm/set-objects-modifiers modif-tree objects)]
(t/is (some? result))
(t/is (contains? result rect-id)))))
(defn- reflow
[objects ids]
(->> objects
(gm/set-objects-modifiers (into {} (map #(vector % {:modifiers (ctm/reflow-modifiers)})) ids))
(gtr/apply-objects-modifiers objects)))
(defn- edges
[{:keys [points]}]
(let [[p0 p1 _ p3] points]
[(gpt/to-vec p0 p1) (gpt/to-vec p0 p3)]))
(t/deftest grid-child-skews-with-its-skewed-grid
(t/testing "a grid board skewed by its turned parent filling a flex skews its children too"
(let [file (-> (thf/sample-file :file1)
(tho/add-frame :outer :width 340 :height 200 :layout :flex :layout-flex-dir :row
:layout-align-items :center :layout-justify-content :center
:layout-padding-type :simple :layout-padding {:p1 30 :p2 30 :p3 30 :p4 30})
(tho/add-frame :card :parent-label :outer :x 120 :y 50 :width 100 :height 100)
(tho/add-grid-frame :inner :parent-label :card :x 140 :y 70 :width 60 :height 60)
(add-rect-child :cell :inner :x 140 :y 70 :width 20 :height 20)
(tho/place-in-grid-columns :inner 1 {1 :cell}))
ids [(thi/id :outer) (thi/id :inner)]
objects (reflow (:objects (thf/current-page file)) ids)
card (get objects (thi/id :card))
objects (->> objects
(gm/set-objects-modifiers
{(:id card) {:modifiers (ctm/rotation-modifiers card (gsh/shape->center card) 45)}})
(gtr/apply-objects-modifiers objects))
objects (reflow (assoc-in objects [(:id card) :layout-item-h-sizing] :fill) ids)
inner (get objects (thi/id :inner))
cell (get objects (thi/id :cell))
[inner-h inner-v] (edges inner)
[cell-h cell-v] (edges cell)]
(t/is (gpt/close? (gpt/scale inner-h (/ 1 3)) cell-h))
(t/is (gpt/close? (gpt/scale inner-v (/ 1 3)) cell-v))
(t/is (gpt/close? (first (:points inner)) (first (:points cell)))))))