diff --git a/frontend/src/arthur/ui/timeline.cljs b/frontend/src/arthur/ui/timeline.cljs index be6efdd..08fdf0e 100644 --- a/frontend/src/arthur/ui/timeline.cljs +++ b/frontend/src/arthur/ui/timeline.cljs @@ -1304,6 +1304,8 @@ :on-click (when (and select unmapped?) (fn [^js e] (.stopPropagation e) (choose-row! e select))) + :ref (when (and select (not unmapped?)) + (fn [^js el] (when el (aset el "arthurSelect" select)))) :on-pointer-down (when (and select (not unmapped?)) #(begin! % (or slides path) :slide select nil nil))} @@ -1480,6 +1482,29 @@ (when (pos? lo) [:div.tl-outside {:style {:left 0 :width (edge% lo frames)}}]) (when (< hi frames) [:div.tl-outside {:style {:left (edge% hi frames) :right 0}}])])))) +(defn- swept + "The clips and bars inside `tracks` that the box from `[x y]` to `[x2 y2]`, in + client pixels, touches, as their selection addresses with their elements." + [^js tracks x y x2 y2] + (let [l (min x x2) r (max x x2) t (min y y2) b (max y y2)] + (keep (fn [^js el] + (when-let [select (or (some-> (aget el "arthurCel") :select) + (aget el "arthurSelect"))] + (let [box (.getBoundingClientRect el)] + (when (and (< (.-left box) r) (< l (.-right box)) + (< (.-top box) b) (< t (.-bottom box))) + [el select])))) + (array-seq (.querySelectorAll tracks ".tl-cel, .tl-span"))))) + +(defn- mark-swept! + "Show what the box holds by a class on the blocks themselves, not by + selecting: selecting re-lays the pane — an expanded lane follows the + selection — and the rows would move under the box being drawn over them." + [^js tracks els] + (doseq [^js el (array-seq (.querySelectorAll tracks ".swept"))] + (.remove (.-classList el) "swept")) + (doseq [^js el els] (.add (.-classList el) "swept"))) + (defn- key-marquee [marquee] (when-let [{:keys [x y x2 y2]} @marquee] (when (and x2 y2) @@ -1532,7 +1557,13 @@ key-context (atom nil) key-anchor (atom nil) row-anchor (atom nil) - marquee (r/atom nil)] + marquee (r/atom nil) + ;; A box dragged from empty track space over clips and bars: + ;; `{:x :y :base}` pressed, then `:x2 :y2 :hits` once it has moved + ;; far enough to be a drag and not a click. `swept?` swallows the + ;; click a drag ends in, which would otherwise select nothing. + sweep (r/atom nil) + swept? (atom false)] (let [clip-id @(rf/subscribe [::render/clip-id]) clip @(rf/subscribe [::render/clip]) ;; THE RULER IS THE OPEN SYMBOL'S OWN FRAME SPACE. Every span, key and @@ -1642,7 +1673,37 @@ ;; own events; the target check keeps ordinary row/bar clicks local. {:on-click (fn [^js e] (when (= (.-target e) (.-currentTarget e)) - (rf/dispatch [::ui/select nil])))} + (rf/dispatch [::ui/select nil]))) + ;; ON A PHONE ONE FINGER IS THE POINTER AND TWO SCROLL, as on the + ;; stage: one drags a clip, a box or the playhead, and the rows only + ;; move under two, by their middle. Properties, not listeners, since + ;; this runs on every render; on an element, so not passive. + :ref (fn [^js el] + (when el + (let [middle (fn [^js ts] + (let [a (aget ts 0) b (aget ts 1)] + [(/ (+ (.-clientX a) (.-clientX b)) 2) + (/ (+ (.-clientY a) (.-clientY b)) 2)]))] + (set! (.-ontouchstart el) + (fn [^js e] + (when (<= 2 (.. e -touches -length)) + (when @sweep + (reset! sweep nil) + (mark-swept! el [])) + (aset el "arthurTwo" (middle (.-touches e)))))) + (set! (.-ontouchmove el) + (fn [^js e] + (when-let [[x y] (and (<= 2 (.. e -touches -length)) + (aget el "arthurTwo"))] + (.preventDefault e) + (let [[x2 y2 :as m] (middle (.-touches e))] + (set! (.-scrollLeft el) (+ (.-scrollLeft el) (- x x2))) + (set! (.-scrollTop el) (+ (.-scrollTop el) (- y y2))) + (aset el "arthurTwo" m))))) + (set! (.-ontouchend el) + (fn [^js e] + (when (< (.. e -touches -length) 2) + (aset el "arthurTwo" nil)))))))} [:div.tl-labels ;; Empty label space takes a row back out to the top of the open symbol. {:on-click (fn [^js e] @@ -1670,6 +1731,16 @@ [:div.tl-tracks {:on-pointer-down-capture (fn [e] + ;; DRAG TO SELECT, FROM EMPTY SPACE: anywhere a press would + ;; otherwise do nothing. Not yet a box — a press that does not move + ;; is still a click, and a double-click in a lane still makes a + ;; drawing — so nothing is stopped until it moves. + (reset! swept? false) + (when (and (= 0 (.-button e)) (.-isPrimary e) + (not (.closest (.-target e) ".tl-track.automation")) + (not (.closest (.-target e) "button, .tl-span, .tl-edge, .tl-junction, .tl-ruler, input"))) + (reset! sweep {:x (.-clientX e) :y (.-clientY e) + :base (if (or (.-metaKey e) (.-ctrlKey e) (.-shiftKey e)) selections [])})) (when (and (= 0 (.-button e)) (.closest (.-target e) ".tl-track.automation") (not (.closest (.-target e) "button, .movable, .unmapped, .tl-edge"))) @@ -1691,6 +1762,16 @@ (try (.setPointerCapture (.-currentTarget e) (.-pointerId e)) (catch :default _ nil)))) :on-pointer-move (fn [e] + (when-let [{:keys [x y x2]} @sweep] + (let [cx (.-clientX e) cy (.-clientY e) tracks (.-currentTarget e)] + ;; Four pixels, as a drag begins anywhere else in the app. + (when (or x2 (< 4 (js/Math.hypot (- cx x) (- cy y)))) + (.stopPropagation e) + (when-not x2 + (try (.setPointerCapture tracks (.-pointerId e)) (catch :default _ nil))) + (let [hits (swept tracks x y cx cy)] + (mark-swept! tracks (map first hits)) + (swap! sweep assoc :x2 cx :y2 cy :hits (mapv second hits)))))) (when-let [{:keys [x y base hits]} @marquee] (.stopPropagation e) (let [x2 (.-clientX e) y2 (.-clientY e)] @@ -1700,8 +1781,20 @@ (mapcat (fn [{cx :x cy :y :keys [items]}] (when (and (<= (min x x2) cx (max x x2)) (<= (min y y2) cy (max y y2))) items)) hits)))))))) - :on-pointer-up (fn [e] (when @marquee (.stopPropagation e) (reset! marquee nil))) - :on-pointer-cancel (fn [_] (when @marquee (reset! key-selection (:base @marquee)) (reset! marquee nil))) + :on-pointer-up (fn [e] + (when-let [{:keys [x2 base hits]} @sweep] + (reset! sweep nil) + (when x2 + (.stopPropagation e) + (mark-swept! (.-currentTarget e) []) + (reset! swept? true) + (rf/dispatch [::ui/select-many (into (vec base) hits)]))) + (when @marquee (.stopPropagation e) (reset! marquee nil))) + :on-pointer-cancel (fn [e] + (when @sweep + (reset! sweep nil) + (mark-swept! (.-currentTarget e) [])) + (when @marquee (reset! key-selection (:base @marquee)) (reset! marquee nil))) :tab-index 0 :on-key-down (fn [e] @@ -1720,8 +1813,9 @@ (rf/dispatch [::ui/edit-keyframes xs df]) (reset! key-selection (keyframes/shifted xs df)))))))) :on-click (fn [^js e] - (when (= (.-target e) (.-currentTarget e)) - (rf/dispatch [::ui/select nil]))) + (cond + @swept? (reset! swept? false) + (= (.-target e) (.-currentTarget e)) (rf/dispatch [::ui/select nil]))) :on-drag-enter (fn [^js e] (when (drag/accepts?) (.preventDefault e))) :on-drag-over (fn [^js e] (when (drag/accepts?) @@ -1793,7 +1887,8 @@ [:div.tl-grid {:class (when-not fine "coarse")}] [outside-marks frames false] [playhead :div.tl-playhead frames] - [key-marquee marquee]]] + [key-marquee marquee] + [key-marquee sweep]]] [cursor-hint hint]]) (finally (.disconnect sizer)))) diff --git a/static/arthur/app.css b/static/arthur/app.css index 411b682..25d17cb 100644 --- a/static/arthur/app.css +++ b/static/arthur/app.css @@ -1115,6 +1115,9 @@ button.share-button:hover, button.share-button.on { filter: brightness(1.1); } first one, full height — which reads as "the timeline draws nothing" rather than as a positioning bug. */ .tl-track { position: relative; } +/* One finger on the rows is a pointer — a clip, a box, a key — and never a + scroll; two fingers scroll, which `ui/timeline` does itself. */ +.tl-tracks { touch-action: none; } .tl-label { /* Scrolled to from the stage: clear of the sticky ruler above. */ @@ -1289,6 +1292,8 @@ button.share-button:hover, button.share-button.on { filter: brightness(1.1); } .tl-cel.on:hover:not(:disabled) { background: var(--pick); filter: brightness(1.12); } .tl-span.on { background: var(--pick-bg); border-color: var(--pick); } .tl-cel.target, .tl-span.target { outline: 2px solid var(--target); outline-offset: -2px; } +/* Inside a box being dragged over the rows: selected when it is let go. */ +.tl-cel.swept, .tl-span.swept { outline: 2px solid var(--pick); outline-offset: -2px; } .tl-span.on.dense { background: repeating-linear-gradient( -45deg, var(--pick-bg) 0 3px, #f0bf77 3px 6px);