mirror of
https://github.com/penpot/penpot.git
synced 2026-03-02 08:13:46 +00:00
96 lines
2.6 KiB
Clojure
96 lines
2.6 KiB
Clojure
(ns app.util.debug
|
|
"Debugging utils"
|
|
(:require
|
|
[app.util.timers :as timers]
|
|
[app.util.object :as obj]
|
|
[app.common.math :as mth]
|
|
[cljs.pprint :refer [pprint]]))
|
|
|
|
(def debug-options #{:bounding-boxes :group :events :rotation-handler :resize-handler :selection-center #_:simple-selection})
|
|
|
|
;; These events are excluded when we activate the :events flag
|
|
(def debug-exclude-events
|
|
#{:app.main.data.workspace.notifications/handle-pointer-update
|
|
:app.main.data.workspace.selection/change-hover-state})
|
|
|
|
(defonce ^:dynamic *debug* (atom #{}))
|
|
|
|
(defn debug-all! [] (reset! *debug* debug-options))
|
|
(defn debug-none! [] (reset! *debug* #{}))
|
|
(defn debug! [option] (swap! *debug* conj option))
|
|
(defn -debug! [option] (swap! *debug* disj option))
|
|
|
|
(defn ^:export ^boolean debug?
|
|
[option]
|
|
(if *assert*
|
|
(boolean (@*debug* option))
|
|
false))
|
|
|
|
(defn ^:export toggle-debug [name] (let [option (keyword name)]
|
|
(if (debug? option)
|
|
(-debug! option)
|
|
(debug! option))))
|
|
(defn ^:export debug-all [] (debug-all!))
|
|
(defn ^:export debug-none [] (debug-none!))
|
|
|
|
(defn ^:export tap
|
|
"Transducer function that can execute a side-effect `effect-fn` per input"
|
|
[effect-fn]
|
|
|
|
(fn [rf]
|
|
(fn
|
|
([] (rf))
|
|
([result] (rf result))
|
|
([result input]
|
|
(effect-fn input)
|
|
(rf result input)))))
|
|
|
|
(defn ^:export logjs
|
|
([str] (tap (partial logjs str)))
|
|
([str val]
|
|
(js/console.log str (clj->js val))
|
|
val))
|
|
|
|
(when (exists? js/window)
|
|
(set! (.-dbg ^js js/window) clj->js)
|
|
(set! (.-pp ^js js/window) pprint))
|
|
|
|
|
|
(defonce widget-style "
|
|
background: black;
|
|
bottom: 10px;
|
|
color: white;
|
|
height: 20px;
|
|
padding-left: 8px;
|
|
position: absolute;
|
|
right: 10px;
|
|
width: 40px;
|
|
z-index: 99999;
|
|
opacity: 0.5;
|
|
")
|
|
|
|
(defn ^:export fps
|
|
"Adds a widget to keep track of the average FPS's"
|
|
[]
|
|
(let [last (volatile! (.now js/performance))
|
|
avg (volatile! 0)
|
|
node (-> (.createElement js/document "div")
|
|
(obj/set! "id" "fps")
|
|
(obj/set! "style" widget-style))
|
|
body (obj/get js/document "body")
|
|
|
|
do-thing (fn do-thing []
|
|
(timers/raf
|
|
(fn []
|
|
(let [cur (.now js/performance)
|
|
ts (/ 1000 (* (- cur @last)))
|
|
val (+ @avg (* (- ts @avg) 0.1))]
|
|
|
|
(obj/set! node "innerText" (mth/precision val 0))
|
|
(vreset! last cur)
|
|
(vreset! avg val)
|
|
(do-thing)))))]
|
|
|
|
(.appendChild body node)
|
|
(do-thing)))
|