Don't render not visible shapes on workspace

This commit is contained in:
Andrey Antukh 2023-09-01 17:37:16 +02:00
parent 5b3e12bb9c
commit a01c64ea57
6 changed files with 54 additions and 41 deletions

View file

@ -275,21 +275,19 @@
[a b] [a b]
(mth/almost-zero? (- a b))) (mth/almost-zero? (- a b)))
;; FIXME: performance
(defn overlaps-rects? (defn overlaps-rects?
"Check for two rects to overlap. Rects won't overlap only if "Check for two rects to overlap. Rects won't overlap only if
one of them is fully to the left or the top" one of them is fully to the left or the top"
[rect-a rect-b] [rect-a rect-b]
(let [x1a (dm/get-prop rect-a :x)
y1a (dm/get-prop rect-a :y)
x2a (+ x1a (dm/get-prop rect-a :width))
y2a (+ y1a (dm/get-prop rect-a :height))
(let [x1a (:x rect-a) x1b (dm/get-prop rect-b :x)
y1a (:y rect-a) y1b (dm/get-prop rect-b :y)
x2a (+ (:x rect-a) (:width rect-a)) x2b (+ x1b (dm/get-prop rect-b :width))
y2a (+ (:y rect-a) (:height rect-a)) y2b (+ y1b (dm/get-prop rect-b :height))]
x1b (:x rect-b)
y1b (:y rect-b)
x2b (+ (:x rect-b) (:width rect-b))
y2b (+ (:y rect-b) (:height rect-b))]
(and (or (> x2a x1b) (s= x2a x1b)) (and (or (> x2a x1b) (s= x2a x1b))
(or (>= x2b x1a) (s= x2b x1a)) (or (>= x2b x1a) (s= x2b x1a))

View file

@ -305,30 +305,27 @@
(defn overlaps? (defn overlaps?
"General case to check for overlapping between shapes and a rectangle" "General case to check for overlapping between shapes and a rectangle"
[shape rect] [shape rect]
(let [stroke-width (/ (or (:stroke-width shape) 0) 2) (let [swidth (/ (or (:stroke-width shape) 0) 2)
rect (-> rect rect (-> rect
(update :x - stroke-width) (update :x - swidth)
(update :y - stroke-width) (update :y - swidth)
(update :width + (* 2 stroke-width)) (update :width + (* 2 swidth))
(update :height + (* 2 stroke-width)))] (update :height + (* 2 swidth)))]
(or (not shape) (or (not shape)
(let [path? (= :path (:type shape))
circle? (= :circle (:type shape))
text? (= :text (:type shape))]
(cond (cond
path? (cph/path-shape? shape)
(and (overlaps-rect-points? rect (:points shape)) (and (overlaps-rect-points? rect (:points shape))
(overlaps-path? shape rect)) (overlaps-path? shape rect))
circle? (cph/circle-shape? shape)
(and (overlaps-rect-points? rect (:points shape)) (and (overlaps-rect-points? rect (:points shape))
(overlaps-ellipse? shape rect)) (overlaps-ellipse? shape rect))
text? (cph/text-shape? shape)
(overlaps-text? shape rect) (overlaps-text? shape rect)
:else :else
(overlaps-rect-points? rect (:points shape))))))) (overlaps-rect-points? rect (:points shape))))))
(defn has-point-rect? (defn has-point-rect?
[rect point] [rect point]

View file

@ -16,6 +16,7 @@
(def current-project-id (mf/create-context nil)) (def current-project-id (mf/create-context nil))
(def current-page-id (mf/create-context nil)) (def current-page-id (mf/create-context nil))
(def current-file-id (mf/create-context nil)) (def current-file-id (mf/create-context nil))
(def current-vbox (mf/create-context nil))
(def active-frames (mf/create-context nil)) (def active-frames (mf/create-context nil))
(def render-thumbnails (mf/create-context nil)) (def render-thumbnails (mf/create-context nil))

View file

@ -13,6 +13,7 @@
common." common."
(:require (:require
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.geom.rect :as grc]
[app.common.pages.helpers :as cph] [app.common.pages.helpers :as cph]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.main.ui.context :as ctx] [app.main.ui.context :as ctx]
@ -54,7 +55,18 @@
;; frame changes won't affect the rendering frame ;; frame changes won't affect the rendering frame
frame-objects frame-objects
(mf/with-memo [objects] (mf/with-memo [objects]
(cph/objects-by-frame objects))] (cph/objects-by-frame objects))
vbox (mf/use-ctx ctx/current-vbox)
shapes
(mf/with-memo [shapes vbox]
(if (some? vbox)
(->> shapes
(filterv (fn [shape]
(grc/overlaps-rects? vbox (dm/get-prop shape :selrect)))))
shapes))]
[:g {:id (dm/str "shape-" uuid/zero)} [:g {:id (dm/str "shape-" uuid/zero)}
[:& (mf/provider ctx/active-frames) {:value active-frames} [:& (mf/provider ctx/active-frames) {:value active-frames}

View file

@ -88,6 +88,7 @@
(let [shape (unchecked-get props "shape")] (let [shape (unchecked-get props "shape")]
[:& frame-shape {:shape shape :ref node-ref}]))))) [:& frame-shape {:shape shape :ref node-ref}])))))
(defn root-frame-wrapper-factory (defn root-frame-wrapper-factory
[shape-wrapper] [shape-wrapper]
@ -101,9 +102,9 @@
thumbnail? (unchecked-get props "thumbnail?") thumbnail? (unchecked-get props "thumbnail?")
page-id (mf/use-ctx ctx/current-page-id) page-id (mf/use-ctx ctx/current-page-id)
frame-id (:id shape) frame-id (dm/get-prop shape :id)
objects (wsh/lookup-page-objects @st/state) objects (wsh/lookup-page-objects @st/state page-id)
node-ref (mf/use-ref nil) node-ref (mf/use-ref nil)
root-ref (mf/use-ref nil) root-ref (mf/use-ref nil)
@ -129,6 +130,7 @@
on-frame-load on-frame-load
(fns/use-node-store node-ref rendered-ref thumbnail? render-frame?) (fns/use-node-store node-ref rendered-ref thumbnail? render-frame?)
] ]
(fdm/use-dynamic-modifiers objects (mf/ref-val node-ref) modifiers) (fdm/use-dynamic-modifiers objects (mf/ref-val node-ref) modifiers)

View file

@ -88,6 +88,8 @@
show-distances? show-distances?
picking-color?]} wglobal picking-color?]} wglobal
vbox' (mf/use-debounce 100 vbox)
;; CONTEXT ;; CONTEXT
page-id (mf/use-ctx ctx/current-page-id) page-id (mf/use-ctx ctx/current-page-id)
@ -323,12 +325,13 @@
:y (:y vbox 0) :y (:y vbox 0)
:fill background}] :fill background}]
[:& (mf/provider ctx/current-vbox) {:value vbox'}
[:& (mf/provider use/include-metadata-ctx) {:value (debug? :show-export-metadata)} [:& (mf/provider use/include-metadata-ctx) {:value (debug? :show-export-metadata)}
[:& (mf/provider embed/context) {:value true} [:& (mf/provider embed/context) {:value true}
;; Render root shape ;; Render root shape
[:& shapes/root-shape {:key page-id [:& shapes/root-shape {:key page-id
:objects base-objects :objects base-objects
:active-frames @active-frames}]]]] :active-frames @active-frames}]]]]]
[:svg.viewport-controls [:svg.viewport-controls
{:xmlns "http://www.w3.org/2000/svg" {:xmlns "http://www.w3.org/2000/svg"