Merge pull request #1914 from penpot/alotor-performance-improvements

Performance improvements
This commit is contained in:
Alejandro 2022-05-18 11:15:40 +02:00 committed by GitHub
commit fa00d674eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 45 deletions

View file

@ -176,45 +176,50 @@
(defn calc-child-modifiers (defn calc-child-modifiers
[parent child modifiers ignore-constraints transformed-parent-rect] [parent child modifiers ignore-constraints transformed-parent-rect]
(let [constraints-h
(if-not ignore-constraints
(:constraints-h child (default-constraints-h child))
:scale)
constraints-v (if (and (nil? (:resize-vector modifiers))
(if-not ignore-constraints (nil? (:resize-vector-2 modifiers)))
(:constraints-v child (default-constraints-v child)) ;; If we don't have a resize modifier we return the same modifiers
:scale) modifiers
(let [constraints-h
(if-not ignore-constraints
(:constraints-h child (default-constraints-h child))
:scale)
modifiers-h (constraint-modifier (constraints-h const->type+axis) :x parent child modifiers transformed-parent-rect) constraints-v
modifiers-v (constraint-modifier (constraints-v const->type+axis) :y parent child modifiers transformed-parent-rect)] (if-not ignore-constraints
(:constraints-v child (default-constraints-v child))
:scale)
;; Build final child modifiers. Apply transform again to the result, to get the modifiers-h (constraint-modifier (constraints-h const->type+axis) :x parent child modifiers transformed-parent-rect)
;; real modifiers that need to be applied to the child, including rotation as needed. modifiers-v (constraint-modifier (constraints-v const->type+axis) :y parent child modifiers transformed-parent-rect)]
(cond-> {}
(or (contains? modifiers-h :displacement)
(contains? modifiers-v :displacement))
(assoc :displacement (cond-> (gpt/point (get-in modifiers-h [:displacement :x] 0)
(get-in modifiers-v [:displacement :y] 0))
(some? (:resize-transform modifiers))
(gpt/transform (:resize-transform modifiers))
:always ;; Build final child modifiers. Apply transform again to the result, to get the
(gmt/translate-matrix))) ;; real modifiers that need to be applied to the child, including rotation as needed.
(cond-> {}
(or (contains? modifiers-h :displacement)
(contains? modifiers-v :displacement))
(assoc :displacement (cond-> (gpt/point (get-in modifiers-h [:displacement :x] 0)
(get-in modifiers-v [:displacement :y] 0))
(some? (:resize-transform modifiers))
(gpt/transform (:resize-transform modifiers))
(:resize-vector modifiers-h) :always
(assoc :resize-origin (:resize-origin modifiers-h) (gmt/translate-matrix)))
:resize-vector (gpt/point (get-in modifiers-h [:resize-vector :x] 1)
(get-in modifiers-h [:resize-vector :y] 1)))
(:resize-vector modifiers-v) (:resize-vector modifiers-h)
(assoc :resize-origin-2 (:resize-origin modifiers-v) (assoc :resize-origin (:resize-origin modifiers-h)
:resize-vector-2 (gpt/point (get-in modifiers-v [:resize-vector :x] 1) :resize-vector (gpt/point (get-in modifiers-h [:resize-vector :x] 1)
(get-in modifiers-v [:resize-vector :y] 1))) (get-in modifiers-h [:resize-vector :y] 1)))
(:resize-transform modifiers) (:resize-vector modifiers-v)
(assoc :resize-transform (:resize-transform modifiers) (assoc :resize-origin-2 (:resize-origin modifiers-v)
:resize-transform-inverse (:resize-transform-inverse modifiers)) :resize-vector-2 (gpt/point (get-in modifiers-v [:resize-vector :x] 1)
(get-in modifiers-v [:resize-vector :y] 1)))
:always (:resize-transform modifiers)
(clean-modifiers)))) (assoc :resize-transform (:resize-transform modifiers)
:resize-transform-inverse (:resize-transform-inverse modifiers))
:always
(clean-modifiers)))))

