diff --git a/frontend/src/arthur/events/ui.cljs b/frontend/src/arthur/events/ui.cljs index 1954534..ba6710d 100644 --- a/frontend/src/arthur/events/ui.cljs +++ b/frontend/src/arthur/events/ui.cljs @@ -665,16 +665,17 @@ (declare finishing-polygon) -(rf/reg-event-db - ::set-tool - ;; Leaving the pen commits what it was drawing, as Esc does: an unfinished - ;; polygon is not a thing the document can hold. - (fn [db [_ tool]] - (if (contains? tools tool) - (-> (if (= tool (get-in db [:ui :tool])) db (finishing-polygon db)) - (update :ui merge {:tool tool :draft []}) - (update :ui dissoc :hover)) - db))) +(defn with-tool + "`db` on `tool`. Leaving the pen commits what it was drawing, as Esc does: an + unfinished polygon is not a thing the document can hold." + [db tool] + (if (contains? tools tool) + (-> (if (= tool (get-in db [:ui :tool])) db (finishing-polygon db)) + (update :ui merge {:tool tool :draft []}) + (update :ui dissoc :hover)) + db)) + +(rf/reg-event-db ::set-tool (fn [db [_ tool]] (with-tool db tool))) (rf/reg-event-db ::cancel-polygon diff --git a/frontend/src/arthur/follow.cljs b/frontend/src/arthur/follow.cljs index 329ed5c..b264f3f 100644 --- a/frontend/src/arthur/follow.cljs +++ b/frontend/src/arthur/follow.cljs @@ -21,6 +21,7 @@ own clock, and a play goes out once with its frame rather than streamed." (:require [arthur.events.collab :as collab] [arthur.events.playback :as pb] + [arthur.events.ui :as ui] [arthur.follow.page :as page] [arthur.follow.party :as party] [arthur.ui.menu :as menu] @@ -52,14 +53,23 @@ [:scroll "panel scroll" nil] [:pointers "pointers" nil]]) +(defn- facet-paths [k] (some (fn [[fk _ ps]] (when (= fk k) ps)) facets)) + +(def ^:private takes + "Values taken the way the app takes them, not written over, because writing + would skip what it does on the way: a new tool commits the pen's unfinished + polygon, and a selection carries the multi-selection along with it." + {[:ui :tool] ui/with-tool + [:ui :selection] ui/selected}) + ;; --------------------------------------------------------------------------- ;; what we keep ;; ;; `[:presence :party]` and `[:presence :joinable]` are what we tell the room; ;; `collab` says them again when the socket comes back, which is the rejoin. ;; `:follow` is ours alone: the facets let go of, a move we could not take yet -;; (`:parked`), and the last move a mate made, to catch up to on taking a facet -;; back. +;; (`:parked`), and `:last`, everything mates have said of where they are, to +;; catch up to on taking a facet back. (def ^:private stored {:joinable "arthur/followable" :off "arthur/follow-off"}) @@ -119,19 +129,26 @@ ;; taken only on the tab it was made on. (fn [{:keys [db]} [_ cid v]] (let [open (get v [:ui :open]) + last (second (get-in db [:follow :last])) place? (and (contains? v [:ui :open]) (not (contains? (off db) :place))) taken (select-keys v (remove #{[:ui :open]} (paths db)))] (cond (not (party/mirrors? (:peers db) (me db) cid)) {} (and place? (not (pb/openable? db open))) - {:db (update db :follow assoc :parked [cid v] :last [cid v])} + {:db (update db :follow assoc :parked [cid v] :last [cid (merge last v)])} :else (let [switch? (and place? (not= open (get-in db [:ui :open]))) - db (cond-> (update db :follow assoc :parked nil :last [cid v]) + db (cond-> (update db :follow assoc :parked nil :last [cid (merge last v)]) switch? (pb/open open)) + ;; In facet order, so `:selections` lands after the `:selection` + ;; it belongs to. db (cond-> db (= (get v ::tab) (get-in db [:ui :open])) - (as-> db (reduce-kv #(assoc-in %1 (at %1 %2) %3) db taken))) + (as-> db (reduce (fn [db p] + (if (contains? taken p) + ((takes p #(assoc-in %1 (at %1 p) %2)) db (taken p)) + db)) + db (paths db)))) n (pb/frames db) f (-> (get-in db [:playback :frame] 0) (max 0) (min (dec n))) db (assoc-in db [:playback :frame] f)] @@ -157,19 +174,22 @@ (rf/->interceptor :id ::steer :after (fn [ctx] - (let [event (first (get-in ctx [:coeffects :event])) - before (get-in ctx [:coeffects :db]) - after (get-in ctx [:effects :db]) - [_ v :as parked] (get-in after [:follow :parked])] - (cond-> ctx - (and parked (pb/openable? after (get v [:ui :open]))) - (update-in [:effects :fx] (fnil conj []) [:dispatch (into [::moved] parked)]) + (let [after (get-in ctx [:effects :db])] + ;; On your own, which is nearly always, an event costs this and + ;; nothing more. + (if-not (and after (together? after)) + ctx + (let [event (first (get-in ctx [:coeffects :event])) + before (get-in ctx [:coeffects :db]) + [_ v :as parked] (get-in after [:follow :parked])] + (cond-> ctx + (and parked (pb/openable? after (get v [:ui :open]))) + (update-in [:effects :fx] (fnil conj []) [:dispatch (into [::moved] parked)]) - (not (or (nil? after) (#{::pb/tick ::moved} event) - (= (view before (paths after)) (view after)) - (not= (get-in before [:project :id]) (get-in after [:project :id])) - (not (together? after)))) - (update-in [:effects :fx] (fnil conj []) [::nav! (view-msg after)])))))) + (not (or (#{::pb/tick ::moved} event) + (not= (get-in before [:project :id]) (get-in after [:project :id])) + (= (view before (paths after)) (view after)))) + (update-in [:effects :fx] (fnil conj []) [::nav! (view-msg after)])))))))) ;; --------------------------------------------------------------------------- ;; the room @@ -243,15 +263,18 @@ (rf/reg-event-fx ::toggle - ;; Taking a facet back catches up to where the party last was. + ;; Taking a facet back catches up on that facet — and only that one: the rest + ;; of what they last said is as old as it is, and their frame, say, has moved + ;; on since if they are playing. (fn [{:keys [db]} [_ k]] (let [was (off db) now (if (contains? was k) (disj was k) (conj was k)) - db (assoc-in db [:follow :off] now)] + db (assoc-in db [:follow :off] now) + [cid v] (get-in db [:follow :last])] (cond-> {:db db ::remember [:off (.join (to-array (map name now)) ",")]} (= :pointers k) (assoc ::forget :all) - (and (contains? was k) (get-in db [:follow :last])) - (assoc :dispatch (into [::moved] (get-in db [:follow :last]))))))) + (and (contains? was k) cid (facet-paths k)) + (assoc :dispatch [::moved cid (select-keys v (conj (facet-paths k) ::tab))]))))) (rf/reg-sub ::joinable? (fn [db _] (get-in db [:presence :joinable] true))) (rf/reg-sub ::together? (fn [db _] (together? db))) diff --git a/frontend/src/arthur/follow/page.cljs b/frontend/src/arthur/follow/page.cljs index f2d56f7..edb6749 100644 --- a/frontend/src/arthur/follow/page.cljs +++ b/frontend/src/arthur/follow/page.cljs @@ -27,6 +27,14 @@ (defn- classes [^js el] (.getAttribute el "class")) (defn- kind [^js el] [(.-localName el) (.. el -classList (item 0))]) +(defn- label + "What `el` is called, if anything says: an id, an aria-label, a field's name. + Written for people and screen readers, so it says WHICH thing it is — the + inspector, not the second `section.pane` — and it does not change when the + layout does." + [^js el] + (some (fn [a] (some->> (.getAttribute el a) not-empty (str a "="))) ["id" "aria-label" "name"])) + (defn- nth-like "Which of `el`'s siblings that `same?` says are like it, it is." [^js el same?] @@ -34,22 +42,22 @@ (filter same? (array-seq (.. el -parentElement -children)))))) (defn- step - "`el` as one step of a path: its tag and classes, which of its siblings with - those it is, and which of those with its tag and FIRST class. The pool and the - inspector are both a `section.pane`, so the first class alone takes one for - the other whenever a pane is shut on one page and not the other; but the rest - come and go with state — `on`, `selected` — and a row lit on one page is still - the same row on the other, so that is what a step falls back to." + "`el` as one step of a path, three ways from surest to least: by what it is + called, among its siblings called that; by its tag and classes; and by its tag + and FIRST class, because the rest come and go with state — `on`, `selected` — + and a row lit on one page is still the same row on the other." [^js el] - (let [tag (.-localName el) cls (classes el) k (kind el)] + (let [tag (.-localName el) cls (classes el) k (kind el) l (label el)] [tag cls (nth-like el #(and (= tag (.-localName %)) (= cls (classes %)))) - (nth-like el #(= k (kind %)))])) + (nth-like el #(= k (kind %))) + l (when l (nth-like el #(= l (label %))))])) -(defn- child [^js parent [tag cls n m]] +(defn- child [^js parent [tag cls n m l i]] (let [kids (array-seq (.-children parent)) k [tag (not-empty (first (some-> cls (.split " "))))]] - (or (nth (filter #(and (= tag (.-localName %)) (= cls (classes %))) kids) n nil) + (or (when l (nth (filter #(= l (label %)) kids) i nil)) + (nth (filter #(and (= tag (.-localName %)) (= cls (classes %))) kids) n nil) (nth (filter #(= k (kind %)) kids) m nil)))) (defn- lineage @@ -94,7 +102,7 @@ ;; drawing them (defonce ^:private shown (atom {})) ; cid → {:msg :found :clip :xy :el} -(defonce ^:private raf (atom nil)) +(defonce ^:private ticking (atom nil)) ; [:frame id] or [:idle id] (defn- layer [] (or (.getElementById js/document "follow-layer") @@ -141,18 +149,36 @@ cx (max l (min r x)) cy (max t (min b y))] {:xy [cx cy] :lost? (< d (count path)) :away? (not= [x y] [cx cy])})) +(declare frame!) + +(defn- tick! + "Draw again: next frame while a pointer is on its way, and four times a + second once they have all arrived — only to notice a layout that moved + under one, a pane opened or a list scrolled, which no message announces." + [moving?] + (reset! ticking (cond (empty? @shown) nil + moving? [:frame (js/requestAnimationFrame frame!)] + :else [:idle (js/setTimeout frame! 250)]))) + (defn- frame! [_] - (doseq [[cid {:keys [found xy ^js el] :as s}] @shown] - (let [s (cond-> s (not (.-isConnected ^js (peek found))) resolved) - {[tx ty] :xy :keys [lost? away?]} (target s) - ;; Halfway there each frame: messages come every 40ms, frames every - ;; 16, and a pointer that jumps between them looks like it stutters. - [x y] (if xy [(+ (xy 0) (* 0.5 (- tx (xy 0)))) (+ (xy 1) (* 0.5 (- ty (xy 1))))] [tx ty])] - (set! (.. el -style -transform) (str "translate(" x "px," y "px)")) - (.toggle (.-classList el) "lost" lost?) - (.toggle (.-classList el) "away" away?) - (swap! shown assoc cid (assoc s :xy [x y])))) - (reset! raf (when (seq @shown) (js/requestAnimationFrame frame!)))) + (tick! + (reduce + (fn [moving? [cid {:keys [found xy ^js el] :as s}]] + (let [s (cond-> s (not (.-isConnected ^js (peek found))) resolved) + {[tx ty] :xy :keys [lost? away?]} (target s) + ;; Halfway there each frame: messages come every 40ms, frames every + ;; 16, and a pointer that jumps between them looks like it stutters. + ;; Within half a pixel, there. + [x y] (if (and xy (< 0.5 (js/Math.hypot (- tx (xy 0)) (- ty (xy 1))))) + [(+ (xy 0) (* 0.5 (- tx (xy 0)))) (+ (xy 1) (* 0.5 (- ty (xy 1))))] + [tx ty])] + (when (not= xy [x y]) + (set! (.. el -style -transform) (str "translate(" x "px," y "px)"))) + (.toggle (.-classList el) "lost" lost?) + (.toggle (.-classList el) "away" away?) + (swap! shown assoc cid (assoc s :xy [x y])) + (or moving? (not= [x y] [tx ty])))) + false @shown))) (defn gone! [cid] (some-> (get-in @shown [cid :el]) .remove) @@ -174,7 +200,10 @@ #js [#js {:transform "scale(.3)" :opacity 1} #js {:transform "scale(1.6)" :opacity 0}] 450)) (swap! shown assoc cid (-> (resolved m) (assoc :msg m :el el :xy (:xy was)))) - (when-not @raf (reset! raf (js/requestAnimationFrame frame!)))))) + (let [[how id] @ticking] + (when-not (= :frame how) + (some-> id js/clearTimeout) + (tick! true)))))) ;; --------------------------------------------------------------------------- ;; scroll diff --git a/frontend/src/arthur/ui/params.cljs b/frontend/src/arthur/ui/params.cljs index 3b75692..473096f 100644 --- a/frontend/src/arthur/ui/params.cljs +++ b/frontend/src/arthur/ui/params.cljs @@ -937,7 +937,7 @@ ;; the open symbol; the open symbol itself is at no path at all. path (if placed (or (nth selection 3 nil) [(second node)]) []) a-face? (and face (face? clip face) (not layer?))] - [:section.pane.params + [:section.pane.params {:aria-label "inspector"} [:div.pane-head "inspector"] [:div.inspect (cond diff --git a/frontend/src/arthur/ui/pool.cljs b/frontend/src/arthur/ui/pool.cljs index 6634186..746eb30 100644 --- a/frontend/src/arthur/ui/pool.cljs +++ b/frontend/src/arthur/ui/pool.cljs @@ -698,7 +698,8 @@ tallies (counts ctx) files? (fn [^js e] (some #{"Files"} (array-seq (.. e -dataTransfer -types))))] [:section.pane.pool - {:class (when (pos? @depth) "dropping") + {:aria-label "media pool" + :class (when (pos? @depth) "dropping") ;; Only a FILE lights the pane up: a row dragged out of the pool is on ;; its way somewhere else. :on-drag-enter (fn [^js e] (when (files? e) (.preventDefault e) (swap! depth inc))) diff --git a/frontend/src/arthur/ui/timeline.cljs b/frontend/src/arthur/ui/timeline.cljs index cfa473b..0d055e1 100644 --- a/frontend/src/arthur/ui/timeline.cljs +++ b/frontend/src/arthur/ui/timeline.cljs @@ -1667,7 +1667,8 @@ (when (not= [clip-id open] @key-context) (reset! key-context [clip-id open]) (reset! key-selection []) (reset! key-preview nil) (reset! key-anchor nil) (reset! row-anchor nil)) [:section.pane.time - {:tab-index -1 + {:aria-label "timeline" + :tab-index -1 :on-key-down (fn [e] (when (and (= (.-target e) (.-currentTarget e)) (#{"Delete" "Backspace"} (.-key e)))