Set selrect for new render modifiers

This commit is contained in:
alonso.torres 2025-05-12 17:43:04 +02:00
parent 6831acb71d
commit fef08dfa18
13 changed files with 200 additions and 72 deletions

View file

@ -504,6 +504,13 @@
(filter (fn [[_ {:keys [type]}]]
(= type :change-property)))))
(defn set-temporary-selrect
[selrect]
(ptk/reify ::set-temporary-selrect
ptk/UpdateEvent
(update [_ state]
(assoc state :workspace-selrect selrect))))
#_:clj-kondo/ignore
(defn set-wasm-modifiers
[modif-tree & {:keys [ignore-constraints ignore-snap-pixel]
@ -519,8 +526,8 @@
(assoc :prev-wasm-props (:wasm-props state))
(assoc :wasm-props property-changes))))
ptk/EffectEvent
(effect [_ state _]
ptk/WatchEvent
(watch [_ state _]
(wasm.api/clean-modifiers)
(let [prev-wasm-props (:prev-wasm-props state)
@ -531,8 +538,9 @@
(let [structure-entries (parse-structure-modifiers modif-tree)]
(wasm.api/set-structure-modifiers structure-entries)
(let [geometry-entries (parse-geometry-modifiers modif-tree)]
(wasm.api/propagate-apply geometry-entries)))))))
(let [geometry-entries (parse-geometry-modifiers modif-tree)
selrect (wasm.api/propagate-apply geometry-entries)]
(rx/of (set-temporary-selrect selrect))))))))
#_:clj-kondo/ignore
(defn apply-wasm-modifiers
@ -567,14 +575,6 @@
(clear-local-transform)
(dwsh/update-shapes ids update-shape))))))
(defn set-selrect-transform
[modifiers]
(ptk/reify ::set-selrect-transform
ptk/UpdateEvent
(update [_ state]
(assoc state :workspace-selrect-transform (ctm/modifiers->transform modifiers)))))
(def ^:private
xf-rotation-shape
(comp

View file

@ -25,6 +25,7 @@
[app.main.data.workspace.modifiers :as dwm]
[app.main.data.workspace.selection :as dws]
[app.main.data.workspace.shapes :as dwsh]
[app.main.data.workspace.transforms :as dwt]
[app.main.data.workspace.undo :as dwu]
[app.main.features :as features]
[app.main.fonts :as fonts]
@ -945,14 +946,17 @@
new-shape))
{:undo-group (when new-shape? id)})
(if finalize?
(if (and (not= :fixed (:grow-type shape)) finalize?)
(dwm/apply-wasm-modifiers
(resize-wasm-text-modifiers shape content)
{:undo-group (when new-shape? id)})
(dwm/set-wasm-modifiers
(resize-wasm-text-modifiers shape content)
{:undo-group (when new-shape? id)}))))
{:undo-group (when new-shape? id)}))
(when finalize?
(dwt/finish-transform))))
(let [objects (dsh/lookup-page-objects state)
shape (get objects id)

View file

@ -136,7 +136,7 @@
(update [_ state]
(-> state
(update :workspace-local dissoc :transform :duplicate-move-started?)
(dissoc :workspace-selrect-transform)))))
(dissoc :workspace-selrect)))))
;; -- Resize --------------------------------------------------------
@ -296,7 +296,6 @@
(fn [modifiers]
(let [modif-tree (dwm/create-modif-tree ids modifiers)]
(rx/of
(dwm/set-selrect-transform modifiers)
(dwm/set-wasm-modifiers
modif-tree
:ignore-constraints (contains? layout :scale-text))))))

View file

@ -147,9 +147,6 @@
(def workspace-drawing
(l/derived :workspace-drawing st/state))
(def workspace-selrect-transform
(l/derived :workspace-selrect-transform st/state))
(def workspace-tokens
"All tokens related ephimeral state"
(l/derived :workspace-tokens st/state))

View file