View file

@ -356,22 +356,27 @@
[modif-tree shape modifiers] [modif-tree shape modifiers]
(let [children (map (d/getf objects) (:shapes shape)) (let [children (map (d/getf objects) (:shapes shape))
modifiers (cond-> modifiers snap-pixel? (set-pixel-precision shape))
transformed-rect (gsh/transform-selrect (:selrect shape) modifiers) transformed-rect (gsh/transform-selrect (:selrect shape) modifiers)
set-child set-child
(fn [modif-tree child] (fn [snap-pixel? modif-tree child]
(let [child-modifiers (gsh/calc-child-modifiers shape child modifiers ignore-constraints transformed-rect)] (let [child-modifiers (gsh/calc-child-modifiers shape child modifiers ignore-constraints transformed-rect)
child-modifiers (cond-> child-modifiers snap-pixel? (set-pixel-precision child))]
(cond-> modif-tree (cond-> modif-tree
(not (gsh/empty-modifiers? child-modifiers)) (not (gsh/empty-modifiers? child-modifiers))
(set-modifiers-rec child child-modifiers)))) (set-modifiers-rec child child-modifiers))))
modif-tree modif-tree
(-> modif-tree (-> modif-tree
(assoc-in [(:id shape) :modifiers] modifiers))] (assoc-in [(:id shape) :modifiers] modifiers))
(reduce set-child modif-tree children)))] resize-modif?
(set-modifiers-rec modif-tree shape modifiers))) (or (:resize-vector modifiers) (:resize-vector-2 modifiers))]
(reduce (partial set-child (and snap-pixel? resize-modif?)) modif-tree children)))]
(let [modifiers (cond-> modifiers snap-pixel? (set-pixel-precision shape))]
(set-modifiers-rec modif-tree shape modifiers))))
(defn- get-ignore-tree (defn- get-ignore-tree
"Retrieves a map with the flag `ignore-geometry?` given a tree of modifiers" "Retrieves a map with the flag `ignore-geometry?` given a tree of modifiers"

View file

@ -28,9 +28,12 @@
(defn strip-position-data [shape] (defn strip-position-data [shape]
(dissoc shape :position-data :transform :transform-inverse)) (dissoc shape :position-data :transform :transform-inverse))
(defn strip-modifier
[modifier]
(d/update-when modifier :modifiers dissoc :displacement :rotation))
(defn process-shape [modifiers {:keys [id] :as shape}] (defn process-shape [modifiers {:keys [id] :as shape}]
(let [modifier (get modifiers id) (let [modifier (-> (get modifiers id) strip-modifier)
modifier (d/update-when modifier :modifiers dissoc :displacement :rotation)
shape (cond-> shape shape (cond-> shape
(not (gsh/empty-modifiers? (:modifiers modifier))) (not (gsh/empty-modifiers? (:modifiers modifier)))
(-> (assoc :grow-type :fixed) (-> (assoc :grow-type :fixed)
@ -117,11 +120,16 @@
text-change? text-change?
(fn [id] (fn [id]
(let [old-shape (get prev-text-shapes id) (let [old-shape (get prev-text-shapes id)
new-shape (get text-shapes id)] new-shape (get text-shapes id)
old-modifiers (-> (get prev-modifiers id) strip-modifier)
new-modifiers (-> (get modifiers id) strip-modifier)]
(or (and (not (identical? old-shape new-shape)) (or (and (not (identical? old-shape new-shape))
(not= old-shape new-shape)) (not= old-shape new-shape))
(not= (get modifiers id)
(get prev-modifiers id))))) ;; The shape has changed only if its modifier is not empty and it's different
(and (not= new-modifiers old-modifiers)
(or (not (gsh/empty-modifiers? (:modifiers old-modifiers)))
(not (gsh/empty-modifiers? (:modifiers new-modifiers))))))))
changed-texts changed-texts
(mf/use-memo (mf/use-memo