diff --git a/frontend/src/arthur/core.cljs b/frontend/src/arthur/core.cljs index cd61510..af33ac6 100644 --- a/frontend/src/arthur/core.cljs +++ b/frontend/src/arthur/core.cljs @@ -17,7 +17,7 @@ [arthur.ui.index :as index] [arthur.ui.player :as player] [arthur.ui.shell :as shell] - [arthur.ui.tools :as tools] + [arthur.ui.shortcuts :as shortcuts] [re-frame.core :as rf] [reagent.dom.client :as rdc])) @@ -46,8 +46,7 @@ ;; After the blank document, so an address that names a project opens it over ;; the blank one, and the blank one is what a bad address leaves on screen. (collab/start!) - (history/install-keys!) - (tools/install-keys!) + (shortcuts/install!) (reset! root (rdc/create-root (js/document.getElementById "app"))) (mount) (player/start!)) diff --git a/frontend/src/arthur/events/history.cljs b/frontend/src/arthur/events/history.cljs index d45ddc7..d5f56e6 100644 --- a/frontend/src/arthur/events/history.cljs +++ b/frontend/src/arthur/events/history.cljs @@ -71,28 +71,3 @@ (:paint/revision db) (history/steps (:history (store/entry (:clip/current db)))))) -(defn- typing? [^js target] - (or (#{"INPUT" "TEXTAREA" "SELECT"} (.-tagName target)) (.-isContentEditable target))) - -(defn install-keys! - "The document edit keys. Not while typing in a field, where the browser's own - clipboard and undo stack are the ones wanted." - [] - (.addEventListener - js/window "keydown" - (fn [^js e] - (when-not (typing? (.-target e)) - (let [k (.toLowerCase (.-key e))] - (when-let [ev (if (and (or (.-metaKey e) (.-ctrlKey e)) - (not (.-altKey e))) - (cond (and (= k "z") (.-shiftKey e)) ::redo - (= k "z") ::undo - (= k "y") ::redo - (= k "c") ::ui/copy - (= k "x") ::ui/cut - (= k "v") ::ui/paste - (and (= k "d") (.-shiftKey e)) ::ui/duplicate-unique - (= k "d") ::ui/duplicate) - (when (#{"delete" "backspace"} k) ::ui/delete-selected))] - (.preventDefault e) - (rf/dispatch [ev]))))))) diff --git a/frontend/src/arthur/events/ui.cljs b/frontend/src/arthur/events/ui.cljs index 6474729..373bccb 100644 --- a/frontend/src/arthur/events/ui.cljs +++ b/frontend/src/arthur/events/ui.cljs @@ -562,8 +562,6 @@ ::hover (fn [db [_ p]] (if p (assoc-in db [:ui :hover] p) (update db :ui dissoc :hover)))) -(def tool-keys #{"v" "p" "b" "e" "[" "]" "enter" "escape"}) - (rf/reg-event-fx ::tool-key ;; A key from `ui/tools`, decided here against the state it changes, and diff --git a/frontend/src/arthur/ui/shell.cljs b/frontend/src/arthur/ui/shell.cljs index f96bf99..20015d7 100644 --- a/frontend/src/arthur/ui/shell.cljs +++ b/frontend/src/arthur/ui/shell.cljs @@ -16,6 +16,7 @@ [arthur.ui.location :as location] [arthur.ui.params :as params] [arthur.ui.pool :as pool] + [arthur.ui.shortcuts :as shortcuts] [arthur.ui.stage :as stage] [arthur.ui.tabs :as tabs] [arthur.ui.timeline :as timeline] @@ -70,6 +71,7 @@ gone? #(contains? shut %)] [:div.app {:style (layout/vars state)} [topbar/view] + [shortcuts/sheet] (when-not (gone? :pool) [pool/view]) (when-not (gone? :pool) [layout/grip :pool pool :col 1]) [:section.view diff --git a/frontend/src/arthur/ui/shortcuts.cljs b/frontend/src/arthur/ui/shortcuts.cljs new file mode 100644 index 0000000..d974a76 --- /dev/null +++ b/frontend/src/arthur/ui/shortcuts.cljs @@ -0,0 +1,181 @@ +(ns arthur.ui.shortcuts + "Every key the window answers to, as one table, and the sheet that lists them. + + A COMMAND IS DATA: what it is called, which keys reach it, and a function of + the db that answers the event to send, or nil for nothing to do. One listener + reads the table, the `?` sheet prints it, and nothing else in the app listens + to the window for keys. What a key does in one element only — nudging the + timeline's selected keys, Enter in a rename field — stays on that element, + which handles it first and stops it, as Blender's modal operators do. + + NO MODES. A key means one thing everywhere and what it acts on is the + selection, so there is no state to remember besides the tool. Space is the one + key with two meanings, and they are told apart by the hand rather than by a + mode: tapped it plays, held and dragged on the stage it pans, as in Photoshop. + + THE KEYS ARE BLENDER'S, then Resolve's, then Flash's. WASD repeats the arrows + for a hand that stays on the left of the keyboard while the other holds the + pen. Letters are matched by where they are on the keyboard, not by what they + type, so WASD is WASD on any layout and ⌥O is O on a Mac too." + (:require [arthur.events.history :as history] + [arthur.events.playback :as pb] + [arthur.events.ui :as ui] + [arthur.footage.store :as store] + [arthur.ui.layout :as layout] + [arthur.ui.timeline :as timeline] + [clojure.string :as str] + [re-frame.core :as rf] + [re-frame.db :as db] + [reagent.core :as r])) + +(defn stops + "The frames ↑ and ↓ stop on: every key and every clip edge the timeline shows, + as frames of the open symbol. In a lane that is drawing to drawing, on a tween + key to key — one rule, read off the rows the timeline draws, so a jump lands + on something you can see." + [db] + (let [{document :clip} (store/entry (:clip/current db)) + selection (get-in db [:ui :selection]) + n (pb/frames db)] + (into (sorted-set) + (comp (mapcat (fn [{:keys [keys span cels]}] + (concat keys span (mapcat (juxt :span :keys) cels)))) + (mapcat #(if (number? %) [%] %)) + ;; The first whole frame that shows it, forgiving the rounding + ;; a mapped key picks up on its way through a clock. + (map #(js/Math.ceil (- % 1e-6))) + (filter #(< -1 % n))) + (when document + (timeline/rows document (get-in db [:ui :open]) (get-in db [:ui :expanded]) + (when (= :node (first selection)) (nth selection 3))))))) + +(defn- frame [db] (get-in db [:playback :frame] 0)) +(defn- seek [f] [::pb/seek f]) + +(def commands + [["Time" + [{:label "Play / pause" :keys ["Space"] :run (constantly [::pb/toggle]) + :note "hold and drag on the stage to pan"} + {:label "Previous frame" :keys ["ArrowLeft" "a"] :run #(seek (dec (frame %)))} + {:label "Next frame" :keys ["ArrowRight" "d"] :run #(seek (inc (frame %)))} + {:label "Previous key" :keys ["ArrowDown" "s"] + :run #(some-> (first (rsubseq (stops %) < (frame %))) seek)} + {:label "Next key" :keys ["ArrowUp" "w"] + :run #(some-> (first (subseq (stops %) > (frame %))) seek)} + {:label "First frame" :keys ["Home" "shift+ArrowLeft" "shift+a"] :run (constantly (seek 0))} + {:label "Last frame" :keys ["End" "shift+ArrowRight" "shift+d"] :run #(seek (dec (pb/frames %)))} + {:label "Onion skin" :keys ["alt+o"] + :run #(let [on? (get-in % [:ui :onion (:clip/current %) (get-in % [:ui :open]) :on?])] + [::layout/onion :on? (not on?)])}]] + ["Draw" + [{:label "Select" :keys ["v"] :run (constantly [::ui/tool-key "v"])} + {:label "Pen" :keys ["p"] :run (constantly [::ui/tool-key "p"])} + {:label "Brush" :keys ["b"] :run (constantly [::ui/tool-key "b"])} + {:label "Eraser" :keys ["e"] :run (constantly [::ui/tool-key "e"])} + {:label "Smaller brush" :keys ["["] :run (constantly [::ui/tool-key "["])} + {:label "Bigger brush" :keys ["]"] :run (constantly [::ui/tool-key "]"])} + {:label "Close the pen's shape" :keys ["Enter"] :run (constantly [::ui/tool-key "enter"])} + {:label "Leave the pen" :keys ["Escape"] :run (constantly [::ui/tool-key "escape"])}]] + ["Edit" + [{:label "Undo" :keys ["mod+z"] :run (constantly [::history/undo])} + {:label "Redo" :keys ["mod+shift+z" "mod+y"] :run (constantly [::history/redo])} + {:label "Copy" :keys ["mod+c"] :run (constantly [::ui/copy])} + {:label "Cut" :keys ["mod+x"] :run (constantly [::ui/cut])} + {:label "Paste" :keys ["mod+v"] :run (constantly [::ui/paste])} + {:label "Duplicate" :keys ["mod+d"] :run (constantly [::ui/duplicate])} + {:label "Duplicate as new symbol" :keys ["mod+shift+d"] :run (constantly [::ui/duplicate-unique])} + {:label "Delete" :keys ["Delete" "Backspace"] :run (constantly [::ui/delete-selected])}]] + ["Help" + [{:label "These shortcuts" :keys ["?"]}]]]) + +(def ^:private by-chord + (into {} (for [[_ cs] commands c cs k (:keys c)] [k c]))) + +(defn- chord + "The key as the table spells it: `mod+` for ⌘ or Ctrl, then `alt+`, then + `shift+` — except on a character that shift itself typed, like `?`." + [^js e] + (let [code (.-code e) + letter (when (str/starts-with? code "Key") (str/lower-case (subs code 3))) + k (cond letter letter (= "Space" code) "Space" :else (.-key e))] + (str (when (or (.-metaKey e) (.-ctrlKey e)) "mod+") + (when (.-altKey e) "alt+") + (when (and (.-shiftKey e) (or letter (< 1 (count k)))) "shift+") + k))) + +(defn- typing? + "Is a key going into a field? A slider or a checkbox has focus without taking + letters, so the window's keys still work there, as a tool's key does in + Photoshop." + [^js target] + (or (#{"TEXTAREA" "SELECT"} (.-tagName target)) + (and (= "INPUT" (.-tagName target)) + (not (#{"range" "checkbox" "radio" "button" "color"} (.-type target)))) + (.-isContentEditable target))) + +(defn- press! + "Send what key `k` answers now, against the db as it is. + + SYNCHRONOUSLY, so a held arrow counts every repeat. Dispatched instead, two + presses in quick succession both read the frame before either had moved it, + and D D went one frame." + [k] + (when-let [ev (some-> (get-in by-chord [k :run]) (#(% @db/app-db)))] + (rf/dispatch-sync ev))) + +;; Nil, :held, or :panned once a drag on the stage has used the hold — see +;; `ui/stage`. A tap is a hold that was never used. +(defonce space (atom nil)) +(defonce ^:private open? (r/atom false)) + +(defn- on-key-down [^js e] + (when-not (typing? (.-target e)) + (let [k (chord e)] + (cond + (and @open? (#{"Escape" "?"} k)) (do (.preventDefault e) (reset! open? false)) + (= "?" k) (do (.preventDefault e) (reset! open? true)) + ;; Played on the way UP, because only then is it known to be a tap. + ;; Default prevented both ways, or a focused button clicks as well. + (= "Space" k) (do (.preventDefault e) + (when-not (.-repeat e) (reset! space :held))) + (by-chord k) (do (.preventDefault e) (press! k)))))) + +(defn- on-key-up [^js e] + (when (= "Space" (.-code e)) + (when (= :held @space) + (.preventDefault e) + (press! "Space")) + (reset! space nil))) + +(defn install! [] + (.addEventListener js/window "keydown" on-key-down) + (.addEventListener js/window "keyup" on-key-up) + (.addEventListener js/window "blur" #(reset! space nil))) + +(def ^:private glyphs + {"mod" "⌘" "alt" "⌥" "shift" "⇧" "ArrowLeft" "←" "ArrowRight" "→" + "ArrowUp" "↑" "ArrowDown" "↓" "Backspace" "⌫" "Escape" "Esc"}) + +(defn- spell [k] + (apply str (map #(or (glyphs %) (str/upper-case %)) (str/split k #"\+")))) + +(defn sheet + "The `?` sheet. Shut by Esc, by `?` again, or by a click anywhere off it." + [] + (when @open? + [:<> + [:div.keys-scrim {:on-click #(reset! open? false)}] + [:section.keys-sheet {:role "dialog" :aria-modal true :aria-label "Keyboard shortcuts"} + (for [[title cs] commands] + ^{:key title} + [:div.keys-group + [:h3 title] + (for [{:keys [label note] ks :keys} cs] + ^{:key label} + [:div.keys-row + [:span label (when note [:small note])] + [:span.keys (for [k ks] ^{:key k} [:kbd (spell k)])]])])]])) + +(defn button [] + [:button {:title "keyboard shortcuts (?)" :aria-label "keyboard shortcuts" + :on-click #(reset! open? true)} "?"]) diff --git a/frontend/src/arthur/ui/stage.cljs b/frontend/src/arthur/ui/stage.cljs index 0575312..ea11433 100644 --- a/frontend/src/arthur/ui/stage.cljs +++ b/frontend/src/arthur/ui/stage.cljs @@ -28,6 +28,7 @@ [arthur.ui.layout :as layout] [arthur.ui.player :as player] [arthur.ui.tracing :as tracing] + [arthur.ui.shortcuts :as shortcuts] [arthur.ui.tools :as tools] [arthur.ui.timeline :as timeline] [re-frame.core :as rf] @@ -758,24 +759,10 @@ (when pen? [draft-lines draft hover]) (when paints? [cursor tool])])) -(defonce space-held? (atom false)) (defonce pan (atom nil)) -(defn- stage-key-target? [target] - (and (.closest target ".stage-area") - (not (.closest target "input, textarea, select, button, a, summary, [role='button']")) - (not (.-isContentEditable target)))) - -(defonce navigation-keys +(defonce navigation-blur (when (exists? js/window) - (.addEventListener js/window "keydown" - (fn [e] - (when (and (= "Space" (.-code e)) - (not (or (.-ctrlKey e) (.-metaKey e) (.-altKey e))) - (stage-key-target? (.-target e))) - (.preventDefault e) - (reset! space-held? true)))) - (.addEventListener js/window "keyup" #(when (= "Space" (.-code %)) (reset! space-held? false))) - (.addEventListener js/window "blur" #(do (reset! space-held? false) (reset! pan nil))))) + (.addEventListener js/window "blur" #(reset! pan nil)))) (defonce pinch-start-zoom (atom nil)) @@ -817,8 +804,10 @@ (fn [e] (.preventDefault e) (reset! pinch-start-zoom nil)))) nil) :on-pointer-down-capture - (fn [e] (when (or (= 1 (.-button e)) (and (= 0 (.-button e)) @space-held?)) + (fn [e] (when (or (= 1 (.-button e)) (and (= 0 (.-button e)) @shortcuts/space)) (.preventDefault e) (.stopPropagation e) + ;; Space held and used to pan, so letting go does not also play. + (when @shortcuts/space (reset! shortcuts/space :panned)) (let [el (.-currentTarget e)] (reset! pan [(.-clientX e) (.-clientY e) (.-scrollLeft el) (.-scrollTop el)]) (.setPointerCapture el (.-pointerId e))))) diff --git a/frontend/src/arthur/ui/timeline.cljs b/frontend/src/arthur/ui/timeline.cljs index 76c3f11..7583a1d 100644 --- a/frontend/src/arthur/ui/timeline.cljs +++ b/frontend/src/arthur/ui/timeline.cljs @@ -542,15 +542,15 @@ ;; Where the playhead is and how it moves. Nothing here depends on a ;; selection, which is why it is the one group that is never greyed. [:div.group - [:button.ico {:aria-label "go to start" :title "go to the first frame" + [:button.ico {:aria-label "go to start" :title "go to the first frame (Home)" :on-click (act [::pb/seek 0])} [icon/view :start]] - [:button {:aria-label "−1" :title "step back one frame" + [:button {:aria-label "−1" :title "step back one frame (← or A)" :on-click (act [::pb/step -1])} "−1"] [:button.ico.ico-play {:aria-label (if playing? "pause" "play") - :title (if playing? "pause" "play") + :title (if playing? "pause (Space)" "play (Space)") :on-click (act [::pb/toggle])} [icon/view (if playing? :pause :play)]] - [:button {:aria-label "+1" :title "step forward one frame" + [:button {:aria-label "+1" :title "step forward one frame (→ or D)" :on-click (act [::pb/step 1])} "+1"]] [:div.group [:button.ico {:class (when loop? "on") :aria-label "loop" diff --git a/frontend/src/arthur/ui/tools.cljs b/frontend/src/arthur/ui/tools.cljs index 2042177..f4823ce 100644 --- a/frontend/src/arthur/ui/tools.cljs +++ b/frontend/src/arthur/ui/tools.cljs @@ -109,24 +109,3 @@ (when (and (= :brush kind) (< 1 (count ids))) [:span.dim (str (count ids) " pieces")]) [fit-control fit ::ui/adjust-last]])) -(defn- typing? - "Is a key going into a field? A slider or a checkbox has focus without - taking letters, and a tool's key still picks the tool there, as in Photoshop." - [^js target] - (or (#{"TEXTAREA" "SELECT"} (.-tagName target)) - (and (= "INPUT" (.-tagName target)) - (not (#{"range" "checkbox" "radio" "button" "color"} (.-type target)))) - (.-isContentEditable target))) - -(defn install-keys! - "V, P, B and E pick a tool. Enter closes the pen's polygon, or takes the pen - to the selected shape's points; Esc closes it too, and then leaves the pen. - [ and ] size the brush. Delete and undo are `events/history`'s." - [] - (.addEventListener - js/window "keydown" - (fn [^js e] - (when-not (or (typing? (.-target e)) (.-metaKey e) (.-ctrlKey e) (.-altKey e)) - (when (contains? ui/tool-keys (.toLowerCase (.-key e))) - (.preventDefault e) - (rf/dispatch [::ui/tool-key (.toLowerCase (.-key e))])))))) diff --git a/frontend/src/arthur/ui/topbar.cljs b/frontend/src/arthur/ui/topbar.cljs index 0d46a8f..f7510e4 100644 --- a/frontend/src/arthur/ui/topbar.cljs +++ b/frontend/src/arthur/ui/topbar.cljs @@ -16,6 +16,7 @@ [arthur.ui.openmenu :as openmenu] [arthur.ui.settings :as settings] [arthur.ui.share :as share] + [arthur.ui.shortcuts :as shortcuts] [arthur.ui.snapshots :as snapshots] [arthur.ui.undo :as undo] [re-frame.core :as rf] @@ -115,4 +116,5 @@ title (when project-seq (str " r" project-seq))]) [:span.status (or export-status status footage-status)] [exporter export-open?] - [share/view]]))) + [share/view] + [shortcuts/button]]))) diff --git a/frontend/test/arthur/ui/shortcuts_test.cljs b/frontend/test/arthur/ui/shortcuts_test.cljs new file mode 100644 index 0000000..9eeb46e --- /dev/null +++ b/frontend/test/arthur/ui/shortcuts_test.cljs @@ -0,0 +1,13 @@ +(ns arthur.ui.shortcuts-test + (:require [cljs.test :refer [deftest is]] + [arthur.domain.sequence-test :as fixture] + [arthur.footage.store :as store] + [arthur.ui.shortcuts :as shortcuts])) + +(deftest the-key-jumps-stop-on-every-cel-edge-and-key-the-timeline-shows + ;; Cels at 0, 4 and 8; `:b` keyed on its own frames 0 and 1, which it shows + ;; at 4 and 5; the plate keyed at 0 and 11. The lane's end, 12, is past the + ;; last frame and is not somewhere to stand. + (let [id (store/install! {:clip (fixture/document) :store {}} "shortcut-stops")] + (is (= [0 4 5 8 11] + (vec (shortcuts/stops {:clip/current id :ui {:open :main}})))))) diff --git a/static/arthur/app.css b/static/arthur/app.css index 9a09734..459866e 100644 --- a/static/arthur/app.css +++ b/static/arthur/app.css @@ -1932,3 +1932,24 @@ button.tl-key.selected { background: var(--pick); box-shadow: 0 0 0 2px var(--pi .choice { display: grid; gap: 6px; margin: 12px 0; } .choice button { display: grid; gap: 2px; text-align: left; padding: 8px; white-space: normal; height: auto; } .choice button .dim { font-size: 11px; } + +/* -------------------------------------------------------------------------- + the keyboard sheet, `?` */ + +.keys-scrim { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .45); } +.keys-sheet { + position: fixed; z-index: 51; top: 50%; left: 50%; transform: translate(-50%, -50%); + width: min(760px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow: auto; + padding: 16px 20px; columns: 2 300px; column-gap: 32px; + background: var(--pane); border: 1px solid var(--line); border-radius: 3px; + box-shadow: 0 8px 30px rgba(0, 0, 0, .45); +} +.keys-group { break-inside: avoid; margin-bottom: 14px; } +.keys-group h3 { margin: 0 0 6px; font: inherit; font-weight: 600; color: var(--dim); } +.keys-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 2px 0; } +.keys-row small { display: block; color: var(--dim); } +.keys-row .keys { display: flex; gap: 4px; flex-shrink: 0; } +.keys-row kbd { + min-width: 1.6em; padding: 0 5px; text-align: center; font: inherit; font-size: .92em; + background: var(--chrome); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 3px; +}