mirror of
https://github.com/penpot/penpot.git
synced 2026-11-21 17:50:38 +00:00
The content bounds of a grid or flex layout, which size a fill child that is itself a layout and a layout that hugs, read the four stored paddings. A simple padding type repeats the top and right values on the bottom and left, so a layout whose sides were edited one by one before its type was set to simple measured its content with the stale left and bottom values: a fill grid with paddings 3, 6, 9 and 12 came out 6 px wider than its content. Both use `ctl/paddings`, as the layouts themselves do. Test: `geom-grid-layout-test/grid-content-bounds-take-simple-paddings` (fails before the fix). Benchmark (bench-components, alone): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 22.925 | 23.239 | 0.65 | | edit-through-a-chain-of-dependents | 68.931 | 45.225 | 46.994 | 0.66 | | library-update-into-copies | 83.083 | 54.842 | 57.554 | 0.66 | | main-edit-among-many-shapes | 4.837 | 2.528 | 2.705 | 0.52 | | main-edit-into-10-copies | 1.79 | 1.136 | 1.163 | 0.63 | | main-edit-into-100-copies | 15.415 | 10.746 | 11.596 | 0.7 | | main-edit-into-1000-copies | 153.086 | 109.63 | 113.323 | 0.72 | | variant-switch-into-copies | 32.052 | 23.103 | 24.367 | 0.72 | AI-assisted-by: claude-opus-5-5
526 lines
27 KiB
Clojure
526 lines
27 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-grid-layout-test
|
||
(:require
|
||
;; Requiring modifiers triggers the side-effect that wires
|
||
;; -child-min-width / -child-min-height into grid layout-data.
|
||
[app.common.geom.modifiers :as gm]
|
||
[app.common.geom.rect :as grc]
|
||
[app.common.geom.shapes :as gsh]
|
||
[app.common.geom.shapes.grid-layout.bounds :as glb]
|
||
[app.common.geom.shapes.grid-layout.layout-data :as gld]
|
||
[app.common.geom.shapes.grid-layout.params :as glparams]
|
||
[app.common.geom.shapes.points :as gpo]
|
||
[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]))
|
||
|
||
;; ---------------------------------------------------------------------------
|
||
;; Shared test-data builders
|
||
;; ---------------------------------------------------------------------------
|
||
|
||
(defn- make-grid-frame
|
||
"Minimal grid-layout frame with two fixed columns of 50.0 px
|
||
and one fixed row. Width and height are explicit, no padding.
|
||
Track values are floats to avoid JVM integer-divide-by-zero when
|
||
there are no flex tracks (column-frs = 0)."
|
||
[& {:as opts}]
|
||
(cts/setup-shape
|
||
(merge {:type :frame
|
||
:layout :grid
|
||
:layout-grid-dir :row
|
||
:layout-grid-columns [{:type :fixed :value 50.0}
|
||
{:type :fixed :value 50.0}]
|
||
:layout-grid-rows [{:type :fixed :value 100.0}]
|
||
:layout-grid-cells {}
|
||
:layout-padding-type :multiple
|
||
:layout-padding {:p1 0 :p2 0 :p3 0 :p4 0}
|
||
:layout-gap {:column-gap 0 :row-gap 0}
|
||
:x 0 :y 0 :width 200 :height 100}
|
||
opts)))
|
||
|
||
(defn- bounds-for
|
||
"Return the 4-point layout-bounds for the frame."
|
||
[frame]
|
||
(grc/rect->points (grc/make-rect (:x frame) (:y frame) (:width frame) (:height frame))))
|
||
|
||
;; Build a simple non-fill child shape with explicit width/height.
|
||
;; No layout-item-margin → child-width-margin = 0.
|
||
(defn- make-child
|
||
[w h]
|
||
(cts/setup-shape {:type :rect :width w :height h :x 0 :y 0}))
|
||
|
||
;; Build the 4-point bounds vector for a child with the given dimensions.
|
||
(defn- child-bounds
|
||
[w h]
|
||
(grc/rect->points (grc/make-rect 0 0 w h)))
|
||
|
||
;; Build an auto track at its initial size (0.01) with infinite max.
|
||
(defn- auto-track [] {:type :auto :size 0.01 :max-size ##Inf})
|
||
|
||
;; Build a fixed track with the given size.
|
||
(defn- fixed-track [v]
|
||
{:type :fixed :value v :size (double v) :max-size (double v)})
|
||
|
||
;; Build a flex track (value = number of fr units) at initial size 0.01.
|
||
(defn- flex-track [fr]
|
||
{:type :flex :value fr :size 0.01 :max-size ##Inf})
|
||
|
||
;; Build a parent frame for column testing with given column-gap.
|
||
(defn- auto-col-parent
|
||
([] (auto-col-parent 0))
|
||
([column-gap]
|
||
(cts/setup-shape
|
||
{:type :frame
|
||
:layout :grid
|
||
:layout-grid-dir :row
|
||
:layout-padding-type :multiple
|
||
:layout-padding {:p1 0 :p2 0 :p3 0 :p4 0}
|
||
:layout-gap {:column-gap column-gap :row-gap 0}
|
||
:x 0 :y 0 :width 500 :height 500})))
|
||
|
||
;; Build a parent frame for row type testing with given row-gap.
|
||
(defn- auto-row-parent
|
||
([] (auto-row-parent 0))
|
||
([row-gap]
|
||
(cts/setup-shape
|
||
{:type :frame
|
||
:layout :grid
|
||
:layout-grid-dir :row
|
||
:layout-padding-type :multiple
|
||
:layout-padding {:p1 0 :p2 0 :p3 0 :p4 0}
|
||
:layout-gap {:column-gap 0 :row-gap row-gap}
|
||
:x 0 :y 0 :width 500 :height 500})))
|
||
|
||
;; Generic frame-bounds (large enough not to interfere).
|
||
(def ^:private frame-bounds
|
||
(grc/rect->points (grc/make-rect 0 0 500 500)))
|
||
|
||
;; Build a cell map for a single shape occupying column/row at given span.
|
||
;; col and row are 1-based.
|
||
(defn- make-cell
|
||
[shape-id col row col-span row-span]
|
||
{:shapes [shape-id]
|
||
:column col :column-span col-span
|
||
:row row :row-span row-span})
|
||
|
||
;; ---------------------------------------------------------------------------
|
||
;; Note on set-auto-multi-span indexing
|
||
;; ---------------------------------------------------------------------------
|
||
;;
|
||
;; Inside set-auto-multi-span, indexed-tracks is computed as:
|
||
;; from-idx = clamp(col - 1, 0, count-1)
|
||
;; to-idx = clamp((col - 1) + col-span, 0, count-1)
|
||
;; indexed-tracks = subvec(enumerate(tracks), from-idx, to-idx)
|
||
;;
|
||
;; Because to-idx is clamped to (dec count), the LAST track of the span is
|
||
;; always excluded unless there is at least one extra track beyond the span.
|
||
;;
|
||
;; Practical implication for tests: to cover N spanned tracks, provide a
|
||
;; track-list with at least N+1 tracks (the extra track acts as a sentinel
|
||
;; that absorbs the off-by-one from the clamp).
|
||
;;
|
||
;; Example: col=1, span=2, 3 total tracks:
|
||
;; to-idx = clamp(0+2, 0, 2) = 2 → subvec(v, 0, 2) = [track0, track1] ✓
|
||
;;
|
||
;; Tests that deliberately check boundary behavior (flex exclusion,
|
||
;; non-spanned tracks) use 2 total tracks so only track 0 is covered.
|
||
|
||
;; ---------------------------------------------------------------------------
|
||
;; Tests: column-gap with justify-content (case → cond fix)
|
||
;; ---------------------------------------------------------------------------
|
||
;;
|
||
;; In get-cell-data, column-gap and row-gap were computed with (case ...)
|
||
;; using boolean locals as dispatch values. case compares compile-time
|
||
;; constants, so those branches never matched at runtime. Fixed with cond.
|
||
|
||
(t/deftest grid-column-gap-space-evenly
|
||
(t/testing "justify-content :space-evenly increases column-gap correctly"
|
||
;; 2 fixed cols × 50 px = 100 px occupied; bound-width = 200; free = 100
|
||
;; formula: free / (num-cols + 1) = 100/3 ≈ 33.33
|
||
(let [frame (make-grid-frame :layout-justify-content :space-evenly
|
||
:layout-gap {:column-gap 0 :row-gap 0})
|
||
bounds (bounds-for frame)
|
||
result (gld/calc-layout-data frame bounds [] {} {})
|
||
col-gap (:column-gap result)]
|
||
(t/is (mth/close? (/ 100.0 3.0) col-gap 0.01)))))
|
||
|
||
(t/deftest grid-column-gap-space-around
|
||
(t/testing "justify-content :space-around increases column-gap correctly"
|
||
;; free = 100; formula: 100 / num-cols = 100/2 = 50
|
||
(let [frame (make-grid-frame :layout-justify-content :space-around
|
||
:layout-gap {:column-gap 0 :row-gap 0})
|
||
bounds (bounds-for frame)
|
||
result (gld/calc-layout-data frame bounds [] {} {})
|
||
col-gap (:column-gap result)]
|
||
(t/is (mth/close? 50.0 col-gap 0.01)))))
|
||
|
||
(t/deftest grid-gap-space-around-without-tracks-keeps-the-gap
|
||
(let [frame (make-grid-frame :layout-justify-content :space-around
|
||
:layout-align-content :space-around
|
||
:layout-grid-columns []
|
||
:layout-grid-rows []
|
||
:layout-gap {:column-gap 5 :row-gap 7})
|
||
result (gld/calc-layout-data frame (bounds-for frame) [] {} {})]
|
||
(t/is (mth/close? 5 (:column-gap result)))
|
||
(t/is (mth/close? 7 (:row-gap result)))))
|
||
|
||
(t/deftest grid-layout-data-takes-an-auto-track-without-a-value
|
||
;; the track editor stores an auto track with no value
|
||
(let [frame (make-grid-frame :layout-grid-rows [{:type :auto :value nil}])
|
||
result (gld/calc-layout-data frame (bounds-for frame) [] {} {})]
|
||
(t/is (= 1 (count (:row-tracks result))))))
|
||
|
||
(t/deftest grid-content-bounds-take-simple-paddings
|
||
;; a simple padding type repeats the top and right values on the bottom
|
||
;; and left: two 50 px columns and 6 px on each side
|
||
(let [frame (make-grid-frame :layout-padding-type :simple
|
||
:layout-padding {:p1 3 :p2 6 :p3 9 :p4 12})
|
||
bounds {(:id frame) (delay (bounds-for frame))}
|
||
data (gld/calc-layout-data frame (bounds-for frame) [] bounds {})
|
||
content (glb/layout-content-bounds bounds frame data)]
|
||
(t/is (mth/close? 112 (gpo/width-points content) 0.01))))
|
||
|
||
(t/deftest grid-column-gap-space-between
|
||
(t/testing "justify-content :space-between increases column-gap correctly"
|
||
;; free = 100; num-cols = 2; formula: 100 / (2-1) = 100
|
||
(let [frame (make-grid-frame :layout-justify-content :space-between
|
||
:layout-gap {:column-gap 0 :row-gap 0})
|
||
bounds (bounds-for frame)
|
||
result (gld/calc-layout-data frame bounds [] {} {})
|
||
col-gap (:column-gap result)]
|
||
(t/is (mth/close? 100.0 col-gap 0.01)))))
|
||
|
||
(t/deftest grid-column-gap-auto-width-bypasses-justify-content
|
||
(t/testing "auto-width? bypasses justify-content gap recalc → gap stays as initial"
|
||
(let [frame (make-grid-frame :layout-justify-content :space-evenly
|
||
:layout-gap {:column-gap 5 :row-gap 0}
|
||
:layout-item-h-sizing :auto)
|
||
bounds (bounds-for frame)
|
||
result (gld/calc-layout-data frame bounds [] {} {})
|
||
col-gap (:column-gap result)]
|
||
(t/is (mth/close? 5.0 col-gap 0.01)))))
|
||
|
||
;; ---------------------------------------------------------------------------
|
||
;; Tests: set-auto-multi-span
|
||
;; ---------------------------------------------------------------------------
|
||
;;
|
||
;; set-auto-multi-span grows auto tracks to accommodate children whose cell
|
||
;; spans more than one track column (or row), but only for spans that contain
|
||
;; no flex tracks (those are handled by set-flex-multi-span).
|
||
;;
|
||
;; The function signature:
|
||
;; (set-auto-multi-span parent track-list children-map shape-cells
|
||
;; bounds objects type)
|
||
;; type – :column or :row
|
||
;; children-map – {shape-id [child-bounds child-shape]}
|
||
;; shape-cells – {cell-id cell-map}
|
||
|
||
(t/deftest set-auto-multi-span-span-1-cells-ignored
|
||
(t/testing "span=1 cells are filtered out; track-list is unchanged"
|
||
(let [sid (random-uuid)
|
||
child (make-child 200 100)
|
||
;; 2 tracks + 1 sentinel (so the span would cover tracks 0-1 if span were 2)
|
||
tracks [(auto-track) (auto-track) (auto-track)]
|
||
cells {:c1 (make-cell sid 1 1 1 1)} ; span = 1 → ignored
|
||
cmap {sid [(child-bounds 200 100) child]}
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks cmap cells frame-bounds {} :column)]
|
||
(t/is (mth/close? 0.01 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 1)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 2)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-empty-cells
|
||
(t/testing "empty shape-cells → track-list unchanged"
|
||
(let [tracks [(auto-track) (auto-track)]
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks {} {} frame-bounds {} :column)]
|
||
(t/is (mth/close? 0.01 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 1)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-two-auto-tracks-split-evenly
|
||
(t/testing "child spanning 2 auto tracks (with sentinel): budget split between the 2 covered tracks"
|
||
;; 3 tracks total (sentinel at index 2 keeps to-idx from being clamped).
|
||
;; col=1, span=2:
|
||
;; from-idx = clamp(0, 0, 2) = 0
|
||
;; to-idx = clamp(2, 0, 2) = 2
|
||
;; subvec(enumerate, 0, 2) = [[0, auto0], [1, auto1]]
|
||
;; size-to-allocate = 200 (child width, no gap)
|
||
;; allocate-auto-tracks pass 1 (non-assigned = both):
|
||
;; idx0: max(0.01, 200/2, 0.01) = 100; rem = 100
|
||
;; idx1: max(0.01, 100/1, 0.01) = 100; rem = 0
|
||
;; pass 2 (to-allocate=0): no change → both 100
|
||
;; sentinel track 2 is never spanned → stays at 0.01.
|
||
(let [sid (random-uuid)
|
||
child (make-child 200 100)
|
||
tracks [(auto-track) (auto-track) (auto-track)] ; sentinel at [2]
|
||
cells {:c1 (make-cell sid 1 1 2 1)}
|
||
cmap {sid [(child-bounds 200 100) child]}
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks cmap cells frame-bounds {} :column)]
|
||
(t/is (mth/close? 100.0 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 100.0 (:size (nth result 1)) 0.001))
|
||
;; sentinel unaffected
|
||
(t/is (mth/close? 0.01 (:size (nth result 2)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-gap-deducted-from-budget
|
||
(t/testing "column-gap is subtracted once per extra span track from size-to-allocate"
|
||
;; child width = 210, column-gap = 10, span = 2
|
||
;; size-to-allocate = child-min-width - gap*(span-1) = 210 - 10*1 = 200
|
||
;; 3 tracks (sentinel at [2]) → indexed = [[0,auto],[1,auto]]
|
||
;; each auto track gets 100
|
||
(let [sid (random-uuid)
|
||
child (make-child 210 100)
|
||
tracks [(auto-track) (auto-track) (auto-track)]
|
||
cells {:c1 (make-cell sid 1 1 2 1)}
|
||
cmap {sid [(child-bounds 210 100) child]}
|
||
result (gld/set-auto-multi-span (auto-col-parent 10) tracks cmap cells frame-bounds {} :column)]
|
||
(t/is (mth/close? 100.0 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 100.0 (:size (nth result 1)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 2)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-fixed-track-reduces-budget
|
||
(t/testing "fixed track in span is deducted from budget; only the auto track grows"
|
||
;; tracks: [fixed 60, auto 0.01, auto-sentinel] (sentinel at [2])
|
||
;; col=1, span=2 → indexed = [[0, fixed60], [1, auto]]
|
||
;; find-auto-allocations: fixed→subtract 60; auto→keep
|
||
;; to-allocate after fixed = 200 - 60 = 140; indexed-auto = [[1, auto]]
|
||
;; pass 1: idx1: max(0.01, 140/1, 0.01) = 140
|
||
;; apply: track0 = max(60, 0) = 60; track1 = max(0.01, 140) = 140
|
||
(let [sid (random-uuid)
|
||
child (make-child 200 100)
|
||
tracks [(fixed-track 60) (auto-track) (auto-track)]
|
||
cells {:c1 (make-cell sid 1 1 2 1)}
|
||
cmap {sid [(child-bounds 200 100) child]}
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks cmap cells frame-bounds {} :column)]
|
||
(t/is (mth/close? 60.0 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 140.0 (:size (nth result 1)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 2)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-child-smaller-than-existing-tracks
|
||
(t/testing "when child is smaller than the existing track sizes, tracks are not shrunk"
|
||
;; tracks: [auto 80, auto 80, auto-sentinel]
|
||
;; child width = 50; size-to-allocate = 50
|
||
;; indexed = [[0, auto80], [1, auto80]]
|
||
;; pass 1 (non-assigned, to-alloc=50):
|
||
;; idx0: max(0.01, 50/2, 80) = 80; rem = 50-80 = -30
|
||
;; idx1: max(0.01, max(-30,0)/1, 80) = 80
|
||
;; pass 2 (to-alloc=max(-30,0)=0): same max, no change
|
||
;; both tracks stay at 80
|
||
(let [sid (random-uuid)
|
||
child (make-child 50 100)
|
||
tracks [{:type :auto :size 80.0 :max-size ##Inf}
|
||
{:type :auto :size 80.0 :max-size ##Inf}
|
||
(auto-track)]
|
||
cells {:c1 (make-cell sid 1 1 2 1)}
|
||
cmap {sid [(child-bounds 50 100) child]}
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks cmap cells frame-bounds {} :column)]
|
||
(t/is (mth/close? 80.0 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 80.0 (:size (nth result 1)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-flex-track-in-span-excluded
|
||
(t/testing "cells whose span contains a flex track are skipped (handled by set-flex-multi-span)"
|
||
;; tracks: [flex 1fr, auto] col=1, span=2 → has-flex-track? = true → cell excluded
|
||
;; 2 tracks total (no sentinel needed since the cell is excluded before indexing)
|
||
(let [sid (random-uuid)
|
||
child (make-child 300 100)
|
||
tracks [(flex-track 1) (auto-track)]
|
||
cells {:c1 (make-cell sid 1 1 2 1)}
|
||
cmap {sid [(child-bounds 300 100) child]}
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks cmap cells frame-bounds {} :column)]
|
||
(t/is (mth/close? 0.01 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 1)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-non-spanned-track-unaffected
|
||
(t/testing "tracks outside the span keep their size – tests (get allocated %1 0) default"
|
||
;; 4 tracks; child at col=2 span=2 → indexed covers tracks 1 and 2 (sentinel [3]).
|
||
;; Track 0 (before the span) and track 3 (sentinel) are never allocated.
|
||
;; from-idx = clamp(2-1, 0, 3) = 1
|
||
;; to-idx = clamp((2-1)+2, 0, 3) = 3
|
||
;; subvec(enumerate, 1, 3) = [[1,auto],[2,auto]]
|
||
;; size-to-allocate = 200 → both indexed tracks get 100
|
||
;; apply: track0 = max(0.01, get({},0,0)) = max(0.01,0) = 0.01 ← uses default 0
|
||
;; track1 = max(0.01, 100) = 100
|
||
;; track2 = max(0.01, 100) = 100
|
||
;; track3 = max(0.01, get({},3,0)) = 0.01 (sentinel)
|
||
(let [sid (random-uuid)
|
||
child (make-child 200 100)
|
||
tracks [(auto-track) (auto-track) (auto-track) (auto-track)]
|
||
cells {:c1 (make-cell sid 2 1 2 1)}
|
||
cmap {sid [(child-bounds 200 100) child]}
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks cmap cells frame-bounds {} :column)]
|
||
;; track before span: size stays at 0.01 (default 0 from missing allocation entry)
|
||
(t/is (mth/close? 0.01 (:size (nth result 0)) 0.001))
|
||
;; spanned tracks grow
|
||
(t/is (mth/close? 100.0 (:size (nth result 1)) 0.001))
|
||
(t/is (mth/close? 100.0 (:size (nth result 2)) 0.001))
|
||
;; sentinel after span also unaffected
|
||
(t/is (mth/close? 0.01 (:size (nth result 3)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-row-type
|
||
(t/testing ":row type uses :row/:row-span and grows row tracks by child height"
|
||
;; child height = 200, row-gap = 0, row=1 span=2, 3 row tracks (sentinel at [2])
|
||
;; from-idx=0, to-idx=clamp(2,0,2)=2 → [[0,auto],[1,auto]]
|
||
;; size-to-allocate = 200 → each row track gets 100
|
||
(let [sid (random-uuid)
|
||
child (make-child 100 200)
|
||
tracks [(auto-track) (auto-track) (auto-track)]
|
||
cells {:c1 (make-cell sid 1 1 1 2)}
|
||
cmap {sid [(child-bounds 100 200) child]}
|
||
result (gld/set-auto-multi-span (auto-row-parent) tracks cmap cells frame-bounds {} :row)]
|
||
(t/is (mth/close? 100.0 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 100.0 (:size (nth result 1)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 2)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-row-gap-deducted
|
||
(t/testing "row-gap is deducted from budget for :row type"
|
||
;; child height = 210, row-gap = 10, row-span = 2
|
||
;; size-to-allocate = 210 - 10*1 = 200 → each track gets 100
|
||
(let [sid (random-uuid)
|
||
child (make-child 100 210)
|
||
tracks [(auto-track) (auto-track) (auto-track)]
|
||
cells {:c1 (make-cell sid 1 1 1 2)}
|
||
cmap {sid [(child-bounds 100 210) child]}
|
||
result (gld/set-auto-multi-span (auto-row-parent 10) tracks cmap cells frame-bounds {} :row)]
|
||
(t/is (mth/close? 100.0 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 100.0 (:size (nth result 1)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 2)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-smaller-span-processed-first
|
||
(t/testing "cells are sorted by span ascending (sort-by span -): smaller span allocates first"
|
||
;; NOTE: (sort-by prop-span -) uses `-` as a comparator; this yields ascending
|
||
;; order (smaller span first), not descending as the code comment implies.
|
||
;;
|
||
;; 4 tracks (sentinel at [3]):
|
||
;; cell-B: col=1 span=2 (covers indexed [0,1]) – processed first (span=2)
|
||
;; cell-A: col=1 span=3 (covers indexed [0,1,2]) – processed second (span=3)
|
||
;;
|
||
;; cell-B: child=100px, to-allocate=100.
|
||
;; non-assigned=[0,1]; pass1: idx0→max(0.01,50,0.01)=50; idx1→max(0.01,50,0.01)=50
|
||
;; allocated = {0:50, 1:50}
|
||
;;
|
||
;; cell-A: child=300px, to-allocate=300.
|
||
;; indexed=[0,1,2]; non-assigned=[2] (tracks 0,1 already allocated)
|
||
;; pass1 (non-assigned only): idx2→max(0.01,300/1,0.01)=300 ; rem=0
|
||
;; pass2 (to-alloc=0): max preserves existing values → no change
|
||
;; allocated = {0:50, 1:50, 2:300}
|
||
;;
|
||
;; Final: track0=50, track1=50, track2=300, track3(sentinel)=0.01
|
||
(let [sid-a (random-uuid)
|
||
sid-b (random-uuid)
|
||
child-a (make-child 300 100)
|
||
child-b (make-child 100 100)
|
||
tracks [(auto-track) (auto-track) (auto-track) (auto-track)] ; sentinel at [3]
|
||
cells {:ca (make-cell sid-a 1 1 3 1)
|
||
:cb (make-cell sid-b 1 1 2 1)}
|
||
cmap {sid-a [(child-bounds 300 100) child-a]
|
||
sid-b [(child-bounds 100 100) child-b]}
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks cmap cells frame-bounds {} :column)]
|
||
(t/is (mth/close? 50.0 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 50.0 (:size (nth result 1)) 0.001))
|
||
(t/is (mth/close? 300.0 (:size (nth result 2)) 0.001))
|
||
(t/is (mth/close? 0.01 (:size (nth result 3)) 0.001)))))
|
||
|
||
(t/deftest set-auto-multi-span-all-fixed-tracks-in-span
|
||
(t/testing "when all spanned tracks are fixed, no auto allocation occurs; fixed tracks unchanged"
|
||
;; tracks: [fixed 100, fixed 100, auto-sentinel]
|
||
;; col=1, span=2 → indexed = [[0,fixed100],[1,fixed100]]
|
||
;; find-auto-allocations: both fixed → auto-indexed-tracks = []
|
||
;; allocate-auto-tracks on empty list → no entries in allocated map
|
||
;; apply: track0 = max(100, get({},0,0)) = max(100,0) = 100 (unchanged)
|
||
;; track1 = max(100, get({},1,0)) = max(100,0) = 100 (unchanged)
|
||
(let [sid (random-uuid)
|
||
child (make-child 50 100)
|
||
tracks [(fixed-track 100) (fixed-track 100) (auto-track)]
|
||
cells {:c1 (make-cell sid 1 1 2 1)}
|
||
cmap {sid [(child-bounds 50 100) child]}
|
||
result (gld/set-auto-multi-span (auto-col-parent) tracks cmap cells frame-bounds {} :column)]
|
||
(t/is (mth/close? 100.0 (:size (nth result 0)) 0.001))
|
||
(t/is (mth/close? 100.0 (:size (nth result 1)) 0.001)))))
|
||
|
||
;; ---------------------------------------------------------------------------
|
||
;; calculate-params: derived paddings are never negative
|
||
;; ---------------------------------------------------------------------------
|
||
;;
|
||
;; Adding a grid layout derives the paddings from the children. A child that
|
||
;; overflows the board gave negative paddings that the shape schema rejects,
|
||
;; which broke autosave (#12107).
|
||
|
||
(defn- grid-params-for-children
|
||
[& children-attrs]
|
||
(let [parent (cts/setup-shape {:type :frame :x 0 :y 0 :width 100 :height 100})
|
||
children (mapv #(cts/setup-shape (merge {:type :rect :parent-id (:id parent)} %))
|
||
children-attrs)]
|
||
(glparams/calculate-params nil children parent)))
|
||
|
||
(t/deftest grid-calculate-params-keeps-positive-padding
|
||
(let [params (grid-params-for-children {:x 10 :y 20 :width 30 :height 30})]
|
||
(t/is (mth/close? 20 (get-in params [:layout-padding :p1])))
|
||
(t/is (mth/close? 10 (get-in params [:layout-padding :p2])))))
|
||
|
||
(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)))))
|
||
|
||
(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))))
|
||
|
||
(t/deftest grid-that-hugs-sizes-its-percent-tracks-by-content
|
||
;; percentages of a size the grid takes from them would never settle
|
||
(let [child-id (uuid/next)
|
||
cell-id (uuid/next)
|
||
frame (-> (make-grid-frame :layout-grid-columns [{:type :percent :value 50.0}]
|
||
:layout-grid-rows [{:type :fixed :value 30.0}]
|
||
:layout-item-h-sizing :auto
|
||
:width 200 :height 30)
|
||
(assoc :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 20
|
||
:parent-id (:id frame) :frame-id (:id frame)})
|
||
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)
|
||
resized (gsh/transform-shape (get objects (:id frame)) (get-in tree [(:id frame) :modifiers]))]
|
||
(t/is (mth/close? 40.0 (:width (:selrect resized)) 0.01))))
|