Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(editor): improve clip, scene, and zoom track segment handles

Richie McIlroyaaa8bf13c85c
diff --git a/apps/desktop/src/routes/editor/Timeline/ClipTrack.tsx b/apps/desktop/src/routes/editor/Timeline/ClipTrack.tsx
index bba9464e0..03e28c5d2 100644
--- a/apps/desktop/src/routes/editor/Timeline/ClipTrack.tsx
+++ b/apps/desktop/src/routes/editor/Timeline/ClipTrack.tsx
@@ -29,6 +29,7 @@ import {
TrackRoot,
useSegmentTranslateX,
useSegmentWidth,
+ useSetPreviewTime,
} from "./Track";
const CANVAS_HEIGHT = 52;
@@ -38,6 +39,7 @@ const WAVEFORM_CONTROL_STEP = 0.05;
const WAVEFORM_PADDING_SECONDS = 0.3;
const WAVEFORM_MUTE_DB = -30;
+const MIN_CLIP_SEGMENT_PIXEL_WIDTH = 100;
function gainToScale(gain?: number) {
if (!Number.isFinite(gain)) return 1;
@@ -319,6 +321,7 @@ export function ClipTrack(
} = useEditorContext();
const { secsPerPixel, duration, isSegmentVisible } = useTimelineContext();
+ const setPreviewTime = useSetPreviewTime();
const segments = (): Array<TimelineSegment> =>
project.timeline?.segments ?? [{ start: 0, end: duration(), timescale: 1 }];
@@ -353,7 +356,10 @@ export function ClipTrack(
const { transform } = editorState.timeline;
if (transform.position + transform.zoom > totalDuration() + 4) {
- transform.updateZoom(totalDuration(), editorState.previewTime!);
+ transform.updateZoom(
+ totalDuration(),
+ editorState.previewTime ?? editorState.playbackTime,
+ );
}
}
@@ -652,6 +658,12 @@ export function ClipTrack(
onMouseDown={(downEvent) => {
if (split()) return;
const seg = segment();
+ const minRecordedDuration = Math.max(
+ 1,
+ secsPerPixel() *
+ MIN_CLIP_SEGMENT_PIXEL_WIDTH *
+ seg.timescale,
+ );
const initialStart = seg.start;
setStartHandleDrag({
@@ -698,7 +710,7 @@ export function ClipTrack(
prevSegmentIsSameClip ? prevSegment.end : 0,
seg.end - maxDuration,
),
- seg.end - 1,
+ seg.end - minRecordedDuration,
);
setStartHandleDrag({
@@ -713,13 +725,13 @@ export function ClipTrack(
"start",
clampedStart,
);
+ setPreviewTime(prevDuration());
}
const resumeHistory = projectHistory.pause();
createRoot((dispose) => {
onCleanup(() => {
resumeHistory();
- console.log("NUL");
setStartHandleDrag(null);
onHandleReleased();
});
@@ -769,6 +781,12 @@ export function ClipTrack(
onMouseDown={(downEvent) => {
const seg = segment();
const end = seg.end;
+ const minRecordedDuration = Math.max(
+ 1,
+ secsPerPixel() *
+ MIN_CLIP_SEGMENT_PIXEL_WIDTH *
+ seg.timescale,
+ );
if (split()) return;
const maxSegmentDuration =
@@ -798,22 +816,27 @@ export function ClipTrack(
secsPerPixel() *
seg.timescale;
const newEnd = end + deltaRecorded;
+ const clampedEnd = Math.max(
+ Math.min(
+ newEnd,
+ end + availableTimelineDuration * seg.timescale,
+ nextSegmentIsSameClip
+ ? nextSegment.start
+ : maxSegmentDuration,
+ ),
+ seg.start + minRecordedDuration,
+ );
setProject(
"timeline",
"segments",
i(),
"end",
- Math.max(
- Math.min(
- newEnd,
- end + availableTimelineDuration * seg.timescale,
- nextSegmentIsSameClip
- ? nextSegment.start
- : maxSegmentDuration,
- ),
- seg.start + 1,
- ),
+ clampedEnd,
+ );
+ setPreviewTime(
+ prevDuration() +
+ (clampedEnd - seg.start) / seg.timescale,
);
}
diff --git a/apps/desktop/src/routes/editor/Timeline/SceneTrack.tsx b/apps/desktop/src/routes/editor/Timeline/SceneTrack.tsx
index 193ce67b3..a864e828e 100644
--- a/apps/desktop/src/routes/editor/Timeline/SceneTrack.tsx
+++ b/apps/desktop/src/routes/editor/Timeline/SceneTrack.tsx
@@ -20,13 +20,21 @@ import {
useTimelineContext,
useTrackContext,
} from "./context";
-import { SegmentContent, SegmentHandle, SegmentRoot, TrackRoot } from "./Track";
+import {
+ SegmentContent,
+ SegmentHandle,
+ SegmentRoot,
+ TrackRoot,
+ useSetPreviewTime,
+} from "./Track";
export type SceneSegmentDragState =
| { type: "idle" }
| { type: "movePending" }
| { type: "moving" };
+const MIN_SCENE_SEGMENT_PIXEL_WIDTH = 80;
+
export function SceneTrack(props: {
onDragStateChanged: (v: SceneSegmentDragState) => void;
handleUpdatePlayhead: (e: MouseEvent) => void;
@@ -41,6 +49,7 @@ export function SceneTrack(props: {
} = useEditorContext();
const { duration, secsPerPixel } = useTimelineContext();
+ const setPreviewTime = useSetPreviewTime();
const [hoveringSegment, setHoveringSegment] = createSignal(false);
const [hoveredTime, setHoveredTime] = createSignal<number>();
@@ -90,7 +99,7 @@ export function SceneTrack(props: {
return;
}
- const bounds = e.target.getBoundingClientRect()!;
+ const bounds = e.currentTarget.getBoundingClientRect();
let time =
(e.clientX - bounds.left) * secsPerPixel() +
@@ -385,13 +394,18 @@ export function SceneTrack(props: {
onMouseDown={createMouseDownDrag(
() => {
const start = segment.start;
+ const minDuration = Math.max(
+ 1,
+ secsPerPixel() * MIN_SCENE_SEGMENT_PIXEL_WIDTH,
+ );
let minValue = 0;
- const maxValue = segment.end - 1;
+ const maxValue = segment.end - minDuration;
for (let i = sceneSegments().length - 1; i >= 0; i--) {
- const segment = sceneSegments()[i]!;
+ const segment = sceneSegments()[i];
+ if (!segment) continue;
if (segment.end <= start) {
minValue = segment.end;
break;
@@ -404,16 +418,17 @@ export function SceneTrack(props: {
const newStart =
value.start +
(e.clientX - initialMouseX) * secsPerPixel();
+ const nextStart = Math.min(
+ value.maxValue,
+ Math.max(value.minValue, newStart),
+ );
setProject(
"timeline",
"sceneSegments",
i(),
"start",
- Math.min(
- value.maxValue,
- Math.max(value.minValue, newStart),
- ),
+ nextStart,
);
setProject(
@@ -425,6 +440,7 @@ export function SceneTrack(props: {
}
}),
);
+ setPreviewTime(nextStart);
},
)}
/>
@@ -492,13 +508,18 @@ export function SceneTrack(props: {
onMouseDown={createMouseDownDrag(
() => {
const end = segment.end;
+ const minDuration = Math.max(
+ 1,
+ secsPerPixel() * MIN_SCENE_SEGMENT_PIXEL_WIDTH,
+ );
- const minValue = segment.start + 1;
+ const minValue = segment.start + minDuration;
let maxValue = duration();
for (let i = 0; i < sceneSegments().length; i++) {
- const segment = sceneSegments()[i]!;
+ const segment = sceneSegments()[i];
+ if (!segment) continue;
if (segment.start > end) {
maxValue = segment.start;
break;
@@ -510,16 +531,17 @@ export function SceneTrack(props: {
(e, value, initialMouseX) => {
const newEnd =
value.end + (e.clientX - initialMouseX) * secsPerPixel();
+ const nextEnd = Math.min(
+ value.maxValue,
+ Math.max(value.minValue, newEnd),
+ );
setProject(
"timeline",
"sceneSegments",
i(),
"end",
- Math.min(
- value.maxValue,
- Math.max(value.minValue, newEnd),
- ),
+ nextEnd,
);
setProject(
@@ -531,6 +553,7 @@ export function SceneTrack(props: {
}
}),
);
+ setPreviewTime(nextEnd);
},
)}
/>
diff --git a/apps/desktop/src/routes/editor/Timeline/ZoomTrack.tsx b/apps/desktop/src/routes/editor/Timeline/ZoomTrack.tsx
index d48cb9239..ccc854c7a 100644
--- a/apps/desktop/src/routes/editor/Timeline/ZoomTrack.tsx
+++ b/apps/desktop/src/routes/editor/Timeline/ZoomTrack.tsx
@@ -20,13 +20,20 @@ import {
useTimelineContext,
useTrackContext,
} from "./context";
-import { SegmentContent, SegmentHandle, SegmentRoot, TrackRoot } from "./Track";
+import {
+ SegmentContent,
+ SegmentHandle,
+ SegmentRoot,
+ TrackRoot,
+ useSetPreviewTime,
+} from "./Track";
export type ZoomSegmentDragState =
| { type: "idle" }
| { type: "movePending" }
| { type: "moving" };
+const MIN_ZOOM_SEGMENT_PIXEL_WIDTH = 40;
const MIN_NEW_SEGMENT_PIXEL_WIDTH = 80;
const MIN_NEW_SEGMENT_SECS_WIDTH = 1;
@@ -45,6 +52,7 @@ export function ZoomTrack(props: {
} = useEditorContext();
const { duration, secsPerPixel } = useTimelineContext();
+ const setPreviewTime = useSetPreviewTime();
const [creatingSegmentViaDrag, setCreatingSegmentViaDrag] =
createSignal(false);
@@ -444,13 +452,18 @@ export function ZoomTrack(props: {
onMouseDown={createMouseDownDrag(
() => {
const start = segment().start;
+ const minDuration = Math.max(
+ 1,
+ secsPerPixel() * MIN_ZOOM_SEGMENT_PIXEL_WIDTH,
+ );
let minValue = 0;
- const maxValue = segment().end - 1;
+ const maxValue = segment().end - minDuration;
for (let i = zoomSegments().length - 1; i >= 0; i--) {
- const segment = zoomSegments()[i]!;
+ const segment = zoomSegments()[i];
+ if (!segment) continue;
if (segment.end <= start) {
minValue = segment.end;
break;
@@ -463,16 +476,17 @@ export function ZoomTrack(props: {
const newStart =
value.start +
(e.clientX - initialMouseX) * secsPerPixel();
+ const nextStart = Math.min(
+ value.maxValue,
+ Math.max(value.minValue, newStart),
+ );
setProject(
"timeline",
"zoomSegments",
i,
"start",
- Math.min(
- value.maxValue,
- Math.max(value.minValue, newStart),
- ),
+ nextStart,
);
setProject(
@@ -482,6 +496,7 @@ export function ZoomTrack(props: {
s.sort((a, b) => a.start - b.start);
}),
);
+ setPreviewTime(nextStart);
},
)}
/>
@@ -558,13 +573,18 @@ export function ZoomTrack(props: {
onMouseDown={createMouseDownDrag(
() => {
const end = segment().end;
+ const minDuration = Math.max(
+ 1,
+ secsPerPixel() * MIN_ZOOM_SEGMENT_PIXEL_WIDTH,
+ );
- const minValue = segment().start + 1;
+ const minValue = segment().start + minDuration;
let maxValue = duration();
for (let i = 0; i < zoomSegments().length; i++) {
- const segment = zoomSegments()[i]!;
+ const segment = zoomSegments()[i];
+ if (!segment) continue;
if (segment.start > end) {
maxValue = segment.start;
break;
@@ -577,18 +597,13 @@ export function ZoomTrack(props: {
const newEnd =
value.end +
(e.clientX - initialMouseX) * secsPerPixel();
-
- setProject(
- "timeline",
- "zoomSegments",
- i,
- "end",
- Math.min(
- value.maxValue,
- Math.max(value.minValue, newEnd),
- ),
+ const nextEnd = Math.min(
+ value.maxValue,
+ Math.max(value.minValue, newEnd),
);
+ setProject("timeline", "zoomSegments", i, "end", nextEnd);
+
setProject(
"timeline",
"zoomSegments",
@@ -596,6 +611,7 @@ export function ZoomTrack(props: {
s.sort((a, b) => a.start - b.start);
}),
);
+ setPreviewTime(nextEnd);
},
)}
/>