Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): integrate audio tracks in timeline UI

Richie McIlroyba99a2d95765
diff --git a/apps/desktop/src/routes/editor/Timeline/index.tsx b/apps/desktop/src/routes/editor/Timeline/index.tsx
index e24007e2b..717102734 100644
--- a/apps/desktop/src/routes/editor/Timeline/index.tsx
+++ b/apps/desktop/src/routes/editor/Timeline/index.tsx
@@ -22,10 +22,10 @@ import toast from "solid-toast";
import "./styles.css";
-import Tooltip from "~/components/Tooltip";
import { defaultCaptionSettings } from "~/store/captions";
import { defaultKeyboardSettings } from "~/store/keyboard";
import { commands } from "~/utils/tauri";
+import type { AudioTrackSegment } from "../audio";
import {
applyCaptionResultToProject,
getCaptionGenerationErrorMessage,
@@ -37,6 +37,7 @@ import type { MaskSegment } from "../masks";
import type { TextSegment } from "../text";
import { getTrackRowsWithCount, getUsedTrackCount } from "../timelineTracks";
import { formatTime } from "../utils";
+import { type AudioSegmentDragState, AudioTrack } from "./AudioTrack";
import { type CaptionSegmentDragState, CaptionsTrack } from "./CaptionsTrack";
import { ClipTrack } from "./ClipTrack";
import { TimelineContextProvider, useTimelineContext } from "./context";
@@ -62,6 +63,7 @@ const trackIcons: Record<TimelineTrackType, () => JSX.Element> = {
mask: () => <IconLucideBoxSelect class="size-4" />,
zoom: () => <IconLucideSearch class="size-4" />,
scene: () => <IconLucideVideo class="size-4" />,
+ audio: () => <IconLucideMusic class="size-4" />,
};
type TrackDefinition = {
@@ -102,6 +104,12 @@ const trackDefinitions: TrackDefinition[] = [
icon: trackIcons.mask,
locked: false,
},
+ {
+ type: "audio",
+ label: "Audio",
+ icon: trackIcons.audio,
+ locked: false,
+ },
{
type: "zoom",
label: "Zoom",
@@ -160,6 +168,13 @@ export function Timeline(props: {
const secsPerPixel = () => transform().zoom / (timelineBounds.width ?? 1);
+ const openAudioPicker = (laneIndex: number) => {
+ batch(() => {
+ setEditorState("timeline", "selection", null);
+ setEditorState("timeline", "audioPicker", laneIndex);
+ });
+ };
+
const trackState = () => editorState.timeline.tracks;
const sceneAvailable = () => meta().hasCamera && !project.camera.hide;
const captionTrackVisible = () => trackState().caption;
@@ -178,10 +193,22 @@ export function Timeline(props: {
? trackState().mask > 0
: definition.type === "text"
? trackState().text > 0
- : true,
+ : definition.type === "audio"
+ ? trackState().audio > 0
+ : true,
available: definition.type === "scene" ? sceneAvailable() : true,
supportsMultiple:
- definition.type === "mask" || definition.type === "text",
+ definition.type === "mask" ||
+ definition.type === "text" ||
+ definition.type === "audio",
+ count:
+ definition.type === "mask"
+ ? trackState().mask
+ : definition.type === "text"
+ ? trackState().text
+ : definition.type === "audio"
+ ? trackState().audio
+ : 0,
})),
);
const sceneTrackVisible = () => trackState().scene && sceneAvailable();
@@ -197,6 +224,12 @@ export function Timeline(props: {
trackState().mask,
),
);
+ const audioTrackRows = createMemo(() =>
+ getTrackRowsWithCount(
+ project.timeline?.audioSegments ?? [],
+ trackState().audio,
+ ),
+ );
const visibleTrackCount = createMemo(
() =>
2 +
@@ -204,6 +237,7 @@ export function Timeline(props: {
(keyboardTrackVisible() ? 1 : 0) +
textTrackRows().length +
maskTrackRows().length +
+ audioTrackRows().length +
(sceneTrackVisible() ? 1 : 0),
);
const trackHeight = createMemo(() =>
@@ -312,10 +346,18 @@ export function Timeline(props: {
if (type === "mask") {
setEditorState("timeline", "tracks", "mask", trackState().mask + 1);
+ return;
+ }
+
+ if (type === "audio") {
+ setEditorState("timeline", "tracks", "audio", trackState().audio + 1);
}
}
- function handleDeleteTrackLane(type: "text" | "mask", laneIndex: number) {
+ function handleDeleteTrackLane(
+ type: "text" | "mask" | "audio",
+ laneIndex: number,
+ ) {
const resumeHistory = projectHistory.pause();
const currentTrackCount = trackState()[type];
const nextTextSegments =
@@ -332,13 +374,20 @@ export function Timeline(props: {
laneIndex,
)
: null;
- const nextTrackCount = Math.max(
+ const nextAudioSegments =
+ type === "audio"
+ ? deleteTrackLane<AudioTrackSegment>(
+ project.timeline?.audioSegments ?? [],
+ laneIndex,
+ )
+ : null;
+ const usedTrackCount =
type === "text"
? getUsedTrackCount(nextTextSegments ?? [])
- : getUsedTrackCount(nextMaskSegments ?? []),
- currentTrackCount - 1,
- 0,
- );
+ : type === "mask"
+ ? getUsedTrackCount(nextMaskSegments ?? [])
+ : getUsedTrackCount(nextAudioSegments ?? []);
+ const nextTrackCount = Math.max(usedTrackCount, currentTrackCount - 1, 0);
batch(() => {
if (editorState.timeline.selection?.type === type) {
@@ -352,8 +401,10 @@ export function Timeline(props: {
if (type === "text" && nextTextSegments) {
timeline.textSegments = nextTextSegments;
- } else if (nextMaskSegments) {
+ } else if (type === "mask" && nextMaskSegments) {
timeline.maskSegments = nextMaskSegments;
+ } else if (nextAudioSegments) {
+ timeline.audioSegments = nextAudioSegments;
}
}),
);
@@ -426,7 +477,7 @@ export function Timeline(props: {
async function handleOpenTrackMenu(
e: MouseEvent,
- type: "text" | "mask",
+ type: "text" | "mask" | "audio",
laneIndex: number,
) {
e.preventDefault();
@@ -435,7 +486,7 @@ export function Timeline(props: {
const menu = await Menu.new({
items: [
await MenuItem.new({
- text: `Delete ${type === "text" ? "text" : "mask"} track`,
+ text: `Delete ${type} track`,
action: () => handleDeleteTrackLane(type, laneIndex),
}),
],
@@ -519,6 +570,7 @@ export function Timeline(props: {
let sceneSegmentDragState = { type: "idle" } as SceneSegmentDragState;
let maskSegmentDragState = { type: "idle" } as MaskSegmentDragState;
let textSegmentDragState = { type: "idle" } as TextSegmentDragState;
+ let audioSegmentDragState = { type: "idle" } as AudioSegmentDragState;
let captionSegmentDragState = { type: "idle" } as CaptionSegmentDragState;
let keyboardSegmentDragState = { type: "idle" } as KeyboardSegmentDragState;
@@ -596,6 +648,7 @@ export function Timeline(props: {
sceneSegmentDragState.type !== "moving" &&
maskSegmentDragState.type !== "moving" &&
textSegmentDragState.type !== "moving" &&
+ audioSegmentDragState.type !== "moving" &&
captionSegmentDragState.type !== "moving" &&
keyboardSegmentDragState.type !== "moving"
) {
@@ -654,6 +707,8 @@ export function Timeline(props: {
projectActions.deleteMaskSegments(selection.indices);
} else if (selection.type === "text") {
projectActions.deleteTextSegments(selection.indices);
+ } else if (selection.type === "audio") {
+ projectActions.deleteAudioSegments(selection.indices);
} else if (selection.type === "clip") {
// Delete all selected clips in reverse order
[...selection.indices]
@@ -678,6 +733,7 @@ export function Timeline(props: {
} else if (e.code === "Escape" && hasNoModifiers) {
// Deselect all selected segments
setEditorState("timeline", "selection", null);
+ setEditorState("timeline", "audioPicker", null);
}
});
@@ -790,6 +846,7 @@ export function Timeline(props: {
handleUpdatePlayhead(e);
if (zoomSegmentDragState.type === "idle") {
setEditorState("timeline", "selection", null);
+ setEditorState("timeline", "audioPicker", null);
}
});
createEventListener(window, "mouseup", () => {
@@ -842,14 +899,15 @@ export function Timeline(props: {
<div class="absolute inset-0 flex items-end">
<TimelineMarkings />
</div>
- <div class="absolute bottom-0 z-30">
- <Tooltip content="Add track">
- <TrackManager
- options={trackOptions()}
- onToggle={handleToggleTrack}
- onAdd={handleAddTrack}
- />
- </Tooltip>
+ <div
+ class="absolute bottom-0 left-0 z-30"
+ style={{ width: `${TRACK_ICON_WIDTH}px` }}
+ >
+ <TrackManager
+ options={trackOptions()}
+ onToggle={handleToggleTrack}
+ onAdd={handleAddTrack}
+ />
</div>
</div>
<Show when={!editorState.playing && editorState.previewTime}>
@@ -910,7 +968,7 @@ export function Timeline(props: {
}}
>
<div class="flex flex-col gap-2 min-h-full">
- <TrackRow icon={trackIcons.clip} label="Video">
+ <TrackRow icon={trackIcons.clip} label="Video" type="clip">
<ClipTrack
ref={setTimelineRef}
handleUpdatePlayhead={handleUpdatePlayhead}
@@ -920,6 +978,7 @@ export function Timeline(props: {
<TrackRow
icon={trackIcons.caption}
label="Captions"
+ type="caption"
onDelete={() => handleDeleteSingleTrack("caption")}
>
<CaptionsTrack
@@ -936,6 +995,7 @@ export function Timeline(props: {
<TrackRow
icon={trackIcons.keyboard}
label="Keyboard"
+ type="keyboard"
onDelete={() => handleDeleteSingleTrack("keyboard")}
>
<KeyboardTrack
@@ -951,6 +1011,7 @@ export function Timeline(props: {
<TrackRow
icon={trackIcons.text}
label="Text"
+ type="text"
onDelete={() => handleDeleteTrackLane("text", laneIndex)}
onContextMenu={(e) =>
handleOpenTrackMenu(e, "text", laneIndex)
@@ -971,6 +1032,7 @@ export function Timeline(props: {
<TrackRow
icon={trackIcons.mask}
label="Mask"
+ type="mask"
onDelete={() => handleDeleteTrackLane("mask", laneIndex)}
onContextMenu={(e) =>
handleOpenTrackMenu(e, "mask", laneIndex)
@@ -986,7 +1048,29 @@ export function Timeline(props: {
</TrackRow>
)}
</For>
- <TrackRow icon={trackIcons.zoom} label="Zoom">
+ <For each={audioTrackRows()}>
+ {(laneIndex) => (
+ <TrackRow
+ icon={trackIcons.audio}
+ label="Audio"
+ type="audio"
+ onDelete={() => handleDeleteTrackLane("audio", laneIndex)}
+ onContextMenu={(e) =>
+ handleOpenTrackMenu(e, "audio", laneIndex)
+ }
+ >
+ <AudioTrack
+ laneIndex={laneIndex}
+ onDragStateChanged={(v) => {
+ audioSegmentDragState = v;
+ }}
+ handleUpdatePlayhead={handleUpdatePlayhead}
+ onRequestAdd={openAudioPicker}
+ />
+ </TrackRow>
+ )}
+ </For>
+ <TrackRow icon={trackIcons.zoom} label="Zoom" type="zoom">
<ZoomTrack
onDragStateChanged={(v) => {
zoomSegmentDragState = v;
@@ -995,7 +1079,7 @@ export function Timeline(props: {
/>
</TrackRow>
<Show when={sceneTrackVisible()}>
- <TrackRow icon={trackIcons.scene} label="Scene">
+ <TrackRow icon={trackIcons.scene} label="Scene" type="scene">
<SceneTrack
onDragStateChanged={(v) => {
sceneSegmentDragState = v;
@@ -1015,6 +1099,7 @@ export function Timeline(props: {
function TrackRow(props: {
icon: () => JSX.Element;
label?: string;
+ type: TimelineTrackType;
children: JSX.Element;
onDelete?: () => void;
onContextMenu?: (e: MouseEvent) => void;
@@ -1028,6 +1113,7 @@ function TrackRow(props: {
<TrackIcon
icon={props.icon()}
label={props.label}
+ type={props.type}
class={
props.onDelete
? "transition-opacity group-hover/icon:pointer-events-none group-hover/icon:opacity-0"