mirror of
https://github.com/penpot/penpot.git
synced 2026-11-21 01:22:01 +00:00
🐛 Keep a grid's turned fill child at least 1px thick
A grid fills a child turned in it along the grid's axes, which skews the child. In a column a fixed sibling left without width, the fill squeezed the child down to a line: its axes became parallel and its transform singular, and its stored rotation stopped describing it. The fill no longer narrows a turned child below 1px of thickness in its own axes; the child overflows its column instead. The grid passes each child's own corners with its bounds in the grid's axes to know its size and turn. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | world | baseline | now | ratio | |------------------------------------+----------+---------+-------| | deepest-edit-through-three-levels | 35.215 | 19.605 | 0.56 | | edit-through-a-chain-of-dependents | 68.931 | 41.613 | 0.60 | | library-update-into-copies | 83.083 | 47.375 | 0.57 | | main-edit-among-many-shapes | 4.837 | 2.321 | 0.48 | | main-edit-into-10-copies | 1.79 | 0.949 | 0.53 | | main-edit-into-100-copies | 15.415 | 10.086 | 0.65 | | main-edit-into-1000-copies | 153.086 | 92.412 | 0.60 | | variant-switch-into-copies | 32.052 | 20.051 | 0.63 | AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
469d97812d
commit
af9792fa44
@ -110,8 +110,10 @@
|
||||
|
||||
(letfn [(apply-modifiers [bounds child]
|
||||
(when-let [child-bounds (get bounds (:id child))]
|
||||
;; the child's own corners stay with its bounds in the grid's axes
|
||||
[(-> @child-bounds
|
||||
(gpo/parent-coords-bounds @transformed-parent-bounds))
|
||||
(gpo/parent-coords-bounds @transformed-parent-bounds)
|
||||
(vary-meta assoc :app.common.geom.shapes.grid-layout.positions/own-points @child-bounds))
|
||||
child]))
|
||||
|
||||
(set-child-modifiers [modif-tree grid-data cell-data [child-bounds child]]
|
||||
|
||||
@ -53,6 +53,22 @@
|
||||
(gpt/add $ (vv (* row-gap (dec (count span-row-tracks))))))]
|
||||
[p1 p2 p3 p4]))))
|
||||
|
||||
(defn- turned-min-scale
|
||||
"The smallest scale a fill applies along the grid's axes to a child turned
|
||||
in it, from the child's own corners `own-points`: one that keeps it at
|
||||
least 1px thick in its own axes, as the skew narrows it. Nil for a child
|
||||
that lines up with the grid's axes or is already thinner."
|
||||
[own-points [g1 g2]]
|
||||
(when-let [[p1 p2 _ p4] (seq own-points)]
|
||||
(let [own-x (gpt/to-vec p1 p2)
|
||||
width (gpt/length own-x)
|
||||
height (gpt/length (gpt/to-vec p1 p4))
|
||||
ux (gpt/unit own-x)
|
||||
gx (gpt/unit (gpt/to-vec g1 g2))
|
||||
sin (mth/abs (- (* (:x ux) (:y gx)) (* (:y ux) (:x gx))))]
|
||||
(when (and (> (min width height) 1) (> sin 1e-4) (< sin (- 1 1e-4)))
|
||||
(/ 1 (min width height))))))
|
||||
|
||||
(defn calc-fill-width-data
|
||||
"Calculates the size and modifiers for the width of an auto-fill child"
|
||||
[_parent
|
||||
@ -60,11 +76,13 @@
|
||||
transform-inverse
|
||||
child
|
||||
child-origin child-width
|
||||
cell-bounds]
|
||||
cell-bounds
|
||||
min-scale]
|
||||
|
||||
(let [target-width (max (- (gpo/width-points cell-bounds) (ctl/child-width-margin child)) 0.01)
|
||||
max-width (max (ctl/child-max-width child) 0.01)
|
||||
target-width (mth/clamp target-width (ctl/child-min-width child) max-width)
|
||||
target-width (cond-> (mth/clamp target-width (ctl/child-min-width child) max-width)
|
||||
(some? min-scale) (max (* min-scale child-width)))
|
||||
fill-scale (/ target-width child-width)]
|
||||
{:width target-width
|
||||
:modifiers (ctm/resize-modifiers (gpt/point fill-scale 1) child-origin transform transform-inverse)}))
|
||||
@ -75,10 +93,12 @@
|
||||
transform transform-inverse
|
||||
child
|
||||
child-origin child-height
|
||||
cell-bounds]
|
||||
cell-bounds
|
||||
min-scale]
|
||||
(let [target-height (max (- (gpo/height-points cell-bounds) (ctl/child-height-margin child)) 0.01)
|
||||
max-height (max (ctl/child-max-height child) 0.01)
|
||||
target-height (mth/clamp target-height (ctl/child-min-height child) max-height)
|
||||
target-height (cond-> (mth/clamp target-height (ctl/child-min-height child) max-height)
|
||||
(some? min-scale) (max (* min-scale child-height)))
|
||||
fill-scale (/ target-height child-height)]
|
||||
{:height target-height
|
||||
:modifiers (ctm/resize-modifiers (gpt/point 1 fill-scale) child-origin transform transform-inverse)}))
|
||||
@ -95,13 +115,18 @@
|
||||
(when (or (ctl/fill-width? child) (ctl/fill-height? child))
|
||||
(gtr/calculate-geometry @parent-bounds))
|
||||
|
||||
;; a child turned in the grid fills along the grid's axes, which skews it
|
||||
min-scale
|
||||
(when (or (ctl/fill-width? child) (ctl/fill-height? child))
|
||||
(turned-min-scale (::own-points (meta child-bounds)) @parent-bounds))
|
||||
|
||||
fill-width
|
||||
(when (ctl/fill-width? child)
|
||||
(calc-fill-width-data parent transform transform-inverse child child-origin child-width cell-bounds))
|
||||
(calc-fill-width-data parent transform transform-inverse child child-origin child-width cell-bounds min-scale))
|
||||
|
||||
fill-height
|
||||
(when (ctl/fill-height? child)
|
||||
(calc-fill-height-data parent transform transform-inverse child child-origin child-height cell-bounds))
|
||||
(calc-fill-height-data parent transform transform-inverse child child-origin child-height cell-bounds min-scale))
|
||||
|
||||
child-width (or (:width fill-width) child-width)
|
||||
child-height (or (:height fill-height) child-height)]
|
||||
|
||||
@ -226,3 +226,41 @@
|
||||
(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)))))))
|
||||
|
||||
(t/deftest grid-fill-keeps-a-turned-child-at-least-one-pixel-thick
|
||||
;; a fill child turned in its grid fills along the grid's axes, which skews
|
||||
;; it; a fixed sibling leaves its column no width, and the child narrows
|
||||
;; only until it is 1px thick in its own axes
|
||||
(let [file (-> (thf/sample-file :file)
|
||||
(ths/add-sample-shape :grid {:type :frame :name "Grid" :width 100 :height 100
|
||||
:layout :grid :layout-grid-dir :row
|
||||
:layout-grid-rows [{:type :flex :value 1}]
|
||||
:layout-grid-columns [{:type :flex :value 1} {:type :flex :value 1}
|
||||
{:type :flex :value 1}]
|
||||
:layout-gap {:row-gap 5 :column-gap 9}
|
||||
:layout-padding {:p1 8 :p2 8 :p3 8 :p4 8}})
|
||||
(ths/add-sample-shape :child {:type :frame :name "Child" :parent-label :grid
|
||||
:width 100 :height 100 :layout-item-h-sizing :fill})
|
||||
(ths/add-sample-shape :rect {:type :rect :name "Rect" :parent-label :grid
|
||||
:width 70 :height 30}))
|
||||
page (thf/current-page file)
|
||||
objects (:objects page)
|
||||
child (get objects (thi/id :child))
|
||||
turned (gsh/transform-shape child (ctm/rotation-modifiers child (gsh/shape->center child) 45))
|
||||
grid (-> (get objects (thi/id :grid))
|
||||
(assoc :shapes [(thi/id :child) (thi/id :rect)])
|
||||
(assoc :layout-grid-cells
|
||||
{(thi/new-id! :cell-1) {:id (thi/id :cell-1) :row 1 :column 1 :row-span 1 :column-span 1
|
||||
:position :manual :shapes [(thi/id :child)]}
|
||||
(thi/new-id! :cell-2) {:id (thi/id :cell-2) :row 1 :column 2 :row-span 1 :column-span 1
|
||||
:position :manual :shapes [(thi/id :rect)]}
|
||||
(thi/new-id! :cell-3) {:id (thi/id :cell-3) :row 1 :column 3 :row-span 1 :column-span 1
|
||||
:position :auto :shapes []}}))
|
||||
objects (assoc objects (thi/id :child) turned (thi/id :grid) grid)
|
||||
tree (gm/set-objects-modifiers {(thi/id :grid) {:modifiers (ctm/reflow-modifiers)}} objects)
|
||||
result (gsh/transform-shape turned (get-in tree [(thi/id :child) :modifiers]))
|
||||
[p1 p2 _ p4] (:points result)
|
||||
e1 (gpt/to-vec p1 p2)
|
||||
e2 (gpt/to-vec p1 p4)
|
||||
area (abs (- (* (:x e1) (:y e2)) (* (:y e1) (:x e2))))]
|
||||
(t/is (>= (/ area (max (gpt/length e1) (gpt/length e2))) 0.999))))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user