0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 15:09:10 -05:00

Improve performance for constraints

This commit is contained in:
alonso.torres 2023-10-18 11:13:07 +02:00 committed by Andrey Antukh
parent 16694f005d
commit 06c8ada6f7
3 changed files with 48 additions and 16 deletions

View file

@ -314,21 +314,37 @@
(let [transformed-parent-bounds @transformed-parent-bounds
modifiers (ctm/select-child modifiers)
transformed-child-bounds (gtr/transform-bounds child-bounds modifiers)
modifiers (normalize-modifiers constraints-h constraints-v modifiers
child-bounds transformed-child-bounds parent-bounds transformed-parent-bounds)
transformed-child-bounds (gtr/transform-bounds child-bounds modifiers)
child-points-before (gpo/parent-coords-bounds child-bounds parent-bounds)
child-points-after (gpo/parent-coords-bounds transformed-child-bounds transformed-parent-bounds)
reset-modifiers?
(and (gpo/axis-aligned? parent-bounds)
(gpo/axis-aligned? child-bounds)
(gpo/axis-aligned? transformed-parent-bounds))
modifiers-h (constraint-modifier (constraints-h const->type+axis) :x
child-points-before parent-bounds
child-points-after transformed-parent-bounds)
modifiers
(if reset-modifiers?
(ctm/empty)
(normalize-modifiers constraints-h constraints-v modifiers
child-bounds (gtr/transform-bounds child-bounds modifiers)
parent-bounds transformed-parent-bounds))
modifiers-v (constraint-modifier (constraints-v const->type+axis) :y
child-points-before parent-bounds
child-points-after transformed-parent-bounds)]
(-> modifiers
(ctm/add-modifiers modifiers-h)
(ctm/add-modifiers modifiers-v))))))
transformed-child-bounds (if reset-modifiers?
child-bounds
(gtr/transform-bounds child-bounds modifiers))]
;; If the parent is a layout we don't need to calculate its constraints. Finish
;; after normalize the children (to keep proper proportions)
(if (ctl/any-layout? parent)
modifiers
(let [child-points-before (gpo/parent-coords-bounds child-bounds parent-bounds)
child-points-after (gpo/parent-coords-bounds transformed-child-bounds transformed-parent-bounds)
modifiers-h (constraint-modifier (constraints-h const->type+axis) :x
child-points-before parent-bounds
child-points-after transformed-parent-bounds)
modifiers-v (constraint-modifier (constraints-v const->type+axis) :y
child-points-before parent-bounds
child-points-after transformed-parent-bounds)]
(-> modifiers
(ctm/add-modifiers modifiers-h)
(ctm/add-modifiers modifiers-v))))))))

View file

@ -100,7 +100,11 @@
[modif-tree children objects bounds parent transformed-parent-bounds ignore-constraints]
(let [modifiers (dm/get-in modif-tree [(:id parent) :modifiers])]
;; Move modifiers don't need to calculate constraints
(if (ctm/only-move? modifiers)
(cond
(ctm/empty? modifiers)
modif-tree
(ctm/only-move? modifiers)
(loop [modif-tree modif-tree
children (seq children)]
(if-let [current (first children)]
@ -109,6 +113,7 @@
modif-tree))
;; Check the constraints, then resize
:else
(let [parent-id (:id parent)
parent-bounds (gtr/transform-bounds @(get bounds parent-id) (ctm/select-parent modifiers))]
(loop [modif-tree modif-tree

View file

@ -91,6 +91,17 @@
:else
0)))
(defn axis-aligned?
"Check if the points are parallel to the coordinate axis."
[[p1 p2 _ p4 :as pts]]
(and (= (count pts) 4)
(let [hv (gpt/to-vec p1 p2)
vv (gpt/to-vec p1 p4)]
(and (mth/almost-zero? (:y hv))
(mth/almost-zero? (:x vv))
(> (:x hv) 0)
(> (:y vv) 0)))))
(defn parent-coords-bounds
[child-bounds [p1 p2 _ p4 :as parent-bounds]]