Skip to content

Commit

Permalink
enhance timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc committed Dec 19, 2023
1 parent cd7edf5 commit c197df8
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 56 deletions.
3 changes: 3 additions & 0 deletions resources/public/icons/timeline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion src/renderer/components.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
["@radix-ui/react-context-menu" :as ContextMenu]
["@radix-ui/react-dropdown-menu" :as DropdownMenu]
["@radix-ui/react-switch" :as Switch]
["react-svg" :refer [ReactSVG]]
[clojure.string :as str]
[re-frame.core :as rf]
Expand All @@ -17,6 +18,17 @@
props
[renderer.components/icon icon]])

(defn switch
[{:keys [id label default-checked? on-checked-change]}]
[:span.inline-flex.items-center
[:label.switch-label {:for id} label]
[:> Switch/Root
{:class "switch-root"
:id id
:default-checked default-checked?
:on-checked-change on-checked-change}
[:> Switch/Thumb {:class "switch-thumb"}]]])

(defn shortcuts
[event]
(let [shortcuts @(rf/subscribe [:event-shortcuts event])]
Expand Down Expand Up @@ -111,7 +123,6 @@
[:div.right-slot
[shortcuts action]]]))


(defn dropdown-menu-item
[{:keys [type label action checked?]}]
(case type
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[renderer.document.db]
[renderer.panel.db]
[renderer.theme.db]
[renderer.timeline.db]
[renderer.tree.db]
[renderer.window.db]))

Expand All @@ -24,7 +25,8 @@
[:tree renderer.tree.db/tree]
[:panel [:map-of :key renderer.panel.db/panel]]
[:window renderer.window.db/window]
[:theme [:mode renderer.theme.db/modes]]])
[:theme [:mode renderer.theme.db/modes]]
[:timeline renderer.timeline.db/timeline]])

(def default
{:tool :select
Expand Down Expand Up @@ -63,4 +65,5 @@
:replay? true
:grid-snap? false
:guide-snap? true
:paused? false}})
:paused? false
:speed 1}})
6 changes: 5 additions & 1 deletion src/renderer/statusbar.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@
{:title "XML view"
:active? [:panel/visible? :xml]
:icon "code"
:action [:panel/toggle :xml]}])
:action [:panel/toggle :xml]}
{:title "Timeline"
:active? [:panel/visible? :timeline]
:icon "timeline"
:action [:panel/toggle :timeline]}])

(defn set-zoom
[e v]
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,8 @@ pre {
transform: translateX(19px);
}
}

.switch-label {
@apply h-auto;
background: transparent;
}
9 changes: 9 additions & 0 deletions src/renderer/timeline/db.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns renderer.timeline.db)

(def timeline
[:map
[:time number?]
[:replay? boolean?]
[:grid-snap? boolean?]
[:guide-snap? boolean?]
[:paused? boolean?]])
10 changes: 8 additions & 2 deletions src/renderer/timeline/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

(defn svg-elements
[]
(-> (dom/canvas-element)
(.querySelectorAll "svg")))
(when-let [canvas (dom/canvas-element)]
(.querySelectorAll canvas "svg")))

