From 971342b3e08166222682c53ea49b2d4a7e8209df Mon Sep 17 00:00:00 2001 From: Konstantinos Kaloutas Date: Mon, 2 Dec 2024 17:16:51 +0200 Subject: [PATCH] refactor --- src/renderer/dialog/events.cljs | 6 +++--- .../element/impl/container/canvas.cljs | 6 +++--- src/renderer/element/impl/custom/blob.cljs | 8 ++++---- src/renderer/element/impl/custom/measure.cljs | 16 +++++++-------- src/renderer/element/impl/shape/circle.cljs | 20 +++++++++---------- src/renderer/element/impl/shape/ellipse.cljs | 12 +++++------ src/renderer/snap/views.cljs | 6 +++--- src/renderer/tool/impl/base/edit.cljs | 12 +++++++---- src/renderer/tool/impl/base/transform.cljs | 16 +++++++-------- src/renderer/tool/impl/base/zoom.cljs | 6 +++--- src/renderer/utils/{overlay.cljs => svg.cljs} | 19 +----------------- 11 files changed, 57 insertions(+), 70 deletions(-) rename src/renderer/utils/{overlay.cljs => svg.cljs} (90%) diff --git a/src/renderer/dialog/events.cljs b/src/renderer/dialog/events.cljs index 8a75944c..88de5ac6 100644 --- a/src/renderer/dialog/events.cljs +++ b/src/renderer/dialog/events.cljs @@ -30,7 +30,7 @@ (rf/reg-event-fx ::close - (fn [{:keys [db]} [_ dispatch]] + (fn [{:keys [db]} [_ event]] (cond-> {:db (update db :dialogs pop)} - dispatch - (assoc :dispatch dispatch)))) + event + (assoc :dispatch event)))) diff --git a/src/renderer/element/impl/container/canvas.cljs b/src/renderer/element/impl/container/canvas.cljs index 15967a45..f6a532a6 100644 --- a/src/renderer/element/impl/container/canvas.cljs +++ b/src/renderer/element/impl/container/canvas.cljs @@ -15,8 +15,8 @@ [renderer.tool.hierarchy :as tool.hierarchy] [renderer.tool.subs :as-alias tool.s] [renderer.utils.keyboard :as keyb] - [renderer.utils.overlay :as overlay] - [renderer.utils.pointer :as pointer])) + [renderer.utils.pointer :as pointer] + [renderer.utils.svg :as svg])) (derive :canvas ::hierarchy/element) @@ -83,7 +83,7 @@ (when snap? [:<> (when snapped-el - [overlay/bounding-box (:bounds snapped-el) true]) + [svg/bounding-box (:bounds snapped-el) true]) (when nearest-neighbor [snap.v/canvas-label nearest-neighbor])]) diff --git a/src/renderer/element/impl/custom/blob.cljs b/src/renderer/element/impl/custom/blob.cljs index 543fe526..2bcf8677 100644 --- a/src/renderer/element/impl/custom/blob.cljs +++ b/src/renderer/element/impl/custom/blob.cljs @@ -15,8 +15,8 @@ [renderer.ui :as ui] [renderer.utils.element :as element] [renderer.utils.length :as length] - [renderer.utils.overlay :as overlay] - [renderer.utils.pointer :as pointer])) + [renderer.utils.pointer :as pointer] + [renderer.utils.svg :as svg])) (derive :blob ::hierarchy/renderable) @@ -158,7 +158,7 @@ [x1 y1] (cond->> [x y] (not (element/svg? el)) (mat/add offset)) [x2 y2] (mat/add [x1 y1] size)] [:<> - [overlay/line [x1 y1] [x2 y2]] + [svg/line [x1 y1] [x2 y2]] [tool.v/square-handle {:type :handle :cursor "move" @@ -167,4 +167,4 @@ :x x2 :y y2 :id :size}] - [overlay/times [x1 y1]]])) + [svg/times [x1 y1]]])) diff --git a/src/renderer/element/impl/custom/measure.cljs b/src/renderer/element/impl/custom/measure.cljs index 2fc71e79..829bc4ad 100644 --- a/src/renderer/element/impl/custom/measure.cljs +++ b/src/renderer/element/impl/custom/measure.cljs @@ -5,7 +5,7 @@ [renderer.element.hierarchy :as hierarchy] [renderer.utils.length :as length] [renderer.utils.math :as math] - [renderer.utils.overlay :as overlay])) + [renderer.utils.svg :as svg])) (derive :measure ::hierarchy/element) @@ -19,19 +19,19 @@ straight? (< angle 180) straight-angle (if straight? angle (- angle 360))] [:g {:key id} - [overlay/cross [x1 y1]] - [overlay/cross [x2 y2]] + [svg/cross [x1 y1]] + [svg/cross [x2 y2]] - [overlay/arc [x1 y1] 20 (if straight? 0 angle) (abs straight-angle)] + [svg/arc [x1 y1] 20 (if straight? 0 angle) (abs straight-angle)] - [overlay/line [x1 y1] [x2 y2] false] - [overlay/line [x1 y1] [(+ x1 (/ 30 zoom)) y1]] + [svg/line [x1 y1] [x2 y2] false] + [svg/line [x1 y1] [(+ x1 (/ 30 zoom)) y1]] - [overlay/label + [svg/label (str (.toFixed straight-angle 2) "°") [(+ x1 (/ 40 zoom)) y1] "start"] - [overlay/label + [svg/label (-> hypotenuse js/parseFloat (.toFixed 2) str) [(/ (+ x1 x2) 2) (/ (+ y1 y2) 2)]]])) diff --git a/src/renderer/element/impl/shape/circle.cljs b/src/renderer/element/impl/shape/circle.cljs index cd48117b..ba6820ce 100644 --- a/src/renderer/element/impl/shape/circle.cljs +++ b/src/renderer/element/impl/shape/circle.cljs @@ -9,7 +9,7 @@ [renderer.utils.bounds :as bounds] [renderer.utils.element :as element] [renderer.utils.length :as length] - [renderer.utils.overlay :as overlay])) + [renderer.utils.svg :as svg])) (derive :circle ::hierarchy/shape) @@ -74,14 +74,14 @@ [cx cy] (bounds/center bounds) r (/ (first (bounds/->dimensions bounds)) 2)] [:g - [overlay/line [cx cy] [(+ cx r) cy]] - [overlay/label (str (.toFixed r 2)) [(+ cx (/ r 2)) cy]] - [overlay/times [cx cy]] + [svg/line [cx cy] [(+ cx r) cy]] + [svg/label (str (.toFixed r 2)) [(+ cx (/ r 2)) cy]] + [svg/times [cx cy]] [tool.v/square-handle {:x (+ cx r) - :y cy - :id :r - :type :handle - :action :edit - :cursor "move" - :element (:id el)} + :y cy + :id :r + :type :handle + :action :edit + :cursor "move" + :element (:id el)} [:title {:key "r-title"} "r"]]])) diff --git a/src/renderer/element/impl/shape/ellipse.cljs b/src/renderer/element/impl/shape/ellipse.cljs index c59c9477..f1a8f41a 100644 --- a/src/renderer/element/impl/shape/ellipse.cljs +++ b/src/renderer/element/impl/shape/ellipse.cljs @@ -9,7 +9,7 @@ [renderer.utils.bounds :as bounds] [renderer.utils.element :as element] [renderer.utils.length :as length] - [renderer.utils.overlay :as overlay])) + [renderer.utils.svg :as svg])) (derive :ellipse ::hierarchy/shape) @@ -67,11 +67,11 @@ [cx cy] (bounds/center bounds) [rx ry] (mat/div (bounds/->dimensions bounds) 2)] [:g ::edit-handles - [overlay/times [cx cy]] - [overlay/line [cx cy] [(+ cx rx) cy]] - [overlay/label (str (.toFixed rx 2)) [(+ cx (/ rx 2)) cy]] - [overlay/line [cx cy] [cx (- cy ry)]] - [overlay/label (str (.toFixed ry 2)) [cx (- cy (/ ry 2))]] + [svg/times [cx cy]] + [svg/line [cx cy] [(+ cx rx) cy]] + [svg/label (str (.toFixed rx 2)) [(+ cx (/ rx 2)) cy]] + [svg/line [cx cy] [cx (- cy ry)]] + [svg/label (str (.toFixed ry 2)) [cx (- cy (/ ry 2))]] (map (fn [handle] ^{:key (:id handle)} [tool.v/square-handle diff --git a/src/renderer/snap/views.cljs b/src/renderer/snap/views.cljs index 96f4c358..8e9d0168 100644 --- a/src/renderer/snap/views.cljs +++ b/src/renderer/snap/views.cljs @@ -9,7 +9,7 @@ [renderer.snap.events :as-alias snap.e] [renderer.snap.subs :as-alias snap.s] [renderer.ui :as ui] - [renderer.utils.overlay :as overlay])) + [renderer.utils.svg :as svg])) (defn options-dropdown [] @@ -59,6 +59,6 @@ label (str/join " to " (remove nil? [base-label point-label])) point (:point nearest-neighbor)] [:<> - [overlay/times point] + [svg/times point] (when (not-empty label) - [overlay/label label (mat/add point margin) "start"])])) + [svg/label label (mat/add point margin) "start"])])) diff --git a/src/renderer/tool/impl/base/edit.cljs b/src/renderer/tool/impl/base/edit.cljs index ae4d3fe4..87ab79aa 100644 --- a/src/renderer/tool/impl/base/edit.cljs +++ b/src/renderer/tool/impl/base/edit.cljs @@ -10,8 +10,9 @@ [renderer.snap.handlers :as snap.h] [renderer.tool.handlers :as h] [renderer.tool.hierarchy :as hierarchy] - [renderer.utils.overlay :as overlay] - [renderer.utils.pointer :as pointer])) + [renderer.utils.element :as element] + [renderer.utils.pointer :as pointer] + [renderer.utils.svg :as svg])) (derive :edit ::hierarchy/tool) @@ -97,5 +98,8 @@ ^{:key (str (:id el) "-edit-points")} [:g [element.hierarchy/render-edit el] - ^{:key (str (:id el) "-centroid")} - [overlay/centroid el]])])) + (when-let [pos (element.hierarchy/centroid el)] + (let [offset (element/offset el) + pos (mat/add offset pos)] + ^{:key (str (:id el) "-centroid")} + [svg/dot pos [:title "Centroid"]]))])])) diff --git a/src/renderer/tool/impl/base/transform.cljs b/src/renderer/tool/impl/base/transform.cljs index a94e6608..5e1802a7 100644 --- a/src/renderer/tool/impl/base/transform.cljs +++ b/src/renderer/tool/impl/base/transform.cljs @@ -22,8 +22,8 @@ [renderer.utils.bounds :as bounds :refer [Bounds]] [renderer.utils.element :as element] [renderer.utils.math :refer [Vec2]] - [renderer.utils.overlay :as overlay] - [renderer.utils.pointer :as pointer])) + [renderer.utils.pointer :as pointer] + [renderer.utils.svg :as svg])) (def ScaleHandle [:enum :middle-right @@ -152,7 +152,7 @@ (defn select-rect [db intersecting?] - (cond-> (overlay/select-box db) + (cond-> (svg/select-box db) (not intersecting?) (assoc-in [:attrs :fill] "transparent"))) @@ -379,7 +379,7 @@ y (+ y2 (/ (+ (/ theme.db/handle-size 2) 15) zoom)) [width height] (bounds/->dimensions bounds) text (str (.toFixed width 2) " x " (.toFixed height 2))] - [overlay/label text [x y]])) + [svg/label text [x y]])) (m/=> area-label [:-> number? Bounds any?]) (defn area-label @@ -390,7 +390,7 @@ x (+ x1 (/ (- x2 x1) 2)) y (+ y1 (/ (- -15 (/ theme.db/handle-size 2)) zoom)) text (str (.toFixed area 2) " px²")] - [overlay/label text [x y]]))) + [svg/label text [x y]]))) (defmethod hierarchy/render :transform [] @@ -404,12 +404,12 @@ (for [el selected-elements] (when (:bounds el) ^{:key (str (:id el) "-bounds")} - [overlay/bounding-box (:bounds el) false])) + [svg/bounding-box (:bounds el) false])) (for [el hovered-ids] (when (:bounds el) ^{:key (str (:id el) "-bounds")} - [overlay/bounding-box (:bounds el) true])) + [svg/bounding-box (:bounds el) true])) (when (and (pos? elements-area) (= state :scale) (seq bounds)) [area-label elements-area bounds]) @@ -423,4 +423,4 @@ nil)]) (when pivot-point - [overlay/times pivot-point])])) + [svg/times pivot-point])])) diff --git a/src/renderer/tool/impl/base/zoom.cljs b/src/renderer/tool/impl/base/zoom.cljs index a92dfda0..7a5d2e19 100644 --- a/src/renderer/tool/impl/base/zoom.cljs +++ b/src/renderer/tool/impl/base/zoom.cljs @@ -5,8 +5,8 @@ [renderer.snap.handlers :as snap.h] [renderer.tool.handlers :as h] [renderer.tool.hierarchy :as hierarchy] - [renderer.utils.overlay :as overlay] - [renderer.utils.pointer :as pointer])) + [renderer.utils.pointer :as pointer] + [renderer.utils.svg :as svg])) (derive :zoom ::hierarchy/tool) @@ -42,7 +42,7 @@ (defmethod hierarchy/on-drag :zoom [db _e] - (h/set-temp db (overlay/select-box db))) + (h/set-temp db (svg/select-box db))) (defmethod hierarchy/on-drag-end :zoom [db e] diff --git a/src/renderer/utils/overlay.cljs b/src/renderer/utils/svg.cljs similarity index 90% rename from src/renderer/utils/overlay.cljs rename to src/renderer/utils/svg.cljs index 0b36c876..20b69010 100644 --- a/src/renderer/utils/overlay.cljs +++ b/src/renderer/utils/svg.cljs @@ -1,20 +1,12 @@ -(ns renderer.utils.overlay +(ns renderer.utils.svg "Render functions for canvas overlay objects." (:require - [clojure.core.matrix :as mat] [malli.core :as m] [re-frame.core :as rf] [renderer.app.db :refer [App]] - [renderer.app.subs :as-alias app.s] [renderer.document.subs :as-alias document.s] - [renderer.element.db :refer [Element]] - [renderer.element.hierarchy :as element.hierarchy] - [renderer.frame.subs :as-alias frame.s] - [renderer.snap.subs :as-alias snap.s] [renderer.theme.db :as theme.db] - [renderer.tool.subs :as-alias tool.s] [renderer.utils.bounds :as bounds :refer [Bounds]] - [renderer.utils.element :as element] [renderer.utils.hiccup :refer [Hiccup]] [renderer.utils.math :as math :refer [Vec2]])) @@ -176,12 +168,3 @@ :stroke theme.db/accent :stroke-opacity ".5" :stroke-width (/ 1 zoom)}})) - -(m/=> centroid [:-> Element any?]) -(defn centroid - [el] - (when-let [pos (element.hierarchy/centroid el)] - (let [offset (element/offset el) - pos (mat/add offset pos)] - [dot pos - [:title "Centroid"]])))