Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(editor): snap playhead and scrubber to timeline origin

Richie McIlroy3e722be9c1b9
diff --git a/apps/desktop/src/routes/editor/Timeline/index.tsx b/apps/desktop/src/routes/editor/Timeline/index.tsx
index 91a858c69..92330e8ad 100644
--- a/apps/desktop/src/routes/editor/Timeline/index.tsx
+++ b/apps/desktop/src/routes/editor/Timeline/index.tsx
@@ -60,6 +60,7 @@ const TRACK_GUTTER = 112;
const TRACK_ICON_WIDTH = TRACK_GUTTER - TRACK_GUTTER_GAP;
const TIMELINE_HEADER_HEIGHT = 32;
const PLAYHEAD_TOP_OFFSET = 24;
+const START_SNAP_PX = 10;
const trackIcons: Record<TimelineTrackType, () => JSX.Element> = {
clip: () => <IconLucideClapperboard class="size-4" />,
@@ -764,7 +765,11 @@ export function Timeline(props: {
if (!metrics) return;
const rawTime =
secsPerPixel() * (e.clientX - metrics.left) + transform().position;
- const newTime = Math.min(Math.max(0, rawTime), totalDuration());
+ // Snap to the very start when the cursor lands within a few pixels
+ // of the timeline origin so hitting exactly 0:00 isn't a battle
+ const snappedTime =
+ rawTime / secsPerPixel() <= START_SNAP_PX ? 0 : rawTime;
+ const newTime = Math.min(Math.max(0, snappedTime), totalDuration());
// If playing, some backends require restart to seek reliably
if (editorState.playing) {
@@ -972,9 +977,10 @@ export function Timeline(props: {
setEditorState("previewTime", null);
return;
}
+ const hoverTime = transform().position + secsPerPixel() * offsetX;
setEditorState(
"previewTime",
- transform().position + secsPerPixel() * offsetX,
+ hoverTime / secsPerPixel() <= START_SNAP_PX ? 0 : hoverTime,
);
}}
onMouseEnter={() => setEditorState("timeline", "hoveredTrack", null)}
@@ -1019,8 +1025,14 @@ export function Timeline(props: {
/>
</div>
</div>
- <Show when={!editorState.playing && editorState.previewTime}>
- {(time) => (
+ <Show
+ when={
+ !editorState.playing && editorState.previewTime !== null
+ ? { time: editorState.previewTime }
+ : null
+ }
+ >
+ {(preview) => (
<div
class={cx(
"flex absolute bottom-0 z-20 justify-center items-center w-px pointer-events-none bg-linear-to-b to-120%",
@@ -1030,7 +1042,8 @@ export function Timeline(props: {
left: `${TIMELINE_PADDING + TRACK_GUTTER}px`,
top: `${PLAYHEAD_TOP_OFFSET}px`,
transform: `translateX(${
- (time() - transform().position) / secsPerPixel()
+ ((preview().time ?? 0) - transform().position) /
+ secsPerPixel()
}px)`,
}}
>
@@ -1271,7 +1284,7 @@ function TimelineMarkings() {
<Index each={Array.from({ length: markingCount() })}>
{(_, index) => {
const second = () => getMarkingTime(index);
- const isVisible = () => second() > 0;
+ const isVisible = () => second() >= 0;
const showLabel = () => second() % 1 === 0;
const translateX = () =>
(second() - transform().position) / secsPerPixel() - 1;
@@ -1285,7 +1298,14 @@ function TimelineMarkings() {
}}
>
<Show when={showLabel()}>
- <div class="absolute -top-4.5 -translate-x-1/2">
+ <div
+ class={cx(
+ "absolute -top-4.5",
+ // Left-anchor the origin label so it doesn't overhang
+ // into the track icon gutter and get covered
+ second() !== 0 && "-translate-x-1/2",
+ )}
+ >
{formatTime(second())}
</div>
</Show>