@ -18,8 +18,8 @@
[{:keys [shape zoom modifiers]}]
(if (features/active-feature? @st/state "render-wasm/v1")
(let [transform (gsh/transform-str shape)
{:keys [id x y]} shape
{:keys [width height]} (wasm.api/text-dimensions id)]
{:keys [id x y grow-type]} shape
{:keys [width height]} (if (= :fixed grow-type) shape (wasm.api/text-dimensions id))]
[:rect.main.viewport-selrect
{:x x
:y y

View file

@ -16,9 +16,13 @@
[app.config :as cf]
[app.main.data.workspace :as dw]
[app.main.data.workspace.texts :as dwt]
[app.main.features :as features]
[app.main.fonts :as fonts]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.css-cursors :as cur]
[app.main.ui.hooks :as h]
[app.render-wasm.api :as wasm.api]
[app.util.dom :as dom]
[app.util.globals :as global]
[app.util.keyboard :as kbd]
@ -34,6 +38,20 @@
result (.-textContent editor-root)]
(when (not= result "") result))))
(defn- get-fonts
[content]
(let [extract-fn (juxt :font-id :font-variant-id)
default (extract-fn txt/default-text-attrs)]
(->> (tree-seq map? :children content)
(into #{default} (keep extract-fn)))))
(defn- load-fonts!
[fonts]
(->> fonts
(run! (fn [[font-id variant-id]]
(when (some? font-id)
(fonts/ensure-loaded! font-id variant-id))))))
(defn- initialize-event-handlers
"Internal editor events handler initializer/destructor"
[shape-id content selection-ref editor-ref container-ref]
@ -137,7 +155,14 @@
;; This reference is to the container
container-ref (mf/use-ref nil)
selection-ref (mf/use-ref nil)]
selection-ref (mf/use-ref nil)
fonts
(-> (mf/use-memo (mf/deps content) #(get-fonts content))
(h/use-equal-memo))]
(mf/with-effect [fonts]
(load-fonts! fonts))
;; WARN: we explicitly do not pass content on effect dependency
;; array because we only need to initialize this once with initial
@ -235,16 +260,24 @@
(some? modifiers)
(gsh/transform-shape modifiers))
bounds (gst/shape->rect shape)
[x y width height]
(if (features/active-feature? @st/state "render-wasm/v1")
(let [{:keys [width height]} (wasm.api/text-dimensions shape-id)
{:keys [x y]} (:selrect shape)]
[x y width height])
(let [bounds (gst/shape->rect shape)
x (mth/min (dm/get-prop bounds :x)
(dm/get-prop shape :x))
y (mth/min (dm/get-prop bounds :y)
(dm/get-prop shape :y))
width (mth/max (dm/get-prop bounds :width)
(dm/get-prop shape :width))
height (mth/max (dm/get-prop bounds :height)
(dm/get-prop shape :height))]
[x y width height]))
x (mth/min (dm/get-prop bounds :x)
(dm/get-prop shape :x))
y (mth/min (dm/get-prop bounds :y)
(dm/get-prop shape :y))
width (mth/max (dm/get-prop bounds :width)
(dm/get-prop shape :width))
height (mth/max (dm/get-prop bounds :height)
(dm/get-prop shape :height))
style
(cond-> #js {:pointerEvents "all"}

View file

@ -24,6 +24,7 @@
[app.util.debug :as dbg]
[app.util.dom :as dom]
[app.util.object :as obj]
[okulary.core :as l]
[rumext.v2 :as mf]))
(def rotation-handler-size 20)
@ -313,17 +314,23 @@
:style {:fill (if (dbg/enabled? :handlers) "yellow" "none")
:stroke-width 0}}]]))
(def workspace-selrect-transform
(l/derived :workspace-selrect st/state))
(defn get-selrect
[selrect-transform shape]
(if (some? selrect-transform)
(let [{:keys [center width height transform]} selrect-transform]
[(gsh/center->rect center width height)
(gmt/transform-in center transform)])
[(dm/get-prop shape :selrect)
(gsh/transform-matrix shape)]))
(mf/defc controls-selection*
[{:keys [shape zoom color on-move-selected on-context-menu disabled]}]
(let [selrect (dm/get-prop shape :selrect)
transform-type (mf/deref refs/current-transform)
sr-transform (mf/deref refs/workspace-selrect-transform)
transform
(dm/str
(cond->> (gsh/transform-matrix shape)
(some? sr-transform)
(gmt/multiply sr-transform)))]
(let [selrect-transform (mf/deref workspace-selrect-transform)
transform-type (mf/deref refs/current-transform)
[selrect transform] (get-selrect selrect-transform shape)]
(when (and (some? selrect)
(not (or (= transform-type :move)
@ -340,19 +347,15 @@
(mf/defc controls-handlers*
{::mf/private true}
[{:keys [shape zoom color on-resize on-rotate disabled]}]
(let [transform-type (mf/deref refs/current-transform)
sr-transform (mf/deref refs/workspace-selrect-transform)
(let [selrect-transform (mf/deref workspace-selrect-transform)
transform-type (mf/deref refs/current-transform)
read-only? (mf/use-ctx ctx/workspace-read-only?)
layout (mf/deref refs/workspace-layout)
scale-text? (contains? layout :scale-text)
selrect (dm/get-prop shape :selrect)
transform (cond->> (gsh/transform-matrix shape)
(some? sr-transform)
(gmt/multiply sr-transform))
[selrect transform] (get-selrect selrect-transform shape)
rotation (-> (gpt/point 1 0)
(gpt/transform (:transform shape))

View file

@ -10,6 +10,8 @@
["react-dom/server" :as rds]
[app.common.data :as d]
[app.common.data.macros :as dm]
[app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt]
[app.common.types.path :as path]
[app.common.types.shape.layout :as ctl]
[app.common.uuid :as uuid]
@ -808,8 +810,29 @@
(sr/heapu32-set-uuid id heapu32 current-offset)
(sr/heapf32-set-matrix transform heapf32 (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-TRANSFORM-OFFSET)))
(recur (rest entries) (+ current-offset (mem/ptr8->ptr32 MODIFIER-ENTRY-SIZE))))))
(h/call wasm/internal-module "_propagate_apply")
(request-render "set-modifiers"))))
(let [offset (h/call wasm/internal-module "_propagate_apply")
heapf32 (mem/get-heap-f32)
width (aget heapf32 (mem/ptr8->ptr32 (+ offset 0)))
height (aget heapf32 (mem/ptr8->ptr32 (+ offset 4)))
cx (aget heapf32 (mem/ptr8->ptr32 (+ offset 8)))
cy (aget heapf32 (mem/ptr8->ptr32 (+ offset 12)))
a (aget heapf32 (mem/ptr8->ptr32 (+ offset 16)))
b (aget heapf32 (mem/ptr8->ptr32 (+ offset 20)))
c (aget heapf32 (mem/ptr8->ptr32 (+ offset 24)))
d (aget heapf32 (mem/ptr8->ptr32 (+ offset 28)))
e (aget heapf32 (mem/ptr8->ptr32 (+ offset 32)))
f (aget heapf32 (mem/ptr8->ptr32 (+ offset 36)))
transform (gmt/matrix a b c d e f)]
(h/call wasm/internal-module "_free_bytes")
(request-render "set-modifiers")
{:width width
:height height
:center (gpt/point cx cy)
:transform transform}))))
(defn set-canvas-background
[background]