finlly fixed it

This commit is contained in:
Olive Vaughn 2026-10-10 17:48:54 -04:00
parent a10d7b2d9f
commit e2ca251ef4
8 changed files with 184 additions and 70 deletions

View file

@ -793,11 +793,32 @@
(rf/reg-event-db
::insert-vertex
(fn [db [_ sid id i t]] (edit/transaction db #(paint/insert-vertex % sid id i t))))
;; The new point is the picked one, so a click that added it in the wrong
;; place is taken back with Delete.
(fn [db [_ sid id i t]]
(-> (edit/transaction db #(paint/insert-vertex % sid id i t))
(assoc-in [:ui :vertex] [sid id (inc i)]))))
(defn- deleting-vertex
"Point `i` of the shape gone, and the one before it picked, so Delete held or
pressed again goes on taking points — as Backspace takes characters — and
never falls through to the shape itself. A triangle keeps its three."
[db sid id i]
(let [{clip :clip} (store/entry (:clip/current db))
n (some-> (get-in clip [:symbols sid :nodes id :channels paint/geometry :keys])
vals first count (quot 2))]
(cond-> (assoc-in db [:ui :vertex] [sid id i])
(and n (< 3 n)) (-> (edit/transaction #(paint/delete-vertex % sid id i))
(assoc-in [:ui :vertex] [sid id (mod (dec i) (dec n))])))))
(rf/reg-event-db
::delete-vertex
(fn [db [_ sid id i]] (edit/transaction db #(paint/delete-vertex % sid id i))))
(fn [db [_ sid id i]] (deleting-vertex db sid id i)))
(rf/reg-event-db
::pick-vertex
;; The point Delete takes away: the last one pressed on the pen.
(fn [db [_ sid id i]] (assoc-in db [:ui :vertex] [sid id i])))
(rf/reg-event-db
::set-knob
@ -1294,20 +1315,28 @@
(rf/reg-event-db
::delete-selected
;; Backspace while drawing takes the last point back, as in Illustrator.
;; Backspace while drawing takes the last point back, as in Illustrator; on
;; the pen, with a point of the selected shape picked, it takes that point.
(fn [db _]
(if (seq (get-in db [:ui :draft]))
(update-in db [:ui :draft] #(subvec % 0 (- (count %) 2)))
(let [primary (get-in db [:ui :selection])
many (vec (get-in db [:ui :selections]))
selections (if (some #{primary} many) many (if primary [primary] []))
nodes (distinct (keep (fn [[kind sid id]] (when (= :node kind) [sid id])) selections))]
(if (seq nodes)
(-> db
(edit/transaction #(reduce (fn [c [sid id]] (nest/delete-node c sid id)) % nodes))
(assoc-in [:ui :selection] nil)
(assoc-in [:ui :selections] []))
db)))))
(let [[vsid vid i] (get-in db [:ui :vertex])
primary (get-in db [:ui :selection])]
(cond
(seq (get-in db [:ui :draft]))
(update-in db [:ui :draft] #(subvec % 0 (- (count %) 2)))
(and i (= :pen (get-in db [:ui :tool])) (= [:node vsid vid] (take 3 primary)))
(deleting-vertex db vsid vid i)
:else
(let [many (vec (get-in db [:ui :selections]))
selections (if (some #{primary} many) many (if primary [primary] []))
nodes (distinct (keep (fn [[kind sid id]] (when (= :node kind) [sid id])) selections))]
(if (seq nodes)
(-> db
(edit/transaction #(reduce (fn [c [sid id]] (nest/delete-node c sid id)) % nodes))
(assoc-in [:ui :selection] nil)
(assoc-in [:ui :selections] []))
db))))))
(rf/reg-event-db
::restack

View file

@ -33,6 +33,7 @@
(rf/reg-sub ::tool (fn [db _] (or (get-in db [:ui :tool]) :select)))
(rf/reg-sub ::auto-key? (fn [db _] (boolean (get-in db [:ui :auto-key?]))))
(rf/reg-sub ::draft (fn [db _] (get-in db [:ui :draft])))
(rf/reg-sub ::vertex (fn [db _] (get-in db [:ui :vertex])))
(rf/reg-sub ::hover (fn [db _] (get-in db [:ui :hover])))
(rf/reg-sub ::brush (fn [db _] (get-in db [:ui :brush] 6)))
(rf/reg-sub ::fit (fn [db _] (get-in db [:ui :fit] 1)))

View file

@ -22,7 +22,8 @@
holding video thumbnails live."
(:require [arthur.domain.onion :as onion]
[arthur.ui.menu :as menu]
[re-frame.core :as rf]))
[re-frame.core :as rf]
[reagent.core :as r]))
(def ^:private narrow?
"A phone-shaped window, read ONCE at load. It decides what the layout opens
@ -58,10 +59,6 @@
;; Pixels for a pane, a multiplier for a zoom, clamped either way.
(fn [db [_ k v]] (assoc-in db [:ui :layout k] (clamp k v))))
(rf/reg-event-db
::normal
(fn [db [_ k]] (assoc-in db [:ui :layout k] (k normal))))
(rf/reg-event-db
::toggle
(fn [db [_ k]]
@ -137,6 +134,90 @@
:on-click #(rf/dispatch [::toggle k])}
label])))]))
;; The work area, `{:stage [w h] :rect [x0 y0 x1 y1]}` in stage pixels: the stage,
;; everything drawn so far and a stage round both, and every view zoomed to. It
;; grows as things are drawn further out, so there is always room to draw
;; further, and it never shrinks, so there is always room to zoom about the
;; pointer. A Reagent atom: the stage lays itself out by it.
(defonce board (r/atom nil))
(defn hold!
"Make the work area hold `[x0 y0 x1 y1]`, on stage `[w h]`, in steps of 64 so
a shape moving out does not lay the stage out again every frame."
[[x0 y0 x1 y1] stage]
(let [step #(* 64 (% (/ %2 64)))
want [(step js/Math.floor x0) (step js/Math.floor y0)
(step js/Math.ceil x1) (step js/Math.ceil y1)]
{was :stage rect :rect} @board
rect (if (= was stage) (mapv #(%1 %2 %3) [min min max max] rect want) want)]
(when (not= rect (:rect @board))
(reset! board {:stage stage :rect rect}))))
;; The stage point `zoom-at!` holds under the pointer, `{:x :y :client-x :client-y
;; :due? :scroll}`. Kept between zooms, so a pinch is anchored on one point and
;; not on each event's re-measurement of it, which is off by the rounding of the
;; last scroll and adds up over a gesture to a slide. Measured again when the
;; pointer moves or the view is scrolled by anything else.
(defonce ^:private anchor (atom nil))
(defn- laid-zoom
"The zoom the stage box is laid out at, which is behind app-db's until React
commits — later than `r/flush`, under a React 18 root."
[^js wrap]
(/ (.-width (.getBoundingClientRect wrap)) (js/parseFloat (.getAttribute wrap "data-width"))))
(declare zoomed!)
(defn zoom-at!
"Zoom to `scale`, keeping the stage point under the pointer under it. The
scroll that does so waits for the stage box to be laid out at the new zoom —
see `zoomed!`. The work area grows to the view that will be on screen, in the
same layout, or that scroll could not reach there and the stage would be
pushed into the corner."
[^js el client-x client-y scale]
(let [wrap (.querySelector el ".stage-wrap")
w (js/parseFloat (.getAttribute wrap "data-width"))
h (js/parseFloat (.getAttribute wrap "data-height"))
z (clamp :stage scale)
was @anchor
{:keys [x y]} (if (and was (= [client-x client-y] [(:client-x was) (:client-y was)])
(or (:due? was) (= (:scroll was) [(.-scrollLeft el) (.-scrollTop el)])))
was
(let [box (.getBoundingClientRect wrap) laid (laid-zoom wrap)]
{:x (/ (- client-x (.-left box)) laid) :y (/ (- client-y (.-top box)) laid)}))
a (.getBoundingClientRect el)
cx (- client-x (.-left a)) cy (- client-y (.-top a))]
(reset! anchor {:x x :y y :client-x client-x :client-y client-y :due? true})
(hold! [(- x (/ cx z)) (- y (/ cy z))
(+ x (/ (- (.-clientWidth el) cx) z)) (+ y (/ (- (.-clientHeight el) cy) z))]
[w h])
(rf/dispatch-sync [::set :stage z])
;; Already laid out at this size, as at the end of the range: the box will
;; not be laid out again, so do not wait for it.
(when (< (js/Math.abs (- (* z w) (.-width (.getBoundingClientRect wrap)))) 0.5)
(zoomed! el))))
(defn zoomed!
"The stage box has been laid out again: if at a zoom from `zoom-at!`, scroll
its point back under the pointer. Before paint, so it never shows elsewhere."
[^js el]
(when-let [{:keys [x y client-x client-y due?]} @anchor]
(when due?
(let [wrap (.querySelector el ".stage-wrap")
box (.getBoundingClientRect wrap)
z (laid-zoom wrap)]
(set! (.-scrollLeft el) (+ (.-scrollLeft el) (- (+ (.-left box) (* x z)) client-x)))
(set! (.-scrollTop el) (+ (.-scrollTop el) (- (+ (.-top box) (* y z)) client-y)))
(swap! anchor assoc :due? false :scroll [(.-scrollLeft el) (.-scrollTop el)])))))
(defn- zoom-stage!
"Zoom the stage about the middle of the view, as its buttons do."
[scale]
(when-let [^js area (.querySelector js/document ".stage-area")]
(let [a (.getBoundingClientRect area)]
(zoom-at! area (+ (.-left a) (/ (.-clientWidth area) 2))
(+ (.-top a) (/ (.-clientHeight area) 2)) scale))))
(defn- stepped [k z out?]
;; Geometric steps work below 100% as well as above it.
(if (= :stage k)
@ -152,19 +233,20 @@
[k label & more]
(let [z @(rf/subscribe [::zoom k])
[lo hi] (limits k)
home (k normal)]
home (k normal)
to! (if (= :stage k) zoom-stage! #(rf/dispatch [::set k %]))]
[:div.group.zoomer
[:button {:disabled (<= z lo) :aria-label (str label " zoom out")
:title (str "zoom " label " out")
:on-click #(rf/dispatch [::set k (stepped k z true)])} "−"]
:on-click #(to! (stepped k z true))} "−"]
[:button.zoom-at {:class (when (not= z home) "on")
:aria-label (str label " zoom")
:title (str "back to " (js/Math.round (* 100 home)) "% — the normal zoom")
:on-click #(rf/dispatch [::normal k])}
:on-click #(to! home)}
(str (js/Math.round (* 100 z)) "%")]
[:button {:disabled (>= z hi) :aria-label (str label " zoom in")
:title (str "zoom " label " in")
:on-click #(rf/dispatch [::set k (stepped k z false)])} "+"]
:on-click #(to! (stepped k z false))} "+"]
(into [:<>] more)]))

View file

@ -105,12 +105,6 @@
[]
(swap! state assoc :last -1))
;; The work area, `{:stage [w h] :rect [x0 y0 x1 y1]}` in stage pixels: the stage,
;; everything drawn so far, and a stage's width and height round both. It grows
;; as things are drawn further out, so there is always room to draw further, and
;; never shrinks under the view. A Reagent atom: the stage lays itself out by it.
(defonce board (ratom/atom nil))
(rf/reg-sub
::snapshot
;; ONE SUB, so the tracker below derefs one input that is never dirty when it
@ -254,16 +248,9 @@
(js/Math.ceil (aget b 2)) (js/Math.ceil (aget b 3))]))
(defn- grow-board!
"Make the work area hold `[x0 y0 x1 y1]` and a stage round it, in steps of 64
so a shape moving out does not lay the stage out again every frame."
"Make the work area hold `[x0 y0 x1 y1]` and a stage round it."
[[x0 y0 x1 y1] w h]
(let [step #(* 64 (% (/ %2 64)))
want [(step js/Math.floor (- x0 w)) (step js/Math.floor (- y0 h))
(step js/Math.ceil (+ x1 w)) (step js/Math.ceil (+ y1 h))]
{:keys [stage rect]} @board
rect (if (= stage [w h]) (mapv #(%1 %2 %3) [min min max max] rect want) want)]
(when (not= rect (:rect @board))
(reset! board {:stage [w h] :rect rect}))))
(layout/hold! [(- x0 w) (- y0 h) (+ x1 w) (+ y1 h)] [w h]))
(defn- view-of
"`{:rect [x0 y0 x1 y1] :zoom z}`: the part of the work area on screen, in

View file

@ -558,10 +558,12 @@
(str "M" (first a) "," (second a) "L" (first b) "," (second b))))))
(defn- points
"The pen on the selected shape: its points to drag, ⌥-click to delete, and a
marker where a click on an edge would add one."
"The pen on the selected shape: its points to drag, the one pressed last
picked for Delete, ⌥-click to delete at once, and a marker where a click on
an edge would add one."
[ctx draft]
(let [[sid id geom active editable? frame matrix] (editing)
picked @(rf/subscribe [::sub/vertex])
pts (when geom (through matrix (channel/value-at geom frame (:store (loaded ctx)))))
edge (when (and editable? (empty? draft) (not (channel/nothing? pts)) @pointer)
(on-edge pts @pointer))]
@ -576,6 +578,7 @@
(for [[i [x y]] (map-indexed vector (pairs pts))]
^{:key i}
[:circle.vertex {:cx x :cy y :r 1.8
:class (when (= picked [sid id i]) "picked")
:on-pointer-down
(fn [^js event]
(.stopPropagation event)
@ -583,6 +586,7 @@
(if (.-altKey event)
(rf/dispatch [::ui/delete-vertex sid id i])
(do (.setPointerCapture (.-currentTarget event) (.-pointerId event))
(rf/dispatch [::ui/pick-vertex sid id i])
(reset! dragging [sid id active i inv]))))}])))])))
(defn- draft-lines
@ -774,30 +778,6 @@
(.addEventListener js/window "blur" #(do (reset! space-held? false) (reset! pan nil)))))
(defonce pinch-start-zoom (atom nil))
(defonce pending-zoom (atom nil))
(defn- zoom-at! [el client-x client-y scale]
(let [wrap (.querySelector el ".stage-wrap")
next (max 0.1 (min 16 scale))
pending @pending-zoom]
;; Retain the anchor from the layout before this batch of events. React
;; may not have committed the new dimensions until the animation frame.
(when-not pending
(let [z @(rf/subscribe [::layout/zoom :stage])
box (.getBoundingClientRect wrap)]
(reset! pending-zoom {:x (/ (- client-x (.-left box)) z)
:y (/ (- client-y (.-top box)) z)})))
(swap! pending-zoom assoc :scale next :client-x client-x :client-y client-y)
(rf/dispatch-sync [::layout/set :stage next])
(when-not pending
(js/requestAnimationFrame
(fn []
(r/flush)
(let [{:keys [x y scale client-x client-y]} @pending-zoom
box (.getBoundingClientRect wrap)]
(reset! pending-zoom nil)
(set! (.-scrollLeft el) (+ (.-scrollLeft el) (- (+ (.-left box) (* x scale)) client-x)))
(set! (.-scrollTop el) (+ (.-scrollTop el) (- (+ (.-top box) (* y scale)) client-y)))))))))
(defn- navigation []
{:title "Two-finger scroll to pan · Pinch or ⌘/Ctrl-wheel to zoom · Space-drag or middle-drag to pan"
@ -807,6 +787,7 @@
:ref (fn [el]
(when el
(.observe (js/ResizeObserver. player/repaint!) el)
(.observe (js/ResizeObserver. #(layout/zoomed! el)) (.querySelector el ".stage-wrap"))
(set! (.-onwheel el)
(fn [e]
(.preventDefault e)
@ -816,7 +797,7 @@
(let [unit (case (.-deltaMode e) 1 16 2 (.-clientHeight el) 1)
dx (* unit (.-deltaX e)) dy (* unit (.-deltaY e))]
(if (or (.-ctrlKey e) (.-metaKey e))
(zoom-at! el (.-clientX e) (.-clientY e)
(layout/zoom-at! el (.-clientX e) (.-clientY e)
(* @(rf/subscribe [::layout/zoom :stage])
(js/Math.exp (* (if (.-ctrlKey e) -0.01 -0.002) dy))))
(do
@ -831,7 +812,7 @@
(fn [e]
(.preventDefault e)
(when-let [z @pinch-start-zoom]
(zoom-at! el (.-clientX e) (.-clientY e) (* z (.-scale e))))))
(layout/zoom-at! el (.-clientX e) (.-clientY e) (* z (.-scale e))))))
(set! (.-ongestureend el)
(fn [e] (.preventDefault e) (reset! pinch-start-zoom nil))))
nil)
@ -883,8 +864,8 @@
opacity (if on? opacity 0)
;; The work area round the stage: as far as anything is drawn, and a
;; stage further. Margins and not an element round it, so it scrolls to
;; the left and the top as well. See `player/board`.
{:keys [stage rect]} @player/board
;; the left and the top as well. See `layout/board`.
{:keys [stage rect]} @layout/board
[x0 y0 x1 y1 :as rect] (if (= stage [w h]) rect [(- w) (- h) (* 2 w) (* 2 h)])
now {:stage [w h] :zoom zoom :rect rect}
was @laid]

View file

@ -19,7 +19,7 @@
:tip "click to select, drag to move · double-click a shape to edit its points"
:icon [:path {:d "M4 2 L4 14 L7 11 L9.5 16 L11.5 15 L9 10 L13 10 Z"}]}
{:tool :pen :key "p" :name "Pen"
:tip "click to place points · click the first to close · on a selected shape, click an edge to add a point and ⌥-click a point to delete it"
:tip "click to place points · click the first to close · on a selected shape, click an edge to add a point, click a point and press Delete (or ⌥-click it) to remove it"
:icon [:<> [:path {:d "M9 1 L14 8 L11.5 13 L6.5 13 L4 8 Z"}]
[:path.cut {:d "M9 2 V8"}]
[:rect {:x 6.5 :y 14 :width 5 :height 2}]]}

View file

@ -0,0 +1,33 @@
(ns arthur.events.vertex-test
(:require [cljs.test :refer [deftest is]]
[arthur.domain.clip :as clip]
[arthur.domain.paint :as paint]
[arthur.events.ui :as ui]
[arthur.footage.store :as store]
[re-frame.core :as rf]
[re-frame.db :as rf-db]))
(deftest delete-takes-the-picked-point-and-then-the-one-before
(let [doc (paint/new-shape (clip/blank) :main :s 0 [0 0 10 0 20 10 10 20 0 20] 1)
id (store/install! {:clip doc :store {}} "vertex-delete")
pts #(-> (store/entry id) :clip
(get-in [:symbols :main :nodes :s :channels paint/geometry :keys 0]))
n #(quot (count (pts)) 2)]
(reset! rf-db/app-db {:clip/current id :paint/revision 0
:ui {:open :main :tool :pen :selection [:node :main :s [:s]]}
:playback {:frame 0}})
(let [before (pts)]
(rf/dispatch-sync [::ui/pick-vertex :main :s 2])
(rf/dispatch-sync [::ui/delete-selected])
(is (= 4 (n)))
(is (= (into (subvec before 0 4) (subvec before 6)) (pts)) "point 2 is the one gone")
(is (= [:main :s 1] (get-in @rf-db/app-db [:ui :vertex])) "the one before it is picked"))
(rf/dispatch-sync [::ui/delete-selected])
(rf/dispatch-sync [::ui/delete-selected])
(is (= 3 (n)) "a triangle keeps its three")
(is (some? (get-in (:clip (store/entry id)) [:symbols :main :nodes :s]))
"and Delete never falls through to the shape")
(swap! rf-db/app-db assoc-in [:ui :tool] :select)
(rf/dispatch-sync [::ui/delete-selected])
(is (nil? (get-in (:clip (store/entry id)) [:symbols :main :nodes :s]))
"off the pen, Delete is the shape's")))

View file

@ -1685,6 +1685,7 @@ button.share-button:hover, button.share-button.on { filter: brightness(1.1); }
.paint-overlay .points .outline { fill: none; stroke: #fff1be; stroke-width: 1; stroke-opacity: .55; vector-effect: non-scaling-stroke; pointer-events: none; }
.paint-overlay .points .vertex { fill: #fff1be; stroke: #161820; stroke-width: 0.5; cursor: move; }
.paint-overlay .points .vertex:hover { fill: #fff; r: 2.4; }
.paint-overlay .points .vertex.picked { fill: var(--sel); stroke: #fff1be; }
.paint-overlay .points .insert { fill: #161820; stroke: #fff1be; stroke-width: 0.5; pointer-events: none; }
/* Blender's Adjust Last Operation: the last stroke's settings, in the corner of