(rf/reg-fx
::set-current-time
Expand All @@ -18,6 +18,7 @@
(fn []
(doall (map #(.pauseAnimations %) (svg-elements)))))

#_:clj-kondo/ignore
(rf/reg-fx
::unpause-animations
(fn []
Expand Down Expand Up @@ -54,3 +55,8 @@
:timeline/toggle-replay
(fn [db _]
(update-in db [:timeline :replay?] not)))

(rf/reg-event-db
:timeline/set-speed
(fn [db [_ speed]]
(assoc-in db [:timeline :speed] speed)))
5 changes: 5 additions & 0 deletions src/renderer/timeline/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@
:timeline/replay?
(fn [db _]
(-> db :timeline :replay?)))

(rf/reg-sub
:timeline/speed
(fn [db _]
(-> db :timeline :speed)))
136 changes: 87 additions & 49 deletions src/renderer/timeline/views.cljs
Original file line number Diff line number Diff line change
@@ -1,41 +1,77 @@
(ns renderer.timeline.views
(:require
["@radix-ui/react-switch" :as Switch]
["@xzdarcy/react-timeline-editor" :refer [Timeline]]
["@radix-ui/react-select" :as Select]
["react" :as react]
[re-frame.core :as rf]
[reagent.core :as ra]
[renderer.components :as comp]))

(def speed-options
[{:id :0.25
:value 0.25
:label "0.25x"}
{:id :0.5
:value 0.5
:label "0.5x"}
{:id :normal
:value 1
:label "1x"}
{:id :1.5
:value 1.5
:label "1.5x"}
{:id :2
:value 2
:label "2x"}])

(defn speed-select
[editor-ref]
(let [speed @(rf/subscribe [:timeline/speed])]
[:div.inline-flex.items-center
[:label {:style {:height "auto"
:background "transparent"}} "Speed"]
[:> Select/Root {:value speed
:onValueChange #(.setPlayRate (.-current editor-ref) %)}
[:> Select/Trigger
{:class "select-trigger"
:aria-label "No a11y filter"}
[:> Select/Value {:placeholder "Filter"}
[:div.flex.gap-1.justify-between.items-center
{:style {:min-width "50px"}}
[:span (str speed "x")]
[:> Select/Icon {:class "select-icon"}
[comp/icon "chevron-down" {:class "small"}]]]]]
[:> Select/Portal
[:> Select/Content
{:class "menu-content rounded select-content"}
[:> Select/ScrollUpButton {:class "select-scroll-button"}
[comp/icon "chevron-up"]]
[:> Select/Viewport {:class "select-viewport"}
[:> Select/Group
(map (fn [{:keys [id value label]}] ^{:key id}
[:> Select/Item
{:value value
:class "menu-item select-item"}
[:> Select/ItemText label]]) speed-options)]]
[:> Select/ScrollDownButton
{:class "select-scroll-button"}
[comp/icon "chevron-down"]]]]]]))

(defn snap-controls
[]
(let [grid-snap? @(rf/subscribe [:timeline/grid-snap?])
guide-snap? @(rf/subscribe [:timeline/guide-snap?])]
[:<>
[:span.inline-flex.items-center
[:label
{:for "grid-snap"
:style {:background "transparent"
:height "auto"}}
"Grid snap"]
[:> Switch/Root
{:class "switch-root"
:id "grid-snap"
:default-checked grid-snap?
:on-checked-change #(rf/dispatch [:timeline/set-grid-snap %])}
[:> Switch/Thumb {:class "switch-thumb"}]]]
[:span.inline-flex.items-center
[:label
{:for "guide-snap"
:style {:background "transparent"
:height "auto"}}
"Guide snap"]
[:> Switch/Root
{:class "switch-root"
:id "guide-snap"
:default-checked guide-snap?
:on-checked-change #(rf/dispatch [:timeline/set-guide-snap %])}
[:> Switch/Thumb {:class "switch-thumb"}]]]]))
[comp/switch
{:id "grid-snap"
:label "Grid snap"
:default-checked? grid-snap?
:on-checked-change #(rf/dispatch [:timeline/set-grid-snap %])}]
[comp/switch
{:id "guide-snap"
:label "Guide snap"
:default-checked? guide-snap?
:on-checked-change #(rf/dispatch [:timeline/set-guide-snap %])}]]))

(defn toolbar
[editor-ref]
Expand All @@ -47,30 +83,30 @@
timeline? @(rf/subscribe [:panel/visible? :timeline])]
[:div.toolbar.level-1.mb-px
[:div.flex-1.flex
[comp/icon-button "go-to-start" {:on-click #(.setTime (.-current editor-ref) 0)
:disabled (zero? time)}]
(if paused?
[comp/icon-button "play" {:on-click #(.play (.-current editor-ref) #js {:autoEnd true})}]
[comp/icon-button "pause" {:on-click #(.pause (.-current editor-ref))}])
[comp/icon-button "go-to-end" {:on-click #(.setTime (.-current editor-ref) end)
:disabled (>= time end)}]
[comp/icon-button "go-to-start"
{:on-click #(.setTime (.-current editor-ref) 0)
:disabled (zero? time)}]
[comp/radio-icon-button
{:title (if paused? "Play" "Pause")
:active? (not paused?)
:icon (if paused? "play" "pause")
:action #(if paused?
(.play (.-current editor-ref) #js {:autoEnd true})
(.pause (.-current editor-ref)))}]
[comp/icon-button "go-to-end"
{:on-click #(.setTime (.-current editor-ref) end)
:disabled (>= time end)}]
[comp/radio-icon-button
{:title "Replay"
:active? replay?
:icon "refresh"
:action #(rf/dispatch [:timeline/toggle-replay])}]
[speed-select editor-ref]
[:span.p-2.font-mono time-formatted]
(when timeline?
[:<>
[:span.v-divider]
[snap-controls]])]
[comp/toggle-icon-button
{:active? (not timeline?)
:active-icon "chevron-up"
:active-text "Show timeline"
:inactive-icon "times"
:inactive-text "Hide timeline"
:action #(rf/dispatch [:panel/toggle :timeline])}]]))
[snap-controls]])]]))

(defn root
[]
Expand All @@ -88,7 +124,8 @@
(.setTime (.-current ref) 0)
(.play (.-current ref) #js {:autoEnd true}))]
["afterSetTime" #(rf/dispatch-sync [:timeline/set-time (.-time %)])]
["setTimeByTick" #(rf/dispatch-sync [:timeline/set-time (.-time %)])]]]
["setTimeByTick" #(rf/dispatch-sync [:timeline/set-time (.-time %)])]
["afterSetPlayRate" #(rf/dispatch [:timeline/set-speed (.-rate %)])]]]
(.on (.-listener (.-current ref)) e f)))

:component-will-unmount
Expand All @@ -103,11 +140,12 @@
timeline? @(rf/subscribe [:panel/visible? :timeline])]
[:div
[toolbar ref]
[:> Timeline {:style {:height (if timeline? "200px" 0)}
:editor-data data
:effects effects
:ref ref
:grid-snap grid-snap?
:drag-line guide-snap?
:auto-scroll true
:on-click-action #(rf/dispatch [:element/select (keyword (.. %2 -action -id))])}]]))})))
[:> Timeline
{:style {:height (if timeline? "200px" 0)}
:editor-data data
:effects effects
:ref ref
:grid-snap grid-snap?
:drag-line guide-snap?
:auto-scroll true
:on-click-action #(rf/dispatch [:element/select (keyword (.. %2 -action -id))])}]]))})))
2 changes: 1 addition & 1 deletion src/renderer/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
{:style {:flex "0 1 30%"}}])]
[status-bar/root]
[history/tree]]
[timeline/root]
(when @(rf/subscribe [:panel/visible? :timeline]) [timeline/root])
[command-input]]]))

(defn main-panel
Expand Down

0 comments on commit c197df8

Please sign in to comment.