🐛 Fix problem with alignment performance

This commit is contained in:
alonso.torres 2023-10-06 16:25:44 +02:00 committed by Andrés Moya
parent 859146ddc2
commit fe3740e329
7 changed files with 82 additions and 63 deletions

View file

@ -1021,9 +1021,6 @@
;; --- Shape / Selection Alignment and Distribution
(declare align-object-to-parent)
(declare align-objects-list)
(defn can-align? [selected objects]
(cond
(empty? selected) false
@ -1031,11 +1028,18 @@
:else
(not= uuid/zero (:parent-id (get objects (first selected))))))
(defn- move-shape
[shape]
(let [bbox (-> shape :points gsh/points->selrect)
pos (gpt/point (:x bbox) (:y bbox))]
(dwt/update-position (:id shape) pos)))
(defn align-object-to-parent
[objects object-id axis]
(let [object (get objects object-id)
parent-id (:parent-id (get objects object-id))
parent (get objects parent-id)]
[(gal/align-to-rect object parent axis)]))
(defn align-objects-list
[objects selected axis]
(let [selected-objs (map #(get objects %) selected)
rect (gsh/shapes->rect selected-objs)]
(map #(gal/align-to-rect % rect axis) selected-objs)))
(defn align-objects
[axis]
@ -1052,28 +1056,12 @@
moved (if (= 1 (count selected))
(align-object-to-parent objects (first selected) axis)
(align-objects-list objects selected axis))
ids (map :id moved)
undo-id (js/Symbol)]
(when (can-align? selected objects)
(rx/concat
(rx/of (dwu/start-undo-transaction undo-id))
(->> (rx/from moved)
(rx/map move-shape))
(rx/of (ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))))
(defn align-object-to-parent
[objects object-id axis]
(let [object (get objects object-id)
parent (:parent-id (get objects object-id))
parent-obj (get objects parent)]
(gal/align-to-rect object parent-obj axis objects)))
(defn align-objects-list
[objects selected axis]
(let [selected-objs (map #(get objects %) selected)
rect (gsh/selection-rect selected-objs)]
(mapcat #(gal/align-to-rect % rect axis objects) selected-objs)))
(rx/of (dwu/start-undo-transaction undo-id)
(dwt/position-shapes moved)
(ptk/data-event :layout/update selected)
(dwu/commit-undo-transaction undo-id)))))))
(defn can-distribute? [selected]
(cond
@ -1094,14 +1082,13 @@
objects (wsh/lookup-page-objects state page-id)
selected (wsh/lookup-selected state)
moved (-> (map #(get objects %) selected)
(gal/distribute-space axis objects))
moved (d/index-by :id moved)
ids (keys moved)
update-fn #(get moved (:id %))]
(gal/distribute-space axis))
undo-id (js/Symbol)]
(when (can-distribute? selected)
(rx/of (dch/update-shapes ids update-fn {:reg-objects? true})))))))
(rx/of (dwu/start-undo-transaction undo-id)
(dwt/position-shapes moved)
(ptk/data-event :layout/update selected)
(dwu/commit-undo-transaction undo-id)))))))
;; --- Shape Proportions

View file

@ -402,14 +402,15 @@
([]
(apply-modifiers nil))
([{:keys [modifiers undo-transation? stack-undo?] :or {undo-transation? true stack-undo? false}}]
([{:keys [modifiers undo-transation? stack-undo? ignore-constraints ignore-snap-pixel]
:or {undo-transation? true stack-undo? false ignore-constraints false ignore-snap-pixel false}}]
(ptk/reify ::apply-modifiers
ptk/WatchEvent
(watch [_ state _]
(let [text-modifiers (get state :workspace-text-modifier)
objects (wsh/lookup-page-objects state)
object-modifiers (if modifiers
(calculate-modifiers state modifiers)
(calculate-modifiers state ignore-constraints ignore-snap-pixel modifiers)
(get state :workspace-modifiers))
ids (or (keys object-modifiers) [])

View file

@ -687,7 +687,6 @@
(defn update-position
"Move shapes to a new position"
[id position]
(js/console.log "DEBUG" (pr-str position))
(dm/assert! (uuid? id))
(ptk/reify ::update-position
@ -707,8 +706,31 @@
modif-tree (dwm/create-modif-tree [id] (ctm/move-modifiers delta))]
(rx/of (dwm/set-modifiers modif-tree false true)
(dwm/apply-modifiers))))))
(rx/of (dwm/apply-modifiers {:modifiers modif-tree
:ignore-constraints false
:ignore-snap-pixel true}))))))
(defn position-shapes
[shapes]
(ptk/reify ::position-shapes
ptk/WatchEvent
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
shapes (d/index-by :id shapes)
modif-tree
(dwm/build-modif-tree
(keys shapes)
objects
(fn [cshape]
(let [oshape (get shapes (:id cshape))
cpos (-> cshape :points first gpt/point)
opos (-> oshape :points first gpt/point)]
(ctm/move-modifiers (gpt/subtract opos cpos)))))]
(rx/of (dwm/apply-modifiers {:modifiers modif-tree
:ignore-constraints false
:ignore-snap-pixel true}))))))
(defn- move-shapes-to-frame
[ids frame-id drop-index]