Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

feat: overhaul timeline drag/resize, zoom playback, and recording hooks

webadderall949b9381f6b0
diff --git a/src/components/video-editor/timeline/AudioWaveform.tsx b/src/components/video-editor/timeline/AudioWaveform.tsx
index b30af529..1fb07b91 100644
--- a/src/components/video-editor/timeline/AudioWaveform.tsx
+++ b/src/components/video-editor/timeline/AudioWaveform.tsx
@@ -1,5 +1,5 @@
-import { useCallback, useEffect, useRef, useState } from "react";
import { useTimelineContext } from "dnd-timeline";
+import { useCallback, useEffect, useRef, useState } from "react";
import type { AudioPeaksData } from "./useAudioPeaks";
interface AudioWaveformProps {
@@ -74,7 +74,7 @@ export default function AudioWaveform({ peaks }: AudioWaveformProps) {
ctx.lineTo(px, midY + barHeight);
}
- ctx.strokeStyle = "rgba(59, 130, 246, 0.45)";
+ ctx.strokeStyle = "rgba(255, 255, 255, 0.55)";
ctx.lineWidth = dpr;
ctx.stroke();
}, [peaks, range.start, range.end, resizeKey]);
diff --git a/src/components/video-editor/timeline/Item.tsx b/src/components/video-editor/timeline/Item.tsx
index a68a38ae..b3b41976 100644
--- a/src/components/video-editor/timeline/Item.tsx
+++ b/src/components/video-editor/timeline/Item.tsx
@@ -1,20 +1,29 @@
+import {
+ ChatCircle as MessageSquare,
+ FilmSlate as Film,
+ Gauge,
+ MagnifyingGlassPlus as ZoomIn,
+ MouseLeftClickIcon as PhMouseLeftClick,
+ MusicNotes as Music,
+ Scissors,
+} from "@phosphor-icons/react";
import type { Span } from "dnd-timeline";
import { useItem } from "dnd-timeline";
-import { Film, Gauge, MessageSquare, Music, Scissors, ZoomIn } from "lucide-react";
import { useMemo } from "react";
import { cn } from "@/lib/utils";
import glassStyles from "./ItemGlass.module.css";
interface ItemProps {
- id: string;
- span: Span;
- rowId: string;
- children: React.ReactNode;
- isSelected?: boolean;
- onSelect?: () => void;
- zoomDepth?: number;
- speedValue?: number;
- variant?: 'zoom' | 'trim' | 'clip' | 'annotation' | 'speed' | 'audio';
+ id: string;
+ span: Span;
+ rowId: string;
+ children: React.ReactNode;
+ isSelected?: boolean;
+ onSelect?: () => void;
+ zoomDepth?: number;
+ zoomMode?: "auto" | "manual";
+ speedValue?: number;
+ variant?: "zoom" | "trim" | "clip" | "annotation" | "speed" | "audio";
}
// Map zoom depth to multiplier labels
@@ -44,6 +53,7 @@ export default function Item({
isSelected = false,
onSelect,
zoomDepth = 1,
+ zoomMode = "auto",
speedValue,
variant = "zoom",
children,
@@ -54,133 +64,142 @@ export default function Item({
data: { rowId },
});
- const isZoom = variant === 'zoom';
- const isTrim = variant === 'trim';
- const isClip = variant === 'clip';
- const isSpeed = variant === 'speed';
- const isAudio = variant === 'audio';
+ const isZoom = variant === "zoom";
+ const isTrim = variant === "trim";
+ const isClip = variant === "clip";
+ const isSpeed = variant === "speed";
+ const isAudio = variant === "audio";
- const glassClass = isZoom
- ? glassStyles.glassPurple
- : isTrim
- ? glassStyles.glassRed
- : isClip
- ? glassStyles.glassCyan
- : isSpeed
- ? glassStyles.glassAmber
- : isAudio
- ? glassStyles.glassDarkGreen
- : glassStyles.glassYellow;
-
- const endCapColor = isZoom
- ? '#a855f7'
- : isTrim
- ? '#ef4444'
- : isClip
- ? '#06b6d4'
- : isSpeed
- ? '#d97706'
- : isAudio
- ? '#166534'
- : '#B4A046';
+ const glassClass = isZoom
+ ? glassStyles.glassPurple
+ : isTrim
+ ? glassStyles.glassRed
+ : isClip
+ ? glassStyles.glassCyan
+ : isSpeed
+ ? glassStyles.glassAmber
+ : isAudio
+ ? glassStyles.glassDarkGreen
+ : glassStyles.glassYellow;
const timeLabel = useMemo(
() => `${formatMs(span.start)} – ${formatMs(span.end)}`,
[span.start, span.end],
);
- const MIN_ITEM_PX = 6;
- const safeItemStyle = { ...itemStyle, minWidth: MIN_ITEM_PX, height: "100%" };
+ const MIN_ITEM_PX = 6;
+ const safeItemStyle = { ...itemStyle, minWidth: MIN_ITEM_PX, height: "100%" };
- return (
- <div
- ref={setNodeRef}
- style={safeItemStyle}
- {...listeners}
- {...attributes}
- onPointerDownCapture={() => onSelect?.()}
- className="group h-full"
- >
- <div className="h-full" style={{ ...itemContentStyle, minWidth: 24, height: "100%" }}>
- <div
- className={cn(
- glassClass,
- "w-full h-full overflow-hidden flex items-center justify-center gap-1.5 cursor-grab active:cursor-grabbing relative",
- isSelected && glassStyles.selected
- )}
- style={{ height: "100%", minHeight: 22, color: '#fff', minWidth: 24 }}
- onClick={(event) => {
- event.stopPropagation();
- onSelect?.();
- }}
- >
- <div
- className={cn(glassStyles.zoomEndCap, glassStyles.left)}
- style={{ cursor: 'col-resize', pointerEvents: 'auto', width: 8, opacity: 0.9, background: endCapColor }}
- title="Resize left"
- />
- <div
- className={cn(glassStyles.zoomEndCap, glassStyles.right)}
- style={{ cursor: 'col-resize', pointerEvents: 'auto', width: 8, opacity: 0.9, background: endCapColor }}
- title="Resize right"
- />
- {/* Content */}
- <div className="relative z-10 flex flex-col items-center justify-center text-white/90 opacity-80 group-hover:opacity-100 transition-opacity select-none overflow-hidden">
- <div className="flex items-center gap-1.5">
- {isZoom ? (
- <>
- <ZoomIn className="w-3.5 h-3.5 shrink-0" />
- <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
- {ZOOM_LABELS[zoomDepth] || `${zoomDepth}×`}
- </span>
- </>
- ) : isTrim ? (
- <>
- <Scissors className="w-3.5 h-3.5 shrink-0" />
- <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
- Trim
- </span>
- </>
- ) : isClip ? (
- <>
- <Film className="w-3.5 h-3.5 shrink-0" />
- <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
- Clip
- </span>
- </>
- ) : isSpeed ? (
- <>
- <Gauge className="w-3.5 h-3.5 shrink-0" />
- <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
- {speedValue !== undefined ? `${speedValue}×` : 'Speed'}
- </span>
- </>
- ) : isAudio ? (
- <>
- <Music className="w-3.5 h-3.5 shrink-0" />
- <span className="text-[11px] font-semibold tracking-tight truncate max-w-full">
- {children}
- </span>
- </>
- ) : (
- <>
- <MessageSquare className="w-3.5 h-3.5 shrink-0" />
- <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
- {children}
- </span>
- </>
- )}
- </div>
- <span
- className={`text-[9px] tabular-nums tracking-tight whitespace-nowrap transition-opacity ${
- isSelected ? 'opacity-60' : 'opacity-0 group-hover:opacity-40'
- }`}
- >
- {timeLabel}
- </span>
- </div>
- </div>
- </div>
- </div>
- );
+ return (
+ <div
+ ref={setNodeRef}
+ style={safeItemStyle}
+ {...listeners}
+ {...attributes}
+ onPointerDownCapture={() => onSelect?.()}
+ className="group h-full"
+ >
+ <div className="h-full" style={{ ...itemContentStyle, minWidth: 24, height: "100%", display: "flex", alignItems: "center" }}>
+ <div
+ className={cn(
+ glassClass,
+ "w-full overflow-hidden flex items-center justify-center gap-1.5 cursor-grab active:cursor-grabbing relative",
+ isSelected && glassStyles.selected,
+ )}
+ style={{
+ height: "85%",
+ minHeight: 22,
+ color: "#fff",
+ minWidth: 24,
+ backgroundImage:
+ "linear-gradient(180deg, transparent 20%, rgba(255,255,255,0.08) 100%)",
+ }}
+ onClick={(event) => {
+ event.stopPropagation();
+ onSelect?.();
+ }}
+ >
+ <div
+ className={cn(glassStyles.zoomEndCap, glassStyles.left)}
+ style={{ cursor: "col-resize", pointerEvents: "auto" }}
+ title="Resize left"
+ />
+ <div
+ className={cn(glassStyles.zoomEndCap, glassStyles.right)}
+ style={{ cursor: "col-resize", pointerEvents: "auto" }}
+ title="Resize right"
+ />
+ {/* Content */}
+ <div className="relative z-10 flex flex-col items-center justify-center text-white/90 opacity-80 group-hover:opacity-100 transition-opacity select-none overflow-hidden">
+ <div className="flex items-center gap-1.5">
+ {isZoom ? (
+ <>
+ <ZoomIn className="w-3.5 h-3.5 shrink-0" />
+ <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
+ {ZOOM_LABELS[zoomDepth] || `${zoomDepth}×`}
+ </span>
+ </>
+ ) : isTrim ? (
+ <>
+ <Scissors className="w-3.5 h-3.5 shrink-0" />
+ <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
+ Trim
+ </span>
+ </>
+ ) : isClip ? (
+ <>
+ <Film className="w-3.5 h-3.5 shrink-0" />
+ <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
+ Clip
+ </span>
+ </>
+ ) : isSpeed ? (
+ <>
+ <Gauge className="w-3.5 h-3.5 shrink-0" />
+ <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
+ {speedValue !== undefined ? `${speedValue}×` : "Speed"}
+ </span>
+ </>
+ ) : isAudio ? (
+ <>
+ <Music className="w-3.5 h-3.5 shrink-0" />
+ <span className="text-[11px] font-semibold tracking-tight truncate max-w-full">
+ {children}
+ </span>
+ </>
+ ) : (
+ <>
+ <MessageSquare className="w-3.5 h-3.5 shrink-0" />
+ <span className="text-[11px] font-semibold tracking-tight whitespace-nowrap">
+ {children}
+ </span>
+ </>
+ )}
+ </div>
+ {isZoom ? (
+ <div
+ className={`flex items-center gap-0.5 transition-opacity ${isSelected ? "opacity-70" : "opacity-0 group-hover:opacity-50"}`}
+ >
+ <PhMouseLeftClick
+ className="w-2.5 h-2.5 shrink-0"
+ weight={zoomMode === "manual" ? "regular" : "fill"}
+ />
+ <span className="text-[9px] font-medium tracking-tight whitespace-nowrap">
+ {zoomMode === "manual" ? "Manual" : "Auto"}
+ </span>
+ </div>
+ ) : (
+ <span
+ className={`text-[9px] tabular-nums tracking-tight whitespace-nowrap transition-opacity ${
+ isSelected ? "opacity-60" : "opacity-0 group-hover:opacity-40"
+ }`}
+ >
+ {timeLabel}
+ </span>
+ )}
+ </div>
+ </div>
+ </div>
+ </div>
+ );
}
diff --git a/src/components/video-editor/timeline/ItemGlass.module.css b/src/components/video-editor/timeline/ItemGlass.module.css
index 59516584..142eeda0 100644
--- a/src/components/video-editor/timeline/ItemGlass.module.css
+++ b/src/components/video-editor/timeline/ItemGlass.module.css
@@ -1,194 +1,187 @@
.glassGreen {
- position: relative;
- border-radius: 8px;
- -corner-smoothing: antialiased;
- background: linear-gradient(to bottom, rgba(37, 99, 235, 0.22), rgba(37, 99, 235, 0.08));
- border: 1px solid rgba(37, 99, 235, 0.3);
- box-shadow: 0 2px 12px 0 rgba(37, 99, 235, 0.1) inset;
- margin: 1px 0;
- backdrop-filter: blur(4px);
- -webkit-backdrop-filter: blur(4px);
- transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
+ position: relative;
+ border-radius: 8px;
+ background: rgba(37, 99, 235, 0.38);
+ border: 1px solid rgba(37, 99, 235, 0.5);
+
+ transition:
+ background 0.15s ease,
+ border-color 0.15s ease;
}
.glassGreen:hover {
- background: linear-gradient(to bottom, rgba(37, 99, 235, 0.32), rgba(37, 99, 235, 0.15));
- border-color: rgba(37, 99, 235, 0.5);
- box-shadow: 0 4px 20px 0 rgba(37, 99, 235, 0.2) inset;
+ background: rgba(37, 99, 235, 0.48);
+ border-color: rgba(37, 99, 235, 0.65);
}
.glassGreen.selected {
- background: linear-gradient(to bottom, rgba(37, 99, 235, 0.42), rgba(37, 99, 235, 0.22));
- border-color: #2563EB;
- box-shadow: 0 0 0 1px #2563EB, 0 4px 20px 0 rgba(37, 99, 235, 0.3) inset;
- z-index: 10;
+ background: rgba(37, 99, 235, 0.55);
+ border-color: #2563eb;
+ box-shadow: inset 0 0 0 1.5px #2563eb;
+ z-index: 10;
}
.glassRed {
- position: relative;
- border-radius: 8px;
- -corner-smoothing: antialiased;
- background: linear-gradient(to bottom, rgba(239, 68, 68, 0.22), rgba(239, 68, 68, 0.08));
- border: 1px solid rgba(239, 68, 68, 0.3);
- box-shadow: 0 2px 12px 0 rgba(239, 68, 68, 0.1) inset;
- margin: 1px 0;
- backdrop-filter: blur(4px);
- -webkit-backdrop-filter: blur(4px);
- transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
+ position: relative;
+ border-radius: 8px;
+ background: rgba(239, 68, 68, 0.38);
+ border: 1px solid rgba(239, 68, 68, 0.5);
+
+ transition:
+ background 0.15s ease,
+ border-color 0.15s ease;
}
.glassRed:hover {
- background: linear-gradient(to bottom, rgba(239, 68, 68, 0.32), rgba(239, 68, 68, 0.15));
- border-color: rgba(239, 68, 68, 0.5);
- box-shadow: 0 4px 20px 0 rgba(239, 68, 68, 0.2) inset;
+ background: rgba(239, 68, 68, 0.48);
+ border-color: rgba(239, 68, 68, 0.65);
}
.glassRed.selected {
- background: linear-gradient(to bottom, rgba(239, 68, 68, 0.42), rgba(239, 68, 68, 0.22));
- border-color: #ef4444;
- box-shadow: 0 0 0 1px #ef4444, 0 4px 20px 0 rgba(239, 68, 68, 0.3) inset;
- z-index: 10;
+ background: rgba(239, 68, 68, 0.55);
+ border-color: #ef4444;
+ box-shadow: inset 0 0 0 1.5px #ef4444;
+ z-index: 10;
}
.glassYellow {
- position: relative;
- border-radius: 8px;
- -corner-smoothing: antialiased;
- background: linear-gradient(to bottom, rgba(180, 160, 70, 0.22), rgba(180, 160, 70, 0.08));
- border: 1px solid rgba(180, 160, 70, 0.3);
- box-shadow: 0 2px 12px 0 rgba(180, 160, 70, 0.1) inset;
- margin: 1px 0;
- backdrop-filter: blur(4px);
- -webkit-backdrop-filter: blur(4px);
- transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
+ position: relative;
+ border-radius: 8px;
+ background: rgba(180, 160, 70, 0.38);
+ border: 1px solid rgba(180, 160, 70, 0.5);
+
+ transition:
+ background 0.15s ease,
+ border-color 0.15s ease;
}
.glassYellow:hover {
- background: linear-gradient(to bottom, rgba(180, 160, 70, 0.32), rgba(180, 160, 70, 0.15));
- border-color: rgba(180, 160, 70, 0.5);
- box-shadow: 0 4px 20px 0 rgba(180, 160, 70, 0.2) inset;
+ background: rgba(180, 160, 70, 0.48);
+ border-color: rgba(180, 160, 70, 0.65);
}
.glassYellow.selected {
- background: linear-gradient(to bottom, rgba(180, 160, 70, 0.42), rgba(180, 160, 70, 0.22));
- border-color: #B4A046;
- box-shadow: 0 0 0 1px #B4A046, 0 4px 20px 0 rgba(180, 160, 70, 0.3) inset;
- z-index: 10;
+ background: rgba(180, 160, 70, 0.55);
+ border-color: #b4a046;
+ box-shadow: inset 0 0 0 1.5px #b4a046;
+ z-index: 10;
}
.glassAmber {
- position: relative;
- border-radius: 8px;
- -corner-smoothing: antialiased;
- background: linear-gradient(to bottom, rgba(245, 158, 11, 0.22), rgba(245, 158, 11, 0.08));
- border: 1px solid rgba(245, 158, 11, 0.3);
- box-shadow: 0 2px 12px 0 rgba(245, 158, 11, 0.1) inset;
- margin: 1px 0;
- backdrop-filter: blur(4px);
- -webkit-backdrop-filter: blur(4px);
- transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
+ position: relative;
+ border-radius: 8px;
+ background: rgba(245, 158, 11, 0.38);
+ border: 1px solid rgba(245, 158, 11, 0.5);
+
+ transition:
+ background 0.15s ease,
+ border-color 0.15s ease;
}
.glassAmber:hover {
- background: linear-gradient(to bottom, rgba(245, 158, 11, 0.32), rgba(245, 158, 11, 0.15));
- border-color: rgba(245, 158, 11, 0.5);
- box-shadow: 0 4px 20px 0 rgba(245, 158, 11, 0.2) inset;
+ background: rgba(245, 158, 11, 0.48);
+ border-color: rgba(245, 158, 11, 0.65);
}
.glassAmber.selected {
- background: linear-gradient(to bottom, rgba(245, 158, 11, 0.42), rgba(245, 158, 11, 0.22));
- border-color: #f59e0b;
- box-shadow: 0 0 0 1px #f59e0b, 0 4px 20px 0 rgba(245, 158, 11, 0.3) inset;
- z-index: 10;
+ background: rgba(245, 158, 11, 0.55);
+ border-color: #f59e0b;
+ box-shadow: inset 0 0 0 1.5px #f59e0b;
+ z-index: 10;
}
.glassPurple {
- position: relative;
- border-radius: 8px;
- -corner-smoothing: antialiased;
- background: linear-gradient(to bottom, rgba(168, 85, 247, 0.22), rgba(168, 85, 247, 0.08));
- border: 1px solid rgba(168, 85, 247, 0.3);
- box-shadow: 0 2px 12px 0 rgba(168, 85, 247, 0.1) inset;
- margin: 1px 0;
- backdrop-filter: blur(4px);
- -webkit-backdrop-filter: blur(4px);
- transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
+ position: relative;
+ border-radius: 8px;
+ background: rgba(168, 85, 247, 0.38);
+ border: 1px solid rgba(168, 85, 247, 0.5);
+
+ transition:
+ background 0.15s ease,
+ border-color 0.15s ease;
}
.glassPurple:hover {
- background: linear-gradient(to bottom, rgba(168, 85, 247, 0.32), rgba(168, 85, 247, 0.15));
- border-color: rgba(168, 85, 247, 0.5);
- box-shadow: 0 4px 20px 0 rgba(168, 85, 247, 0.2) inset;
+ background: rgba(168, 85, 247, 0.48);
+ border-color: rgba(168, 85, 247, 0.65);
}
.glassPurple.selected {
- background: linear-gradient(to bottom, rgba(168, 85, 247, 0.42), rgba(168, 85, 247, 0.22));
- border-color: #a855f7;
- box-shadow: 0 0 0 1px #a855f7, 0 4px 20px 0 rgba(168, 85, 247, 0.3) inset;
- z-index: 10;
+ background: rgba(168, 85, 247, 0.55);
+ border-color: #a855f7;
+ box-shadow: inset 0 0 0 1.5px #a855f7;
+ z-index: 10;
}
.glassCyan {
- position: relative;
- border-radius: 8px;
- -corner-smoothing: antialiased;
- background: linear-gradient(to bottom, rgba(6, 182, 212, 0.22), rgba(6, 182, 212, 0.08));
- border: 1px solid rgba(6, 182, 212, 0.3);
- box-shadow: 0 2px 12px 0 rgba(6, 182, 212, 0.1) inset;
- margin: 1px 0;
- backdrop-filter: blur(4px);
- -webkit-backdrop-filter: blur(4px);
- transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
+ position: relative;
+ border-radius: 8px;
+ background: rgba(37, 99, 235, 0.38);
+ border: 1px solid rgba(37, 99, 235, 0.5);
+
+ transition:
+ background 0.15s ease,
+ border-color 0.15s ease;
}
.glassCyan:hover {
- background: linear-gradient(to bottom, rgba(6, 182, 212, 0.32), rgba(6, 182, 212, 0.15));
- border-color: rgba(6, 182, 212, 0.5);
- box-shadow: 0 4px 20px 0 rgba(6, 182, 212, 0.2) inset;
+ background: rgba(37, 99, 235, 0.48);
+ border-color: rgba(37, 99, 235, 0.65);
}
.glassCyan.selected {
- background: linear-gradient(to bottom, rgba(6, 182, 212, 0.42), rgba(6, 182, 212, 0.22));
- border-color: #06b6d4;
- box-shadow: 0 0 0 1px #06b6d4, 0 4px 20px 0 rgba(6, 182, 212, 0.3) inset;
- z-index: 10;
+ background: rgba(37, 99, 235, 0.55);
+ border-color: #2563eb;
+ box-shadow: inset 0 0 0 1.5px #2563eb;
+ z-index: 10;
}
.glassDarkGreen {
- position: relative;
- border-radius: 8px;
- -corner-smoothing: antialiased;
- background: linear-gradient(to bottom, rgba(22, 101, 52, 0.22), rgba(22, 101, 52, 0.08));
- border: 1px solid rgba(22, 101, 52, 0.3);
- box-shadow: 0 2px 12px 0 rgba(22, 101, 52, 0.1) inset;
- margin: 1px 0;
- backdrop-filter: blur(4px);
- -webkit-backdrop-filter: blur(4px);
- transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
+ position: relative;
+ border-radius: 8px;
+ background: rgba(34, 197, 94, 0.35);
+ border: 1px solid rgba(34, 197, 94, 0.5);
+
+ transition:
+ background 0.15s ease,
+ border-color 0.15s ease;
}
.glassDarkGreen:hover {
- background: linear-gradient(to bottom, rgba(22, 101, 52, 0.32), rgba(22, 101, 52, 0.15));
- border-color: rgba(22, 101, 52, 0.5);
- box-shadow: 0 4px 20px 0 rgba(22, 101, 52, 0.2) inset;
+ background: rgba(34, 197, 94, 0.45);
+ border-color: rgba(34, 197, 94, 0.65);
}
.glassDarkGreen.selected {
- background: linear-gradient(to bottom, rgba(22, 101, 52, 0.42), rgba(22, 101, 52, 0.22));
- border-color: #166534;
- box-shadow: 0 0 0 1px #166534, 0 4px 20px 0 rgba(22, 101, 52, 0.3) inset;
- z-index: 10;
+ background: rgba(34, 197, 94, 0.52);
+ border-color: #22c55e;
+ box-shadow: inset 0 0 0 1.5px #22c55e;
+ z-index: 10;
}
+/* Pill resize handle — visible on hover */
.zoomEndCap {
- position: absolute;
- top: 0;
- bottom: 0;
- width: 4px;
- pointer-events: none;
- z-index: 2;
- opacity: 0.45;
- transition: opacity 0.2s, width 0.2s;
+ position: absolute;
+ top: 50%;
+ bottom: auto;
+ height: 70%;
+ width: 4px;
+ pointer-events: auto;
+ z-index: 2;
+ opacity: 0;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ transform: translateY(-50%);
+ border-radius: 2px;
+ background: rgba(255, 255, 255, 0.88);
+ transition:
+ opacity 0.15s ease,
+ background 0.15s ease;
+}
+
+.zoomEndCap:hover {
+ background: rgba(255, 255, 255, 1);
}
.glassGreen:hover .zoomEndCap,
@@ -205,19 +198,21 @@
.glassCyan.selected .zoomEndCap,
.glassDarkGreen:hover .zoomEndCap,
.glassDarkGreen.selected .zoomEndCap {
- opacity: 1;
+ opacity: 1;
}
.zoomEndCap.left {
- left: 0;
- cursor: ew-resize;
- border-top-left-radius: 7px;
- border-bottom-left-radius: 7px;
+ left: 6px;
+ cursor: col-resize;
}
.zoomEndCap.right {
- right: 0;
- cursor: ew-resize;
- border-top-right-radius: 7px;
- border-bottom-right-radius: 7px;
+ right: 6px;
+ cursor: col-resize;
+}
+
+/* Grip lines removed */
+.zoomEndCap::before,
+.zoomEndCap::after {
+ content: none;
}
diff --git a/src/components/video-editor/timeline/KeyframeMarkers.tsx b/src/components/video-editor/timeline/KeyframeMarkers.tsx
index d5c18be3..a791b4de 100644
--- a/src/components/video-editor/timeline/KeyframeMarkers.tsx
+++ b/src/components/video-editor/timeline/KeyframeMarkers.tsx
@@ -1,105 +1,114 @@
-import React, { useState, useEffect } from "react";
import { useTimelineContext } from "dnd-timeline";
+import React, { useEffect, useState } from "react";
interface Keyframe {
- id: string;
- time: number;
+ id: string;
+ time: number;
}
interface KeyframeMarkersProps {
- keyframes: Keyframe[];
- selectedKeyframeId: string | null;
- setSelectedKeyframeId: (id: string | null) => void;
- onKeyframeMove: (id: string, newTime: number) => void;
- videoDurationMs: number;
- timelineRef: React.RefObject<HTMLDivElement>;
+ keyframes: Keyframe[];
+ selectedKeyframeId: string | null;
+ setSelectedKeyframeId: (id: string | null) => void;
+ onKeyframeMove: (id: string, newTime: number) => void;
+ videoDurationMs: number;
+ timelineRef: React.RefObject<HTMLDivElement>;
}
const KeyframeMarkers: React.FC<KeyframeMarkersProps> = ({
- keyframes,
- selectedKeyframeId,
- setSelectedKeyframeId,
- onKeyframeMove,
- videoDurationMs,
- timelineRef
+ keyframes,
+ selectedKeyframeId,
+ setSelectedKeyframeId,
+ onKeyframeMove,
+ videoDurationMs,
+ timelineRef,
}) => {
- const { sidebarWidth, range, valueToPixels, pixelsToValue } = useTimelineContext();
- const [draggingKeyframeId, setDraggingKeyframeId] = useState<string | null>(null);
+ const { sidebarWidth, range, valueToPixels, pixelsToValue } = useTimelineContext();
+ const [draggingKeyframeId, setDraggingKeyframeId] = useState<string | null>(null);
- useEffect(() => {
- if (!draggingKeyframeId) return;
+ useEffect(() => {
+ if (!draggingKeyframeId) return;
- const handleMouseMove = (e: MouseEvent) => {
- if (!timelineRef.current) return;
+ const handleMouseMove = (e: MouseEvent) => {
+ if (!timelineRef.current) return;
- const rect = timelineRef.current.getBoundingClientRect();
- const clickX = e.clientX - rect.left - sidebarWidth;
- const relativeMs = pixelsToValue(clickX);
- const absoluteMs = Math.max(0, Math.min(range.start + relativeMs, videoDurationMs));
+ const rect = timelineRef.current.getBoundingClientRect();
+ const clickX = e.clientX - rect.left - sidebarWidth;
+ const relativeMs = pixelsToValue(clickX);
+ const absoluteMs = Math.max(0, Math.min(range.start + relativeMs, videoDurationMs));
- // Update the keyframe position in real-time
- onKeyframeMove(draggingKeyframeId, absoluteMs);
- };
+ // Update the keyframe position in real-time
+ onKeyframeMove(draggingKeyframeId, absoluteMs);
+ };
- const handleMouseUp = () => {
- setDraggingKeyframeId(null);
- document.body.style.cursor = '';
- };
+ const handleMouseUp = () => {
+ setDraggingKeyframeId(null);
+ document.body.style.cursor = "";
+ };
- window.addEventListener('mousemove', handleMouseMove);
- window.addEventListener('mouseup', handleMouseUp);
- document.body.style.cursor = 'ew-resize';
+ window.addEventListener("mousemove", handleMouseMove);
+ window.addEventListener("mouseup", handleMouseUp);
+ document.body.style.cursor = "ew-resize";
- return () => {
- window.removeEventListener('mousemove', handleMouseMove);
- window.removeEventListener('mouseup', handleMouseUp);
- document.body.style.cursor = '';
- };
- }, [draggingKeyframeId, onKeyframeMove, timelineRef, sidebarWidth, range.start, videoDurationMs, pixelsToValue]);
+ return () => {
+ window.removeEventListener("mousemove", handleMouseMove);
+ window.removeEventListener("mouseup", handleMouseUp);
+ document.body.style.cursor = "";
+ };
+ }, [
+ draggingKeyframeId,
+ onKeyframeMove,
+ timelineRef,
+ sidebarWidth,
+ range.start,
+ videoDurationMs,
+ pixelsToValue,
+ ]);
- return (
- <>
- {keyframes.map(kf => {
- const offset = valueToPixels(kf.time - range.start);
- const isSelected = kf.id === selectedKeyframeId;
- const isDragging = kf.id === draggingKeyframeId;
+ return (
+ <>
+ {keyframes.map((kf) => {
+ const offset = valueToPixels(kf.time - range.start);
+ const isSelected = kf.id === selectedKeyframeId;
+ const isDragging = kf.id === draggingKeyframeId;
- return (
- <div
- key={kf.id}
- className={`absolute top-8 cursor-grab active:cursor-grabbing ${isSelected ? 'ring-2 ring-[#2563EB]' : ''}`}
- style={{
- left: `${sidebarWidth + offset - 8}px`,
- zIndex: isDragging ? 50 : 40,
- transition: isDragging ? 'none' : 'left 0.1s ease-out'
- }}
- onMouseDown={e => {
- e.stopPropagation();
- setSelectedKeyframeId(kf.id);
- setDraggingKeyframeId(kf.id);
- }}
- onContextMenu={e => {
- e.preventDefault();
- e.stopPropagation();
- setSelectedKeyframeId(kf.id);
- }}
- title={`Keyframe @ ${Math.round(kf.time)}ms (drag to move, Delete/Backspace to remove)`}
- >
- <div style={{
- width: '10px',
- height: '10px',
- background: '#ffe100ff',
- transform: 'rotate(45deg)',
- border: 'none',
- opacity: isSelected ? 1 : 0.6,
- transition: 'opacity 0.15s',
- }} />
- </div>
- );
- })}
- </>
- );
+ return (
+ <div
+ key={kf.id}
+ className={`absolute top-8 cursor-grab active:cursor-grabbing ${isSelected ? "ring-2 ring-[#2563EB]" : ""}`}
+ style={{
+ left: `${sidebarWidth + offset - 8}px`,
+ zIndex: isDragging ? 50 : 40,
+ transition: isDragging ? "none" : "left 0.1s ease-out",
+ }}
+ onMouseDown={(e) => {
+ e.stopPropagation();
+ setSelectedKeyframeId(kf.id);
+ setDraggingKeyframeId(kf.id);
+ }}
+ onContextMenu={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setSelectedKeyframeId(kf.id);
+ }}
+ title={`Keyframe @ ${Math.round(kf.time)}ms (drag to move, Delete/Backspace to remove)`}
+ >
+ <div
+ style={{
+ width: "10px",
+ height: "10px",
+ background: "#ffe100ff",
+ transform: "rotate(45deg)",
+ border: "none",
+ opacity: isSelected ? 1 : 0.6,
+ transition: "opacity 0.15s",
+ }}
+ />
+ </div>
+ );
+ })}
+ </>
+ );
};
export default KeyframeMarkers;
-
diff --git a/src/components/video-editor/timeline/Row.tsx b/src/components/video-editor/timeline/Row.tsx
index 434aab29..473c3f08 100644
--- a/src/components/video-editor/timeline/Row.tsx
+++ b/src/components/video-editor/timeline/Row.tsx
@@ -1,39 +1,38 @@
-import { useRow } from "dnd-timeline";
import type { RowDefinition } from "dnd-timeline";
+import { useRow } from "dnd-timeline";
interface RowProps extends RowDefinition {
- children: React.ReactNode;
- label?: string;
- hint?: string;
- isEmpty?: boolean;
- labelColor?: string;
+ children: React.ReactNode;
+ label?: string;
+ hint?: string;
+ isEmpty?: boolean;
+ labelColor?: string;
}
-export default function Row({ id, children, label, hint, isEmpty, labelColor = '#666' }: RowProps) {
- const { setNodeRef, rowWrapperStyle, rowStyle } = useRow({ id });
+export default function Row({ id, children, label, hint, isEmpty, labelColor = "#666" }: RowProps) {
+ const { setNodeRef, rowWrapperStyle, rowStyle } = useRow({ id });
- return (
- <div
- className="border-b border-[#18181b] bg-[#18181b] relative flex-1 min-h-[26px]"
- style={{ ...rowWrapperStyle, marginBottom: 2 }}
- >
- {label && (
- <div
- className="absolute left-1.5 top-1/2 -translate-y-1/2 text-[9px] font-semibold uppercase tracking-widest z-20 pointer-events-none select-none"
- style={{ color: labelColor, writingMode: 'horizontal-tb' }}
- >
- {label}
- </div>
- )}
- {isEmpty && hint && (
- <div className="absolute inset-0 flex items-center justify-center pointer-events-none select-none z-10">
- <span className="text-[11px] text-white/15 font-medium">{hint}</span>
- </div>
- )}
- <div ref={setNodeRef} className="relative h-full min-h-0" style={rowStyle}>
- {children}
- </div>
- </div>
- );
+ return (
+ <div
+ className="border-b border-[#18181b] bg-[#18181b] relative flex-1 min-h-[26px]"
+ style={{ ...rowWrapperStyle, marginBottom: 2 }}
+ >
+ {label && (
+ <div
+ className="absolute left-1.5 top-1/2 -translate-y-1/2 text-[9px] font-semibold uppercase tracking-widest z-20 pointer-events-none select-none"
+ style={{ color: labelColor, writingMode: "horizontal-tb" }}
+ >
+ {label}
+ </div>
+ )}
+ {isEmpty && hint && (
+ <div className="absolute inset-0 flex items-center justify-center pointer-events-none select-none z-10">
+ <span className="text-[11px] text-white/15 font-medium">{hint}</span>
+ </div>
+ )}
+ <div ref={setNodeRef} className="relative h-full min-h-0" style={rowStyle}>
+ {children}
+ </div>
+ </div>
+ );
}
-
diff --git a/src/components/video-editor/timeline/Subrow.tsx b/src/components/video-editor/timeline/Subrow.tsx
index 18695536..803f5301 100644
--- a/src/components/video-editor/timeline/Subrow.tsx
+++ b/src/components/video-editor/timeline/Subrow.tsx
@@ -1,12 +1,17 @@
import { cn } from "@/lib/utils";
+
interface SubrowProps {
- children: React.ReactNode;
+ children: React.ReactNode;
}
export default function Subrow({ children }: SubrowProps) {
- return (
- <div className={cn("flex items-center min-h-[24px] gap-1 px-1.5 py-0 bg-[#23232a] rounded-md text-slate-300")}>
- {children}
- </div>
- );
+ return (
+ <div
+ className={cn(
+ "flex items-center min-h-[24px] gap-1 px-1.5 py-0 bg-[#23232a] rounded-md text-slate-300",
+ )}
+ >
+ {children}
+ </div>
+ );
}
diff --git a/src/components/video-editor/timeline/Timeline.module.css b/src/components/video-editor/timeline/Timeline.module.css
new file mode 100644
index 00000000..b6d92783
--- /dev/null
+++ b/src/components/video-editor/timeline/Timeline.module.css
@@ -0,0 +1,28 @@
+.scrollArea {
+ scrollbar-width: thin;
+ scrollbar-color: rgba(107, 107, 107, 0.7) transparent;
+}
+
+.scrollArea::-webkit-scrollbar {
+ width: 8px;
+ height: 8px;
+ background-color: transparent;
+}
+
+.scrollArea::-webkit-scrollbar-corner {
+ background-color: transparent;
+}
+
+.scrollArea::-webkit-scrollbar-thumb {
+ background-color: rgba(107, 107, 107, 0.7);
+ border-radius: 10px;
+ border: 2px solid #1c1c1e;
+}
+
+.scrollArea::-webkit-scrollbar-thumb:hover {
+ background-color: rgba(140, 140, 140, 0.9);
+}
+
+.scrollArea::-webkit-scrollbar-thumb:active {
+ background-color: rgba(160, 160, 160, 1);
+}
diff --git a/src/components/video-editor/timeline/TimelineEditor.tsx b/src/components/video-editor/timeline/TimelineEditor.tsx
index 144913c4..3336ce96 100644
--- a/src/components/video-editor/timeline/TimelineEditor.tsx
+++ b/src/components/video-editor/timeline/TimelineEditor.tsx
@@ -1,5 +1,32 @@
-import { useCallback, useEffect, useMemo, useRef, useState, type KeyboardEvent as ReactKeyboardEvent, type WheelEvent } from "react";
+import type { Range, Span } from "dnd-timeline";
import { useTimelineContext } from "dnd-timeline";
+import {
+ CaretDown as ChevronDown,
+ CaretUp as ChevronUp,
+ MagicWand as WandSparkles,
+ MagnifyingGlassPlus as ZoomIn,
+ Pause,
+ Play,
+ Plus,
+ Scissors,
+ SkipBack,
+ SkipForward,
+ SpeakerHigh as Volume2,
+ SpeakerLow as Volume1,
+ SpeakerX as VolumeX,
+} from "@phosphor-icons/react";
+import {
+ forwardRef,
+ useCallback,
+ useEffect,
+ useImperativeHandle,
+ useMemo,
+ useRef,
+ useState,
+ type WheelEvent,
+} from "react";
+import { toast } from "sonner";
+import { v4 as uuidv4 } from "uuid";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
@@ -7,83 +34,144 @@ import {
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
-import { Plus, Scissors, ZoomIn, MessageSquare, ChevronDown, Check, WandSparkles, Music, Crop } from "lucide-react";
-import { toast } from "sonner";
-import { cn } from "@/lib/utils";
-import { v4 as uuidv4 } from 'uuid';
-import { useScopedT } from "@/contexts/I18nContext";
+
import { useShortcuts } from "@/contexts/ShortcutsContext";
import { matchesShortcut } from "@/lib/shortcuts";
-import { ASPECT_RATIOS, type AspectRatio, getAspectRatioLabel, isCustomAspectRatio } from "@/utils/aspectRatioUtils";
-import { formatShortcut } from "@/utils/platformUtils";
-import { loadEditorPreferences, saveEditorPreferences } from "../editorPreferences";
-import TimelineWrapper from "./TimelineWrapper";
-import Row from "./Row";
+import { cn } from "@/lib/utils";
+import type { AspectRatio } from "@/utils/aspectRatioUtils";
+
+import { toFileUrl } from "../projectPersistence";
+import type {
+ AnnotationRegion,
+ AudioRegion,
+ ClipRegion,
+ CursorTelemetryPoint,
+ SpeedRegion,
+ TrimRegion,
+ ZoomFocus,
+ ZoomMode,
+ ZoomRegion,
+} from "../types";
+import AudioWaveform from "./AudioWaveform";
import Item from "./Item";
import KeyframeMarkers from "./KeyframeMarkers";
-import type { Range, Span } from "dnd-timeline";
-import type { ZoomRegion, TrimRegion, ClipRegion, AnnotationRegion, SpeedRegion, AudioRegion, CursorTelemetryPoint, ZoomFocus } from "../types";
-import { toFileUrl } from "../projectPersistence";
+import timelineStyles from "./Timeline.module.css";
+import TimelineWrapper from "./TimelineWrapper";
+import Track from "./Track";
+import { type AudioPeaksData, useAudioPeaks } from "./useAudioPeaks";
import { buildInteractionZoomSuggestions } from "./zoomSuggestionUtils";
-import { useAudioPeaks, type AudioPeaksData } from "./useAudioPeaks";
-import AudioWaveform from "./AudioWaveform";
const ZOOM_ROW_ID = "row-zoom";
const CLIP_ROW_ID = "row-clip";
const ANNOTATION_ROW_ID = "row-annotation";
const AUDIO_ROW_ID = "row-audio";
+const ANNOTATION_ROW_PREFIX = `${ANNOTATION_ROW_ID}-`;
+const AUDIO_ROW_PREFIX = "row-audio-";
const FALLBACK_RANGE_MS = 1000;
const TARGET_MARKER_COUNT = 12;
+
+function getAnnotationTrackRowId(trackIndex: number) {
+ return `${ANNOTATION_ROW_ID}-${Math.max(0, Math.floor(trackIndex))}`;
+}
+
+function isAnnotationTrackRowId(rowId: string) {
+ return rowId === ANNOTATION_ROW_ID || rowId.startsWith(ANNOTATION_ROW_PREFIX);
+}
+
+function getAnnotationTrackIndex(rowId: string) {
+ if (rowId === ANNOTATION_ROW_ID) {
+ return 0;
+ }
+
+ const parsed = Number.parseInt(rowId.slice(ANNOTATION_ROW_PREFIX.length), 10);
+ return Number.isFinite(parsed) ? Math.max(0, parsed) : 0;
+}
+
+function getAudioTrackRowId(trackIndex: number) {
+ return `${AUDIO_ROW_PREFIX}${Math.max(0, Math.floor(trackIndex))}`;
+}
+
+function isAudioTrackRowId(rowId: string) {
+ return rowId === AUDIO_ROW_ID || rowId.startsWith(AUDIO_ROW_PREFIX);
+}
+
+function getAudioTrackIndex(rowId: string) {
+ if (rowId === AUDIO_ROW_ID) {
+ return 0;
+ }
+
+ const parsed = Number.parseInt(rowId.slice(AUDIO_ROW_PREFIX.length), 10);
+ return Number.isFinite(parsed) ? Math.max(0, parsed) : 0;
+}
+
interface TimelineEditorProps {
- videoDuration: number;
- currentTime: number;
- onSeek?: (time: number) => void;
- cursorTelemetry?: CursorTelemetryPoint[];
- autoSuggestZoomsTrigger?: number;
- onAutoSuggestZoomsConsumed?: () => void;
- disableSuggestedZooms?: boolean;
- zoomRegions: ZoomRegion[];
- onZoomAdded: (span: Span) => void;
- onZoomSuggested?: (span: Span, focus: ZoomFocus) => void;
- onZoomSpanChange: (id: string, span: Span) => void;
- onZoomDelete: (id: string) => void;
- selectedZoomId: string | null;
- onSelectZoom: (id: string | null) => void;
- trimRegions?: TrimRegion[];
- onTrimAdded?: (span: Span) => void;
- onTrimSpanChange?: (id: string, span: Span) => void;
- onTrimDelete?: (id: string) => void;
- selectedTrimId?: string | null;
- onSelectTrim?: (id: string | null) => void;
- clipRegions?: ClipRegion[];
- onClipSplit?: (splitMs: number) => void;
- onClipSpanChange?: (id: string, span: Span) => void;
- onClipDelete?: (id: string) => void;
- selectedClipId?: string | null;
- onSelectClip?: (id: string | null) => void;
- annotationRegions?: AnnotationRegion[];
- onAnnotationAdded?: (span: Span) => void;
- onAnnotationSpanChange?: (id: string, span: Span) => void;
- onAnnotationDelete?: (id: string) => void;
- selectedAnnotationId?: string | null;
- onSelectAnnotation?: (id: string | null) => void;
- speedRegions?: SpeedRegion[];
- onSpeedAdded?: (span: Span) => void;
- onSpeedSpanChange?: (id: string, span: Span) => void;
- onSpeedDelete?: (id: string) => void;
- selectedSpeedId?: string | null;
- onSelectSpeed?: (id: string | null) => void;
- audioRegions?: AudioRegion[];
- onAudioAdded?: (span: Span, audioPath: string) => void;
- onAudioSpanChange?: (id: string, span: Span) => void;
- onAudioDelete?: (id: string) => void;
- selectedAudioId?: string | null;
- onSelectAudio?: (id: string | null) => void;
- aspectRatio: AspectRatio;
- onAspectRatioChange: (aspectRatio: AspectRatio) => void;
- onOpenCropEditor?: () => void;
- isCropped?: boolean;
- videoPath?: string | null;
+ videoDuration: number;
+ currentTime: number;
+ playheadTime?: number;
+ onSeek?: (time: number) => void;
+ cursorTelemetry?: CursorTelemetryPoint[];
+ autoSuggestZoomsTrigger?: number;
+ onAutoSuggestZoomsConsumed?: () => void;
+ disableSuggestedZooms?: boolean;
+ zoomRegions: ZoomRegion[];
+ onZoomAdded: (span: Span) => void;
+ onZoomSuggested?: (span: Span, focus: ZoomFocus) => void;
+ onZoomSpanChange: (id: string, span: Span) => void;
+ onZoomDelete: (id: string) => void;
+ selectedZoomId: string | null;
+ onSelectZoom: (id: string | null) => void;
+ trimRegions?: TrimRegion[];
+ onTrimAdded?: (span: Span) => void;
+ onTrimSpanChange?: (id: string, span: Span) => void;
+ onTrimDelete?: (id: string) => void;
+ selectedTrimId?: string | null;
+ onSelectTrim?: (id: string | null) => void;
+ clipRegions?: ClipRegion[];
+ onClipSplit?: (splitMs: number) => void;
+ onClipSpanChange?: (id: string, span: Span) => void;
+ onClipDelete?: (id: string) => void;
+ selectedClipId?: string | null;
+ onSelectClip?: (id: string | null) => void;
+ annotationRegions?: AnnotationRegion[];
+ onAnnotationAdded?: (span: Span, trackIndex?: number) => void;
+ onAnnotationSpanChange?: (id: string, span: Span) => void;
+ onAnnotationDelete?: (id: string) => void;
+ selectedAnnotationId?: string | null;
+ onSelectAnnotation?: (id: string | null) => void;
+ speedRegions?: SpeedRegion[];
+ onSpeedAdded?: (span: Span) => void;
+ onSpeedSpanChange?: (id: string, span: Span) => void;
+ onSpeedDelete?: (id: string) => void;
+ selectedSpeedId?: string | null;
+ onSelectSpeed?: (id: string | null) => void;
+ audioRegions?: AudioRegion[];
+ onAudioAdded?: (span: Span, audioPath: string, trackIndex?: number) => void;
+ onAudioSpanChange?: (id: string, span: Span) => void;
+ onAudioDelete?: (id: string) => void;
+ selectedAudioId?: string | null;
+ onSelectAudio?: (id: string | null) => void;
+ aspectRatio?: AspectRatio;
+ onAspectRatioChange?: (aspectRatio: AspectRatio) => void;
+ onOpenCropEditor?: () => void;
+ isCropped?: boolean;
+ videoPath?: string | null;
+ isPlaying?: boolean;
+ onTogglePlayPause?: () => void;
+ volume?: number;
+ onVolumeChange?: (volume: number) => void;
+ hideToolbar?: boolean;
+}
+
+export interface TimelineEditorHandle {
+ addZoom: () => void;
+ suggestZooms: () => void;
+ splitClip: () => void;
+ addAnnotation: (trackIndex?: number) => void;
+ addAudio: () => void;
+ isCollapsed: boolean;
+ toggleCollapsed: () => void;
+ keyframes: { id: string; time: number }[];
+ formatTime: (seconds: number) => string;
}
interface TimelineScaleConfig {
@@ -93,13 +181,14 @@ interface TimelineScaleConfig {
}
interface TimelineRenderItem {
- id: string;
- rowId: string;
- span: Span;
- label: string;
- zoomDepth?: number;
- speedValue?: number;
- variant: 'zoom' | 'trim' | 'clip' | 'annotation' | 'speed' | 'audio';
+ id: string;
+ rowId: string;
+ span: Span;
+ label: string;
+ zoomDepth?: number;
+ zoomMode?: ZoomMode;
+ speedValue?: number;
+ variant: "zoom" | "trim" | "clip" | "annotation" | "speed" | "audio";
}
const SCALE_CANDIDATES = [
@@ -214,25 +303,87 @@ function PlaybackCursor({
onSeek,
timelineRef,
keyframes = [],
+ onPanTimeline,
}: {
currentTimeMs: number;
videoDurationMs: number;
onSeek?: (time: number) => void;
timelineRef: React.RefObject<HTMLDivElement>;
keyframes?: { id: string; time: number }[];
+ onPanTimeline?: (deltaMs: number) => void;
}) {
const { sidebarWidth, direction, range, valueToPixels, pixelsToValue } = useTimelineContext();
const sideProperty = direction === "rtl" ? "right" : "left";
const [isDragging, setIsDragging] = useState(false);
+ const edgeScrubRef = useRef<number | null>(null);
+ const lastMouseXRef = useRef(0);
useEffect(() => {
if (!isDragging) return;
+ const edgeZone = 60; // px from edge to trigger scrub
+ const scrubSpeed = 0.015; // fraction of visible range per frame
+
+ const startEdgeScrub = () => {
+ if (edgeScrubRef.current !== null) return;
+ const tick = () => {
+ const el = timelineRef.current;
+ if (!el || !onPanTimeline || !onSeek) {
+ edgeScrubRef.current = null;
+ return;
+ }
+ const rect = el.getBoundingClientRect();
+ const x = lastMouseXRef.current;
+ const leftEdge = rect.left + sidebarWidth;
+ const rightEdge = rect.right;
+ const visibleMs = range.end - range.start;
+ let delta = 0;
+ if (x < leftEdge + edgeZone) {
+ delta = -scrubSpeed * visibleMs * (1 - Math.max(0, x - leftEdge) / edgeZone);
+ } else if (x > rightEdge - edgeZone) {
+ delta = scrubSpeed * visibleMs * (1 - Math.max(0, rightEdge - x) / edgeZone);
+ }
+ if (delta !== 0) {
+ onPanTimeline(delta);
+ // Also move the playhead to track the edge
+ const clickX = lastMouseXRef.current - rect.left - sidebarWidth;
+ const relativeMs = pixelsToValue(clickX);
+ const absoluteMs = Math.max(
+ 0,
+ Math.min(range.start + delta + relativeMs, videoDurationMs),
+ );
+ onSeek(absoluteMs / 1000);
+ }
+ edgeScrubRef.current = requestAnimationFrame(tick);
+ };
+ edgeScrubRef.current = requestAnimationFrame(tick);
+ };
+
+ const stopEdgeScrub = () => {
+ if (edgeScrubRef.current !== null) {
+ cancelAnimationFrame(edgeScrubRef.current);
+ edgeScrubRef.current = null;
+ }
+ };
+
const handleMouseMove = (e: MouseEvent) => {
if (!timelineRef.current || !onSeek) return;
+ lastMouseXRef.current = e.clientX;
const rect = timelineRef.current.getBoundingClientRect();
const clickX = e.clientX - rect.left - sidebarWidth;
+ const leftEdge = rect.left + sidebarWidth;
+ const rightEdge = rect.right;
+
+ // Start/stop edge scrubbing
+ if (
+ onPanTimeline &&
+ (e.clientX < leftEdge + edgeZone || e.clientX > rightEdge - edgeZone)
+ ) {
+ startEdgeScrub();
+ } else {
+ stopEdgeScrub();
+ }
// Allow dragging outside to 0 or max, but clamp the value
const relativeMs = pixelsToValue(clickX);
@@ -256,6 +407,7 @@ function PlaybackCursor({
const handleMouseUp = () => {
setIsDragging(false);
+ stopEdgeScrub();
document.body.style.cursor = "";
};
@@ -266,6 +418,7 @@ function PlaybackCursor({
return () => {
window.removeEventListener("mousemove", handleMouseMove);
window.removeEventListener("mouseup", handleMouseUp);
+ stopEdgeScrub();
document.body.style.cursor = "";
};
}, [
@@ -278,6 +431,7 @@ function PlaybackCursor({
videoDurationMs,
pixelsToValue,
keyframes,
+ onPanTimeline,
]);
if (videoDurationMs <= 0 || currentTimeMs < 0) {
@@ -301,7 +455,7 @@ function PlaybackCursor({
}}
>
<div
- className="absolute top-0 bottom-0 w-[2px] bg-[#2563EB] shadow-[0_0_10px_rgba(37,99,235,0.5)] cursor-ew-resize pointer-events-auto hover:shadow-[0_0_15px_rgba(37,99,235,0.7)] transition-shadow"
+ className="absolute top-0 bottom-0 w-[2px] bg-white/80 shadow-[0_0_8px_rgba(255,255,255,0.15)] cursor-ew-resize pointer-events-auto hover:shadow-[0_0_12px_rgba(255,255,255,0.25)] transition-shadow group/handle"
style={{
[sideProperty]: `${offset}px`,
}}
@@ -314,10 +468,13 @@ function PlaybackCursor({
className="absolute -top-1 left-1/2 -translate-x-1/2 hover:scale-125 transition-transform"
style={{ width: "16px", height: "16px" }}
>
- <div className="w-3 h-3 mx-auto mt-[2px] bg-[#2563EB] rotate-45 rounded-sm shadow-lg border border-white/20" />
+ <div className="w-3 h-3 mx-auto mt-[2px] bg-white rotate-45 rounded-sm shadow-lg border border-white/30" />
+ </div>
+ <div className="absolute -top-7 left-1/2 -translate-x-1/2 px-1.5 py-0.5 rounded-md bg-white text-[10px] text-black font-medium tabular-nums whitespace-nowrap shadow-lg pointer-events-none opacity-0 group-hover/handle:opacity-100 transition-opacity">
+ {formatPlayheadTime(clampedTime)}
</div>
{isDragging && (
- <div className="absolute -top-6 left-1/2 -translate-x-1/2 px-1.5 py-0.5 rounded bg-black/80 text-[10px] text-white/90 font-medium tabular-nums whitespace-nowrap border border-white/10 shadow-lg pointer-events-none">
+ <div className="absolute -top-7 left-1/2 -translate-x-1/2 px-1.5 py-0.5 rounded-md bg-white text-[10px] text-black font-medium tabular-nums whitespace-nowrap shadow-lg pointer-events-none">
{formatPlayheadTime(clampedTime)}
</div>
)}
@@ -394,103 +551,189 @@ function TimelineAxis({
};
}, [intervalMs, range.end, range.start, videoDurationMs]);
+ return (
+ <div className="relative h-9 overflow-hidden border-b border-[rgba(255,255,255,0.06)] bg-transparent select-none">
+ {sidebarWidth > 0 ? (
+ <div
+ className="absolute inset-y-0 z-[1] border-r border-[rgba(255,255,255,0.06)] bg-transparent"
+ style={{ width: `${sidebarWidth}px`, [sideProperty]: 0 }}
+ />
+ ) : null}
+ <div
+ className="relative h-full"
+ style={{
+ [sideProperty === "right" ? "marginRight" : "marginLeft"]: `${sidebarWidth}px`,
+ }}
+ >
+ {/* Minor Ticks */}
+ {markers.minorTicks.map((time) => {
+ const offset = valueToPixels(time - range.start);
+ return (
+ <div
+ key={`minor-${time}`}
+ className="absolute bottom-1 h-1 w-[1px] bg-white/5"
+ style={{ [sideProperty]: `${offset}px` }}
+ />
+ );
+ })}
+
+ {/* Major Markers */}
+ {markers.markers.map((marker) => {
+ const offset = valueToPixels(marker.time - range.start);
+ const markerStyle: React.CSSProperties = {
+ position: "absolute",
+ bottom: 0,
+ height: "100%",
+ display: "flex",
+ flexDirection: "row",
+ alignItems: "flex-end",
+ [sideProperty]: `${offset}px`,
+ transform: "translateX(-50%)",
+ };
+
+ return (
+ <div key={marker.time} style={markerStyle}>
+ <div className="flex flex-col items-center pb-1">
+ <div className="mb-1.5 h-[5px] w-[5px] rounded-full bg-white/30" />
+ <span
+ className={cn(
+ "text-[10px] font-medium tabular-nums tracking-tight",
+ marker.time === currentTimeMs
+ ? "text-[#2563EB]"
+ : "text-white/40",
+ )}
+ >
+ {marker.label}
+ </span>
+ </div>
+ </div>
+ );
+ })}
+ </div>
+ </div>
+ );
+}
+
+function ClipMarkerOverlay({ videoDurationMs }: { videoDurationMs: number }) {
+ const { direction, range, valueToPixels } = useTimelineContext();
+ const sideProperty = direction === "rtl" ? "right" : "left";
+
+ const { intervalMs } = useMemo(
+ () => calculateAxisScale(range.end - range.start),
+ [range.end, range.start],
+ );
+
+ const markers = useMemo(() => {
+ if (intervalMs <= 0) return [];
+ const maxTime = videoDurationMs > 0 ? videoDurationMs : range.end;
+ const visibleStart = Math.max(0, range.start);
+ const visibleEnd = Math.min(range.end, maxTime);
+ const firstMarker = Math.ceil(visibleStart / intervalMs) * intervalMs;
+ const result: { time: number; offset: number }[] = [];
+ for (let time = firstMarker; time <= maxTime; time += intervalMs) {
+ if (time > visibleStart && time < visibleEnd) {
+ result.push({ time: Math.round(time), offset: valueToPixels(Math.round(time) - range.start) });
+ }
+ }
+ return result;
+ }, [intervalMs, range.start, range.end, videoDurationMs, valueToPixels]);
+
+ return (
+ <div className="pointer-events-none absolute inset-0 z-[1]">
+ {markers.map(({ time, offset }) => (
+ <div
+ key={time}
+ className="absolute w-px"
+ style={{
+ top: "7.5%",
+ bottom: "7.5%",
+ [sideProperty]: `${offset}px`,
+ background:
+ "linear-gradient(to bottom, transparent 0%, rgba(255,255,255,0.10) 35%, rgba(255,255,255,0.10) 65%, transparent 100%)",
+ }}
+ />
+ ))}
+ </div>
+ );
+}
+
+function HoverCursor({ hoverX }: { hoverX: number }) {
+ const { sidebarWidth, direction, range, pixelsToValue } = useTimelineContext();
+ const sideProperty = direction === "rtl" ? "right" : "left";
+ const timeMs = range.start + pixelsToValue(hoverX);
return (
<div
- className="h-8 bg-[#161619] border-b border-white/10 relative overflow-hidden select-none"
- style={{
- [sideProperty === "right" ? "marginRight" : "marginLeft"]: `${sidebarWidth}px`,
- }}
+ className="pointer-events-none absolute top-0 bottom-0 z-40"
+ style={{ [sideProperty === "right" ? "marginRight" : "marginLeft"]: `${sidebarWidth - 1}px` }}
>
- {/* Minor Ticks */}
- {markers.minorTicks.map((time) => {
- const offset = valueToPixels(time - range.start);
- return (
- <div
- key={`minor-${time}`}
- className="absolute bottom-0 h-1 w-[1px] bg-white/5"
- style={{ [sideProperty]: `${offset}px` }}
- />
- );
- })}
-
- {/* Major Markers */}
- {markers.markers.map((marker) => {
- const offset = valueToPixels(marker.time - range.start);
- const markerStyle: React.CSSProperties = {
- position: "absolute",
- bottom: 0,
- height: "100%",
- display: "flex",
- flexDirection: "row",
- alignItems: "flex-end",
- [sideProperty]: `${offset}px`,
- };
-
- return (
- <div key={marker.time} style={markerStyle}>
- <div className="flex flex-col items-center pb-1">
- <div className="h-2 w-[1px] bg-white/20 mb-1" />
- <span
- className={cn(
- "text-[10px] font-medium tabular-nums tracking-tight",
- marker.time === currentTimeMs ? "text-[#2563EB]" : "text-slate-500",
- )}
- >
- {marker.label}
- </span>
- </div>
- </div>
- );
- })}
+ <div
+ className="absolute top-0 bottom-0 w-[2px] bg-white/30"
+ style={{ [sideProperty]: `${hoverX}px` }}
+ >
+ <div
+ className="absolute -top-1 left-1/2 -translate-x-1/2"
+ style={{ width: "16px", height: "16px" }}
+ >
+ <div className="w-3 h-3 mx-auto mt-[2px] bg-white/30 rotate-45 rounded-sm border border-white/20" />
+ </div>
+ <div className="absolute -top-7 left-1/2 -translate-x-1/2 px-1.5 py-0.5 rounded-md bg-white/20 text-[10px] text-white/60 font-medium tabular-nums whitespace-nowrap">
+ {formatPlayheadTime(timeMs)}
+ </div>
+ </div>
</div>
);
}
function Timeline({
- items,
- videoDurationMs,
- currentTimeMs,
- onSeek,
- onSelectZoom,
- onSelectTrim,
- onSelectClip,
- onSelectAnnotation,
- onSelectSpeed,
- onSelectAudio,
- selectedZoomId,
- selectedTrimId: _selectedTrimId,
- selectedClipId,
- selectedAnnotationId,
- selectedSpeedId: _selectedSpeedId,
- selectedAudioId,
- selectAllBlocksActive = false,
- onClearBlockSelection,
- keyframes = [],
- audioPeaks,
+ items,
+ videoDurationMs,
+ currentTimeMs,
+ onSeek,
+ onSelectZoom,
+ onSelectTrim,
+ onSelectClip,
+ onSelectAnnotation,
+ onSelectSpeed,
+ onSelectAudio,
+ selectedZoomId,
+ selectedTrimId: _selectedTrimId,
+ selectedClipId,
+ selectedAnnotationId,
+ selectedSpeedId: _selectedSpeedId,
+ selectedAudioId,
+ selectAllBlocksActive = false,
+ onClearBlockSelection,
+ keyframes = [],
+ audioPeaks,
+ onPanTimeline,
+ hiddenTrackIds,
}: {
- items: TimelineRenderItem[];
- videoDurationMs: number;
- currentTimeMs: number;
- onSeek?: (time: number) => void;
- onSelectZoom?: (id: string | null) => void;
- onSelectTrim?: (id: string | null) => void;
- onSelectClip?: (id: string | null) => void;
- onSelectAnnotation?: (id: string | null) => void;
- onSelectSpeed?: (id: string | null) => void;
- onSelectAudio?: (id: string | null) => void;
- selectedZoomId: string | null;
- selectedTrimId?: string | null;
- selectedClipId?: string | null;
- selectedAnnotationId?: string | null;
- selectedSpeedId?: string | null;
- selectedAudioId?: string | null;
- selectAllBlocksActive?: boolean;
- onClearBlockSelection?: () => void;
- keyframes?: { id: string; time: number }[];
- audioPeaks?: AudioPeaksData | null;
+ items: TimelineRenderItem[];
+ videoDurationMs: number;
+ currentTimeMs: number;
+ onSeek?: (time: number) => void;
+ onSelectZoom?: (id: string | null) => void;
+ onSelectTrim?: (id: string | null) => void;
+ onSelectClip?: (id: string | null) => void;
+ onSelectAnnotation?: (id: string | null) => void;
+ onSelectSpeed?: (id: string | null) => void;
+ onSelectAudio?: (id: string | null) => void;
+ selectedZoomId: string | null;
+ selectedTrimId?: string | null;
+ selectedClipId?: string | null;
+ selectedAnnotationId?: string | null;
+ selectedSpeedId?: string | null;
+ selectedAudioId?: string | null;
+ selectAllBlocksActive?: boolean;
+ onClearBlockSelection?: () => void;
+ keyframes?: { id: string; time: number }[];
+ audioPeaks?: AudioPeaksData | null;
+ onPanTimeline?: (deltaMs: number) => void;
+ hiddenTrackIds?: Set<string>;
}) {
const { setTimelineRef, style, sidebarWidth, range, pixelsToValue } = useTimelineContext();
const localTimelineRef = useRef<HTMLDivElement | null>(null);
+ const [hoverX, setHoverX] = useState<number | null>(null);
const setRefs = useCallback(
(node: HTMLDivElement | null) => {
@@ -504,15 +747,15 @@ function Timeline({
(e: React.MouseEvent<HTMLDivElement>) => {
if (!onSeek || videoDurationMs <= 0) return;
- // Only clear selection if clicking on empty space (not on items)
- // This is handled by event propagation - items stop propagation
- onSelectZoom?.(null);
- onSelectTrim?.(null);
- onSelectClip?.(null);
- onSelectAnnotation?.(null);
- onSelectSpeed?.(null);
- onSelectAudio?.(null);
- onClearBlockSelection?.();
+ // Only clear selection if clicking on empty space (not on items)
+ // This is handled by event propagation - items stop propagation
+ onSelectZoom?.(null);
+ onSelectTrim?.(null);
+ onSelectClip?.(null);
+ onSelectAnnotation?.(null);
+ onSelectSpeed?.(null);
+ onSelectAudio?.(null);
+ onClearBlockSelection?.();
const rect = e.currentTarget.getBoundingClientRect();
const clickX = e.clientX - rect.left - sidebarWidth;
@@ -523,23 +766,63 @@ function Timeline({
const absoluteMs = Math.max(0, Math.min(range.start + relativeMs, videoDurationMs));
const timeInSeconds = absoluteMs / 1000;
- onSeek(timeInSeconds);
- }, [onSeek, onSelectZoom, onSelectTrim, onSelectClip, onSelectAnnotation, onSelectSpeed, onSelectAudio, videoDurationMs, sidebarWidth, range.start, pixelsToValue]);
-
- const zoomItems = items.filter(item => item.rowId === ZOOM_ROW_ID);
+ onSeek(timeInSeconds);
+ },
+ [
+ onSeek,
+ onSelectZoom,
+ onSelectTrim,
+ onSelectClip,
+ onSelectAnnotation,
+ onSelectSpeed,
+ onSelectAudio,
+ videoDurationMs,
+ sidebarWidth,
+ range.start,
+ pixelsToValue,
+ onClearBlockSelection,
+ ],
+ );
- const clipItems = items.filter(item => item.rowId === CLIP_ROW_ID);
- const annotationItems = items.filter(item => item.rowId === ANNOTATION_ROW_ID);
- const audioItems = items.filter(item => item.rowId === AUDIO_ROW_ID);
+ const zoomItems = items.filter((item) => item.rowId === ZOOM_ROW_ID);
+ const clipItems = items.filter((item) => item.rowId === CLIP_ROW_ID);
+ const annotationItems = items.filter((item) => isAnnotationTrackRowId(item.rowId));
+ const audioItems = items.filter((item) => isAudioTrackRowId(item.rowId));
+ const audioRowIds = useMemo(
+ () =>
+ Array.from(
+ new Set(
+ audioItems.map((item) => getAudioTrackRowId(getAudioTrackIndex(item.rowId))),
+ ),
+ ).sort((left, right) => getAudioTrackIndex(left) - getAudioTrackIndex(right)),
+ [audioItems],
+ );
+ const annotationRowIds = useMemo(
+ () =>
+ Array.from(
+ new Set(
+ annotationItems.map((item) =>
+ getAnnotationTrackRowId(getAnnotationTrackIndex(item.rowId)),
+ ),
+ ),
+ ).sort((left, right) => getAnnotationTrackIndex(left) - getAnnotationTrackIndex(right)),
+ [annotationItems],
+ );
return (
<div
ref={setRefs}
style={style}
- className="select-none bg-[#17171a] h-full min-h-0 relative cursor-pointer group flex flex-col"
+ className="group relative flex h-full min-h-0 cursor-pointer flex-col overflow-hidden select-none bg-transparent"
onClick={handleTimelineClick}
+ onMouseMove={(e) => {
+ const rect = e.currentTarget.getBoundingClientRect();
+ const x = e.clientX - rect.left - sidebarWidth;
+ if (x >= 0) setHoverX(x);
+ else setHoverX(null);
+ }}
+ onMouseLeave={() => setHoverX(null)}
>
- <div className="absolute inset-0 bg-[linear-gradient(to_right,#ffffff03_1px,transparent_1px)] bg-[length:20px_100%] pointer-events-none" />
<TimelineAxis videoDurationMs={videoDurationMs} currentTimeMs={currentTimeMs} />
<PlaybackCursor
currentTimeMs={currentTimeMs}
@@ -547,1143 +830,1557 @@ function Timeline({
onSeek={onSeek}
timelineRef={localTimelineRef}
keyframes={keyframes}
+ onPanTimeline={onPanTimeline}
/>
+ {hoverX !== null && <HoverCursor hoverX={hoverX} />}
+
+ <div className="relative z-10 flex flex-1 min-h-0 flex-col">
+ <Track
+ id={CLIP_ROW_ID}
+ isEmpty={clipItems.length === 0}
+ hint="Press C to split clip"
+ trackStyle={{ background: "rgba(255,255,255,0)" }}
+ >
+ {audioPeaks && <AudioWaveform peaks={audioPeaks} />}
+ <ClipMarkerOverlay videoDurationMs={videoDurationMs} />
+ {clipItems.map((item) => (
+ <Item
+ id={item.id}
+ key={item.id}
+ rowId={item.rowId}
+ span={item.span}
+ isSelected={selectAllBlocksActive || item.id === selectedClipId}
+ onSelect={() => onSelectClip?.(item.id)}
+ variant="clip"
+ >
+ {item.label}
+ </Item>
+ ))}
+ </Track>
+
+ {!hiddenTrackIds?.has(ZOOM_ROW_ID) && (
+ <Track
+ id={ZOOM_ROW_ID}
+ isEmpty={zoomItems.length === 0}
+ hint="Press Z to add zoom"
+ trackStyle={{ background: "transparent" }}
+ >
+ {zoomItems.map((item) => (
+ <Item
+ id={item.id}
+ key={item.id}
+ rowId={item.rowId}
+ span={item.span}
+ isSelected={selectAllBlocksActive || item.id === selectedZoomId}
+ onSelect={() => onSelectZoom?.(item.id)}
+ zoomDepth={item.zoomDepth}
+ zoomMode={item.zoomMode}
+ variant="zoom"
+ >
+ {item.label}
+ </Item>
+ ))}
+ </Track>
+ )}
- <div className="relative z-10 flex flex-1 min-h-0 flex-col">
- <Row id={CLIP_ROW_ID} isEmpty={clipItems.length === 0} hint="Press C to split clip">
- {audioPeaks && <AudioWaveform peaks={audioPeaks} />}
- {clipItems.map((item) => (
- <Item
- id={item.id}
- key={item.id}
- rowId={item.rowId}
- span={item.span}
- isSelected={selectAllBlocksActive || item.id === selectedClipId}
- onSelect={() => onSelectClip?.(item.id)}
- variant="clip"
- >
- {item.label}
- </Item>
- ))}
- </Row>
-
- <Row id={ZOOM_ROW_ID} isEmpty={zoomItems.length === 0} hint="Press Z to add zoom">
- {zoomItems.map((item) => (
- <Item
- id={item.id}
- key={item.id}
- rowId={item.rowId}
- span={item.span}
- isSelected={selectAllBlocksActive || item.id === selectedZoomId}
- onSelect={() => onSelectZoom?.(item.id)}
- zoomDepth={item.zoomDepth}
- variant="zoom"
- >
- {item.label}
- </Item>
- ))}
- </Row>
-
-
- <Row
- id={ANNOTATION_ROW_ID}
- isEmpty={annotationItems.length === 0}
- hint="Press A to add annotation"
- >
- {annotationItems.map((item) => (
- <Item
- id={item.id}
- key={item.id}
- rowId={item.rowId}
- span={item.span}
- isSelected={selectAllBlocksActive || item.id === selectedAnnotationId}
- onSelect={() => onSelectAnnotation?.(item.id)}
- variant="annotation"
- >
- {item.label}
- </Item>
- ))}
- </Row>
-
- <Row id={AUDIO_ROW_ID} isEmpty={audioItems.length === 0} hint="Click music icon to add audio">
- {audioItems.map((item) => (
- <Item
- id={item.id}
- key={item.id}
- rowId={item.rowId}
- span={item.span}
- isSelected={selectAllBlocksActive || item.id === selectedAudioId}
- onSelect={() => onSelectAudio?.(item.id)}
- variant="audio"
- >
- {item.label}
- </Item>
- ))}
- </Row>
- </div>
- </div>
- );
+ {!hiddenTrackIds?.has(ANNOTATION_ROW_ID) &&
+ annotationRowIds.map((rowId) => {
+ const rowItems = annotationItems.filter(
+ (item) =>
+ getAnnotationTrackRowId(getAnnotationTrackIndex(item.rowId)) ===
+ rowId,
+ );
+
+ return (
+ <Track key={rowId} id={rowId} isEmpty={false}>
+ {rowItems.map((item) => (
+ <Item
+ id={item.id}
+ key={item.id}
+ rowId={item.rowId}
+ span={item.span}
+ isSelected={
+ selectAllBlocksActive ||
+ item.id === selectedAnnotationId
+ }
+ onSelect={() => onSelectAnnotation?.(item.id)}
+ variant="annotation"
+ >
+ {item.label}
+ </Item>
+ ))}
+ </Track>
+ );
+ })}
+
+ {!hiddenTrackIds?.has(AUDIO_ROW_ID) &&
+ audioRowIds.map((rowId) => {
+ const rowItems = audioItems.filter(
+ (item) => getAudioTrackRowId(getAudioTrackIndex(item.rowId)) === rowId,
+ );
+ return (
+ <Track
+ key={rowId}
+ id={rowId}
+ isEmpty={false}
+ trackStyle={{ background: "rgba(255,255,255,0.054)" }}
+ >
+ {rowItems.map((item) => (
+ <Item
+ id={item.id}
+ key={item.id}
+ rowId={item.rowId}
+ span={item.span}
+ isSelected={
+ selectAllBlocksActive || item.id === selectedAudioId
+ }
+ onSelect={() => onSelectAudio?.(item.id)}
+ variant="audio"
+ >
+ {item.label}
+ </Item>
+ ))}
+ </Track>
+ );
+ })}
+ </div>
+ </div>
+ );
}
-export default function TimelineEditor({
- videoDuration,
- currentTime,
- onSeek,
- cursorTelemetry = [],
- autoSuggestZoomsTrigger = 0,
- onAutoSuggestZoomsConsumed,
- disableSuggestedZooms = false,
- zoomRegions,
- onZoomAdded,
- onZoomSuggested,
- onZoomSpanChange,
- onZoomDelete,
- selectedZoomId,
- onSelectZoom,
- trimRegions = [],
- onTrimAdded,
- onTrimSpanChange,
- onTrimDelete,
- selectedTrimId,
- onSelectTrim,
- clipRegions = [],
- onClipSplit,
- onClipSpanChange,
- onClipDelete,
- selectedClipId,
- onSelectClip,
- annotationRegions = [],
- onAnnotationAdded,
- onAnnotationSpanChange,
- onAnnotationDelete,
- selectedAnnotationId,
- onSelectAnnotation,
- speedRegions = [],
- onSpeedAdded,
- onSpeedSpanChange,
- onSpeedDelete,
- selectedSpeedId,
- onSelectSpeed,
- audioRegions = [],
- onAudioAdded,
- onAudioSpanChange,
- onAudioDelete,
- selectedAudioId,
- onSelectAudio,
- aspectRatio,
- onAspectRatioChange,
- onOpenCropEditor,
- isCropped = false,
- videoPath,
-}: TimelineEditorProps) {
- const t = useScopedT("settings");
- const initialEditorPreferences = useMemo(() => loadEditorPreferences(), []);
- const totalMs = useMemo(() => Math.max(0, Math.round(videoDuration * 1000)), [videoDuration]);
- const currentTimeMs = useMemo(() => Math.round(currentTime * 1000), [currentTime]);
- const timelineScale = useMemo(() => calculateTimelineScale(videoDuration), [videoDuration]);
- const safeMinDurationMs = useMemo(
- () => (totalMs > 0 ? Math.min(timelineScale.minItemDurationMs, totalMs) : timelineScale.minItemDurationMs),
- [timelineScale.minItemDurationMs, totalMs],
- );
-
- const [range, setRange] = useState<Range>(() => createInitialRange(totalMs));
- const [keyframes, setKeyframes] = useState<{ id: string; time: number }[]>([]);
- const [selectedKeyframeId, setSelectedKeyframeId] = useState<string | null>(null);
- const [selectAllBlocksActive, setSelectAllBlocksActive] = useState(false);
- const [customAspectWidth, setCustomAspectWidth] = useState(initialEditorPreferences.customAspectWidth);
- const [customAspectHeight, setCustomAspectHeight] = useState(initialEditorPreferences.customAspectHeight);
- const [scrollLabels, setScrollLabels] = useState({
- pan: 'Shift + Ctrl + Scroll',
- zoom: 'Ctrl + Scroll'
- });
- const isTimelineFocusedRef = useRef(false);
- const timelineContainerRef = useRef<HTMLDivElement>(null);
- const { shortcuts: keyShortcuts, isMac } = useShortcuts();
- const audioPeaks = useAudioPeaks(videoPath);
-
- useEffect(() => {
- setRange(createInitialRange(totalMs));
- }, [totalMs]);
-
- useEffect(() => {
- if (aspectRatio === 'native') {
- return;
- }
- const [width, height] = aspectRatio.split(':');
- if (width && height) {
- setCustomAspectWidth(width);
- setCustomAspectHeight(height);
- }
- }, [aspectRatio]);
-
- useEffect(() => {
- saveEditorPreferences({
- customAspectWidth,
- customAspectHeight,
- });
- }, [customAspectHeight, customAspectWidth]);
-
- const applyCustomAspectRatio = useCallback(() => {
- const width = Number.parseInt(customAspectWidth, 10);
- const height = Number.parseInt(customAspectHeight, 10);
- if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) {
- toast.error('Custom aspect ratio must be positive numbers.');
- return;
- }
- onAspectRatioChange(`${width}:${height}` as AspectRatio);
- }, [customAspectHeight, customAspectWidth, onAspectRatioChange]);
-
- const handleCustomAspectRatioKeyDown = useCallback((event: ReactKeyboardEvent<HTMLInputElement>) => {
- // Prevent Radix DropdownMenu typeahead from selecting preset items while typing.
- event.stopPropagation();
- if (event.key === 'Enter') {
- event.preventDefault();
- applyCustomAspectRatio();
- }
- }, [applyCustomAspectRatio]);
-
- useEffect(() => {
- formatShortcut(['shift', 'mod', 'Scroll']).then(pan => {
- formatShortcut(['mod', 'Scroll']).then(zoom => {
- setScrollLabels({ pan, zoom });
- });
- });
- }, []);
-
- // Add keyframe at current playhead position
- const addKeyframe = useCallback(() => {
- if (totalMs === 0) return;
- const time = Math.max(0, Math.min(currentTimeMs, totalMs));
- if (keyframes.some(kf => Math.abs(kf.time - time) < 1)) return;
- setKeyframes(prev => [...prev, { id: uuidv4(), time }]);
- }, [currentTimeMs, totalMs, keyframes]);
-
- // Delete selected keyframe
- const deleteSelectedKeyframe = useCallback(() => {
- if (!selectedKeyframeId) return;
- setKeyframes(prev => prev.filter(kf => kf.id !== selectedKeyframeId));
- setSelectedKeyframeId(null);
- }, [selectedKeyframeId]);
-
- // Move keyframe to new time position
- const handleKeyframeMove = useCallback((id: string, newTime: number) => {
- setKeyframes(prev => prev.map(kf => kf.id === id ? { ...kf, time: Math.max(0, Math.min(newTime, totalMs)) } : kf));
- }, [totalMs]);
-
- // Delete selected zoom item
- const deleteSelectedZoom = useCallback(() => {
- if (!selectedZoomId) return;
- onZoomDelete(selectedZoomId);
- onSelectZoom(null);
- }, [selectedZoomId, onZoomDelete, onSelectZoom]);
-
- // Delete selected trim item
- const deleteSelectedTrim = useCallback(() => {
- if (!selectedTrimId || !onTrimDelete || !onSelectTrim) return;
- onTrimDelete(selectedTrimId);
- onSelectTrim(null);
- }, [selectedTrimId, onTrimDelete, onSelectTrim]);
-
- const deleteSelectedClip = useCallback(() => {
- if (!selectedClipId || !onClipDelete || !onSelectClip) return;
- onClipDelete(selectedClipId);
- onSelectClip(null);
- }, [selectedClipId, onClipDelete, onSelectClip]);
-
- const deleteSelectedAnnotation = useCallback(() => {
- if (!selectedAnnotationId || !onAnnotationDelete || !onSelectAnnotation) return;
- onAnnotationDelete(selectedAnnotationId);
- onSelectAnnotation(null);
- }, [selectedAnnotationId, onAnnotationDelete, onSelectAnnotation]);
-
- const deleteSelectedSpeed = useCallback(() => {
- if (!selectedSpeedId || !onSpeedDelete || !onSelectSpeed) return;
- onSpeedDelete(selectedSpeedId);
- onSelectSpeed(null);
- }, [selectedSpeedId, onSpeedDelete, onSelectSpeed]);
-
- const deleteSelectedAudio = useCallback(() => {
- if (!selectedAudioId || !onAudioDelete || !onSelectAudio) return;
- onAudioDelete(selectedAudioId);
- onSelectAudio(null);
- }, [selectedAudioId, onAudioDelete, onSelectAudio]);
-
- const clearSelectedBlocks = useCallback(() => {
- onSelectZoom(null);
- onSelectTrim?.(null);
- onSelectClip?.(null);
- onSelectAnnotation?.(null);
- onSelectSpeed?.(null);
- onSelectAudio?.(null);
- setSelectAllBlocksActive(false);
- }, [onSelectAnnotation, onSelectAudio, onSelectClip, onSelectSpeed, onSelectTrim, onSelectZoom]);
-
- const hasAnyTimelineBlocks =
- zoomRegions.length > 0 ||
- trimRegions.length > 0 ||
- clipRegions.length > 0 ||
- annotationRegions.length > 0 ||
- speedRegions.length > 0 ||
- audioRegions.length > 0;
-
- const deleteAllBlocks = useCallback(() => {
- const zoomIds = zoomRegions.map((region) => region.id);
- const trimIds = trimRegions.map((region) => region.id);
- const clipIds = clipRegions.map((region) => region.id);
- const annotationIds = annotationRegions.map((region) => region.id);
- const speedIds = speedRegions.map((region) => region.id);
- const audioIds = audioRegions.map((region) => region.id);
-
- zoomIds.forEach((id) => onZoomDelete(id));
- trimIds.forEach((id) => onTrimDelete?.(id));
- clipIds.forEach((id) => onClipDelete?.(id));
- annotationIds.forEach((id) => onAnnotationDelete?.(id));
- speedIds.forEach((id) => onSpeedDelete?.(id));
- audioIds.forEach((id) => onAudioDelete?.(id));
-
- clearSelectedBlocks();
- setSelectedKeyframeId(null);
- }, [
- annotationRegions,
- audioRegions,
- clearSelectedBlocks,
- clipRegions,
- onAnnotationDelete,
- onAudioDelete,
- onClipDelete,
- onSpeedDelete,
- onTrimDelete,
- onZoomDelete,
- speedRegions,
- trimRegions,
- zoomRegions,
- ]);
-
- const handleSelectZoom = useCallback((id: string | null) => {
- setSelectAllBlocksActive(false);
- onSelectZoom(id);
- }, [onSelectZoom]);
-
- const handleSelectTrim = useCallback((id: string | null) => {
- setSelectAllBlocksActive(false);
- onSelectTrim?.(id);
- }, [onSelectTrim]);
-
- const handleSelectClip = useCallback((id: string | null) => {
- setSelectAllBlocksActive(false);
- onSelectClip?.(id);
- }, [onSelectClip]);
-
- const handleSelectAnnotation = useCallback((id: string | null) => {
- setSelectAllBlocksActive(false);
- onSelectAnnotation?.(id);
- }, [onSelectAnnotation]);
-
- const handleSelectSpeed = useCallback((id: string | null) => {
- setSelectAllBlocksActive(false);
- onSelectSpeed?.(id);
- }, [onSelectSpeed]);
-
- const handleSelectAudio = useCallback((id: string | null) => {
- setSelectAllBlocksActive(false);
- onSelectAudio?.(id);
- }, [onSelectAudio]);
-
- useEffect(() => {
- setRange(createInitialRange(totalMs));
- }, [totalMs]);
-
- // Normalize regions only when timeline bounds change (not on every region edit).
- // Using refs to read current regions avoids a dependency-loop that re-fires
- // this effect on every drag/resize and races with dnd-timeline's internal state.
- const zoomRegionsRef = useRef(zoomRegions);
- const trimRegionsRef = useRef(trimRegions);
- const speedRegionsRef = useRef(speedRegions);
- const audioRegionsRef = useRef(audioRegions);
- zoomRegionsRef.current = zoomRegions;
- trimRegionsRef.current = trimRegions;
- speedRegionsRef.current = speedRegions;
- audioRegionsRef.current = audioRegions;
-
- useEffect(() => {
- if (totalMs === 0 || safeMinDurationMs <= 0) {
- return;
- }
-
- zoomRegionsRef.current.forEach((region) => {
- const clampedStart = Math.max(0, Math.min(region.startMs, totalMs));
- const minEnd = clampedStart + safeMinDurationMs;
- const clampedEnd = Math.min(totalMs, Math.max(minEnd, region.endMs));
- const normalizedStart = Math.max(0, Math.min(clampedStart, totalMs - safeMinDurationMs));
- const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs));
-
- if (normalizedStart !== region.startMs || normalizedEnd !== region.endMs) {
- onZoomSpanChange(region.id, { start: normalizedStart, end: normalizedEnd });
- }
- });
-
- trimRegionsRef.current.forEach((region) => {
- const clampedStart = Math.max(0, Math.min(region.startMs, totalMs));
- const minEnd = clampedStart + safeMinDurationMs;
- const clampedEnd = Math.min(totalMs, Math.max(minEnd, region.endMs));
- const normalizedStart = Math.max(0, Math.min(clampedStart, totalMs - safeMinDurationMs));
- const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs));
-
- if (normalizedStart !== region.startMs || normalizedEnd !== region.endMs) {
- onTrimSpanChange?.(region.id, { start: normalizedStart, end: normalizedEnd });
- }
- });
-
- speedRegionsRef.current.forEach((region) => {
- const clampedStart = Math.max(0, Math.min(region.startMs, totalMs));
- const minEnd = clampedStart + safeMinDurationMs;
- const clampedEnd = Math.min(totalMs, Math.max(minEnd, region.endMs));
- const normalizedStart = Math.max(0, Math.min(clampedStart, totalMs - safeMinDurationMs));
- const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs));
-
- if (normalizedStart !== region.startMs || normalizedEnd !== region.endMs) {
- onSpeedSpanChange?.(region.id, { start: normalizedStart, end: normalizedEnd });
- }
- });
-
- audioRegionsRef.current.forEach((region) => {
- const clampedStart = Math.max(0, Math.min(region.startMs, totalMs));
- const minEnd = clampedStart + safeMinDurationMs;
- const clampedEnd = Math.min(totalMs, Math.max(minEnd, region.endMs));
- const normalizedStart = Math.max(0, Math.min(clampedStart, totalMs - safeMinDurationMs));
- const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs));
-
- if (normalizedStart !== region.startMs || normalizedEnd !== region.endMs) {
- onAudioSpanChange?.(region.id, { start: normalizedStart, end: normalizedEnd });
- }
- });
- // Only re-run when the timeline scale changes, not on every region edit
- }, [totalMs, safeMinDurationMs, onZoomSpanChange, onTrimSpanChange, onSpeedSpanChange, onAudioSpanChange]);
-
- const hasOverlap = useCallback((newSpan: Span, excludeId?: string): boolean => {
- // Determine which row the item belongs to
- const isZoomItem = zoomRegions.some(r => r.id === excludeId);
- const isTrimItem = trimRegions.some(r => r.id === excludeId);
- const isClipItem = clipRegions.some(r => r.id === excludeId);
- const isAnnotationItem = annotationRegions.some(r => r.id === excludeId);
- const isSpeedItem = speedRegions.some(r => r.id === excludeId);
- const isAudioItem = audioRegions.some(r => r.id === excludeId);
-
- if (isAnnotationItem) {
- return false;
- }
-
- // Helper to check overlap against a specific set of regions
- const checkOverlap = (regions: (ZoomRegion | TrimRegion | ClipRegion | SpeedRegion | AudioRegion)[]) => {
- return regions.some((region) => {
- if (region.id === excludeId) return false;
- // True overlap: regions actually intersect (not just adjacent)
- return newSpan.end > region.startMs && newSpan.start < region.endMs;
- });
- };
-
- if (isZoomItem) {
- return checkOverlap(zoomRegions);
- }
-
- if (isTrimItem) {
- return checkOverlap(trimRegions);
- }
-
- if (isClipItem) {
- return checkOverlap(clipRegions);
- }
-
- if (isSpeedItem) {
- return checkOverlap(speedRegions);
- }
-
- if (isAudioItem) {
- return checkOverlap(audioRegions);
- }
-
- return false;
- }, [zoomRegions, trimRegions, clipRegions, annotationRegions, speedRegions, audioRegions]);
-
- // Keep newly added timeline regions at the original short default instead of
- // scaling them with the full recording length.
- const defaultRegionDurationMs = useMemo(
- () => Math.min(1000, totalMs),
- [totalMs],
- );
-
- const handleAddZoom = useCallback(() => {
- if (!videoDuration || videoDuration === 0 || totalMs === 0) {
- return;
- }
-
- const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
- if (defaultDuration <= 0) {
- return;
- }
-
- // Always place zoom at playhead
- const startPos = Math.max(0, Math.min(currentTimeMs, totalMs));
- // Find the next zoom region after the playhead
- const sorted = [...zoomRegions].sort((a, b) => a.startMs - b.startMs);
- const nextRegion = sorted.find(region => region.startMs > startPos);
- const gapToNext = nextRegion ? nextRegion.startMs - startPos : totalMs - startPos;
-
- // Check if playhead is inside any zoom region
- const isOverlapping = sorted.some(region => startPos >= region.startMs && startPos < region.endMs);
- if (isOverlapping || gapToNext <= 0) {
- toast.error("Cannot place zoom here", {
- description: "Zoom already exists at this location or not enough space available.",
- });
- return;
- }
-
- const actualDuration = Math.min(defaultRegionDurationMs, gapToNext);
- onZoomAdded({ start: startPos, end: startPos + actualDuration });
- }, [videoDuration, totalMs, currentTimeMs, zoomRegions, onZoomAdded, defaultRegionDurationMs]);
-
- const handleSuggestZooms = useCallback(() => {
- if (!videoDuration || videoDuration === 0 || totalMs === 0) {
- return;
- }
-
- if (disableSuggestedZooms) {
- toast.info("Suggested zooms are unavailable while cursor looping is enabled.");
- return;
- }
-
- if (!onZoomSuggested) {
- toast.error("Zoom suggestion handler unavailable");
- return;
- }
-
- if (cursorTelemetry.length < 2) {
- toast.info("No cursor telemetry available", {
- description: "Record a screencast first to generate cursor-based suggestions.",
- });
- return;
- }
-
- const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
- if (defaultDuration <= 0) {
- return;
- }
-
- const result = buildInteractionZoomSuggestions({
- cursorTelemetry,
- totalMs,
- defaultDurationMs: defaultDuration,
- reservedSpans: zoomRegions
- .map((region) => ({ start: region.startMs, end: region.endMs }))
- .sort((a, b) => a.start - b.start),
- });
-
- if (result.status === 'no-telemetry') {
- toast.info("No usable cursor telemetry", {
- description: "The recording does not include enough cursor movement data.",
- });
- return;
- }
-
- if (result.status === 'no-interactions') {
- toast.info("No clear interaction moments found", {
- description: "Try a recording with pauses or clicks around important actions.",
- });
- return;
- }
-
- if (result.status === 'no-slots' || result.suggestions.length === 0) {
- toast.info("No auto-zoom slots available", {
- description: "Detected dwell points overlap existing zoom regions.",
- });
- return;
- }
-
- for (const region of result.suggestions) {
- onZoomSuggested({ start: region.start, end: region.end }, region.focus);
- }
-
- toast.success(`Added ${result.suggestions.length} interaction-based zoom suggestion${result.suggestions.length === 1 ? "" : "s"}`);
- }, [
- videoDuration,
- totalMs,
- defaultRegionDurationMs,
- zoomRegions,
- disableSuggestedZooms,
- onZoomSuggested,
- cursorTelemetry,
- ]);
-
- useEffect(() => {
- if (autoSuggestZoomsTrigger <= 0) {
- return;
- }
-
- onAutoSuggestZoomsConsumed?.();
-
- handleSuggestZooms();
- }, [autoSuggestZoomsTrigger, handleSuggestZooms, onAutoSuggestZoomsConsumed]);
-
- const handleAddTrim = useCallback(() => {
- if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onTrimAdded) {
- return;
- }
-
- const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
- if (defaultDuration <= 0) {
- return;
- }
-
- // Always place trim at playhead
- const startPos = Math.max(0, Math.min(currentTimeMs, totalMs));
- // Find the next trim region after the playhead
- const sorted = [...trimRegions].sort((a, b) => a.startMs - b.startMs);
- const nextRegion = sorted.find(region => region.startMs > startPos);
- const gapToNext = nextRegion ? nextRegion.startMs - startPos : totalMs - startPos;
-
- // Check if playhead is inside any trim region
- const isOverlapping = sorted.some(region => startPos >= region.startMs && startPos < region.endMs);
- if (isOverlapping || gapToNext <= 0) {
- toast.error("Cannot place trim here", {
- description: "Trim already exists at this location or not enough space available.",
- });
- return;
- }
-
- const actualDuration = Math.min(defaultRegionDurationMs, gapToNext);
- onTrimAdded({ start: startPos, end: startPos + actualDuration });
- }, [videoDuration, totalMs, currentTimeMs, trimRegions, onTrimAdded, defaultRegionDurationMs]);
-
- const handleSplitClip = useCallback(() => {
- if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onClipSplit) {
- return;
- }
- onClipSplit(currentTimeMs);
- }, [videoDuration, totalMs, currentTimeMs, onClipSplit]);
-
- const handleAddSpeed = useCallback(() => {
- if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onSpeedAdded) {
- return;
- }
-
- const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
- if (defaultDuration <= 0) {
- return;
- }
-
- // Always place speed region at playhead
- const startPos = Math.max(0, Math.min(currentTimeMs, totalMs));
- // Find the next speed region after the playhead
- const sorted = [...speedRegions].sort((a, b) => a.startMs - b.startMs);
- const nextRegion = sorted.find(region => region.startMs > startPos);
- const gapToNext = nextRegion ? nextRegion.startMs - startPos : totalMs - startPos;
-
- // Check if playhead is inside any speed region
- const isOverlapping = sorted.some(region => startPos >= region.startMs && startPos < region.endMs);
- if (isOverlapping || gapToNext <= 0) {
- toast.error("Cannot place speed here", {
- description: "Speed region already exists at this location or not enough space available.",
- });
- return;
- }
-
- const actualDuration = Math.min(defaultRegionDurationMs, gapToNext);
- onSpeedAdded({ start: startPos, end: startPos + actualDuration });
- }, [videoDuration, totalMs, currentTimeMs, speedRegions, onSpeedAdded, defaultRegionDurationMs]);
-
- const handleAddAudio = useCallback(async () => {
- if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onAudioAdded) {
- return;
- }
-
- const result = await (window as any).electronAPI.openAudioFilePicker();
- if (!result?.success || !result.path) {
- return;
- }
-
- const audioPath = result.path;
-
- // Load the audio file to get its full duration
- const audioDurationMs = await new Promise<number>((resolve) => {
- const audio = new Audio(toFileUrl(audioPath));
- audio.addEventListener('loadedmetadata', () => {
- resolve(Math.round(audio.duration * 1000));
- });
- audio.addEventListener('error', () => {
- resolve(0);
- });
- });
-
- if (audioDurationMs <= 0) {
- toast.error("Could not read audio file", {
- description: "The selected file may be corrupted or in an unsupported format.",
- });
- return;
- }
-
- const startPos = Math.max(0, Math.min(currentTimeMs, totalMs));
- const sorted = [...audioRegions].sort((a, b) => a.startMs - b.startMs);
- const nextRegion = sorted.find(region => region.startMs > startPos);
- const gapToNext = nextRegion ? nextRegion.startMs - startPos : totalMs - startPos;
-
- const isOverlapping = sorted.some(region => startPos >= region.startMs && startPos < region.endMs);
- if (isOverlapping || gapToNext <= 0) {
- toast.error("Cannot place audio here", {
- description: "Audio region already exists at this location or not enough space available.",
- });
- return;
- }
-
- // Use full audio duration, but clamp to available gap and video length
- const actualDuration = Math.min(audioDurationMs, gapToNext, totalMs - startPos);
- onAudioAdded({ start: startPos, end: startPos + actualDuration }, result.path);
- }, [videoDuration, totalMs, currentTimeMs, audioRegions, onAudioAdded]);
-
- const handleAddAnnotation = useCallback(() => {
- if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onAnnotationAdded) {
- return;
- }
-
- const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
- if (defaultDuration <= 0) {
- return;
- }
-
- // Multiple annotations can exist at the same timestamp
- const startPos = Math.max(0, Math.min(currentTimeMs, totalMs));
- const endPos = Math.min(startPos + defaultDuration, totalMs);
-
- onAnnotationAdded({ start: startPos, end: endPos });
- }, [videoDuration, totalMs, currentTimeMs, onAnnotationAdded, defaultRegionDurationMs]);
-
- useEffect(() => {
- const handleKeyDown = (e: KeyboardEvent) => {
- if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
- return;
- }
-
- if (matchesShortcut(e, { key: 'a', ctrl: true }, isMac)) {
- if (!hasAnyTimelineBlocks || !isTimelineFocusedRef.current) {
- return;
- }
-
- e.preventDefault();
- setSelectedKeyframeId(null);
- setSelectAllBlocksActive(true);
- return;
- }
-
- if (matchesShortcut(e, keyShortcuts.addKeyframe, isMac)) {
- addKeyframe();
- }
- if (matchesShortcut(e, keyShortcuts.addZoom, isMac)) {
- handleAddZoom();
- }
- if (matchesShortcut(e, keyShortcuts.addTrim, isMac)) {
- handleAddTrim();
- }
- if (matchesShortcut(e, keyShortcuts.splitClip, isMac)) {
- handleSplitClip();
- }
- if (matchesShortcut(e, keyShortcuts.addAnnotation, isMac)) {
- handleAddAnnotation();
- }
- if (matchesShortcut(e, keyShortcuts.addSpeed, isMac)) {
- handleAddSpeed();
- }
-
- // Tab: Cycle through overlapping annotations at current time
- if (e.key === 'Tab' && annotationRegions.length > 0) {
- const currentTimeMs = Math.round(currentTime * 1000);
- const overlapping = annotationRegions
- .filter(a => currentTimeMs >= a.startMs && currentTimeMs <= a.endMs)
- .sort((a, b) => a.zIndex - b.zIndex); // Sort by z-index
-
- if (overlapping.length > 0) {
- e.preventDefault();
-
- if (!selectedAnnotationId || !overlapping.some(a => a.id === selectedAnnotationId)) {
- onSelectAnnotation?.(overlapping[0].id);
- } else {
- // Cycle to next annotation
- const currentIndex = overlapping.findIndex(a => a.id === selectedAnnotationId);
- const nextIndex = e.shiftKey
- ? (currentIndex - 1 + overlapping.length) % overlapping.length // Shift+Tab = backward
- : (currentIndex + 1) % overlapping.length; // Tab = forward
- onSelectAnnotation?.(overlapping[nextIndex].id);
- }
- }
- }
- // Delete key or Ctrl+D / Cmd+D
- if (e.key === 'Delete' || e.key === 'Backspace' || matchesShortcut(e, keyShortcuts.deleteSelected, isMac)) {
- if (selectAllBlocksActive) {
- e.preventDefault();
- deleteAllBlocks();
- } else if (selectedKeyframeId) {
- deleteSelectedKeyframe();
- } else if (selectedZoomId) {
- deleteSelectedZoom();
- } else if (selectedTrimId) {
- deleteSelectedTrim();
- } else if (selectedClipId) {
- deleteSelectedClip();
- } else if (selectedAnnotationId) {
- deleteSelectedAnnotation();
- } else if (selectedSpeedId) {
- deleteSelectedSpeed();
- } else if (selectedAudioId) {
- deleteSelectedAudio();
- }
- }
- };
- window.addEventListener('keydown', handleKeyDown);
- return () => window.removeEventListener('keydown', handleKeyDown);
- }, [addKeyframe, handleAddZoom, handleAddTrim, handleSplitClip, handleAddAnnotation, handleAddSpeed, deleteAllBlocks, deleteSelectedKeyframe, deleteSelectedZoom, deleteSelectedTrim, deleteSelectedClip, deleteSelectedAnnotation, deleteSelectedSpeed, deleteSelectedAudio, selectedKeyframeId, selectedZoomId, selectedTrimId, selectedClipId, selectedAnnotationId, selectedSpeedId, selectedAudioId, annotationRegions, currentTime, hasAnyTimelineBlocks, onSelectAnnotation, keyShortcuts, isMac, selectAllBlocksActive]);
-
- const clampedRange = useMemo<Range>(() => {
- if (totalMs === 0) {
- return range;
- }
-
- return {
- start: Math.max(0, Math.min(range.start, totalMs)),
- end: Math.min(range.end, totalMs),
- };
- }, [range, totalMs]);
-
- const timelineItems = useMemo<TimelineRenderItem[]>(() => {
- const zooms: TimelineRenderItem[] = zoomRegions.map((region, index) => ({
- id: region.id,
- rowId: ZOOM_ROW_ID,
- span: { start: region.startMs, end: region.endMs },
- label: `Zoom ${index + 1}`,
- zoomDepth: region.depth,
- variant: 'zoom',
- }));
-
- const clips: TimelineRenderItem[] = clipRegions.map((region, index) => ({
- id: region.id,
- rowId: CLIP_ROW_ID,
- span: { start: region.startMs, end: region.endMs },
- label: `Clip ${index + 1}`,
- variant: 'clip',
- }));
-
- const annotations: TimelineRenderItem[] = annotationRegions.map((region) => {
- let label: string;
-
- if (region.type === 'text') {
- // Show text preview
- const preview = region.content.trim() || 'Empty text';
- label = preview.length > 20 ? `${preview.substring(0, 20)}...` : preview;
- } else if (region.type === 'image') {
- label = 'Image';
- } else {
- label = 'Annotation';
- }
-
- return {
- id: region.id,
- rowId: ANNOTATION_ROW_ID,
- span: { start: region.startMs, end: region.endMs },
- label,
- variant: 'annotation',
- };
- });
-
- const audios: TimelineRenderItem[] = audioRegions.map((region) => {
- const fileName = region.audioPath.split(/[\\/]/).pop()?.replace(/\.[^.]+$/, '') || 'Audio';
- return {
- id: region.id,
- rowId: AUDIO_ROW_ID,
- span: { start: region.startMs, end: region.endMs },
- label: fileName,
- variant: 'audio',
- };
- });
-
- return [...zooms, ...clips, ...annotations, ...audios];
- }, [zoomRegions, clipRegions, annotationRegions, audioRegions]);
-
- // Flat list of draggable row spans for neighbour-clamping during drag/resize.
- const allRegionSpans = useMemo(() => {
- const zooms = zoomRegions.map((r) => ({ id: r.id, start: r.startMs, end: r.endMs, rowId: ZOOM_ROW_ID }));
- const clips = clipRegions.map((r) => ({ id: r.id, start: r.startMs, end: r.endMs, rowId: CLIP_ROW_ID }));
- const audios = audioRegions.map((r) => ({ id: r.id, start: r.startMs, end: r.endMs, rowId: AUDIO_ROW_ID }));
- return [...zooms, ...clips, ...audios];
- }, [zoomRegions, clipRegions, audioRegions]);
-
- const handleItemSpanChange = useCallback((id: string, span: Span) => {
- // Check if it's a zoom, trim, clip, speed, or annotation item
- if (zoomRegions.some(r => r.id === id)) {
- onZoomSpanChange(id, span);
- } else if (clipRegions.some(r => r.id === id)) {
- onClipSpanChange?.(id, span);
- } else if (annotationRegions.some(r => r.id === id)) {
- onAnnotationSpanChange?.(id, span);
- } else if (audioRegions.some(r => r.id === id)) {
- onAudioSpanChange?.(id, span);
- }
- }, [zoomRegions, clipRegions, annotationRegions, audioRegions, onZoomSpanChange, onClipSpanChange, onAnnotationSpanChange, onAudioSpanChange]);
-
- const panTimelineRange = useCallback((deltaMs: number) => {
- if (!Number.isFinite(deltaMs) || deltaMs === 0 || totalMs <= 0) {
- return;
- }
-
- setRange((previous) => {
- const visibleSpan = Math.max(1, previous.end - previous.start);
- const maxStart = Math.max(0, totalMs - visibleSpan);
- const nextStart = Math.max(0, Math.min(previous.start + deltaMs, maxStart));
-
- return {
- start: nextStart,
- end: nextStart + visibleSpan,
- };
- });
- }, [totalMs]);
-
- const handleTimelineWheel = useCallback((event: WheelEvent<HTMLDivElement>) => {
- if (event.ctrlKey || event.metaKey || totalMs <= 0) {
- return;
- }
-
- const rawHorizontalDelta = Math.abs(event.deltaX) > 0
- ? event.deltaX
- : event.shiftKey && Math.abs(event.deltaY) > 0
- ? event.deltaY
- : 0;
-
- if (rawHorizontalDelta === 0) {
- return;
- }
-
- const containerWidth = timelineContainerRef.current?.clientWidth ?? 0;
- const visibleRangeMs = clampedRange.end - clampedRange.start;
-
- if (containerWidth <= 0 || visibleRangeMs <= 0) {
- return;
- }
-
- event.preventDefault();
-
- const horizontalDeltaPx = normalizeWheelDeltaToPixels(rawHorizontalDelta, event.deltaMode);
- const deltaMs = (horizontalDeltaPx / containerWidth) * visibleRangeMs;
-
- panTimelineRange(deltaMs);
- }, [clampedRange.end, clampedRange.start, panTimelineRange, totalMs]);
-
- if (!videoDuration || videoDuration === 0) {
- return (
- <div className="flex-1 flex flex-col items-center justify-center rounded-lg bg-[#17171a] gap-3">
- <div className="w-12 h-12 rounded-full bg-white/5 flex items-center justify-center">
- <Plus className="w-6 h-6 text-slate-600" />
- </div>
- <div className="text-center">
- <p className="text-sm font-medium text-slate-300">No Video Loaded</p>
- <p className="text-xs text-slate-500 mt-1">Drag and drop a video to start editing</p>
- </div>
- </div>
- );
- }
-
- return (
- <div className="flex-1 min-h-0 flex flex-col bg-[#17171a] overflow-auto">
- <div className="flex items-center gap-2 px-4 py-2 border-b border-white/10 bg-[#161619]">
- <div className="flex items-center gap-1">
- <Button
- onClick={handleAddZoom}
- variant="ghost"
- size="icon"
- className="h-7 w-7 text-slate-400 hover:text-[#2563EB] hover:bg-[#2563EB]/10 transition-all"
- title="Add Zoom (Z)"
- >
- <ZoomIn className="w-4 h-4" />
- </Button>
- <Button
- onClick={handleSuggestZooms}
- variant="ghost"
- size="icon"
- className="h-7 w-7 text-slate-400 hover:text-[#2563EB] hover:bg-[#2563EB]/10 transition-all"
- title="Suggest Zooms from Cursor"
- >
- <WandSparkles className="w-4 h-4" />
- </Button>
- <Button
- onClick={handleAddAnnotation}
- variant="ghost"
- size="icon"
- className="h-7 w-7 text-slate-400 hover:text-[#B4A046] hover:bg-[#B4A046]/10 transition-all"
- title="Add Annotation (A)"
- >
- <MessageSquare className="w-4 h-4" />
- </Button>
- <Button
- onClick={handleAddAudio}
- variant="ghost"
- size="icon"
- className="h-7 w-7 text-slate-400 hover:text-[#a855f7] hover:bg-[#a855f7]/10 transition-all"
- title="Add Audio"
- >
- <Music className="w-4 h-4" />
- </Button>
- <Button
- onClick={handleSplitClip}
- variant="ghost"
- size="icon"
- className="h-7 w-7 text-slate-400 hover:text-white hover:bg-white/10 transition-all"
- title="Split Clip (C)"
- >
- <Scissors className="w-4 h-4" />
- </Button>
- </div>
- <div className="flex items-center gap-2">
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <Button
- variant="ghost"
- size="sm"
- className="h-7 px-2 text-xs text-slate-400 hover:text-slate-200 hover:bg-white/10 transition-all gap-1"
- >
- <span className="font-medium">{getAspectRatioLabel(aspectRatio)}</span>
- <ChevronDown className="w-3 h-3" />
- </Button>
- </DropdownMenuTrigger>
- <DropdownMenuContent align="end" className="bg-[#1a1a1c] border-white/10">
- {ASPECT_RATIOS.map((ratio) => (
- <DropdownMenuItem
- key={ratio}
- onClick={() => onAspectRatioChange(ratio)}
- className="text-slate-300 hover:text-white hover:bg-white/10 cursor-pointer flex items-center justify-between gap-3"
- >
- <span>{getAspectRatioLabel(ratio)}</span>
- {aspectRatio === ratio && <Check className="w-3 h-3 text-[#2563EB]" />}
- </DropdownMenuItem>
- ))}
- <div className="mx-1 my-1 h-px bg-white/10" />
- <div className="px-2 py-1.5 flex items-center gap-2 text-slate-300">
- <span className="text-sm">Custom</span>
- <input
- type="text"
- inputMode="numeric"
- value={customAspectWidth}
- onChange={(event) => setCustomAspectWidth(event.target.value.replace(/\D/g, ''))}
- onKeyDown={handleCustomAspectRatioKeyDown}
- className="w-12 h-7 rounded border border-white/15 bg-black/20 px-1.5 text-sm text-slate-100 focus:outline-none focus:ring-1 focus:ring-[#2563EB]"
- aria-label="Custom aspect width"
- />
- <span className="text-slate-500">:</span>
- <input
- type="text"
- inputMode="numeric"
- value={customAspectHeight}
- onChange={(event) => setCustomAspectHeight(event.target.value.replace(/\D/g, ''))}
- onKeyDown={handleCustomAspectRatioKeyDown}
- className="w-12 h-7 rounded border border-white/15 bg-black/20 px-1.5 text-sm text-slate-100 focus:outline-none focus:ring-1 focus:ring-[#2563EB]"
- aria-label="Custom aspect height"
- />
- <Button
- variant="ghost"
- size="sm"
- onClick={applyCustomAspectRatio}
- className="h-7 px-2 text-xs text-slate-300 hover:text-white hover:bg-white/10"
- >
- Set
- </Button>
- {isCustomAspectRatio(aspectRatio) && <Check className="w-3 h-3 text-[#2563EB] ml-auto" />}
- </div>
- </DropdownMenuContent>
- </DropdownMenu>
- <div className="w-[1px] h-4 bg-white/10" />
- <Button
- variant="ghost"
- size="sm"
- onClick={() => onOpenCropEditor?.()}
- className="h-7 px-2 text-xs text-slate-400 hover:text-slate-200 hover:bg-white/10 transition-all gap-1.5"
- >
- <Crop className="w-3.5 h-3.5" />
- <span className="font-medium">{t("sections.crop", "Crop")}</span>
- {isCropped ? <span className="h-1.5 w-1.5 rounded-full bg-[#2563EB]" /> : null}
- </Button>
- </div>
- <div className="flex-1" />
- <div className="flex items-center gap-4 text-[10px] text-slate-500 font-medium">
- <span className="flex items-center gap-1.5">
- <kbd className="px-1.5 py-0.5 bg-white/5 border border-white/10 rounded text-[#2563EB] font-sans">Side Scroll</kbd>
- <span>Pan</span>
- </span>
- <span className="flex items-center gap-1.5">
- <kbd className="px-1.5 py-0.5 bg-white/5 border border-white/10 rounded text-[#2563EB] font-sans">{scrollLabels.pan}</kbd>
- <span>Pan</span>
- </span>
- <span className="flex items-center gap-1.5">
- <kbd className="px-1.5 py-0.5 bg-white/5 border border-white/10 rounded text-[#2563EB] font-sans">{scrollLabels.zoom}</kbd>
- <span>Zoom</span>
- </span>
- </div>
- </div>
- <div
- ref={timelineContainerRef}
- className="flex-1 min-h-0 overflow-auto bg-[#17171a] relative"
- tabIndex={0}
- onFocus={() => {
- isTimelineFocusedRef.current = true;
- }}
- onBlur={() => {
- isTimelineFocusedRef.current = false;
- }}
- onMouseDown={() => {
- timelineContainerRef.current?.focus();
- isTimelineFocusedRef.current = true;
- }}
- onClick={() => {
- setSelectedKeyframeId(null);
- setSelectAllBlocksActive(false);
- }}
- onWheel={handleTimelineWheel}
- >
- <TimelineWrapper
- range={clampedRange}
- videoDuration={videoDuration}
- hasOverlap={hasOverlap}
- onRangeChange={setRange}
- minItemDurationMs={timelineScale.minItemDurationMs}
- minVisibleRangeMs={timelineScale.minVisibleRangeMs}
- onItemSpanChange={handleItemSpanChange}
- allRegionSpans={allRegionSpans}
- >
- <KeyframeMarkers
- keyframes={keyframes}
- selectedKeyframeId={selectedKeyframeId}
- setSelectedKeyframeId={setSelectedKeyframeId}
- onKeyframeMove={handleKeyframeMove}
- videoDurationMs={totalMs}
- timelineRef={timelineContainerRef}
- />
- <Timeline
- items={timelineItems}
- videoDurationMs={totalMs}
- currentTimeMs={currentTimeMs}
- onSeek={onSeek}
- onSelectZoom={handleSelectZoom}
- onSelectTrim={handleSelectTrim}
- onSelectClip={handleSelectClip}
- onSelectAnnotation={handleSelectAnnotation}
- onSelectSpeed={handleSelectSpeed}
- onSelectAudio={handleSelectAudio}
- selectedZoomId={selectedZoomId}
- selectedTrimId={selectedTrimId}
- selectedClipId={selectedClipId}
- selectedAnnotationId={selectedAnnotationId}
- selectedSpeedId={selectedSpeedId}
- selectedAudioId={selectedAudioId}
- selectAllBlocksActive={selectAllBlocksActive}
- onClearBlockSelection={clearSelectedBlocks}
- keyframes={keyframes}
- audioPeaks={audioPeaks}
- />
- </TimelineWrapper>
- </div>
- </div>
- );
-}
+const TimelineEditor = forwardRef<TimelineEditorHandle, TimelineEditorProps>(
+ function TimelineEditor(
+ {
+ videoDuration,
+ currentTime,
+ playheadTime,
+ onSeek,
+ cursorTelemetry = [],
+ autoSuggestZoomsTrigger = 0,
+ onAutoSuggestZoomsConsumed,
+ disableSuggestedZooms = false,
+ zoomRegions,
+ onZoomAdded,
+ onZoomSuggested,
+ onZoomSpanChange,
+ onZoomDelete,
+ selectedZoomId,
+ onSelectZoom,
+ trimRegions = [],
+ onTrimAdded,
+ onTrimSpanChange,
+ onTrimDelete,
+ selectedTrimId,
+ onSelectTrim,
+ clipRegions = [],
+ onClipSplit,
+ onClipSpanChange,
+ onClipDelete,
+ selectedClipId,
+ onSelectClip,
+ annotationRegions = [],
+ onAnnotationAdded,
+ onAnnotationSpanChange,
+ onAnnotationDelete,
+ selectedAnnotationId,
+ onSelectAnnotation,
+ speedRegions = [],
+ onSpeedAdded,
+ onSpeedSpanChange,
+ onSpeedDelete,
+ selectedSpeedId,
+ onSelectSpeed,
+ audioRegions = [],
+ onAudioAdded,
+ onAudioSpanChange,
+ onAudioDelete,
+ selectedAudioId,
+ onSelectAudio,
+ videoPath,
+ isPlaying = false,
+ onTogglePlayPause,
+ volume = 1,
+ onVolumeChange,
+ hideToolbar = false,
+ },
+ ref,
+ ) {
+ const totalMs = useMemo(
+ () => Math.max(0, Math.round(videoDuration * 1000)),
+ [videoDuration],
+ );
+ const playheadTimeMs = useMemo(
+ () => Math.round((playheadTime ?? currentTime) * 1000),
+ [playheadTime, currentTime],
+ );
+ const timelineCurrentTimeMs = playheadTimeMs;
+ const timelineScale = useMemo(() => calculateTimelineScale(videoDuration), [videoDuration]);
+ const safeMinDurationMs = useMemo(
+ () =>
+ totalMs > 0
+ ? Math.min(timelineScale.minItemDurationMs, totalMs)
+ : timelineScale.minItemDurationMs,
+ [timelineScale.minItemDurationMs, totalMs],
+ );
+
+ const [range, setRange] = useState<Range>(() => createInitialRange(totalMs));
+ const [keyframes, setKeyframes] = useState<{ id: string; time: number }[]>([]);
+ const [selectedKeyframeId, setSelectedKeyframeId] = useState<string | null>(null);
+ const [selectAllBlocksActive, setSelectAllBlocksActive] = useState(false);
+ const [isCollapsed, setIsCollapsed] = useState(false);
+ const [hiddenTrackIds, setHiddenTrackIds] = useState<Set<string>>(() => new Set());
+ const isTimelineFocusedRef = useRef(false);
+ const timelineContainerRef = useRef<HTMLDivElement>(null);
+ const { shortcuts: keyShortcuts, isMac } = useShortcuts();
+ const audioPeaks = useAudioPeaks(videoPath);
+
+ function formatTime(seconds: number) {
+ if (!isFinite(seconds) || isNaN(seconds) || seconds < 0) return "0:00";
+ const mins = Math.floor(seconds / 60);
+ const secs = Math.floor(seconds % 60);
+ return `${mins}:${secs.toString().padStart(2, "0")}`;
+ }
+
+ useEffect(() => {
+ if (totalMs === 0) {
+ setRange({ start: 0, end: FALLBACK_RANGE_MS });
+ return;
+ }
+ if (videoPath) {
+ try {
+ const saved = localStorage.getItem(`timeline-zoom:${videoPath}`);
+ if (saved) {
+ const parsed: Range = JSON.parse(saved);
+ if (
+ typeof parsed.start === "number" &&
+ typeof parsed.end === "number" &&
+ parsed.start >= 0 &&
+ parsed.end > parsed.start
+ ) {
+ setRange({
+ start: Math.max(0, parsed.start),
+ end: Math.min(totalMs, parsed.end),
+ });
+ return;
+ }
+ }
+ } catch {
+ // ignore malformed storage
+ }
+ }
+ setRange(createInitialRange(totalMs));
+ }, [totalMs, videoPath]);
+
+ useEffect(() => {
+ if (!videoPath || totalMs === 0) return;
+ try {
+ localStorage.setItem(`timeline-zoom:${videoPath}`, JSON.stringify(range));
+ } catch {
+ // ignore storage errors
+ }
+ }, [range, videoPath, totalMs]);
+
+ useEffect(() => {
+ if (!videoPath) {
+ setHiddenTrackIds(new Set());
+ return;
+ }
+ try {
+ const saved = localStorage.getItem(`timeline-hidden-tracks:${videoPath}`);
+ if (saved) {
+ setHiddenTrackIds(new Set(JSON.parse(saved) as string[]));
+ return;
+ }
+ } catch {
+ // ignore
+ }
+ setHiddenTrackIds(new Set());
+ }, [videoPath]);
+
+ useEffect(() => {
+ if (!videoPath) return;
+ try {
+ localStorage.setItem(
+ `timeline-hidden-tracks:${videoPath}`,
+ JSON.stringify([...hiddenTrackIds]),
+ );
+ } catch {
+ // ignore
+ }
+ }, [hiddenTrackIds, videoPath]);
+
+ // Add keyframe at current playhead position
+ const addKeyframe = useCallback(() => {
+ if (totalMs === 0) return;
+ const time = Math.max(0, Math.min(timelineCurrentTimeMs, totalMs));
+ if (keyframes.some((kf) => Math.abs(kf.time - time) < 1)) return;
+ setKeyframes((prev) => [...prev, { id: uuidv4(), time }]);
+ }, [timelineCurrentTimeMs, totalMs, keyframes]);
+
+ // Delete selected keyframe
+ const deleteSelectedKeyframe = useCallback(() => {
+ if (!selectedKeyframeId) return;
+ setKeyframes((prev) => prev.filter((kf) => kf.id !== selectedKeyframeId));
+ setSelectedKeyframeId(null);
+ }, [selectedKeyframeId]);
+
+ // Move keyframe to new time position
+ const handleKeyframeMove = useCallback(
+ (id: string, newTime: number) => {
+ setKeyframes((prev) =>
+ prev.map((kf) =>
+ kf.id === id
+ ? { ...kf, time: Math.max(0, Math.min(newTime, totalMs)) }
+ : kf,
+ ),
+ );
+ },
+ [totalMs],
+ );
+
+ // Delete selected zoom item
+ const deleteSelectedZoom = useCallback(() => {
+ if (!selectedZoomId) return;
+ onZoomDelete(selectedZoomId);
+ onSelectZoom(null);
+ }, [selectedZoomId, onZoomDelete, onSelectZoom]);
+
+ // Delete selected trim item
+ const deleteSelectedTrim = useCallback(() => {
+ if (!selectedTrimId || !onTrimDelete || !onSelectTrim) return;
+ onTrimDelete(selectedTrimId);
+ onSelectTrim(null);
+ }, [selectedTrimId, onTrimDelete, onSelectTrim]);
+
+ const deleteSelectedClip = useCallback(() => {
+ if (!selectedClipId || !onClipDelete || !onSelectClip) return;
+ onClipDelete(selectedClipId);
+ onSelectClip(null);
+ }, [selectedClipId, onClipDelete, onSelectClip]);
+
+ const deleteSelectedAnnotation = useCallback(() => {
+ if (!selectedAnnotationId || !onAnnotationDelete || !onSelectAnnotation) return;
+ onAnnotationDelete(selectedAnnotationId);
+ onSelectAnnotation(null);
+ }, [selectedAnnotationId, onAnnotationDelete, onSelectAnnotation]);
+
+ const deleteSelectedSpeed = useCallback(() => {
+ if (!selectedSpeedId || !onSpeedDelete || !onSelectSpeed) return;
+ onSpeedDelete(selectedSpeedId);
+ onSelectSpeed(null);
+ }, [selectedSpeedId, onSpeedDelete, onSelectSpeed]);
+
+ const deleteSelectedAudio = useCallback(() => {
+ if (!selectedAudioId || !onAudioDelete || !onSelectAudio) return;
+ onAudioDelete(selectedAudioId);
+ onSelectAudio(null);
+ }, [selectedAudioId, onAudioDelete, onSelectAudio]);
+
+ const clearSelectedBlocks = useCallback(() => {
+ onSelectZoom(null);
+ onSelectTrim?.(null);
+ onSelectClip?.(null);
+ onSelectAnnotation?.(null);
+ onSelectSpeed?.(null);
+ onSelectAudio?.(null);
+ setSelectAllBlocksActive(false);
+ }, [
+ onSelectAnnotation,
+ onSelectAudio,
+ onSelectClip,
+ onSelectSpeed,
+ onSelectTrim,
+ onSelectZoom,
+ ]);
+
+ const hasAnyTimelineBlocks =
+ zoomRegions.length > 0 ||
+ trimRegions.length > 0 ||
+ clipRegions.length > 0 ||
+ annotationRegions.length > 0 ||
+ speedRegions.length > 0 ||
+ audioRegions.length > 0;
+
+ const deleteAllBlocks = useCallback(() => {
+ const zoomIds = zoomRegions.map((region) => region.id);
+ const trimIds = trimRegions.map((region) => region.id);
+ const clipIds = clipRegions.map((region) => region.id);
+ const annotationIds = annotationRegions.map((region) => region.id);
+ const speedIds = speedRegions.map((region) => region.id);
+ const audioIds = audioRegions.map((region) => region.id);
+
+ zoomIds.forEach((id) => onZoomDelete(id));
+ trimIds.forEach((id) => onTrimDelete?.(id));
+ clipIds.forEach((id) => onClipDelete?.(id));
+ annotationIds.forEach((id) => onAnnotationDelete?.(id));
+ speedIds.forEach((id) => onSpeedDelete?.(id));
+ audioIds.forEach((id) => onAudioDelete?.(id));
+
+ clearSelectedBlocks();
+ setSelectedKeyframeId(null);
+ }, [
+ annotationRegions,
+ audioRegions,
+ clearSelectedBlocks,
+ clipRegions,
+ onAnnotationDelete,
+ onAudioDelete,
+ onClipDelete,
+ onSpeedDelete,
+ onTrimDelete,
+ onZoomDelete,
+ speedRegions,
+ trimRegions,
+ zoomRegions,
+ ]);
+
+ const handleSelectZoom = useCallback(
+ (id: string | null) => {
+ setSelectAllBlocksActive(false);
+ onSelectZoom(id);
+ },
+ [onSelectZoom],
+ );
+
+ const handleSelectTrim = useCallback(
+ (id: string | null) => {
+ setSelectAllBlocksActive(false);
+ onSelectTrim?.(id);
+ },
+ [onSelectTrim],
+ );
+
+ const handleSelectClip = useCallback(
+ (id: string | null) => {
+ setSelectAllBlocksActive(false);
+ onSelectClip?.(id);
+ },
+ [onSelectClip],
+ );
+
+ const handleSelectAnnotation = useCallback(
+ (id: string | null) => {
+ setSelectAllBlocksActive(false);
+ onSelectAnnotation?.(id);
+ },
+ [onSelectAnnotation],
+ );
+
+ const handleSelectSpeed = useCallback(
+ (id: string | null) => {
+ setSelectAllBlocksActive(false);
+ onSelectSpeed?.(id);
+ },
+ [onSelectSpeed],
+ );
+
+ const handleSelectAudio = useCallback(
+ (id: string | null) => {
+ setSelectAllBlocksActive(false);
+ onSelectAudio?.(id);
+ },
+ [onSelectAudio],
+ );
+
+ // (zoom persistence is handled above; duplicate effect removed)
+
+ // Normalize regions only when timeline bounds change (not on every region edit).
+ // Using refs to read current regions avoids a dependency-loop that re-fires
+ // this effect on every drag/resize and races with dnd-timeline's internal state.
+ const zoomRegionsRef = useRef(zoomRegions);
+ const trimRegionsRef = useRef(trimRegions);
+ const speedRegionsRef = useRef(speedRegions);
+ const audioRegionsRef = useRef(audioRegions);
+ zoomRegionsRef.current = zoomRegions;
+ trimRegionsRef.current = trimRegions;
+ speedRegionsRef.current = speedRegions;
+ audioRegionsRef.current = audioRegions;
+
+ useEffect(() => {
+ if (totalMs === 0 || safeMinDurationMs <= 0) {
+ return;
+ }
+
+ zoomRegionsRef.current.forEach((region) => {
+ const clampedStart = Math.max(0, Math.min(region.startMs, totalMs));
+ const minEnd = clampedStart + safeMinDurationMs;
+ const clampedEnd = Math.min(totalMs, Math.max(minEnd, region.endMs));
+ const normalizedStart = Math.max(
+ 0,
+ Math.min(clampedStart, totalMs - safeMinDurationMs),
+ );
+ const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs));
+
+ if (normalizedStart !== region.startMs || normalizedEnd !== region.endMs) {
+ onZoomSpanChange(region.id, { start: normalizedStart, end: normalizedEnd });
+ }
+ });
+
+ trimRegionsRef.current.forEach((region) => {
+ const clampedStart = Math.max(0, Math.min(region.startMs, totalMs));
+ const minEnd = clampedStart + safeMinDurationMs;
+ const clampedEnd = Math.min(totalMs, Math.max(minEnd, region.endMs));
+ const normalizedStart = Math.max(
+ 0,
+ Math.min(clampedStart, totalMs - safeMinDurationMs),
+ );
+ const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs));
+
+ if (normalizedStart !== region.startMs || normalizedEnd !== region.endMs) {
+ onTrimSpanChange?.(region.id, { start: normalizedStart, end: normalizedEnd });
+ }
+ });
+
+ speedRegionsRef.current.forEach((region) => {
+ const clampedStart = Math.max(0, Math.min(region.startMs, totalMs));
+ const minEnd = clampedStart + safeMinDurationMs;
+ const clampedEnd = Math.min(totalMs, Math.max(minEnd, region.endMs));
+ const normalizedStart = Math.max(
+ 0,
+ Math.min(clampedStart, totalMs - safeMinDurationMs),
+ );
+ const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs));
+
+ if (normalizedStart !== region.startMs || normalizedEnd !== region.endMs) {
+ onSpeedSpanChange?.(region.id, { start: normalizedStart, end: normalizedEnd });
+ }
+ });
+
+ audioRegionsRef.current.forEach((region) => {
+ const clampedStart = Math.max(0, Math.min(region.startMs, totalMs));
+ const minEnd = clampedStart + safeMinDurationMs;
+ const clampedEnd = Math.min(totalMs, Math.max(minEnd, region.endMs));
+ const normalizedStart = Math.max(
+ 0,
+ Math.min(clampedStart, totalMs - safeMinDurationMs),
+ );
+ const normalizedEnd = Math.max(minEnd, Math.min(clampedEnd, totalMs));
+
+ if (normalizedStart !== region.startMs || normalizedEnd !== region.endMs) {
+ onAudioSpanChange?.(region.id, { start: normalizedStart, end: normalizedEnd });
+ }
+ });
+ // Only re-run when the timeline scale changes, not on every region edit
+ }, [
+ totalMs,
+ safeMinDurationMs,
+ onZoomSpanChange,
+ onTrimSpanChange,
+ onSpeedSpanChange,
+ onAudioSpanChange,
+ ]);
+
+ const hasOverlap = useCallback(
+ (newSpan: Span, excludeId?: string): boolean => {
+ // Determine which row the item belongs to
+ const isZoomItem = zoomRegions.some((r) => r.id === excludeId);
+ const isTrimItem = trimRegions.some((r) => r.id === excludeId);
+ const isClipItem = clipRegions.some((r) => r.id === excludeId);
+ const isAnnotationItem = annotationRegions.some((r) => r.id === excludeId);
+ const isSpeedItem = speedRegions.some((r) => r.id === excludeId);
+ const isAudioItem = audioRegions.some((r) => r.id === excludeId);
+
+ if (isAnnotationItem) {
+ return false;
+ }
+
+ // Helper to check overlap against a specific set of regions
+ const checkOverlap = (
+ regions: (ZoomRegion | TrimRegion | ClipRegion | SpeedRegion | AudioRegion)[],
+ ) => {
+ return regions.some((region) => {
+ if (region.id === excludeId) return false;
+ // True overlap: regions actually intersect (not just adjacent)
+ return newSpan.end > region.startMs && newSpan.start < region.endMs;
+ });
+ };
+
+ if (isZoomItem) {
+ return checkOverlap(zoomRegions);
+ }
+
+ if (isTrimItem) {
+ return checkOverlap(trimRegions);
+ }
+
+ if (isClipItem) {
+ return checkOverlap(clipRegions);
+ }
+
+ if (isSpeedItem) {
+ return checkOverlap(speedRegions);
+ }
+
+ if (isAudioItem) {
+ return checkOverlap(audioRegions);
+ }
+
+ return false;
+ },
+ [zoomRegions, trimRegions, clipRegions, annotationRegions, speedRegions, audioRegions],
+ );
+
+ // Keep newly added timeline regions at the original short default instead of
+ // scaling them with the full recording length.
+ const defaultRegionDurationMs = useMemo(() => Math.min(1500, totalMs), [totalMs]);
+
+ const handleAddZoom = useCallback(() => {
+ if (!videoDuration || videoDuration === 0 || totalMs === 0) {
+ return;
+ }
+
+ const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
+ if (defaultDuration <= 0) {
+ return;
+ }
+
+ // Always place zoom at playhead
+ const startPos = Math.max(0, Math.min(timelineCurrentTimeMs, totalMs));
+ // Find the next zoom region after the playhead
+ const sorted = [...zoomRegions].sort((a, b) => a.startMs - b.startMs);
+ const nextRegion = sorted.find((region) => region.startMs > startPos);
+ const gapToNext = nextRegion ? nextRegion.startMs - startPos : totalMs - startPos;
+
+ // Check if playhead is inside any zoom region
+ const isOverlapping = sorted.some(
+ (region) => startPos >= region.startMs && startPos < region.endMs,
+ );
+ if (isOverlapping || gapToNext <= 0) {
+ toast.error("Cannot place zoom here", {
+ description:
+ "Zoom already exists at this location or not enough space available.",
+ });
+ return;
+ }
+
+ const actualDuration = Math.min(defaultRegionDurationMs, gapToNext);
+ onZoomAdded({ start: startPos, end: startPos + actualDuration });
+ }, [
+ videoDuration,
+ totalMs,
+ timelineCurrentTimeMs,
+ zoomRegions,
+ onZoomAdded,
+ defaultRegionDurationMs,
+ ]);
+
+ const handleSuggestZooms = useCallback(() => {
+ if (!videoDuration || videoDuration === 0 || totalMs === 0) {
+ return;
+ }
+
+ if (disableSuggestedZooms) {
+ toast.info("Suggested zooms are unavailable while cursor looping is enabled.");
+ return;
+ }
+
+ if (!onZoomSuggested) {
+ toast.error("Zoom suggestion handler unavailable");
+ return;
+ }
+
+ if (cursorTelemetry.length < 2) {
+ toast.info("No cursor telemetry available", {
+ description: "Record a screencast first to generate cursor-based suggestions.",
+ });
+ return;
+ }
+
+ const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
+ if (defaultDuration <= 0) {
+ return;
+ }
+
+ const result = buildInteractionZoomSuggestions({
+ cursorTelemetry,
+ totalMs,
+ defaultDurationMs: defaultDuration,
+ reservedSpans: zoomRegions
+ .map((region) => ({ start: region.startMs, end: region.endMs }))
+ .sort((a, b) => a.start - b.start),
+ });
+
+ if (result.status === "no-telemetry") {
+ toast.info("No usable cursor telemetry", {
+ description: "The recording does not include enough cursor movement data.",
+ });
+ return;
+ }
+
+ if (result.status === "no-interactions") {
+ toast.info("No clear interaction moments found", {
+ description: "Try a recording with pauses or clicks around important actions.",
+ });
+ return;
+ }
+
+ if (result.status === "no-slots" || result.suggestions.length === 0) {
+ toast.info("No auto-zoom slots available", {
+ description: "Detected dwell points overlap existing zoom regions.",
+ });
+ return;
+ }
+
+ for (const region of result.suggestions) {
+ onZoomSuggested({ start: region.start, end: region.end }, region.focus);
+ }
+
+ toast.success(
+ `Added ${result.suggestions.length} interaction-based zoom suggestion${result.suggestions.length === 1 ? "" : "s"}`,
+ );
+ }, [
+ videoDuration,
+ totalMs,
+ defaultRegionDurationMs,
+ zoomRegions,
+ disableSuggestedZooms,
+ onZoomSuggested,
+ cursorTelemetry,
+ ]);
+
+ useEffect(() => {
+ if (autoSuggestZoomsTrigger <= 0) {
+ return;
+ }
+
+ onAutoSuggestZoomsConsumed?.();
+
+ handleSuggestZooms();
+ }, [autoSuggestZoomsTrigger, handleSuggestZooms, onAutoSuggestZoomsConsumed]);
+
+ const handleAddTrim = useCallback(() => {
+ if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onTrimAdded) {
+ return;
+ }
+
+ const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
+ if (defaultDuration <= 0) {
+ return;
+ }
+
+ // Always place trim at playhead
+ const startPos = Math.max(0, Math.min(timelineCurrentTimeMs, totalMs));
+ // Find the next trim region after the playhead
+ const sorted = [...trimRegions].sort((a, b) => a.startMs - b.startMs);
+ const nextRegion = sorted.find((region) => region.startMs > startPos);
+ const gapToNext = nextRegion ? nextRegion.startMs - startPos : totalMs - startPos;
+
+ // Check if playhead is inside any trim region
+ const isOverlapping = sorted.some(
+ (region) => startPos >= region.startMs && startPos < region.endMs,
+ );
+ if (isOverlapping || gapToNext <= 0) {
+ toast.error("Cannot place trim here", {
+ description:
+ "Trim already exists at this location or not enough space available.",
+ });
+ return;
+ }
+
+ const actualDuration = Math.min(defaultRegionDurationMs, gapToNext);
+ onTrimAdded({ start: startPos, end: startPos + actualDuration });
+ }, [
+ videoDuration,
+ totalMs,
+ timelineCurrentTimeMs,
+ trimRegions,
+ onTrimAdded,
+ defaultRegionDurationMs,
+ ]);
+
+ const handleSplitClip = useCallback(() => {
+ if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onClipSplit) {
+ return;
+ }
+ onClipSplit(timelineCurrentTimeMs);
+ }, [videoDuration, totalMs, timelineCurrentTimeMs, onClipSplit]);
+
+ const handleAddSpeed = useCallback(() => {
+ if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onSpeedAdded) {
+ return;
+ }
+
+ const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
+ if (defaultDuration <= 0) {
+ return;
+ }
+
+ // Always place speed region at playhead
+ const startPos = Math.max(0, Math.min(timelineCurrentTimeMs, totalMs));
+ // Find the next speed region after the playhead
+ const sorted = [...speedRegions].sort((a, b) => a.startMs - b.startMs);
+ const nextRegion = sorted.find((region) => region.startMs > startPos);
+ const gapToNext = nextRegion ? nextRegion.startMs - startPos : totalMs - startPos;
+
+ // Check if playhead is inside any speed region
+ const isOverlapping = sorted.some(
+ (region) => startPos >= region.startMs && startPos < region.endMs,
+ );
+ if (isOverlapping || gapToNext <= 0) {
+ toast.error("Cannot place speed here", {
+ description:
+ "Speed region already exists at this location or not enough space available.",
+ });
+ return;
+ }
+
+ const actualDuration = Math.min(defaultRegionDurationMs, gapToNext);
+ onSpeedAdded({ start: startPos, end: startPos + actualDuration });
+ }, [
+ videoDuration,
+ totalMs,
+ timelineCurrentTimeMs,
+ speedRegions,
+ onSpeedAdded,
+ defaultRegionDurationMs,
+ ]);
+
+ const handleAddAudio = useCallback(async () => {
+ if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onAudioAdded) {
+ return;
+ }
+
+ const result = await window.electronAPI?.openAudioFilePicker?.();
+ if (!result?.success || !result.path) {
+ return;
+ }
+
+ const audioPath = result.path;
+
+ // Load the audio file to get its full duration
+ const audioDurationMs = await new Promise<number>((resolve) => {
+ const audio = new Audio(toFileUrl(audioPath));
+ audio.addEventListener("loadedmetadata", () => {
+ resolve(Math.round(audio.duration * 1000));
+ });
+ audio.addEventListener("error", () => {
+ resolve(0);
+ });
+ });
+
+ if (audioDurationMs <= 0) {
+ toast.error("Could not read audio file", {
+ description: "The selected file may be corrupted or in an unsupported format.",
+ });
+ return;
+ }
+
+ const startPos = Math.max(0, Math.min(timelineCurrentTimeMs, totalMs));
+
+ // Find the first track row with no overlap at startPos
+ let rowTrackIndex = 0;
+ let rowGap = 0;
+ for (let attempt = 0; attempt < 100; attempt++) {
+ const rowRegions = audioRegions.filter((r) => (r.trackIndex ?? 0) === attempt);
+ const sortedRow = [...rowRegions].sort((a, b) => a.startMs - b.startMs);
+ const isRowOverlapping = sortedRow.some(
+ (region) => startPos >= region.startMs && startPos < region.endMs,
+ );
+ if (!isRowOverlapping) {
+ const nextInRow = sortedRow.find((region) => region.startMs > startPos);
+ const gap = nextInRow ? nextInRow.startMs - startPos : totalMs - startPos;
+ if (gap > 0) {
+ rowTrackIndex = attempt;
+ rowGap = gap;
+ break;
+ }
+ }
+ }
+
+ if (rowGap <= 0) {
+ toast.error("Cannot place audio here", {
+ description: "Not enough space available at this position.",
+ });
+ return;
+ }
+
+ // Use full audio duration, but clamp to available gap and video length
+ const actualDuration = Math.min(audioDurationMs, rowGap, totalMs - startPos);
+ onAudioAdded(
+ { start: startPos, end: startPos + actualDuration },
+ result.path,
+ rowTrackIndex,
+ );
+ }, [videoDuration, totalMs, timelineCurrentTimeMs, audioRegions, onAudioAdded]);
+
+ const handleAddAnnotation = useCallback(
+ (trackIndex = 0) => {
+ if (!videoDuration || videoDuration === 0 || totalMs === 0 || !onAnnotationAdded) {
+ return;
+ }
+
+ const defaultDuration = Math.min(defaultRegionDurationMs, totalMs);
+ if (defaultDuration <= 0) {
+ return;
+ }
+
+ // Multiple annotations can exist at the same timestamp
+ const startPos = Math.max(0, Math.min(timelineCurrentTimeMs, totalMs));
+ const endPos = Math.min(startPos + defaultDuration, totalMs);
+
+ onAnnotationAdded({ start: startPos, end: endPos }, trackIndex);
+ },
+ [
+ videoDuration,
+ totalMs,
+ timelineCurrentTimeMs,
+ onAnnotationAdded,
+ defaultRegionDurationMs,
+ ],
+ );
+
+ useEffect(() => {
+ const handleKeyDown = (e: KeyboardEvent) => {
+ if (
+ e.target instanceof HTMLInputElement ||
+ e.target instanceof HTMLTextAreaElement
+ ) {
+ return;
+ }
+
+ if (matchesShortcut(e, { key: "a", ctrl: true }, isMac)) {
+ if (!hasAnyTimelineBlocks || !isTimelineFocusedRef.current) {
+ return;
+ }
+
+ e.preventDefault();
+ setSelectedKeyframeId(null);
+ setSelectAllBlocksActive(true);
+ return;
+ }
+
+ if (matchesShortcut(e, keyShortcuts.addKeyframe, isMac)) {
+ addKeyframe();
+ }
+ if (matchesShortcut(e, keyShortcuts.addZoom, isMac)) {
+ handleAddZoom();
+ }
+ if (matchesShortcut(e, keyShortcuts.addTrim, isMac)) {
+ handleAddTrim();
+ }
+ if (matchesShortcut(e, keyShortcuts.splitClip, isMac)) {
+ handleSplitClip();
+ }
+ if (matchesShortcut(e, keyShortcuts.addAnnotation, isMac)) {
+ handleAddAnnotation();
+ }
+ if (matchesShortcut(e, keyShortcuts.addSpeed, isMac)) {
+ handleAddSpeed();
+ }
+
+ // Tab: Cycle through overlapping annotations at current time
+ if (e.key === "Tab" && annotationRegions.length > 0) {
+ const overlapping = annotationRegions
+ .filter(
+ (a) =>
+ timelineCurrentTimeMs >= a.startMs &&
+ timelineCurrentTimeMs <= a.endMs,
+ )
+ .sort((a, b) => a.zIndex - b.zIndex); // Sort by z-index
+
+ if (overlapping.length > 0) {
+ e.preventDefault();
+
+ if (
+ !selectedAnnotationId ||
+ !overlapping.some((a) => a.id === selectedAnnotationId)
+ ) {
+ onSelectAnnotation?.(overlapping[0].id);
+ } else {
+ // Cycle to next annotation
+ const currentIndex = overlapping.findIndex(
+ (a) => a.id === selectedAnnotationId,
+ );
+ const nextIndex = e.shiftKey
+ ? (currentIndex - 1 + overlapping.length) % overlapping.length // Shift+Tab = backward
+ : (currentIndex + 1) % overlapping.length; // Tab = forward
+ onSelectAnnotation?.(overlapping[nextIndex].id);
+ }
+ }
+ }
+ // Delete key or Ctrl+D / Cmd+D
+ if (
+ e.key === "Delete" ||
+ e.key === "Backspace" ||
+ matchesShortcut(e, keyShortcuts.deleteSelected, isMac)
+ ) {
+ if (selectAllBlocksActive) {
+ e.preventDefault();
+ deleteAllBlocks();
+ } else if (selectedKeyframeId) {
+ deleteSelectedKeyframe();
+ } else if (selectedZoomId) {
+ deleteSelectedZoom();
+ } else if (selectedTrimId) {
+ deleteSelectedTrim();
+ } else if (selectedClipId) {
+ deleteSelectedClip();
+ } else if (selectedAnnotationId) {
+ deleteSelectedAnnotation();
+ } else if (selectedSpeedId) {
+ deleteSelectedSpeed();
+ } else if (selectedAudioId) {
+ deleteSelectedAudio();
+ }
+ }
+ };
+ window.addEventListener("keydown", handleKeyDown);
+ return () => window.removeEventListener("keydown", handleKeyDown);
+ }, [
+ addKeyframe,
+ handleAddZoom,
+ handleAddTrim,
+ handleSplitClip,
+ handleAddAnnotation,
+ handleAddSpeed,
+ deleteAllBlocks,
+ deleteSelectedKeyframe,
+ deleteSelectedZoom,
+ deleteSelectedTrim,
+ deleteSelectedClip,
+ deleteSelectedAnnotation,
+ deleteSelectedSpeed,
+ deleteSelectedAudio,
+ selectedKeyframeId,
+ selectedZoomId,
+ selectedTrimId,
+ selectedClipId,
+ selectedAnnotationId,
+ selectedSpeedId,
+ selectedAudioId,
+ annotationRegions,
+ hasAnyTimelineBlocks,
+ onSelectAnnotation,
+ keyShortcuts,
+ isMac,
+ selectAllBlocksActive,
+ timelineCurrentTimeMs,
+ ]);
+
+ const clampedRange = useMemo<Range>(() => {
+ if (totalMs === 0) {
+ return range;
+ }
+
+ return {
+ start: Math.max(0, Math.min(range.start, totalMs)),
+ end: Math.min(range.end, totalMs),
+ };
+ }, [range, totalMs]);
+
+ useImperativeHandle(
+ ref,
+ () => ({
+ addZoom: handleAddZoom,
+ suggestZooms: handleSuggestZooms,
+ splitClip: handleSplitClip,
+ addAnnotation: handleAddAnnotation,
+ addAudio: handleAddAudio,
+ isCollapsed,
+ toggleCollapsed: () => setIsCollapsed((prev) => !prev),
+ keyframes,
+ formatTime,
+ }),
+ [
+ handleAddZoom,
+ handleSuggestZooms,
+ handleSplitClip,
+ handleAddAnnotation,
+ handleAddAudio,
+ isCollapsed,
+ keyframes,
+ formatTime,
+ ],
+ );
+
+ const timelineItems = useMemo<TimelineRenderItem[]>(() => {
+ const zooms: TimelineRenderItem[] = zoomRegions.map((region, index) => ({
+ id: region.id,
+ rowId: ZOOM_ROW_ID,
+ span: { start: region.startMs, end: region.endMs },
+ label: `Zoom ${index + 1}`,
+ zoomDepth: region.depth,
+ zoomMode: region.mode ?? "auto",
+ variant: "zoom",
+ }));
+
+ const clips: TimelineRenderItem[] = clipRegions.map((region, index) => ({
+ id: region.id,
+ rowId: CLIP_ROW_ID,
+ span: { start: region.startMs, end: region.endMs },
+ label: `Clip ${index + 1}`,
+ variant: "clip",
+ }));
+
+ const annotations: TimelineRenderItem[] = annotationRegions.map((region) => {
+ let label: string;
+
+ if (region.type === "text") {
+ // Show text preview
+ const preview = region.content.trim() || "Empty text";
+ label = preview.length > 20 ? `${preview.substring(0, 20)}...` : preview;
+ } else if (region.type === "image") {
+ label = "Image";
+ } else {
+ label = "Annotation";
+ }
+
+ return {
+ id: region.id,
+ rowId: getAnnotationTrackRowId(region.trackIndex ?? 0),
+ span: { start: region.startMs, end: region.endMs },
+ label,
+ variant: "annotation",
+ };
+ });
+
+ const audios: TimelineRenderItem[] = audioRegions.map((region) => {
+ const fileName =
+ region.audioPath
+ .split(/[\\/]/)
+ .pop()
+ ?.replace(/\.[^.]+$/, "") || "Audio";
+ return {
+ id: region.id,
+ rowId: getAudioTrackRowId(region.trackIndex ?? 0),
+ span: { start: region.startMs, end: region.endMs },
+ label: fileName,
+ variant: "audio",
+ };
+ });
+
+ return [...zooms, ...clips, ...annotations, ...audios];
+ }, [zoomRegions, clipRegions, annotationRegions, audioRegions]);
+
+ // Flat list of draggable row spans for neighbour-clamping during drag/resize.
+ const allRegionSpans = useMemo(() => {
+ const zooms = zoomRegions.map((r) => ({
+ id: r.id,
+ start: r.startMs,
+ end: r.endMs,
+ rowId: ZOOM_ROW_ID,
+ }));
+ const clips = clipRegions.map((r) => ({
+ id: r.id,
+ start: r.startMs,
+ end: r.endMs,
+ rowId: CLIP_ROW_ID,
+ }));
+ const audios = audioRegions.map((r) => ({
+ id: r.id,
+ start: r.startMs,
+ end: r.endMs,
+ rowId: getAudioTrackRowId(r.trackIndex ?? 0),
+ }));
+ return [...zooms, ...clips, ...audios];
+ }, [zoomRegions, clipRegions, audioRegions]);
+
+ const handleItemSpanChange = useCallback(
+ (id: string, span: Span) => {
+ // Check if it's a zoom, trim, clip, speed, or annotation item
+ if (zoomRegions.some((r) => r.id === id)) {
+ onZoomSpanChange(id, span);
+ } else if (clipRegions.some((r) => r.id === id)) {
+ onClipSpanChange?.(id, span);
+ } else if (annotationRegions.some((r) => r.id === id)) {
+ onAnnotationSpanChange?.(id, span);
+ } else if (audioRegions.some((r) => r.id === id)) {
+ onAudioSpanChange?.(id, span);
+ }
+ },
+ [
+ zoomRegions,
+ clipRegions,
+ annotationRegions,
+ audioRegions,
+ onZoomSpanChange,
+ onClipSpanChange,
+ onAnnotationSpanChange,
+ onAudioSpanChange,
+ ],
+ );
+
+ const panTimelineRange = useCallback(
+ (deltaMs: number) => {
+ if (!Number.isFinite(deltaMs) || deltaMs === 0 || totalMs <= 0) {
+ return;
+ }
+
+ setRange((previous) => {
+ const visibleSpan = Math.max(1, previous.end - previous.start);
+ const maxStart = Math.max(0, totalMs - visibleSpan);
+ const nextStart = Math.max(0, Math.min(previous.start + deltaMs, maxStart));
+
+ return {
+ start: nextStart,
+ end: nextStart + visibleSpan,
+ };
+ });
+ },
+ [totalMs],
+ );
+
+ const handleTimelineWheel = useCallback(
+ (event: WheelEvent<HTMLDivElement>) => {
+ if (totalMs <= 0) {
+ return;
+ }
+
+ // Horizontal scroll (trackpad side-scroll or shift+scroll) → pan
+ const rawHorizontalDelta =
+ Math.abs(event.deltaX) > 0
+ ? event.deltaX
+ : event.shiftKey && Math.abs(event.deltaY) > 0
+ ? event.deltaY
+ : 0;
+
+ if (rawHorizontalDelta !== 0) {
+ event.preventDefault();
+ const containerWidth = timelineContainerRef.current?.clientWidth ?? 0;
+ const visibleRangeMs = clampedRange.end - clampedRange.start;
+ if (containerWidth <= 0 || visibleRangeMs <= 0) return;
+ const horizontalDeltaPx = normalizeWheelDeltaToPixels(
+ rawHorizontalDelta,
+ event.deltaMode,
+ );
+ const deltaMs = (horizontalDeltaPx / containerWidth) * visibleRangeMs;
+ panTimelineRange(deltaMs);
+ return;
+ }
+
+ // Vertical scroll → zoom in/out (no modifier key needed)
+ if (Math.abs(event.deltaY) > 0) {
+ event.preventDefault();
+ event.stopPropagation();
+ const containerWidth = timelineContainerRef.current?.clientWidth ?? 0;
+ const visibleRangeMs = clampedRange.end - clampedRange.start;
+ if (containerWidth <= 0 || visibleRangeMs <= 0) return;
+
+ // Zoom centered on cursor position
+ const rect = timelineContainerRef.current!.getBoundingClientRect();
+ const cursorFraction = Math.max(
+ 0,
+ Math.min(1, (event.clientX - rect.left) / containerWidth),
+ );
+ const cursorMs = clampedRange.start + cursorFraction * visibleRangeMs;
+
+ const zoomFactor = event.deltaY > 0 ? 1.15 : 1 / 1.15;
+ const newVisibleMs = Math.max(
+ 500,
+ Math.min(totalMs, visibleRangeMs * zoomFactor),
+ );
+
+ const newStart = Math.max(0, cursorMs - cursorFraction * newVisibleMs);
+ const newEnd = Math.min(totalMs, newStart + newVisibleMs);
+
+ setRange({
+ start: newEnd - newVisibleMs > 0 ? newEnd - newVisibleMs : 0,
+ end: newEnd,
+ });
+ }
+ },
+ [clampedRange.end, clampedRange.start, panTimelineRange, totalMs],
+ );
+
+ if (!videoDuration || videoDuration === 0) {
+ return (
+ <div className="flex-1 flex flex-col items-center justify-center rounded-lg bg-[#17171a] gap-3">
+ <div className="w-12 h-12 rounded-full bg-white/5 flex items-center justify-center">
+ <Plus className="w-6 h-6 text-slate-600" />
+ </div>
+ <div className="text-center">
+ <p className="text-sm font-medium text-slate-300">No Video Loaded</p>
+ <p className="text-xs text-slate-500 mt-1">
+ Drag and drop a video to start editing
+ </p>
+ </div>
+ </div>
+ );
+ }
+
+ return (
+ <div className="flex min-h-0 flex-1 flex-col gap-1.5">
+ {!hideToolbar && (
+ <div className="relative flex items-center px-1 py-1">
+ {/* Left tools */}
+ <div className="z-10 flex min-w-0 flex-1 items-center gap-1.5">
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ variant="ghost"
+ size="sm"
+ className="h-7 gap-1 rounded-full border border-white/[0.08] bg-white/[0.04] px-2.5 text-[11px] text-white/65 shadow-[inset_0_1px_0_rgba(255,255,255,0.06)] transition-all hover:bg-white/[0.08] hover:text-white"
+ >
+ <Plus className="w-3.5 h-3.5" />
+ <span className="font-medium">Add Layer</span>
+ <ChevronDown className="w-3 h-3" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent
+ align="start"
+ className="bg-[#1a1a1c] border-white/10"
+ >
+ <DropdownMenuItem
+ onClick={() => {
+ const nextTrackIndex =
+ annotationRegions.length > 0
+ ? Math.max(
+ ...annotationRegions.map(
+ (region) => region.trackIndex ?? 0,
+ ),
+ ) + 1
+ : 0;
+ handleAddAnnotation(nextTrackIndex);
+ }}
+ className="text-slate-300 hover:text-white hover:bg-white/10 cursor-pointer"
+ >
+ Annotation
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={handleAddAudio}
+ className="text-slate-300 hover:text-white hover:bg-white/10 cursor-pointer"
+ >
+ Audio
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ <div className="w-[1px] h-4 bg-white/10 mx-1" />
+ <Button
+ onClick={handleAddZoom}
+ variant="ghost"
+ size="icon"
+ className="h-7 w-7 rounded-full text-slate-400 transition-all hover:bg-[#2563EB]/10 hover:text-[#2563EB]"
+ title="Add Zoom (Z)"
+ >
+ <ZoomIn className="w-4 h-4" />
+ </Button>
+ <Button
+ onClick={handleSuggestZooms}
+ variant="ghost"
+ size="icon"
+ className="h-7 w-7 rounded-full text-slate-400 transition-all hover:bg-[#2563EB]/10 hover:text-[#2563EB]"
+ title="Suggest Zooms from Cursor"
+ >
+ <WandSparkles className="w-4 h-4" />
+ </Button>
+ <Button
+ onClick={handleSplitClip}
+ variant="ghost"
+ size="icon"
+ className="h-7 w-7 rounded-full text-slate-400 transition-all hover:bg-white/10 hover:text-white"
+ title="Split Clip (C)"
+ >
+ <Scissors className="w-4 h-4" />
+ </Button>
+ </div>
+ {/* Playback controls - absolutely centered under the preview column */}
+ <div className="absolute inset-0 flex items-center pointer-events-none">
+ <div className="flex items-center gap-1.5 pointer-events-auto w-full justify-center pr-8">
+ <span className="mr-1 text-[10px] font-medium tabular-nums text-slate-400">
+ {formatTime(currentTime)}
+ </span>
+ <Button
+ variant="ghost"
+ size="icon"
+ className="h-7 w-7 rounded-full text-slate-400 transition-all hover:bg-white/10 hover:text-white"
+ title="Skip Back"
+ onClick={() => {
+ if (!onSeek) return;
+ const currentMs = currentTime * 1000;
+ const prevKeyframe = [...keyframes]
+ .reverse()
+ .find((k) => k.time < currentMs - 50);
+ if (prevKeyframe) {
+ onSeek(prevKeyframe.time / 1000);
+ } else {
+ onSeek(Math.max(0, currentTime - 5));
+ }
+ }}
+ >
+ <SkipBack className="w-3.5 h-3.5" weight="fill" />
+ </Button>
+ <Button
+ variant="ghost"
+ size="icon"
+ className={cn(
+ "h-7 w-7 rounded-full border border-white/10 transition-all shadow-[0_8px_18px_rgba(0,0,0,0.18)]",
+ isPlaying
+ ? "bg-white/10 text-white hover:bg-white/20"
+ : "bg-white text-black hover:bg-white/90",
+ )}
+ onClick={onTogglePlayPause}
+ title={isPlaying ? "Pause" : "Play"}
+ >
+ {isPlaying ? (
+ <Pause className="w-3.5 h-3.5" weight="fill" />
+ ) : (
+ <Play className="w-3.5 h-3.5" weight="fill" />
+ )}
+ </Button>
+ <Button
+ variant="ghost"
+ size="icon"
+ className="h-7 w-7 rounded-full text-slate-400 transition-all hover:bg-white/10 hover:text-white"
+ title="Skip Forward"
+ onClick={() => {
+ if (!onSeek) return;
+ const currentMs = currentTime * 1000;
+ const nextKeyframe = keyframes.find(
+ (k) => k.time > currentMs + 50,
+ );
+ if (nextKeyframe) {
+ onSeek(nextKeyframe.time / 1000);
+ } else {
+ onSeek(Math.min(videoDuration, currentTime + 5));
+ }
+ }}
+ >
+ <SkipForward className="w-3.5 h-3.5" weight="fill" />
+ </Button>
+ <span className="text-[10px] font-medium text-slate-500 tabular-nums ml-1">
+ {formatTime(videoDuration)}
+ </span>
+ </div>
+ </div>
+ {/* Right section: volume inline slider */}
+ <div className="z-10 ml-auto flex items-center gap-2">
+ <Button
+ variant="ghost"
+ size="icon"
+ title={isCollapsed ? "Expand Timeline" : "Collapse Timeline"}
+ className="h-7 w-7 rounded-full text-slate-400 transition-all hover:bg-white/10 hover:text-white"
+ onClick={() => setIsCollapsed((prev) => !prev)}
+ >
+ {isCollapsed ? (
+ <ChevronUp className="w-3.5 h-3.5" />
+ ) : (
+ <ChevronDown className="w-3.5 h-3.5" />
+ )}
+ </Button>
+ {/* Volume icon + inline pill slider */}
+ <div className="flex items-center gap-1.5">
+ <button
+ type="button"
+ className="text-slate-400 hover:text-white transition-colors"
+ title="Mute/Unmute"
+ onClick={() => onVolumeChange?.(volume <= 0.001 ? 1 : 0)}
+ >
+ {volume <= 0.001 ? (
+ <VolumeX className="w-3.5 h-3.5" />
+ ) : volume < 0.5 ? (
+ <Volume1 className="w-3.5 h-3.5" />
+ ) : (
+ <Volume2 className="w-3.5 h-3.5" />
+ )}
+ </button>
+ <div className="relative flex h-7 w-24 select-none items-center overflow-hidden rounded-full border border-white/[0.06] bg-black/55 shadow-[inset_0_1px_0_rgba(255,255,255,0.06)]">
+ <div
+ className="absolute inset-y-[3px] left-[3px] right-auto rounded-[10px] bg-white/[0.08]"
+ style={{
+ width:
+ volume > 0
+ ? `max(calc(${volume * 100}% - 6px), 1.2rem)`
+ : 0,
+ }}
+ />
+ <div
+ className="pointer-events-none absolute bottom-[18%] top-[18%] z-10 w-[2px] rounded-full bg-white/95 shadow-[0_0_10px_rgba(37,99,235,0.28)]"
+ style={{ left: `calc(${volume * 100}% - 8px)` }}
+ />
+ <span className="pointer-events-none relative z-10 pl-2 text-[10px] font-medium text-slate-400">
+ {Math.round(volume * 100)}%
+ </span>
+ <input
+ type="range"
+ min="0"
+ max="1"
+ step="0.01"
+ value={volume}
+ onChange={(e) => onVolumeChange?.(Number(e.target.value))}
+ className="absolute inset-0 h-full w-full cursor-ew-resize opacity-0"
+ />
+ </div>
+ </div>
+ </div>
+ </div>
+ )}
+ <div
+ className={cn(
+ "flex min-h-0 flex-1 flex-col overflow-hidden rounded-[16px]",
+ isCollapsed && "hidden",
+ )}
+ >
+ <div
+ ref={timelineContainerRef}
+ className={cn(
+ "relative min-h-0 flex-1 overflow-auto outline-none",
+ timelineStyles.scrollArea,
+ )}
+ tabIndex={0}
+ onFocus={() => {
+ isTimelineFocusedRef.current = true;
+ }}
+ onBlur={() => {
+ isTimelineFocusedRef.current = false;
+ }}
+ onMouseDown={() => {
+ timelineContainerRef.current?.focus();
+ isTimelineFocusedRef.current = true;
+ }}
+ onClick={() => {
+ setSelectedKeyframeId(null);
+ setSelectAllBlocksActive(false);
+ }}
+ onWheel={handleTimelineWheel}
+ >
+ <TimelineWrapper
+ range={clampedRange}
+ videoDuration={videoDuration}
+ hasOverlap={hasOverlap}
+ onRangeChange={setRange}
+ minItemDurationMs={timelineScale.minItemDurationMs}
+ minVisibleRangeMs={timelineScale.minVisibleRangeMs}
+ onItemSpanChange={handleItemSpanChange}
+ allRegionSpans={allRegionSpans}
+ >
+ <KeyframeMarkers
+ keyframes={keyframes}
+ selectedKeyframeId={selectedKeyframeId}
+ setSelectedKeyframeId={setSelectedKeyframeId}
+ onKeyframeMove={handleKeyframeMove}
+ videoDurationMs={totalMs}
+ timelineRef={timelineContainerRef}
+ />
+ <Timeline
+ items={timelineItems}
+ videoDurationMs={totalMs}
+ currentTimeMs={playheadTimeMs}
+ onSeek={onSeek}
+ onSelectZoom={handleSelectZoom}
+ onSelectTrim={handleSelectTrim}
+ onSelectClip={handleSelectClip}
+ onSelectAnnotation={handleSelectAnnotation}
+ onSelectSpeed={handleSelectSpeed}
+ onSelectAudio={handleSelectAudio}
+ selectedZoomId={selectedZoomId}
+ selectedTrimId={selectedTrimId}
+ selectedClipId={selectedClipId}
+ selectedAnnotationId={selectedAnnotationId}
+ selectedSpeedId={selectedSpeedId}
+ selectedAudioId={selectedAudioId}
+ selectAllBlocksActive={selectAllBlocksActive}
+ onClearBlockSelection={clearSelectedBlocks}
+ keyframes={keyframes}
+ audioPeaks={audioPeaks}
+ onPanTimeline={panTimelineRange}
+ hiddenTrackIds={hiddenTrackIds}
+ />
+ </TimelineWrapper>
+ </div>
+ </div>
+ </div>
+ );
+ },
+);
+
+export default TimelineEditor;
diff --git a/src/components/video-editor/timeline/TimelineWrapper.tsx b/src/components/video-editor/timeline/TimelineWrapper.tsx
index 9d586f80..1e2a17f9 100644
--- a/src/components/video-editor/timeline/TimelineWrapper.tsx
+++ b/src/components/video-editor/timeline/TimelineWrapper.tsx
@@ -1,367 +1,389 @@
-import { useCallback, useRef } from "react";
-import type { Dispatch, ReactNode, SetStateAction } from "react";
-import { TimelineContext } from "dnd-timeline";
import type {
- DragEndEvent,
- DragMoveEvent,
- DragStartEvent,
- Range,
- ResizeEndEvent,
- ResizeMoveEvent,
- Span,
+ DragEndEvent,
+ DragMoveEvent,
+ DragStartEvent,
+ Range,
+ ResizeEndEvent,
+ ResizeMoveEvent,
+ Span,
} from "dnd-timeline";
+import { TimelineContext } from "dnd-timeline";
+import type { Dispatch, ReactNode, SetStateAction } from "react";
+import { useCallback, useRef } from "react";
interface TimelineWrapperProps {
- children: ReactNode;
- range: Range;
- videoDuration: number;
- hasOverlap: (newSpan: Span, excludeId?: string) => boolean;
- onRangeChange: Dispatch<SetStateAction<Range>>;
- minItemDurationMs: number;
- minVisibleRangeMs: number;
- gridSizeMs?: number;
- onItemSpanChange: (id: string, span: Span) => void;
- allRegionSpans?: { id: string; start: number; end: number; rowId: string }[];
+ children: ReactNode;
+ range: Range;
+ videoDuration: number;
+ hasOverlap: (newSpan: Span, excludeId?: string) => boolean;
+ onRangeChange: Dispatch<SetStateAction<Range>>;
+ minItemDurationMs: number;
+ minVisibleRangeMs: number;
+ gridSizeMs?: number;
+ onItemSpanChange: (id: string, span: Span) => void;
+ allRegionSpans?: { id: string; start: number; end: number; rowId: string }[];
}
export default function TimelineWrapper({
- children,
- range,
- videoDuration,
- hasOverlap,
- onRangeChange,
- minItemDurationMs,
- minVisibleRangeMs,
- gridSizeMs: _gridSizeMs,
- onItemSpanChange,
- allRegionSpans = [],
+ children,
+ range,
+ videoDuration,
+ hasOverlap,
+ onRangeChange,
+ minItemDurationMs,
+ minVisibleRangeMs,
+ gridSizeMs: _gridSizeMs,
+ onItemSpanChange,
+ allRegionSpans = [],
}: TimelineWrapperProps) {
- const totalMs = Math.max(0, Math.round(videoDuration * 1000));
-
- const clampSpanToBounds = useCallback(
- (span: Span): Span => {
- const rawDuration = Math.max(span.end - span.start, 0);
- const normalizedStart = Number.isFinite(span.start) ? span.start : 0;
-
- if (totalMs === 0) {
- const minDuration = Math.max(minItemDurationMs, 1);
- const duration = Math.max(rawDuration, minDuration);
- const start = Math.max(0, normalizedStart);
- return {
- start,
- end: start + duration,
- };
- }
-
- const minDuration = Math.min(Math.max(minItemDurationMs, 1), totalMs);
- const duration = Math.min(Math.max(rawDuration, minDuration), totalMs);
-
- const start = Math.max(0, Math.min(normalizedStart, totalMs - duration));
- const end = start + duration;
-
- return { start, end };
- },
- [minItemDurationMs, totalMs],
- );
-
- const clampRange = useCallback(
- (candidate: Range): Range => {
- if (totalMs === 0) {
- const minSpan = Math.max(minVisibleRangeMs, 1);
- const span = Math.max(candidate.end - candidate.start, minSpan);
- const start = Math.max(0, Math.min(candidate.start, candidate.end - span));
- return { start, end: start + span };
- }
-
- const rawStart = Math.max(0, candidate.start);
- const rawEnd = candidate.end;
- const clampedEnd = Math.min(rawEnd, totalMs);
-
- const minSpan = Math.min(Math.max(minVisibleRangeMs, 1), totalMs);
- const desiredSpan = clampedEnd - rawStart;
- const span = Math.min(Math.max(desiredSpan, minSpan), totalMs);
-
- let finalStart = rawStart;
- let finalEnd = finalStart + span;
-
- if (finalEnd > totalMs) {
- finalEnd = totalMs;
- finalStart = Math.max(0, finalEnd - span);
- }
-
- return { start: finalStart, end: finalEnd };
- },
- [minVisibleRangeMs, totalMs],
- );
-
- const getSiblingSpans = useCallback(
- (activeItemId: string) => {
- const activeItem = allRegionSpans.find((region) => region.id === activeItemId);
- if (!activeItem) {
- return [];
- }
-
- return allRegionSpans
- .filter((region) => region.id !== activeItemId && region.rowId === activeItem.rowId)
- .sort((left, right) => left.start - right.start);
- },
- [allRegionSpans],
- );
-
- // When a resize overlaps neighbours, clamp the resized edge to the nearest boundary.
- const clampResizedSpanToNeighbours = useCallback(
- (span: Span, activeItemId: string): Span => {
- const siblings = getSiblingSpans(activeItemId);
- const activeItem = allRegionSpans.find((region) => region.id === activeItemId);
- let { start, end } = span;
-
- for (const r of siblings) {
- // Span's right edge crossed into a region to the right
- if (end > r.start && start < r.start) {
- end = r.start;
- }
- // Span's left edge crossed into a region to the left
- if (start < r.end && end > r.end) {
- start = r.end;
- }
- }
-
- // Ensure minimum duration after clamping
- const minDur = Math.min(minItemDurationMs, totalMs || minItemDurationMs);
- if (end - start < minDur) {
- const resizedLeft = Boolean(activeItem && span.start !== activeItem.start && span.end === activeItem.end);
- if (resizedLeft) {
- start = end - minDur;
- } else {
- end = start + minDur;
- }
- }
-
- return { start: Math.max(0, start), end: Math.min(end, totalMs || end) };
- },
- [allRegionSpans, getSiblingSpans, minItemDurationMs, totalMs],
- );
-
- // When a drag overlaps neighbours, keep duration fixed and stop at the nearest gap boundary.
- const clampDraggedSpanToNeighbours = useCallback(
- (span: Span, activeItemId: string): Span => {
- const activeItem = allRegionSpans.find((region) => region.id === activeItemId);
- if (!activeItem) {
- return clampSpanToBounds(span);
- }
-
- const siblings = getSiblingSpans(activeItemId);
- const duration = Math.max(
- activeItem.end - activeItem.start,
- Math.min(minItemDurationMs, totalMs || minItemDurationMs),
- );
- const proposedStart = Number.isFinite(span.start) ? span.start : activeItem.start;
-
- const previousSibling = [...siblings].reverse().find((region) => region.end <= activeItem.start);
- const nextSibling = siblings.find((region) => region.start >= activeItem.end);
-
- const minStart = previousSibling ? previousSibling.end : 0;
- const maxStart = nextSibling
- ? nextSibling.start - duration
- : totalMs > 0
- ? totalMs - duration
- : proposedStart;
-
- const start = Math.max(minStart, Math.min(proposedStart, maxStart));
- return clampSpanToBounds({ start, end: start + duration });
- },
- [allRegionSpans, clampSpanToBounds, getSiblingSpans, minItemDurationMs, totalMs],
- );
-
- const onResizeEnd = useCallback(
- (event: ResizeEndEvent) => {
- const updatedSpan = event.active.data.current.getSpanFromResizeEvent?.(event);
- if (!updatedSpan) return;
-
- const activeItemId = event.active.id as string;
- let clampedSpan = clampSpanToBounds(updatedSpan);
-
- const effectiveMinDuration = totalMs > 0
- ? Math.min(minItemDurationMs, totalMs)
- : minItemDurationMs;
- if (clampedSpan.end - clampedSpan.start < effectiveMinDuration) {
- return;
- }
-
- // Clamp to neighbour boundaries instead of rejecting
- if (hasOverlap(clampedSpan, activeItemId)) {
- clampedSpan = clampSpanToBounds(clampResizedSpanToNeighbours(clampedSpan, activeItemId));
- // If still overlapping after clamping, fall back to original position
- if (hasOverlap(clampedSpan, activeItemId)) {
- return;
- }
- }
-
- onItemSpanChange(activeItemId, clampedSpan);
- },
- [clampResizedSpanToNeighbours, clampSpanToBounds, hasOverlap, minItemDurationMs, onItemSpanChange, totalMs]
- );
-
- const onDragEnd = useCallback(
- (event: DragEndEvent) => {
- const activeRowId = event.over?.id as string;
- const updatedSpan = event.active.data.current.getSpanFromDragEvent?.(event);
- if (!updatedSpan || !activeRowId) return;
-
- const activeItemId = event.active.id as string;
-
- // Drags are pure translations — always preserve the original duration.
- // The span from getSpanFromDragEvent can drift due to pixel-to-ms
- // rounding at different zoom levels, so pin to the known duration.
- const activeItem = allRegionSpans.find((r) => r.id === activeItemId);
- const originalDuration = activeItem
- ? activeItem.end - activeItem.start
- : updatedSpan.end - updatedSpan.start;
- const dragSpan: Span = { start: updatedSpan.start, end: updatedSpan.start + originalDuration };
-
- let clampedSpan = clampSpanToBounds(dragSpan);
-
- // Clamp to neighbour boundaries instead of rejecting
- if (hasOverlap(clampedSpan, activeItemId)) {
- clampedSpan = clampDraggedSpanToNeighbours(clampedSpan, activeItemId);
- if (hasOverlap(clampedSpan, activeItemId)) {
- return;
- }
- }
-
- onItemSpanChange(activeItemId, clampedSpan);
- },
- [allRegionSpans, clampDraggedSpanToNeighbours, clampSpanToBounds, hasOverlap, onItemSpanChange]
- );
-
- // Drag/resize tooltip (direct DOM updates, no re-renders)
- const tooltipRef = useRef<HTMLDivElement>(null);
-
- const formatTooltipMs = (ms: number) => {
- const s = ms / 1000;
- const min = Math.floor(s / 60);
- const sec = s % 60;
- return min > 0
- ? `${min}:${sec.toFixed(1).padStart(4, '0')}`
- : `${sec.toFixed(1)}s`;
- };
-
- const showTooltip = useCallback(
- (span: { start: number; end: number } | null, screenX?: number) => {
- const el = tooltipRef.current;
- if (!el) return;
- if (!span) {
- el.style.opacity = '0';
- return;
- }
- el.textContent = `${formatTooltipMs(span.start)} – ${formatTooltipMs(span.end)}`;
- el.style.opacity = '1';
- if (screenX !== undefined) {
- const parent = el.parentElement;
- if (parent) {
- const rect = parent.getBoundingClientRect();
- const x = Math.max(0, Math.min(screenX - rect.left, rect.width - 100));
- el.style.left = `${x}px`;
- }
- }
- },
- [],
- );
-
- const onDragStart = useCallback(
- (event: DragStartEvent) => {
- const span = event.active.data.current.getSpanFromDragEvent?.(event);
- if (span) showTooltip(span);
- },
- [showTooltip],
- );
-
- const onDragMove = useCallback(
- (event: DragMoveEvent) => {
- const span = event.active.data.current.getSpanFromDragEvent?.(event);
- const screenX = event.activatorEvent && 'clientX' in event.activatorEvent
- ? (event.activatorEvent as PointerEvent).clientX + (event.delta?.x ?? 0)
- : undefined;
- if (span) showTooltip(span, screenX);
- },
- [showTooltip],
- );
-
- const onResizeMove = useCallback(
- (event: ResizeMoveEvent) => {
- const span = event.active.data.current.getSpanFromResizeEvent?.(event);
- const screenX = event.activatorEvent && 'clientX' in event.activatorEvent
- ? (event.activatorEvent as PointerEvent).clientX + (event.delta?.x ?? 0)
- : undefined;
- if (span) showTooltip(span, screenX);
- },
- [showTooltip],
- );
-
- const hideTooltip = useCallback(() => showTooltip(null), [showTooltip]);
-
- const onResizeEndWithTooltip = useCallback(
- (event: ResizeEndEvent) => {
- hideTooltip();
- onResizeEnd(event);
- },
- [hideTooltip, onResizeEnd],
- );
-
- const onDragEndWithTooltip = useCallback(
- (event: DragEndEvent) => {
- hideTooltip();
- onDragEnd(event);
- },
- [hideTooltip, onDragEnd],
- );
-
- const handleRangeChange = useCallback(
- (updater: (previous: Range) => Range) => {
- onRangeChange((prev) => {
- const normalized = totalMs > 0 ? clampRange(prev) : prev;
- const desired = updater(normalized);
-
- if (totalMs > 0) {
- const clamped = clampRange(desired);
-
- if (clamped.end > totalMs) {
- const span = Math.min(clamped.end - clamped.start, totalMs);
- return {
- start: Math.max(0, totalMs - span),
- end: totalMs,
- };
- }
-
- return clamped;
- }
-
- return desired;
- });
- },
- [clampRange, onRangeChange, totalMs],
- );
-
- return (
- <TimelineContext
- range={range}
- onRangeChanged={handleRangeChange}
- onResizeEnd={onResizeEndWithTooltip}
- onResizeMove={onResizeMove}
- onDragStart={onDragStart}
- onDragMove={onDragMove}
- onDragEnd={onDragEndWithTooltip}
- autoScroll={{ enabled: false }}
- >
- <div className="relative h-full min-h-0">
- {children}
- {/* Floating tooltip shown during drag/resize */}
- <div
- ref={tooltipRef}
- className="absolute top-1 pointer-events-none z-[60] px-1.5 py-0.5 rounded bg-black/80 text-[10px] text-white/90 font-medium tabular-nums whitespace-nowrap border border-white/10 shadow-lg"
- style={{ opacity: 0, transition: 'opacity 0.1s' }}
- />
- </div>
- </TimelineContext>
- );
+ const totalMs = Math.max(0, Math.round(videoDuration * 1000));
+
+ const clampSpanToBounds = useCallback(
+ (span: Span): Span => {
+ const rawDuration = Math.max(span.end - span.start, 0);
+ const normalizedStart = Number.isFinite(span.start) ? span.start : 0;
+
+ if (totalMs === 0) {
+ const minDuration = Math.max(minItemDurationMs, 1);
+ const duration = Math.max(rawDuration, minDuration);
+ const start = Math.max(0, normalizedStart);
+ return {
+ start,
+ end: start + duration,
+ };
+ }
+
+ const minDuration = Math.min(Math.max(minItemDurationMs, 1), totalMs);
+ const duration = Math.min(Math.max(rawDuration, minDuration), totalMs);
+
+ const start = Math.max(0, Math.min(normalizedStart, totalMs - duration));
+ const end = start + duration;
+
+ return { start, end };
+ },
+ [minItemDurationMs, totalMs],
+ );
+
+ const clampRange = useCallback(
+ (candidate: Range): Range => {
+ if (totalMs === 0) {
+ const minSpan = Math.max(minVisibleRangeMs, 1);
+ const span = Math.max(candidate.end - candidate.start, minSpan);
+ const start = Math.max(0, Math.min(candidate.start, candidate.end - span));
+ return { start, end: start + span };
+ }
+
+ const rawStart = Math.max(0, candidate.start);
+ const rawEnd = candidate.end;
+ const clampedEnd = Math.min(rawEnd, totalMs);
+
+ const minSpan = Math.min(Math.max(minVisibleRangeMs, 1), totalMs);
+ const desiredSpan = clampedEnd - rawStart;
+ const span = Math.min(Math.max(desiredSpan, minSpan), totalMs);
+
+ let finalStart = rawStart;
+ let finalEnd = finalStart + span;
+
+ if (finalEnd > totalMs) {
+ finalEnd = totalMs;
+ finalStart = Math.max(0, finalEnd - span);
+ }
+
+ return { start: finalStart, end: finalEnd };
+ },
+ [minVisibleRangeMs, totalMs],
+ );
+
+ const getSiblingSpans = useCallback(
+ (activeItemId: string) => {
+ const activeItem = allRegionSpans.find((region) => region.id === activeItemId);
+ if (!activeItem) {
+ return [];
+ }
+
+ return allRegionSpans
+ .filter((region) => region.id !== activeItemId && region.rowId === activeItem.rowId)
+ .sort((left, right) => left.start - right.start);
+ },
+ [allRegionSpans],
+ );
+
+ // When a resize overlaps neighbours, clamp the resized edge to the nearest boundary.
+ const clampResizedSpanToNeighbours = useCallback(
+ (span: Span, activeItemId: string): Span => {
+ const siblings = getSiblingSpans(activeItemId);
+ const activeItem = allRegionSpans.find((region) => region.id === activeItemId);
+ let { start, end } = span;
+
+ for (const r of siblings) {
+ // Span's right edge crossed into a region to the right
+ if (end > r.start && start < r.start) {
+ end = r.start;
+ }
+ // Span's left edge crossed into a region to the left
+ if (start < r.end && end > r.end) {
+ start = r.end;
+ }
+ }
+
+ // Ensure minimum duration after clamping
+ const minDur = Math.min(minItemDurationMs, totalMs || minItemDurationMs);
+ if (end - start < minDur) {
+ const resizedLeft = Boolean(
+ activeItem && span.start !== activeItem.start && span.end === activeItem.end,
+ );
+ if (resizedLeft) {
+ start = end - minDur;
+ } else {
+ end = start + minDur;
+ }
+ }
+
+ return { start: Math.max(0, start), end: Math.min(end, totalMs || end) };
+ },
+ [allRegionSpans, getSiblingSpans, minItemDurationMs, totalMs],
+ );
+
+ // When a drag overlaps neighbours, keep duration fixed and stop at the nearest gap boundary.
+ const clampDraggedSpanToNeighbours = useCallback(
+ (span: Span, activeItemId: string): Span => {
+ const activeItem = allRegionSpans.find((region) => region.id === activeItemId);
+ if (!activeItem) {
+ return clampSpanToBounds(span);
+ }
+
+ const siblings = getSiblingSpans(activeItemId);
+ const duration = Math.max(
+ activeItem.end - activeItem.start,
+ Math.min(minItemDurationMs, totalMs || minItemDurationMs),
+ );
+ const proposedStart = Number.isFinite(span.start) ? span.start : activeItem.start;
+
+ const previousSibling = [...siblings]
+ .reverse()
+ .find((region) => region.end <= activeItem.start);
+ const nextSibling = siblings.find((region) => region.start >= activeItem.end);
+
+ const minStart = previousSibling ? previousSibling.end : 0;
+ const maxStart = nextSibling
+ ? nextSibling.start - duration
+ : totalMs > 0
+ ? totalMs - duration
+ : proposedStart;
+
+ const start = Math.max(minStart, Math.min(proposedStart, maxStart));
+ return clampSpanToBounds({ start, end: start + duration });
+ },
+ [allRegionSpans, clampSpanToBounds, getSiblingSpans, minItemDurationMs, totalMs],
+ );
+
+ const onResizeEnd = useCallback(
+ (event: ResizeEndEvent) => {
+ const updatedSpan = event.active.data.current.getSpanFromResizeEvent?.(event);
+ if (!updatedSpan) return;
+
+ const activeItemId = event.active.id as string;
+ let clampedSpan = clampSpanToBounds(updatedSpan);
+
+ const effectiveMinDuration =
+ totalMs > 0 ? Math.min(minItemDurationMs, totalMs) : minItemDurationMs;
+ if (clampedSpan.end - clampedSpan.start < effectiveMinDuration) {
+ return;
+ }
+
+ // Clamp to neighbour boundaries instead of rejecting
+ if (hasOverlap(clampedSpan, activeItemId)) {
+ clampedSpan = clampSpanToBounds(
+ clampResizedSpanToNeighbours(clampedSpan, activeItemId),
+ );
+ // If still overlapping after clamping, fall back to original position
+ if (hasOverlap(clampedSpan, activeItemId)) {
+ return;
+ }
+ }
+
+ onItemSpanChange(activeItemId, clampedSpan);
+ },
+ [
+ clampResizedSpanToNeighbours,
+ clampSpanToBounds,
+ hasOverlap,
+ minItemDurationMs,
+ onItemSpanChange,
+ totalMs,
+ ],
+ );
+
+ const onDragEnd = useCallback(
+ (event: DragEndEvent) => {
+ const activeRowId = event.over?.id as string;
+ const updatedSpan = event.active.data.current.getSpanFromDragEvent?.(event);
+ if (!updatedSpan || !activeRowId) return;
+
+ const activeItemId = event.active.id as string;
+
+ // Drags are pure translations — always preserve the original duration.
+ // The span from getSpanFromDragEvent can drift due to pixel-to-ms
+ // rounding at different zoom levels, so pin to the known duration.
+ const activeItem = allRegionSpans.find((r) => r.id === activeItemId);
+ const originalDuration = activeItem
+ ? activeItem.end - activeItem.start
+ : updatedSpan.end - updatedSpan.start;
+ const dragSpan: Span = {
+ start: updatedSpan.start,
+ end: updatedSpan.start + originalDuration,
+ };
+
+ let clampedSpan = clampSpanToBounds(dragSpan);
+
+ // Clamp to neighbour boundaries instead of rejecting
+ if (hasOverlap(clampedSpan, activeItemId)) {
+ clampedSpan = clampDraggedSpanToNeighbours(clampedSpan, activeItemId);
+ if (hasOverlap(clampedSpan, activeItemId)) {
+ return;
+ }
+ }
+
+ onItemSpanChange(activeItemId, clampedSpan);
+ },
+ [
+ allRegionSpans,
+ clampDraggedSpanToNeighbours,
+ clampSpanToBounds,
+ hasOverlap,
+ onItemSpanChange,
+ ],
+ );
+
+ // Drag/resize tooltip (direct DOM updates, no re-renders)
+ const tooltipRef = useRef<HTMLDivElement>(null);
+
+ const formatTooltipMs = (ms: number) => {
+ const s = ms / 1000;
+ const min = Math.floor(s / 60);
+ const sec = s % 60;
+ return min > 0 ? `${min}:${sec.toFixed(1).padStart(4, "0")}` : `${sec.toFixed(1)}s`;
+ };
+
+ const showTooltip = useCallback(
+ (span: { start: number; end: number } | null, screenX?: number) => {
+ const el = tooltipRef.current;
+ if (!el) return;
+ if (!span) {
+ el.style.opacity = "0";
+ return;
+ }
+ el.textContent = `${formatTooltipMs(span.start)} – ${formatTooltipMs(span.end)}`;
+ el.style.opacity = "1";
+ if (screenX !== undefined) {
+ const parent = el.parentElement;
+ if (parent) {
+ const rect = parent.getBoundingClientRect();
+ const x = Math.max(0, Math.min(screenX - rect.left, rect.width - 100));
+ el.style.left = `${x}px`;
+ }
+ }
+ },
+ [],
+ );
+
+ const onDragStart = useCallback(
+ (event: DragStartEvent) => {
+ const span = event.active.data.current.getSpanFromDragEvent?.(event);
+ if (span) showTooltip(span);
+ },
+ [showTooltip],
+ );
+
+ const onDragMove = useCallback(
+ (event: DragMoveEvent) => {
+ const span = event.active.data.current.getSpanFromDragEvent?.(event);
+ const screenX =
+ event.activatorEvent && "clientX" in event.activatorEvent
+ ? (event.activatorEvent as PointerEvent).clientX + (event.delta?.x ?? 0)
+ : undefined;
+ if (span) showTooltip(span, screenX);
+ },
+ [showTooltip],
+ );
+
+ const onResizeMove = useCallback(
+ (event: ResizeMoveEvent) => {
+ const span = event.active.data.current.getSpanFromResizeEvent?.(event);
+ const screenX =
+ event.activatorEvent && "clientX" in event.activatorEvent
+ ? (event.activatorEvent as PointerEvent).clientX + (event.delta?.x ?? 0)
+ : undefined;
+ if (span) showTooltip(span, screenX);
+ },
+ [showTooltip],
+ );
+
+ const hideTooltip = useCallback(() => showTooltip(null), [showTooltip]);
+
+ const onResizeEndWithTooltip = useCallback(
+ (event: ResizeEndEvent) => {
+ hideTooltip();
+ onResizeEnd(event);
+ },
+ [hideTooltip, onResizeEnd],
+ );
+
+ const onDragEndWithTooltip = useCallback(
+ (event: DragEndEvent) => {
+ hideTooltip();
+ onDragEnd(event);
+ },
+ [hideTooltip, onDragEnd],
+ );
+
+ const handleRangeChange = useCallback(
+ (updater: (previous: Range) => Range) => {
+ onRangeChange((prev) => {
+ const normalized = totalMs > 0 ? clampRange(prev) : prev;
+ const desired = updater(normalized);
+
+ if (totalMs > 0) {
+ const clamped = clampRange(desired);
+
+ if (clamped.end > totalMs) {
+ const span = Math.min(clamped.end - clamped.start, totalMs);
+ return {
+ start: Math.max(0, totalMs - span),
+ end: totalMs,
+ };
+ }
+
+ return clamped;
+ }
+
+ return desired;
+ });
+ },
+ [clampRange, onRangeChange, totalMs],
+ );
+
+ return (
+ <TimelineContext
+ range={range}
+ onRangeChanged={handleRangeChange}
+ onResizeEnd={onResizeEndWithTooltip}
+ onResizeMove={onResizeMove}
+ onDragStart={onDragStart}
+ onDragMove={onDragMove}
+ onDragEnd={onDragEndWithTooltip}
+ autoScroll={{ enabled: false }}
+ resizeHandleWidth={28}
+ >
+ <div className="relative h-full min-h-0">
+ {children}
+ {/* Floating tooltip shown during drag/resize */}
+ <div
+ ref={tooltipRef}
+ className="absolute top-1 pointer-events-none z-[60] px-1.5 py-0.5 rounded bg-black/80 text-[10px] text-white/90 font-medium tabular-nums whitespace-nowrap border border-white/10 shadow-lg"
+ style={{ opacity: 0, transition: "opacity 0.1s" }}
+ />
+ </div>
+ </TimelineContext>
+ );
}
diff --git a/src/components/video-editor/timeline/Track.tsx b/src/components/video-editor/timeline/Track.tsx
new file mode 100644
index 00000000..b1a542ce
--- /dev/null
+++ b/src/components/video-editor/timeline/Track.tsx
@@ -0,0 +1,39 @@
+import type { RowDefinition } from "dnd-timeline";
+import { useRow } from "dnd-timeline";
+import type { CSSProperties, ReactNode } from "react";
+
+interface TrackProps extends RowDefinition {
+ children: ReactNode;
+ hint?: string;
+ isEmpty?: boolean;
+ trackStyle?: CSSProperties;
+}
+
+export default function Track({ id, children, hint, isEmpty, trackStyle }: TrackProps) {
+ const { setNodeRef, rowWrapperStyle, rowStyle, rowSidebarStyle, setSidebarRef } = useRow({
+ id,
+ });
+
+ return (
+ <div
+ className="group/track flex-1 overflow-hidden bg-transparent"
+ style={{ ...rowWrapperStyle, marginBottom: 0, minHeight: 44, ...trackStyle }}
+ >
+ <div ref={setSidebarRef} style={rowSidebarStyle} />
+ <div
+ ref={setNodeRef}
+ className="relative flex-1 overflow-hidden"
+ style={{ ...rowStyle, minHeight: 44 }}
+ >
+ {isEmpty && hint ? (
+ <div className="pointer-events-none absolute inset-0 z-10 flex items-center justify-center select-none">
+ <span className="rounded-full border border-white/[0.05] bg-white/[0.02] px-2 py-0.5 text-[9px] font-medium tracking-[0.04em] text-white/30 uppercase">
+ {hint}
+ </span>
+ </div>
+ ) : null}
+ {children}
+ </div>
+ </div>
+ );
+}
diff --git a/src/components/video-editor/timeline/zoomSuggestionUtils.ts b/src/components/video-editor/timeline/zoomSuggestionUtils.ts
index 1c38a32d..2df2db85 100644
--- a/src/components/video-editor/timeline/zoomSuggestionUtils.ts
+++ b/src/components/video-editor/timeline/zoomSuggestionUtils.ts
@@ -5,292 +5,338 @@ export const MAX_DWELL_DURATION_MS = 2600;
export const DWELL_MOVE_THRESHOLD = 0.02;
export interface ZoomDwellCandidate {
- centerTimeMs: number;
- focus: ZoomFocus;
- strength: number;
+ centerTimeMs: number;
+ focus: ZoomFocus;
+ strength: number;
}
export interface CursorInteractionCandidate extends ZoomDwellCandidate {
- kind: 'dwell' | 'click-like' | 'double-click-like' | 'text-focus-like' | 'dropdown-open' | 'text-selection' | 'text-field-click';
+ kind:
+ | "dwell"
+ | "click-like"
+ | "double-click-like"
+ | "text-focus-like"
+ | "dropdown-open"
+ | "text-selection"
+ | "text-field-click";
}
export interface SuggestedZoomRegion {
- start: number;
- end: number;
- focus: ZoomFocus;
+ start: number;
+ end: number;
+ focus: ZoomFocus;
}
export type InteractionZoomSuggestionStatus =
- | 'ok'
- | 'no-telemetry'
- | 'no-interactions'
- | 'no-slots';
+ | "ok"
+ | "no-telemetry"
+ | "no-interactions"
+ | "no-slots";
export interface InteractionZoomSuggestionResult {
- status: InteractionZoomSuggestionStatus;
- suggestions: SuggestedZoomRegion[];
+ status: InteractionZoomSuggestionStatus;
+ suggestions: SuggestedZoomRegion[];
}
const DEFAULT_SUGGESTION_SPACING_MS = 1800;
const DEFAULT_MERGE_NEARBY_GAP_MS = 1500;
-function normalizeTelemetrySample(sample: CursorTelemetryPoint, totalMs: number): CursorTelemetryPoint {
- return {
- timeMs: Math.max(0, Math.min(sample.timeMs, totalMs)),
- cx: Math.max(0, Math.min(sample.cx, 1)),
- cy: Math.max(0, Math.min(sample.cy, 1)),
- interactionType: sample.interactionType,
- cursorType: sample.cursorType,
- };
+function normalizeTelemetrySample(
+ sample: CursorTelemetryPoint,
+ totalMs: number,
+): CursorTelemetryPoint {
+ return {
+ timeMs: Math.max(0, Math.min(sample.timeMs, totalMs)),
+ cx: Math.max(0, Math.min(sample.cx, 1)),
+ cy: Math.max(0, Math.min(sample.cy, 1)),
+ interactionType: sample.interactionType,
+ cursorType: sample.cursorType,
+ };
}
-function applyCursorTypeInRange(samples: CursorTelemetryPoint[], startMs: number, endMs: number, cursorType: NonNullable<CursorTelemetryPoint['cursorType']>) {
- for (const sample of samples) {
- if (sample.timeMs < startMs || sample.timeMs > endMs) continue;
- if (!sample.cursorType) {
- sample.cursorType = cursorType;
- }
- }
+function applyCursorTypeInRange(
+ samples: CursorTelemetryPoint[],
+ startMs: number,
+ endMs: number,
+ cursorType: NonNullable<CursorTelemetryPoint["cursorType"]>,
+) {
+ for (const sample of samples) {
+ if (sample.timeMs < startMs || sample.timeMs > endMs) continue;
+ if (!sample.cursorType) {
+ sample.cursorType = cursorType;
+ }
+ }
}
export function normalizeCursorTelemetry(
- telemetry: CursorTelemetryPoint[],
- totalMs: number,
+ telemetry: CursorTelemetryPoint[],
+ totalMs: number,
): CursorTelemetryPoint[] {
- const normalized = [...telemetry]
- .filter((sample) => Number.isFinite(sample.timeMs) && Number.isFinite(sample.cx) && Number.isFinite(sample.cy))
- .sort((a, b) => a.timeMs - b.timeMs)
- .map((sample) => normalizeTelemetrySample(sample, totalMs));
-
- const interactions = detectInteractionCandidates(normalized);
- for (const candidate of interactions) {
- if (candidate.kind === 'text-selection') {
- applyCursorTypeInRange(normalized, candidate.centerTimeMs - 140, candidate.centerTimeMs + 1200, 'text');
- continue;
- }
-
- if (candidate.kind === 'text-field-click' || candidate.kind === 'text-focus-like') {
- applyCursorTypeInRange(normalized, candidate.centerTimeMs - 100, candidate.centerTimeMs + 900, 'text');
- continue;
- }
- }
-
- for (const sample of normalized) {
- if (sample.interactionType !== 'click' && sample.interactionType !== 'double-click') {
- continue;
- }
-
- const mouseUp = normalized.find((candidate) => candidate.timeMs > sample.timeMs && candidate.interactionType === 'mouseup');
- if (!mouseUp) {
- continue;
- }
-
- const dragDuration = mouseUp.timeMs - sample.timeMs;
- const dragDistance = Math.hypot(mouseUp.cx - sample.cx, mouseUp.cy - sample.cy);
- if (dragDuration >= 160 && dragDistance > 0.015) {
- const isTextDrag = Math.abs(mouseUp.cx - sample.cx) > Math.abs(mouseUp.cy - sample.cy) * 1.8;
- applyCursorTypeInRange(normalized, sample.timeMs, mouseUp.timeMs, isTextDrag ? 'text' : 'closed-hand');
- }
- }
-
- return normalized;
+ const normalized = [...telemetry]
+ .filter(
+ (sample) =>
+ Number.isFinite(sample.timeMs) &&
+ Number.isFinite(sample.cx) &&
+ Number.isFinite(sample.cy),
+ )
+ .sort((a, b) => a.timeMs - b.timeMs)
+ .map((sample) => normalizeTelemetrySample(sample, totalMs));
+
+ const interactions = detectInteractionCandidates(normalized);
+ for (const candidate of interactions) {
+ if (candidate.kind === "text-selection") {
+ applyCursorTypeInRange(
+ normalized,
+ candidate.centerTimeMs - 140,
+ candidate.centerTimeMs + 1200,
+ "text",
+ );
+ continue;
+ }
+
+ if (candidate.kind === "text-field-click" || candidate.kind === "text-focus-like") {
+ applyCursorTypeInRange(
+ normalized,
+ candidate.centerTimeMs - 100,
+ candidate.centerTimeMs + 900,
+ "text",
+ );
+ continue;
+ }
+ }
+
+ for (const sample of normalized) {
+ if (sample.interactionType !== "click" && sample.interactionType !== "double-click") {
+ continue;
+ }
+
+ const mouseUp = normalized.find(
+ (candidate) =>
+ candidate.timeMs > sample.timeMs && candidate.interactionType === "mouseup",
+ );
+ if (!mouseUp) {
+ continue;
+ }
+
+ const dragDuration = mouseUp.timeMs - sample.timeMs;
+ const dragDistance = Math.hypot(mouseUp.cx - sample.cx, mouseUp.cy - sample.cy);
+ if (dragDuration >= 160 && dragDistance > 0.015) {
+ const isTextDrag =
+ Math.abs(mouseUp.cx - sample.cx) > Math.abs(mouseUp.cy - sample.cy) * 1.8;
+ applyCursorTypeInRange(
+ normalized,
+ sample.timeMs,
+ mouseUp.timeMs,
+ isTextDrag ? "text" : "closed-hand",
+ );
+ }
+ }
+
+ return normalized;
}
export function detectZoomDwellCandidates(samples: CursorTelemetryPoint[]): ZoomDwellCandidate[] {
- if (samples.length < 2) {
- return [];
- }
-
- const dwellCandidates: ZoomDwellCandidate[] = [];
- let runStart = 0;
-
- const pushRunIfDwell = (startIndex: number, endIndexExclusive: number) => {
- if (endIndexExclusive - startIndex < 2) {
- return;
- }
-
- const start = samples[startIndex];
- const end = samples[endIndexExclusive - 1];
- const runDuration = end.timeMs - start.timeMs;
- if (runDuration < MIN_DWELL_DURATION_MS || runDuration > MAX_DWELL_DURATION_MS) {
- return;
- }
-
- const runSamples = samples.slice(startIndex, endIndexExclusive);
- const avgCx = runSamples.reduce((sum, sample) => sum + sample.cx, 0) / runSamples.length;
- const avgCy = runSamples.reduce((sum, sample) => sum + sample.cy, 0) / runSamples.length;
-
- dwellCandidates.push({
- centerTimeMs: Math.round((start.timeMs + end.timeMs) / 2),
- focus: { cx: avgCx, cy: avgCy },
- strength: runDuration,
- });
- };
-
- for (let index = 1; index < samples.length; index += 1) {
- const prev = samples[index - 1];
- const curr = samples[index];
- const distance = Math.hypot(curr.cx - prev.cx, curr.cy - prev.cy);
-
- if (distance > DWELL_MOVE_THRESHOLD) {
- pushRunIfDwell(runStart, index);
- runStart = index;
- }
- }
- pushRunIfDwell(runStart, samples.length);
-
- return dwellCandidates;
+ if (samples.length < 2) {
+ return [];
+ }
+
+ const dwellCandidates: ZoomDwellCandidate[] = [];
+ let runStart = 0;
+
+ const pushRunIfDwell = (startIndex: number, endIndexExclusive: number) => {
+ if (endIndexExclusive - startIndex < 2) {
+ return;
+ }
+
+ const start = samples[startIndex];
+ const end = samples[endIndexExclusive - 1];
+ const runDuration = end.timeMs - start.timeMs;
+ if (runDuration < MIN_DWELL_DURATION_MS || runDuration > MAX_DWELL_DURATION_MS) {
+ return;
+ }
+
+ const runSamples = samples.slice(startIndex, endIndexExclusive);
+ const avgCx = runSamples.reduce((sum, sample) => sum + sample.cx, 0) / runSamples.length;
+ const avgCy = runSamples.reduce((sum, sample) => sum + sample.cy, 0) / runSamples.length;
+
+ dwellCandidates.push({
+ centerTimeMs: Math.round((start.timeMs + end.timeMs) / 2),
+ focus: { cx: avgCx, cy: avgCy },
+ strength: runDuration,
+ });
+ };
+
+ for (let index = 1; index < samples.length; index += 1) {
+ const prev = samples[index - 1];
+ const curr = samples[index];
+ const distance = Math.hypot(curr.cx - prev.cx, curr.cy - prev.cy);
+
+ if (distance > DWELL_MOVE_THRESHOLD) {
+ pushRunIfDwell(runStart, index);
+ runStart = index;
+ }
+ }
+ pushRunIfDwell(runStart, samples.length);
+
+ return dwellCandidates;
}
-export function detectInteractionCandidates(samples: CursorTelemetryPoint[]): CursorInteractionCandidate[] {
- // --- Phase 1: Explicit interaction events (from uiohook telemetry) ---
- const clickEvents = samples.filter(
- (s) => s.interactionType && s.interactionType !== 'move' && s.interactionType !== 'mouseup',
- );
-
- const explicitInteractionCandidates: CursorInteractionCandidate[] = [];
-
- for (const clickSample of clickEvents) {
- // Classify what happened AFTER this click by analyzing cursor trajectory
- const kind = classifyPostClickBehavior(samples, clickSample);
-
- const baseStrength =
- kind === 'double-click-like' ? 1500 :
- kind === 'dropdown-open' ? 1200 :
- kind === 'text-selection' ? 1300 :
- kind === 'text-field-click' ? 1100 :
- 900;
-
- explicitInteractionCandidates.push({
- centerTimeMs: Math.round(clickSample.timeMs),
- focus: { cx: clickSample.cx, cy: clickSample.cy },
- strength: baseStrength,
- kind,
- });
- }
-
- // --- Phase 2: Dwell-based heuristic candidates ---
- const dwellCandidates = detectZoomDwellCandidates(samples)
- .map<CursorInteractionCandidate>((candidate) => {
- if (candidate.strength >= 1100) {
- return { ...candidate, kind: 'text-focus-like' };
- }
- if (candidate.strength <= 800) {
- return { ...candidate, kind: 'click-like' };
- }
- return { ...candidate, kind: 'dwell' };
- });
-
- // --- Phase 3: Synthetic double-click detection from dwell pairs ---
- const doubleClickCandidates: CursorInteractionCandidate[] = [];
- const sortedByTime = [...dwellCandidates].sort((a, b) => a.centerTimeMs - b.centerTimeMs);
-
- for (let index = 1; index < sortedByTime.length; index += 1) {
- const prev = sortedByTime[index - 1];
- const curr = sortedByTime[index];
- const timeGap = curr.centerTimeMs - prev.centerTimeMs;
- const spatialGap = Math.hypot(curr.focus.cx - prev.focus.cx, curr.focus.cy - prev.focus.cy);
- const bothShort = prev.strength <= 900 && curr.strength <= 900;
-
- if (bothShort && timeGap <= 450 && spatialGap <= 0.035) {
- doubleClickCandidates.push({
- centerTimeMs: Math.round((prev.centerTimeMs + curr.centerTimeMs) / 2),
- focus: {
- cx: (prev.focus.cx + curr.focus.cx) / 2,
- cy: (prev.focus.cy + curr.focus.cy) / 2,
- },
- strength: prev.strength + curr.strength + 500,
- kind: 'double-click-like',
- });
- }
- }
-
- return [...explicitInteractionCandidates, ...dwellCandidates, ...doubleClickCandidates];
+export function detectInteractionCandidates(
+ samples: CursorTelemetryPoint[],
+): CursorInteractionCandidate[] {
+ // --- Phase 1: Explicit interaction events (from uiohook telemetry) ---
+ const clickEvents = samples.filter(
+ (s) => s.interactionType && s.interactionType !== "move" && s.interactionType !== "mouseup",
+ );
+
+ const explicitInteractionCandidates: CursorInteractionCandidate[] = [];
+
+ for (const clickSample of clickEvents) {
+ // Classify what happened AFTER this click by analyzing cursor trajectory
+ const kind = classifyPostClickBehavior(samples, clickSample);
+
+ const baseStrength =
+ kind === "double-click-like"
+ ? 1500
+ : kind === "dropdown-open"
+ ? 1200
+ : kind === "text-selection"
+ ? 1300
+ : kind === "text-field-click"
+ ? 1100
+ : 900;
+
+ explicitInteractionCandidates.push({
+ centerTimeMs: Math.round(clickSample.timeMs),
+ focus: { cx: clickSample.cx, cy: clickSample.cy },
+ strength: baseStrength,
+ kind,
+ });
+ }
+
+ // --- Phase 2: Dwell-based heuristic candidates ---
+ const dwellCandidates = detectZoomDwellCandidates(samples).map<CursorInteractionCandidate>(
+ (candidate) => {
+ if (candidate.strength >= 1100) {
+ return { ...candidate, kind: "text-focus-like" };
+ }
+ if (candidate.strength <= 800) {
+ return { ...candidate, kind: "click-like" };
+ }
+ return { ...candidate, kind: "dwell" };
+ },
+ );
+
+ // --- Phase 3: Synthetic double-click detection from dwell pairs ---
+ const doubleClickCandidates: CursorInteractionCandidate[] = [];
+ const sortedByTime = [...dwellCandidates].sort((a, b) => a.centerTimeMs - b.centerTimeMs);
+
+ for (let index = 1; index < sortedByTime.length; index += 1) {
+ const prev = sortedByTime[index - 1];
+ const curr = sortedByTime[index];
+ const timeGap = curr.centerTimeMs - prev.centerTimeMs;
+ const spatialGap = Math.hypot(curr.focus.cx - prev.focus.cx, curr.focus.cy - prev.focus.cy);
+ const bothShort = prev.strength <= 900 && curr.strength <= 900;
+
+ if (bothShort && timeGap <= 450 && spatialGap <= 0.035) {
+ doubleClickCandidates.push({
+ centerTimeMs: Math.round((prev.centerTimeMs + curr.centerTimeMs) / 2),
+ focus: {
+ cx: (prev.focus.cx + curr.focus.cx) / 2,
+ cy: (prev.focus.cy + curr.focus.cy) / 2,
+ },
+ strength: prev.strength + curr.strength + 500,
+ kind: "double-click-like",
+ });
+ }
+ }
+
+ return [...explicitInteractionCandidates, ...dwellCandidates, ...doubleClickCandidates];
}
export function buildInteractionZoomSuggestions(params: {
- cursorTelemetry: CursorTelemetryPoint[];
- totalMs: number;
- defaultDurationMs: number;
- reservedSpans?: Array<{ start: number; end: number }>;
- spacingMs?: number;
- mergeGapMs?: number;
+ cursorTelemetry: CursorTelemetryPoint[];
+ totalMs: number;
+ defaultDurationMs: number;
+ reservedSpans?: Array<{ start: number; end: number }>;
+ spacingMs?: number;
+ mergeGapMs?: number;
}): InteractionZoomSuggestionResult {
- const {
- cursorTelemetry,
- totalMs,
- defaultDurationMs,
- reservedSpans = [],
- spacingMs = DEFAULT_SUGGESTION_SPACING_MS,
- mergeGapMs = DEFAULT_MERGE_NEARBY_GAP_MS,
- } = params;
-
- const defaultDuration = Math.min(defaultDurationMs, totalMs);
- if (defaultDuration <= 0) {
- return { status: 'no-slots', suggestions: [] };
- }
-
- const normalizedSamples = normalizeCursorTelemetry(cursorTelemetry, totalMs);
- if (normalizedSamples.length < 2) {
- return { status: 'no-telemetry', suggestions: [] };
- }
-
- const interactionCandidates = detectInteractionCandidates(normalizedSamples);
- if (interactionCandidates.length === 0) {
- return { status: 'no-interactions', suggestions: [] };
- }
-
- const sortedCandidates = [...interactionCandidates].sort((a, b) => b.strength - a.strength);
- const acceptedCenters: number[] = [];
- const accepted: SuggestedZoomRegion[] = [];
- const reserved = [...reservedSpans].sort((a, b) => a.start - b.start);
-
- sortedCandidates.forEach((candidate) => {
- const tooCloseToAccepted = acceptedCenters.some(
- (center) => Math.abs(center - candidate.centerTimeMs) < spacingMs,
- );
-
- if (tooCloseToAccepted) {
- return;
- }
-
- const centeredStart = Math.round(candidate.centerTimeMs - defaultDuration / 2);
- const candidateStart = Math.max(0, Math.min(centeredStart, totalMs - defaultDuration));
- const candidateEnd = candidateStart + defaultDuration;
- const hasOverlap = reserved.some(
- (span) => candidateEnd > span.start && candidateStart < span.end,
- );
-
- if (hasOverlap) {
- return;
- }
-
- reserved.push({ start: candidateStart, end: candidateEnd });
- acceptedCenters.push(candidate.centerTimeMs);
- accepted.push({
- start: candidateStart,
- end: candidateEnd,
- focus: candidate.focus,
- });
- });
-
- const sortedAccepted = [...accepted].sort((a, b) => a.start - b.start);
- const merged: SuggestedZoomRegion[] = [];
- for (const region of sortedAccepted) {
- const previous = merged[merged.length - 1];
- if (previous && region.start - previous.end <= mergeGapMs) {
- previous.end = Math.max(previous.end, region.end);
- continue;
- }
-
- merged.push({ ...region });
- }
-
- if (merged.length === 0) {
- return { status: 'no-slots', suggestions: [] };
- }
-
- return { status: 'ok', suggestions: merged };
+ const {
+ cursorTelemetry,
+ totalMs,
+ defaultDurationMs,
+ reservedSpans = [],
+ spacingMs = DEFAULT_SUGGESTION_SPACING_MS,
+ mergeGapMs = DEFAULT_MERGE_NEARBY_GAP_MS,
+ } = params;
+
+ const defaultDuration = Math.min(defaultDurationMs, totalMs);
+ if (defaultDuration <= 0) {
+ return { status: "no-slots", suggestions: [] };
+ }
+
+ const normalizedSamples = normalizeCursorTelemetry(cursorTelemetry, totalMs);
+ if (normalizedSamples.length < 2) {
+ return { status: "no-telemetry", suggestions: [] };
+ }
+
+ const interactionCandidates = detectInteractionCandidates(normalizedSamples);
+ if (interactionCandidates.length === 0) {
+ return { status: "no-interactions", suggestions: [] };
+ }
+
+ const sortedCandidates = [...interactionCandidates].sort((a, b) => b.strength - a.strength);
+ const acceptedCenters: number[] = [];
+ const accepted: SuggestedZoomRegion[] = [];
+ const reserved = [...reservedSpans].sort((a, b) => a.start - b.start);
+
+ sortedCandidates.forEach((candidate) => {
+ const tooCloseToAccepted = acceptedCenters.some(
+ (center) => Math.abs(center - candidate.centerTimeMs) < spacingMs,
+ );
+
+ if (tooCloseToAccepted) {
+ return;
+ }
+
+ const centeredStart = Math.round(candidate.centerTimeMs - defaultDuration / 2);
+ const candidateStart = Math.max(0, Math.min(centeredStart, totalMs - defaultDuration));
+ const candidateEnd = candidateStart + defaultDuration;
+ const hasOverlap = reserved.some(
+ (span) => candidateEnd > span.start && candidateStart < span.end,
+ );
+
+ if (hasOverlap) {
+ return;
+ }
+
+ reserved.push({ start: candidateStart, end: candidateEnd });
+ acceptedCenters.push(candidate.centerTimeMs);
+ accepted.push({
+ start: candidateStart,
+ end: candidateEnd,
+ focus: candidate.focus,
+ });
+ });
+
+ const sortedAccepted = [...accepted].sort((a, b) => a.start - b.start);
+ const merged: SuggestedZoomRegion[] = [];
+ for (const region of sortedAccepted) {
+ const previous = merged[merged.length - 1];
+ if (previous && region.start - previous.end <= mergeGapMs) {
+ previous.end = Math.max(previous.end, region.end);
+ continue;
+ }
+
+ merged.push({ ...region });
+ }
+
+ if (merged.length === 0) {
+ return { status: "no-slots", suggestions: [] };
+ }
+
+ return { status: "ok", suggestions: merged };
}
/**
@@ -303,72 +349,74 @@ export function buildInteractionZoomSuggestions(params: {
* - **click-like**: generic click with no recognizable post-click pattern
*/
function classifyPostClickBehavior(
- samples: CursorTelemetryPoint[],
- clickSample: CursorTelemetryPoint,
-): CursorInteractionCandidate['kind'] {
- // Explicit double-click from uiohook
- if (clickSample.interactionType === 'double-click') {
- return 'double-click-like';
- }
-
- const clickTime = clickSample.timeMs;
-
- // Check for mouseup shortly after (drag detection)
- const mouseUpAfter = samples.find(
- (s) => s.interactionType === 'mouseup' && s.timeMs > clickTime && s.timeMs - clickTime < 3000,
- );
-
- if (mouseUpAfter) {
- const dragDx = Math.abs(mouseUpAfter.cx - clickSample.cx);
- const dragDy = Math.abs(mouseUpAfter.cy - clickSample.cy);
- const dragDuration = mouseUpAfter.timeMs - clickTime;
-
- // Text selection: horizontal drag > 3% of screen, mostly horizontal, duration 200ms+
- if (dragDuration >= 200 && dragDx > 0.03 && dragDx > dragDy * 1.8) {
- return 'text-selection';
- }
- }
-
- // Analyze trajectory in the 400ms-2000ms window after click
- const moveSamples = samples.filter(
- (s) => s.timeMs > clickTime + 100 && s.timeMs <= clickTime + 2000 &&
- (s.interactionType === 'move' || !s.interactionType),
- );
-
- if (moveSamples.length < 3) {
- // Very few move samples after click = cursor stayed still = text field click
- return 'text-field-click';
- }
-
- // Compute displacement from click position
- let maxDist = 0;
- let totalAbsDy = 0;
- let totalAbsDx = 0;
- for (const s of moveSamples) {
- const dist = Math.hypot(s.cx - clickSample.cx, s.cy - clickSample.cy);
- maxDist = Math.max(maxDist, dist);
- totalAbsDx += Math.abs(s.cx - clickSample.cx);
- totalAbsDy += Math.abs(s.cy - clickSample.cy);
- }
-
- // Cursor barely moved after click: text field click (dwell)
- if (maxDist < 0.02) {
- return 'text-field-click';
- }
-
- // Primarily downward movement after click: dropdown open
- const lastMoveSample = moveSamples[moveSamples.length - 1];
- const netDy = lastMoveSample.cy - clickSample.cy;
-
- if (netDy > 0.03 && totalAbsDy > totalAbsDx * 1.5) {
- return 'dropdown-open';
- }
-
- // Primarily horizontal movement: text selection (fallback if no mouseup)
- if (totalAbsDx > 0.03 && totalAbsDx > totalAbsDy * 1.8) {
- return 'text-selection';
- }
-
- return 'click-like';
+ samples: CursorTelemetryPoint[],
+ clickSample: CursorTelemetryPoint,
+): CursorInteractionCandidate["kind"] {
+ // Explicit double-click from uiohook
+ if (clickSample.interactionType === "double-click") {
+ return "double-click-like";
+ }
+
+ const clickTime = clickSample.timeMs;
+
+ // Check for mouseup shortly after (drag detection)
+ const mouseUpAfter = samples.find(
+ (s) =>
+ s.interactionType === "mouseup" && s.timeMs > clickTime && s.timeMs - clickTime < 3000,
+ );
+
+ if (mouseUpAfter) {
+ const dragDx = Math.abs(mouseUpAfter.cx - clickSample.cx);
+ const dragDy = Math.abs(mouseUpAfter.cy - clickSample.cy);
+ const dragDuration = mouseUpAfter.timeMs - clickTime;
+
+ // Text selection: horizontal drag > 3% of screen, mostly horizontal, duration 200ms+
+ if (dragDuration >= 200 && dragDx > 0.03 && dragDx > dragDy * 1.8) {
+ return "text-selection";
+ }
+ }
+
+ // Analyze trajectory in the 400ms-2000ms window after click
+ const moveSamples = samples.filter(
+ (s) =>
+ s.timeMs > clickTime + 100 &&
+ s.timeMs <= clickTime + 2000 &&
+ (s.interactionType === "move" || !s.interactionType),
+ );
+
+ if (moveSamples.length < 3) {
+ // Very few move samples after click = cursor stayed still = text field click
+ return "text-field-click";
+ }
+
+ // Compute displacement from click position
+ let maxDist = 0;
+ let totalAbsDy = 0;
+ let totalAbsDx = 0;
+ for (const s of moveSamples) {
+ const dist = Math.hypot(s.cx - clickSample.cx, s.cy - clickSample.cy);
+ maxDist = Math.max(maxDist, dist);
+ totalAbsDx += Math.abs(s.cx - clickSample.cx);
+ totalAbsDy += Math.abs(s.cy - clickSample.cy);
+ }
+
+ // Cursor barely moved after click: text field click (dwell)
+ if (maxDist < 0.02) {
+ return "text-field-click";
+ }
+
+ // Primarily downward movement after click: dropdown open
+ const lastMoveSample = moveSamples[moveSamples.length - 1];
+ const netDy = lastMoveSample.cy - clickSample.cy;
+
+ if (netDy > 0.03 && totalAbsDy > totalAbsDx * 1.5) {
+ return "dropdown-open";
+ }
+
+ // Primarily horizontal movement: text selection (fallback if no mouseup)
+ if (totalAbsDx > 0.03 && totalAbsDx > totalAbsDy * 1.8) {
+ return "text-selection";
+ }
+
+ return "click-like";
}
-
diff --git a/src/components/video-editor/videoPlayback/constants.ts b/src/components/video-editor/videoPlayback/constants.ts
index 3df5e265..64a2f4e4 100644
--- a/src/components/video-editor/videoPlayback/constants.ts
+++ b/src/components/video-editor/videoPlayback/constants.ts
@@ -3,9 +3,8 @@ import type { ZoomFocus } from "../types";
export const DEFAULT_FOCUS: ZoomFocus = { cx: 0.5, cy: 0.5 };
export const TRANSITION_WINDOW_MS = 1015.05;
export const ZOOM_IN_TRANSITION_WINDOW_MS = TRANSITION_WINDOW_MS * 1.5;
-export const ZOOM_OUT_EARLY_START_MS = 0;
+export const ZOOM_OUT_EARLY_START_MS = 500;
export const MIN_DELTA = 0.0001;
export const VIEWPORT_SCALE = 0.8;
export const ZOOM_TRANSLATION_DEADZONE_PX = 1.25;
export const ZOOM_SCALE_DEADZONE = 0.002;
-
diff --git a/src/components/video-editor/videoPlayback/cursorFollowCamera.ts b/src/components/video-editor/videoPlayback/cursorFollowCamera.ts
index 708c8843..b18cdec1 100644
--- a/src/components/video-editor/videoPlayback/cursorFollowCamera.ts
+++ b/src/components/video-editor/videoPlayback/cursorFollowCamera.ts
@@ -19,49 +19,49 @@ export const SNAP_TO_EDGES_RATIO_MANUAL = 0.25;
export const SNAP_TO_EDGES_RATIO_AUTO = 0.25;
export interface CursorFollowCameraState {
- /** Whether the state has been initialized with a starting position */
- initialized: boolean;
- /** Time of last update in ms (video time, not wall clock) */
- lastTimeMs: number;
- /** Whether the camera was active (zoomed) on the previous frame */
- wasZoomed: boolean;
- /** Whether the zoom reached full strength (≈1) — used to detect zoom-out */
- reachedFullZoom: boolean;
- /** Frozen focus when zooming out (camera holds position) */
- frozenFocusX: number;
- frozenFocusY: number;
+ /** Whether the state has been initialized with a starting position */
+ initialized: boolean;
+ /** Time of last update in ms (video time, not wall clock) */
+ lastTimeMs: number;
+ /** Whether the camera was active (zoomed) on the previous frame */
+ wasZoomed: boolean;
+ /** Whether the zoom reached full strength (≈1) — used to detect zoom-out */
+ reachedFullZoom: boolean;
+ /** Frozen focus when zooming out (camera holds position) */
+ frozenFocusX: number;
+ frozenFocusY: number;
}
export interface CursorFollowConfig {
- /**
- * snapToEdgesRatio — how much of the screen edge pins the camera.
- * 0.25 for manual zooms, 0.5 for auto/system zooms.
- */
- snapToEdgesRatio: number;
+ /**
+ * snapToEdgesRatio — how much of the screen edge pins the camera.
+ * 0.25 for manual zooms, 0.5 for auto/system zooms.
+ */
+ snapToEdgesRatio: number;
}
export const DEFAULT_CURSOR_FOLLOW_CONFIG: CursorFollowConfig = {
- snapToEdgesRatio: SNAP_TO_EDGES_RATIO_AUTO,
+ snapToEdgesRatio: SNAP_TO_EDGES_RATIO_AUTO,
};
export function createCursorFollowCameraState(): CursorFollowCameraState {
- return {
- initialized: false,
- lastTimeMs: 0,
- wasZoomed: false,
- reachedFullZoom: false,
- frozenFocusX: 0.5,
- frozenFocusY: 0.5,
- };
+ return {
+ initialized: false,
+ lastTimeMs: 0,
+ wasZoomed: false,
+ reachedFullZoom: false,
+ frozenFocusX: 0.5,
+ frozenFocusY: 0.5,
+ };
}
export function resetCursorFollowCamera(state: CursorFollowCameraState): void {
- state.initialized = false;
- state.lastTimeMs = 0;
- state.wasZoomed = false;
- state.reachedFullZoom = false;
- state.frozenFocusX = 0.5;
- state.frozenFocusY = 0.5;
+ state.initialized = false;
+ state.lastTimeMs = 0;
+ state.wasZoomed = false;
+ state.reachedFullZoom = false;
+ state.frozenFocusX = 0.5;
+ state.frozenFocusY = 0.5;
}
/**
@@ -75,60 +75,60 @@ export function resetCursorFollowCamera(state: CursorFollowCameraState): void {
* @returns The target focus point for this frame (normalized 0-1).
*/
export function computeCursorFollowFocus(
- state: CursorFollowCameraState,
- cursorSamples: CursorTelemetryPoint[],
- timeMs: number,
- zoomScale: number,
- zoomStrength: number,
- regionFocus: ZoomFocus,
- config: CursorFollowConfig = DEFAULT_CURSOR_FOLLOW_CONFIG,
+ state: CursorFollowCameraState,
+ cursorSamples: CursorTelemetryPoint[],
+ timeMs: number,
+ zoomScale: number,
+ zoomStrength: number,
+ regionFocus: ZoomFocus,
+ config: CursorFollowConfig = DEFAULT_CURSOR_FOLLOW_CONFIG,
): ZoomFocus {
- // If no cursor data available, fall back to static region focus
- const cursorPos = interpolateCursorPosition(cursorSamples, timeMs);
- if (!cursorPos) {
- return regionFocus;
- }
-
- // If not zoomed (strength ≈ 0), reset state and return region focus
- if (zoomStrength < 0.01) {
- if (state.wasZoomed) {
- state.wasZoomed = false;
- state.initialized = false;
- state.reachedFullZoom = false;
- }
- return regionFocus;
- }
-
- // Track when zoom reaches full strength
- if (zoomStrength >= 0.99) {
- state.reachedFullZoom = true;
- }
-
- // Zooming out: was fully zoomed but strength is now dropping — freeze camera
- if (state.reachedFullZoom && zoomStrength < 0.99) {
- return { cx: state.frozenFocusX, cy: state.frozenFocusY };
- }
-
- // First frame of a zoom: mark initialized
- if (!state.initialized || !state.wasZoomed) {
- state.lastTimeMs = timeMs;
- state.initialized = true;
- state.wasZoomed = true;
- }
-
- state.lastTimeMs = timeMs;
-
- // Edge snap: maps cursor through clamped linear remap.
- // Camera pins to edge when cursor is within snapToEdgesRatio of boundary.
- const targetFocus = edgeSnapFocus(
- { cx: cursorPos.cx, cy: cursorPos.cy },
- zoomScale,
- config.snapToEdgesRatio,
- );
-
- // Save for zoom-out freeze
- state.frozenFocusX = targetFocus.cx;
- state.frozenFocusY = targetFocus.cy;
-
- return targetFocus;
+ // If no cursor data available, fall back to static region focus
+ const cursorPos = interpolateCursorPosition(cursorSamples, timeMs);
+ if (!cursorPos) {
+ return regionFocus;
+ }
+
+ // If not zoomed (strength ≈ 0), reset state and return region focus
+ if (zoomStrength < 0.01) {
+ if (state.wasZoomed) {
+ state.wasZoomed = false;
+ state.initialized = false;
+ state.reachedFullZoom = false;
+ }
+ return regionFocus;
+ }
+
+ // Track when zoom reaches full strength
+ if (zoomStrength >= 0.99) {
+ state.reachedFullZoom = true;
+ }
+
+ // Zooming out: was fully zoomed but strength is now dropping — freeze camera
+ if (state.reachedFullZoom && zoomStrength < 0.99) {
+ return { cx: state.frozenFocusX, cy: state.frozenFocusY };
+ }
+
+ // First frame of a zoom: mark initialized
+ if (!state.initialized || !state.wasZoomed) {
+ state.lastTimeMs = timeMs;
+ state.initialized = true;
+ state.wasZoomed = true;
+ }
+
+ state.lastTimeMs = timeMs;
+
+ // Edge snap: maps cursor through clamped linear remap.
+ // Camera pins to edge when cursor is within snapToEdgesRatio of boundary.
+ const targetFocus = edgeSnapFocus(
+ { cx: cursorPos.cx, cy: cursorPos.cy },
+ zoomScale,
+ config.snapToEdgesRatio,
+ );
+
+ // Save for zoom-out freeze
+ state.frozenFocusX = targetFocus.cx;
+ state.frozenFocusY = targetFocus.cy;
+
+ return targetFocus;
}
diff --git a/src/components/video-editor/videoPlayback/cursorLoopTelemetry.ts b/src/components/video-editor/videoPlayback/cursorLoopTelemetry.ts
index bdb5e6a6..87ed503c 100644
--- a/src/components/video-editor/videoPlayback/cursorLoopTelemetry.ts
+++ b/src/components/video-editor/videoPlayback/cursorLoopTelemetry.ts
@@ -1,4 +1,4 @@
-import type { CursorTelemetryPoint, TrimRegion } from '../types';
+import type { CursorTelemetryPoint, TrimRegion } from "../types";
const LOOP_CURSOR_FREEZE_DURATION_MS = 670;
const LOOP_CURSOR_RETURN_STEPS = 20;
@@ -6,292 +6,312 @@ const LOOP_CURSOR_SETTLE_DURATION_MS = 120;
const CURSOR_TRAILING_MOVEMENT_EPSILON = 0.0015;
function clamp(value: number, min: number, max: number) {
- return Math.min(max, Math.max(min, value));
+ return Math.min(max, Math.max(min, value));
}
function interpolateCursorPosition(
- samples: CursorTelemetryPoint[],
- timeMs: number,
+ samples: CursorTelemetryPoint[],
+ timeMs: number,
): { cx: number; cy: number } | null {
- if (!samples || samples.length === 0) return null;
-
- if (timeMs <= samples[0].timeMs) {
- return { cx: samples[0].cx, cy: samples[0].cy };
- }
-
- if (timeMs >= samples[samples.length - 1].timeMs) {
- return { cx: samples[samples.length - 1].cx, cy: samples[samples.length - 1].cy };
- }
-
- let lo = 0;
- let hi = samples.length - 1;
- while (lo < hi - 1) {
- const mid = (lo + hi) >> 1;
- if (samples[mid].timeMs <= timeMs) {
- lo = mid;
- } else {
- hi = mid;
- }
- }
-
- const a = samples[lo];
- const b = samples[hi];
- const span = b.timeMs - a.timeMs;
- if (span <= 0) return { cx: a.cx, cy: a.cy };
-
- const t = (timeMs - a.timeMs) / span;
- return {
- cx: a.cx + (b.cx - a.cx) * t,
- cy: a.cy + (b.cy - a.cy) * t,
- };
+ if (!samples || samples.length === 0) return null;
+
+ if (timeMs <= samples[0].timeMs) {
+ return { cx: samples[0].cx, cy: samples[0].cy };
+ }
+
+ if (timeMs >= samples[samples.length - 1].timeMs) {
+ return { cx: samples[samples.length - 1].cx, cy: samples[samples.length - 1].cy };
+ }
+
+ let lo = 0;
+ let hi = samples.length - 1;
+ while (lo < hi - 1) {
+ const mid = (lo + hi) >> 1;
+ if (samples[mid].timeMs <= timeMs) {
+ lo = mid;
+ } else {
+ hi = mid;
+ }
+ }
+
+ const a = samples[lo];
+ const b = samples[hi];
+ const span = b.timeMs - a.timeMs;
+ if (span <= 0) return { cx: a.cx, cy: a.cy };
+
+ const t = (timeMs - a.timeMs) / span;
+ return {
+ cx: a.cx + (b.cx - a.cx) * t,
+ cy: a.cy + (b.cy - a.cy) * t,
+ };
}
function findFirstStableCursorType(samples: CursorTelemetryPoint[]) {
- for (const sample of samples) {
- if (!sample.cursorType) {
- continue;
- }
-
- if (sample.interactionType === 'click'
- || sample.interactionType === 'double-click'
- || sample.interactionType === 'right-click'
- || sample.interactionType === 'middle-click') {
- continue;
- }
-
- return sample.cursorType;
- }
-
- return samples[0]?.cursorType ?? 'arrow';
+ for (const sample of samples) {
+ if (!sample.cursorType) {
+ continue;
+ }
+
+ if (
+ sample.interactionType === "click" ||
+ sample.interactionType === "double-click" ||
+ sample.interactionType === "right-click" ||
+ sample.interactionType === "middle-click"
+ ) {
+ continue;
+ }
+
+ return sample.cursorType;
+ }
+
+ return samples[0]?.cursorType ?? "arrow";
}
function easeOutQuint(progress: number) {
- return 1 - ((1 - progress) ** 5);
+ return 1 - (1 - progress) ** 5;
}
function findLastMovingSampleTime(samples: CursorTelemetryPoint[]) {
- if (samples.length <= 1) {
- return samples[0]?.timeMs ?? 0;
- }
-
- for (let index = samples.length - 1; index > 0; index -= 1) {
- const curr = samples[index];
- const prev = samples[index - 1];
- const distance = Math.hypot(curr.cx - prev.cx, curr.cy - prev.cy);
- if (distance > CURSOR_TRAILING_MOVEMENT_EPSILON) {
- return curr.timeMs;
- }
- }
-
- return samples[samples.length - 1].timeMs;
+ if (samples.length <= 1) {
+ return samples[0]?.timeMs ?? 0;
+ }
+
+ for (let index = samples.length - 1; index > 0; index -= 1) {
+ const curr = samples[index];
+ const prev = samples[index - 1];
+ const distance = Math.hypot(curr.cx - prev.cx, curr.cy - prev.cy);
+ if (distance > CURSOR_TRAILING_MOVEMENT_EPSILON) {
+ return curr.timeMs;
+ }
+ }
+
+ return samples[samples.length - 1].timeMs;
}
function findLatestStableCursorType(samples: CursorTelemetryPoint[], timeMs: number) {
- let lo = 0;
- let hi = samples.length - 1;
- while (lo < hi) {
- const mid = Math.ceil((lo + hi) / 2);
- if (samples[mid].timeMs <= timeMs) {
- lo = mid;
- } else {
- hi = mid - 1;
- }
- }
-
- for (let index = lo; index >= 0; index -= 1) {
- const sample = samples[index];
- if (sample.timeMs > timeMs) {
- continue;
- }
-
- if (!sample.cursorType) {
- continue;
- }
-
- if (sample.interactionType === 'click'
- || sample.interactionType === 'double-click'
- || sample.interactionType === 'right-click'
- || sample.interactionType === 'middle-click') {
- continue;
- }
-
- return sample.cursorType;
- }
-
- return findLatestSample(samples, timeMs)?.cursorType ?? 'arrow';
+ let lo = 0;
+ let hi = samples.length - 1;
+ while (lo < hi) {
+ const mid = Math.ceil((lo + hi) / 2);
+ if (samples[mid].timeMs <= timeMs) {
+ lo = mid;
+ } else {
+ hi = mid - 1;
+ }
+ }
+
+ for (let index = lo; index >= 0; index -= 1) {
+ const sample = samples[index];
+ if (sample.timeMs > timeMs) {
+ continue;
+ }
+
+ if (!sample.cursorType) {
+ continue;
+ }
+
+ if (
+ sample.interactionType === "click" ||
+ sample.interactionType === "double-click" ||
+ sample.interactionType === "right-click" ||
+ sample.interactionType === "middle-click"
+ ) {
+ continue;
+ }
+
+ return sample.cursorType;
+ }
+
+ return findLatestSample(samples, timeMs)?.cursorType ?? "arrow";
}
function findLatestSample(samples: CursorTelemetryPoint[], timeMs: number) {
- if (samples.length === 0) return null;
-
- let lo = 0;
- let hi = samples.length - 1;
- while (lo < hi) {
- const mid = Math.ceil((lo + hi) / 2);
- if (samples[mid].timeMs <= timeMs) {
- lo = mid;
- } else {
- hi = mid - 1;
- }
- }
-
- return samples[lo]?.timeMs <= timeMs ? samples[lo] : null;
+ if (samples.length === 0) return null;
+
+ let lo = 0;
+ let hi = samples.length - 1;
+ while (lo < hi) {
+ const mid = Math.ceil((lo + hi) / 2);
+ if (samples[mid].timeMs <= timeMs) {
+ lo = mid;
+ } else {
+ hi = mid - 1;
+ }
+ }
+
+ return samples[lo]?.timeMs <= timeMs ? samples[lo] : null;
}
export function buildLoopedCursorTelemetry(
- samples: CursorTelemetryPoint[],
- totalDurationMs: number,
- timelineStartMs = 0,
+ samples: CursorTelemetryPoint[],
+ totalDurationMs: number,
+ timelineStartMs = 0,
): CursorTelemetryPoint[] {
- if (!samples || samples.length === 0) {
- return [];
- }
-
- const timelineEndMs = Math.max(0, Math.round(totalDurationMs));
- const timelineStartClampedMs = clamp(Math.round(timelineStartMs), 0, timelineEndMs);
- if (timelineEndMs <= timelineStartClampedMs) {
- return samples;
- }
-
- const boundedSamples = samples.filter(
- (sample) => sample.timeMs >= timelineStartClampedMs && sample.timeMs <= timelineEndMs,
- );
- if (boundedSamples.length === 0) {
- return samples;
- }
-
- const firstSample = boundedSamples[0];
- const lastSample = boundedSamples[boundedSamples.length - 1];
- const maxFreezeWindowMs = timelineEndMs - firstSample.timeMs;
- if (maxFreezeWindowMs <= 1) {
- return boundedSamples;
- }
-
- const freezeDurationMs = Math.min(LOOP_CURSOR_FREEZE_DURATION_MS, maxFreezeWindowMs);
- const motionEndMs = timelineEndMs - freezeDurationMs;
- const clampedSettleDurationMs = Math.min(LOOP_CURSOR_SETTLE_DURATION_MS, Math.max(0, freezeDurationMs - 1));
- const returnMotionDurationMs = Math.max(1, freezeDurationMs - clampedSettleDurationMs);
- const sourceStartMs = firstSample.timeMs;
- const sourceEndMs = Math.max(sourceStartMs, findLastMovingSampleTime(boundedSamples));
- const sourceDurationMs = Math.max(1, sourceEndMs - sourceStartMs);
- // Keep remapped times in source-time coordinates so cursor lookups
- // (which always use video.currentTime, i.e. source time) stay correct
- // even when the visible timeline window doesn't start at 0.
- const motionDurationMs = Math.max(1, motionEndMs - timelineStartClampedMs);
- const startingCursorType = findFirstStableCursorType(boundedSamples);
- const loopedSamples: CursorTelemetryPoint[] = [
- {
- ...firstSample,
- timeMs: timelineStartClampedMs,
- interactionType: undefined,
- cursorType: startingCursorType,
- },
- ];
-
- for (const sample of boundedSamples) {
- const progress = clamp((sample.timeMs - sourceStartMs) / sourceDurationMs, 0, 1);
- const mappedTimeMs = Math.round(timelineStartClampedMs + motionDurationMs * progress);
-
- if (mappedTimeMs <= loopedSamples[loopedSamples.length - 1].timeMs) {
- loopedSamples[loopedSamples.length - 1] = {
- ...sample,
- timeMs: loopedSamples[loopedSamples.length - 1].timeMs,
- };
- continue;
- }
-
- loopedSamples.push({
- ...sample,
- timeMs: mappedTimeMs,
- });
- }
-
- const returnStartPoint = interpolateCursorPosition(loopedSamples, motionEndMs)
- ?? { cx: lastSample.cx, cy: lastSample.cy };
- const returnStartCursorType = findLatestStableCursorType(loopedSamples, motionEndMs);
- const returnMotionStartMs = motionEndMs;
-
- for (let step = 0; step <= LOOP_CURSOR_RETURN_STEPS; step += 1) {
- const progress = step / LOOP_CURSOR_RETURN_STEPS;
- const easedProgress = easeOutQuint(progress);
- const timeMs = Math.round(returnMotionStartMs + returnMotionDurationMs * progress);
- loopedSamples.push({
- timeMs,
- cx: returnStartPoint.cx + (firstSample.cx - returnStartPoint.cx) * easedProgress,
- cy: returnStartPoint.cy + (firstSample.cy - returnStartPoint.cy) * easedProgress,
- interactionType: progress > 0 ? 'move' : undefined,
- cursorType: progress >= 1 ? startingCursorType : progress <= 0 ? returnStartCursorType : undefined,
- });
- }
-
- if (clampedSettleDurationMs > 0) {
- const settleSteps = Math.max(2, Math.round((clampedSettleDurationMs / LOOP_CURSOR_FREEZE_DURATION_MS) * LOOP_CURSOR_RETURN_STEPS));
- const settleStartMs = returnMotionStartMs + returnMotionDurationMs;
-
- for (let step = 1; step <= settleSteps; step += 1) {
- const progress = step / settleSteps;
- const timeMs = Math.round(settleStartMs + clampedSettleDurationMs * progress);
- loopedSamples.push({
- timeMs,
- cx: firstSample.cx,
- cy: firstSample.cy,
- interactionType: 'move',
- cursorType: startingCursorType,
- });
- }
- }
-
- return loopedSamples;
+ if (!samples || samples.length === 0) {
+ return [];
+ }
+
+ const timelineEndMs = Math.max(0, Math.round(totalDurationMs));
+ const timelineStartClampedMs = clamp(Math.round(timelineStartMs), 0, timelineEndMs);
+ if (timelineEndMs <= timelineStartClampedMs) {
+ return samples;
+ }
+
+ const boundedSamples = samples.filter(
+ (sample) => sample.timeMs >= timelineStartClampedMs && sample.timeMs <= timelineEndMs,
+ );
+ if (boundedSamples.length === 0) {
+ return samples;
+ }
+
+ const firstSample = boundedSamples[0];
+ const lastSample = boundedSamples[boundedSamples.length - 1];
+ const maxFreezeWindowMs = timelineEndMs - firstSample.timeMs;
+ if (maxFreezeWindowMs <= 1) {
+ return boundedSamples;
+ }
+
+ const freezeDurationMs = Math.min(LOOP_CURSOR_FREEZE_DURATION_MS, maxFreezeWindowMs);
+ const motionEndMs = timelineEndMs - freezeDurationMs;
+ const clampedSettleDurationMs = Math.min(
+ LOOP_CURSOR_SETTLE_DURATION_MS,
+ Math.max(0, freezeDurationMs - 1),
+ );
+ const returnMotionDurationMs = Math.max(1, freezeDurationMs - clampedSettleDurationMs);
+ const sourceStartMs = firstSample.timeMs;
+ const sourceEndMs = Math.max(sourceStartMs, findLastMovingSampleTime(boundedSamples));
+ const sourceDurationMs = Math.max(1, sourceEndMs - sourceStartMs);
+ // Keep remapped times in source-time coordinates so cursor lookups
+ // (which always use video.currentTime, i.e. source time) stay correct
+ // even when the visible timeline window doesn't start at 0.
+ const motionDurationMs = Math.max(1, motionEndMs - timelineStartClampedMs);
+ const startingCursorType = findFirstStableCursorType(boundedSamples);
+ const loopedSamples: CursorTelemetryPoint[] = [
+ {
+ ...firstSample,
+ timeMs: timelineStartClampedMs,
+ interactionType: undefined,
+ cursorType: startingCursorType,
+ },
+ ];
+
+ for (const sample of boundedSamples) {
+ const progress = clamp((sample.timeMs - sourceStartMs) / sourceDurationMs, 0, 1);
+ const mappedTimeMs = Math.round(timelineStartClampedMs + motionDurationMs * progress);
+
+ if (mappedTimeMs <= loopedSamples[loopedSamples.length - 1].timeMs) {
+ loopedSamples[loopedSamples.length - 1] = {
+ ...sample,
+ timeMs: loopedSamples[loopedSamples.length - 1].timeMs,
+ };
+ continue;
+ }
+
+ loopedSamples.push({
+ ...sample,
+ timeMs: mappedTimeMs,
+ });
+ }
+
+ const returnStartPoint = interpolateCursorPosition(loopedSamples, motionEndMs) ?? {
+ cx: lastSample.cx,
+ cy: lastSample.cy,
+ };
+ const returnStartCursorType = findLatestStableCursorType(loopedSamples, motionEndMs);
+ const returnMotionStartMs = motionEndMs;
+
+ for (let step = 0; step <= LOOP_CURSOR_RETURN_STEPS; step += 1) {
+ const progress = step / LOOP_CURSOR_RETURN_STEPS;
+ const easedProgress = easeOutQuint(progress);
+ const timeMs = Math.round(returnMotionStartMs + returnMotionDurationMs * progress);
+ loopedSamples.push({
+ timeMs,
+ cx: returnStartPoint.cx + (firstSample.cx - returnStartPoint.cx) * easedProgress,
+ cy: returnStartPoint.cy + (firstSample.cy - returnStartPoint.cy) * easedProgress,
+ interactionType: progress > 0 ? "move" : undefined,
+ cursorType:
+ progress >= 1
+ ? startingCursorType
+ : progress <= 0
+ ? returnStartCursorType
+ : undefined,
+ });
+ }
+
+ if (clampedSettleDurationMs > 0) {
+ const settleSteps = Math.max(
+ 2,
+ Math.round(
+ (clampedSettleDurationMs / LOOP_CURSOR_FREEZE_DURATION_MS) *
+ LOOP_CURSOR_RETURN_STEPS,
+ ),
+ );
+ const settleStartMs = returnMotionStartMs + returnMotionDurationMs;
+
+ for (let step = 1; step <= settleSteps; step += 1) {
+ const progress = step / settleSteps;
+ const timeMs = Math.round(settleStartMs + clampedSettleDurationMs * progress);
+ loopedSamples.push({
+ timeMs,
+ cx: firstSample.cx,
+ cy: firstSample.cy,
+ interactionType: "move",
+ cursorType: startingCursorType,
+ });
+ }
+ }
+
+ return loopedSamples;
}
export function getDisplayedTimelineEndMs(totalDurationMs: number, trimRegions: TrimRegion[]) {
- return getDisplayedTimelineWindowMs(totalDurationMs, trimRegions).endMs;
+ return getDisplayedTimelineWindowMs(totalDurationMs, trimRegions).endMs;
}
export function getDisplayedTimelineWindowMs(totalDurationMs: number, trimRegions: TrimRegion[]) {
- const durationMs = Math.max(0, Math.round(totalDurationMs));
- if (durationMs <= 0) {
- return { startMs: 0, endMs: 0 };
- }
-
- if (!trimRegions || trimRegions.length === 0) {
- return { startMs: 0, endMs: durationMs };
- }
-
- const sortedTrimRegions = trimRegions
- .map((region) => ({
- startMs: clamp(Math.round(region.startMs), 0, durationMs),
- endMs: clamp(Math.round(region.endMs), 0, durationMs),
- }))
- .filter((region) => region.endMs > region.startMs)
- .sort((a, b) => a.startMs - b.startMs);
-
- if (sortedTrimRegions.length === 0) {
- return { startMs: 0, endMs: durationMs };
- }
-
- let cursorMs = 0;
- let firstVisibleStartMs: number | null = null;
- let lastVisibleEndMs = 0;
-
- for (const trimRegion of sortedTrimRegions) {
- if (trimRegion.startMs > cursorMs) {
- if (firstVisibleStartMs === null) {
- firstVisibleStartMs = cursorMs;
- }
- lastVisibleEndMs = trimRegion.startMs;
- }
- cursorMs = Math.max(cursorMs, trimRegion.endMs);
- }
-
- if (cursorMs < durationMs) {
- if (firstVisibleStartMs === null) {
- firstVisibleStartMs = cursorMs;
- }
- lastVisibleEndMs = durationMs;
- }
-
- const clampedEndMs = clamp(lastVisibleEndMs, 0, durationMs);
- const clampedStartMs = clamp(firstVisibleStartMs ?? 0, 0, clampedEndMs);
- return { startMs: clampedStartMs, endMs: clampedEndMs };
-}
\ No newline at end of file
+ const durationMs = Math.max(0, Math.round(totalDurationMs));
+ if (durationMs <= 0) {
+ return { startMs: 0, endMs: 0 };
+ }
+
+ if (!trimRegions || trimRegions.length === 0) {
+ return { startMs: 0, endMs: durationMs };
+ }
+
+ const sortedTrimRegions = trimRegions
+ .map((region) => ({
+ startMs: clamp(Math.round(region.startMs), 0, durationMs),
+ endMs: clamp(Math.round(region.endMs), 0, durationMs),
+ }))
+ .filter((region) => region.endMs > region.startMs)
+ .sort((a, b) => a.startMs - b.startMs);
+
+ if (sortedTrimRegions.length === 0) {
+ return { startMs: 0, endMs: durationMs };
+ }
+
+ let cursorMs = 0;
+ let firstVisibleStartMs: number | null = null;
+ let lastVisibleEndMs = 0;
+
+ for (const trimRegion of sortedTrimRegions) {
+ if (trimRegion.startMs > cursorMs) {
+ if (firstVisibleStartMs === null) {
+ firstVisibleStartMs = cursorMs;
+ }
+ lastVisibleEndMs = trimRegion.startMs;
+ }
+ cursorMs = Math.max(cursorMs, trimRegion.endMs);
+ }
+
+ if (cursorMs < durationMs) {
+ if (firstVisibleStartMs === null) {
+ firstVisibleStartMs = cursorMs;
+ }
+ lastVisibleEndMs = durationMs;
+ }
+
+ const clampedEndMs = clamp(lastVisibleEndMs, 0, durationMs);
+ const clampedStartMs = clamp(firstVisibleStartMs ?? 0, 0, clampedEndMs);
+ return { startMs: clampedStartMs, endMs: clampedEndMs };
+}
diff --git a/src/components/video-editor/videoPlayback/cursorRenderer.ts b/src/components/video-editor/videoPlayback/cursorRenderer.ts
index 5c2e2626..5f84799c 100644
--- a/src/components/video-editor/videoPlayback/cursorRenderer.ts
+++ b/src/components/video-editor/videoPlayback/cursorRenderer.ts
@@ -530,8 +530,16 @@ export async function preloadCursorAssets() {
width = rasterized.width;
height = rasterized.height;
normalizedAnchor = {
- x: clamp((uploadedAsset.fallbackAnchor.x * trim.width) / width, 0, 1),
- y: clamp((uploadedAsset.fallbackAnchor.y * trim.height) / height, 0, 1),
+ x: clamp(
+ (uploadedAsset.fallbackAnchor.x * trim.width) / width,
+ 0,
+ 1,
+ ),
+ y: clamp(
+ (uploadedAsset.fallbackAnchor.y * trim.height) / height,
+ 0,
+ 1,
+ ),
};
} else {
finalUrl = assetUrl;
@@ -559,7 +567,10 @@ export async function preloadCursorAssets() {
} satisfies LoadedCursorAsset,
] as const;
} catch (error) {
- console.warn(`[CursorRenderer] Failed to load cursor image for: ${key}`, error);
+ console.warn(
+ `[CursorRenderer] Failed to load cursor image for: ${key}`,
+ error,
+ );
return null;
}
}),
@@ -570,7 +581,9 @@ export async function preloadCursorAssets() {
) as Partial<Record<CursorAssetKey, LoadedCursorAsset>>;
const invertedEntries = await Promise.all(
- (Object.entries(loadedCursorAssets) as Array<[CursorAssetKey, LoadedCursorAsset]>).map(
+ (
+ Object.entries(loadedCursorAssets) as Array<[CursorAssetKey, LoadedCursorAsset]>
+ ).map(
async ([key, asset]) => [key, await createInvertedCursorAsset(asset)] as const,
),
);
@@ -817,7 +830,8 @@ export class SmoothedCursorState {
this.trail.length = this.trailLength;
}
- const deltaMs = this.lastTimeMs === null ? 1000 / 60 : Math.max(1, timeMs - this.lastTimeMs);
+ const deltaMs =
+ this.lastTimeMs === null ? 1000 / 60 : Math.max(1, timeMs - this.lastTimeMs);
this.lastTimeMs = timeMs;
const springConfig = getCursorSpringConfig(this.smoothingFactor);
@@ -852,7 +866,6 @@ export class SmoothedCursorState {
}
}
-
export class PixiCursorOverlay {
public readonly container: Container;
private clickRingGraphics: Graphics;
@@ -1076,19 +1089,22 @@ export class PixiCursorOverlay {
this.clickRingGraphics.clear();
- const spriteKey = (cursorType in this.cursorSprites ? cursorType : "arrow") as CursorAssetKey;
+ const spriteKey = (
+ cursorType in this.cursorSprites ? cursorType : "arrow"
+ ) as CursorAssetKey;
if (isStatefulCursorStyle(this.config.style)) {
this.customCursorShadowSprite.visible = false;
this.customCursorSprite.visible = false;
const asset = getStatefulCursorAsset(this.config.style, spriteKey);
- const shadowSprite = this.cursorShadowSprites[spriteKey] ?? this.cursorShadowSprites.arrow!;
+ const shadowSprite =
+ this.cursorShadowSprites[spriteKey] ?? this.cursorShadowSprites.arrow!;
const sprite = this.cursorSprites[spriteKey] ?? this.cursorSprites.arrow!;
- for (const [key, currentShadowSprite] of Object.entries(this.cursorShadowSprites) as Array<
- [CursorAssetKey, Sprite]
- >) {
+ for (const [key, currentShadowSprite] of Object.entries(
+ this.cursorShadowSprites,
+ ) as Array<[CursorAssetKey, Sprite]>) {
currentShadowSprite.visible = key === spriteKey;
}
diff --git a/src/components/video-editor/videoPlayback/cursorSway.ts b/src/components/video-editor/videoPlayback/cursorSway.ts
index dcbeef41..4b3094d9 100644
--- a/src/components/video-editor/videoPlayback/cursorSway.ts
+++ b/src/components/video-editor/videoPlayback/cursorSway.ts
@@ -28,7 +28,11 @@ export function computeCursorSwayRotation(dx: number, dy: number, deltaMs: numbe
const directionalBias = clamp((dx + dy * CURSOR_SWAY_VERTICAL_WEIGHT) / distance, -1, 1);
return (
- directionalBias * speedFactor * CURSOR_SWAY_MAX_ROTATION * sway * CURSOR_SWAY_INTENSITY_SCALE
+ directionalBias *
+ speedFactor *
+ CURSOR_SWAY_MAX_ROTATION *
+ sway *
+ CURSOR_SWAY_INTENSITY_SCALE
);
}
diff --git a/src/components/video-editor/videoPlayback/cursorViewport.test.ts b/src/components/video-editor/videoPlayback/cursorViewport.test.ts
index a473a26e..89906272 100644
--- a/src/components/video-editor/videoPlayback/cursorViewport.test.ts
+++ b/src/components/video-editor/videoPlayback/cursorViewport.test.ts
@@ -3,37 +3,35 @@ import { describe, expect, it } from "vitest";
import { projectCursorPositionToViewport } from "./cursorViewport";
describe("projectCursorPositionToViewport", () => {
- it("leaves coordinates unchanged when no crop is active", () => {
- expect(
- projectCursorPositionToViewport({ cx: 0.25, cy: 0.75 }),
- ).toEqual({
- cx: 0.25,
- cy: 0.75,
- visible: true,
- });
- });
+ it("leaves coordinates unchanged when no crop is active", () => {
+ expect(projectCursorPositionToViewport({ cx: 0.25, cy: 0.75 })).toEqual({
+ cx: 0.25,
+ cy: 0.75,
+ visible: true,
+ });
+ });
- it("remaps source-space coordinates into crop-relative viewport space", () => {
- const projected = projectCursorPositionToViewport(
- { cx: 0.3, cy: 0.55 },
- { x: 0.2, y: 0.4, width: 0.5, height: 0.4 },
- );
+ it("remaps source-space coordinates into crop-relative viewport space", () => {
+ const projected = projectCursorPositionToViewport(
+ { cx: 0.3, cy: 0.55 },
+ { x: 0.2, y: 0.4, width: 0.5, height: 0.4 },
+ );
- expect(projected.visible).toBe(true);
- expect(projected.cx).toBeCloseTo(0.2, 6);
- expect(projected.cy).toBeCloseTo(0.375, 6);
- });
+ expect(projected.visible).toBe(true);
+ expect(projected.cx).toBeCloseTo(0.2, 6);
+ expect(projected.cy).toBeCloseTo(0.375, 6);
+ });
- it("marks cursor invisible when it falls outside the cropped source region", () => {
- expect(
- projectCursorPositionToViewport(
- { cx: 0.1, cy: 0.5 },
- { x: 0.2, y: 0.25, width: 0.5, height: 0.5 },
- ),
- ).toEqual({
- cx: -0.2,
- cy: 0.5,
- visible: false,
- });
- });
-});
\ No newline at end of file
+ it("marks cursor invisible when it falls outside the cropped source region", () => {
+ expect(
+ projectCursorPositionToViewport(
+ { cx: 0.1, cy: 0.5 },
+ { x: 0.2, y: 0.25, width: 0.5, height: 0.5 },
+ ),
+ ).toEqual({
+ cx: -0.2,
+ cy: 0.5,
+ visible: false,
+ });
+ });
+});
diff --git a/src/components/video-editor/videoPlayback/cursorViewport.ts b/src/components/video-editor/videoPlayback/cursorViewport.ts
index 8a3def64..96abfc07 100644
--- a/src/components/video-editor/videoPlayback/cursorViewport.ts
+++ b/src/components/video-editor/videoPlayback/cursorViewport.ts
@@ -3,53 +3,53 @@ import type { CropRegion } from "../types";
const CURSOR_VIEWPORT_EPSILON = 0.000001;
export interface CursorViewportRect {
- x: number;
- y: number;
- width: number;
- height: number;
- sourceCrop?: CropRegion;
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ sourceCrop?: CropRegion;
}
export interface ProjectedCursorPosition {
- cx: number;
- cy: number;
- visible: boolean;
+ cx: number;
+ cy: number;
+ visible: boolean;
}
export function projectCursorPositionToViewport(
- position: { cx: number; cy: number },
- sourceCrop?: CropRegion,
+ position: { cx: number; cy: number },
+ sourceCrop?: CropRegion,
): ProjectedCursorPosition {
- if (!sourceCrop) {
- return {
- cx: position.cx,
- cy: position.cy,
- visible: true,
- };
- }
-
- const cropWidth = sourceCrop.width;
- const cropHeight = sourceCrop.height;
-
- if (cropWidth <= 0 || cropHeight <= 0) {
- return {
- cx: position.cx,
- cy: position.cy,
- visible: false,
- };
- }
-
- const projectedX = (position.cx - sourceCrop.x) / cropWidth;
- const projectedY = (position.cy - sourceCrop.y) / cropHeight;
- const visible =
- projectedX >= -CURSOR_VIEWPORT_EPSILON &&
- projectedX <= 1 + CURSOR_VIEWPORT_EPSILON &&
- projectedY >= -CURSOR_VIEWPORT_EPSILON &&
- projectedY <= 1 + CURSOR_VIEWPORT_EPSILON;
-
- return {
- cx: projectedX,
- cy: projectedY,
- visible,
- };
-}
\ No newline at end of file
+ if (!sourceCrop) {
+ return {
+ cx: position.cx,
+ cy: position.cy,
+ visible: true,
+ };
+ }
+
+ const cropWidth = sourceCrop.width;
+ const cropHeight = sourceCrop.height;
+
+ if (cropWidth <= 0 || cropHeight <= 0) {
+ return {
+ cx: position.cx,
+ cy: position.cy,
+ visible: false,
+ };
+ }
+
+ const projectedX = (position.cx - sourceCrop.x) / cropWidth;
+ const projectedY = (position.cy - sourceCrop.y) / cropHeight;
+ const visible =
+ projectedX >= -CURSOR_VIEWPORT_EPSILON &&
+ projectedX <= 1 + CURSOR_VIEWPORT_EPSILON &&
+ projectedY >= -CURSOR_VIEWPORT_EPSILON &&
+ projectedY <= 1 + CURSOR_VIEWPORT_EPSILON;
+
+ return {
+ cx: projectedX,
+ cy: projectedY,
+ visible,
+ };
+}
diff --git a/src/components/video-editor/videoPlayback/focusUtils.ts b/src/components/video-editor/videoPlayback/focusUtils.ts
index c6e6af09..27f408c5 100644
--- a/src/components/video-editor/videoPlayback/focusUtils.ts
+++ b/src/components/video-editor/videoPlayback/focusUtils.ts
@@ -1,104 +1,98 @@
-import { ZOOM_DEPTH_SCALES, clampFocusToDepth, type ZoomFocus, type ZoomDepth } from "../types";
+import { clampFocusToDepth, ZOOM_DEPTH_SCALES, type ZoomDepth, type ZoomFocus } from "../types";
interface StageSize {
- width: number;
- height: number;
+ width: number;
+ height: number;
}
function clamp(value: number, min: number, max: number) {
- return Math.max(min, Math.min(max, value));
+ return Math.max(min, Math.min(max, value));
}
function easeIntoBoundary(normalized: number) {
- const t = clamp(normalized, 0, 1);
- return -t * t * t + 2 * t * t;
+ const t = clamp(normalized, 0, 1);
+ return -t * t * t + 2 * t * t;
}
function softClampToRange(value: number, min: number, max: number, softness: number) {
- const clamped = clamp(value, min, max);
+ const clamped = clamp(value, min, max);
- if (softness <= 0 || max <= min) {
- return clamped;
- }
+ if (softness <= 0 || max <= min) {
+ return clamped;
+ }
- if (clamped < min + softness) {
- const normalized = (clamped - min) / softness;
- return min + softness * easeIntoBoundary(normalized);
- }
+ if (clamped < min + softness) {
+ const normalized = (clamped - min) / softness;
+ return min + softness * easeIntoBoundary(normalized);
+ }
- if (clamped > max - softness) {
- const normalized = (max - clamped) / softness;
- return max - softness * easeIntoBoundary(normalized);
- }
+ if (clamped > max - softness) {
+ const normalized = (max - clamped) / softness;
+ return max - softness * easeIntoBoundary(normalized);
+ }
- return clamped;
+ return clamped;
}
function getFocusBounds(depth: ZoomDepth) {
- const zoomScale = ZOOM_DEPTH_SCALES[depth];
- return getFocusBoundsForScale(zoomScale);
+ const zoomScale = ZOOM_DEPTH_SCALES[depth];
+ return getFocusBoundsForScale(zoomScale);
}
function getFocusBoundsForScale(zoomScale: number) {
- const marginX = 1 / (2 * zoomScale);
- const marginY = 1 / (2 * zoomScale);
-
- return {
- minX: marginX,
- maxX: 1 - marginX,
- minY: marginY,
- maxY: 1 - marginY,
- };
+ const marginX = 1 / (2 * zoomScale);
+ const marginY = 1 / (2 * zoomScale);
+
+ return {
+ minX: marginX,
+ maxX: 1 - marginX,
+ minY: marginY,
+ maxY: 1 - marginY,
+ };
}
export function clampFocusToStage(
- focus: ZoomFocus,
- depth: ZoomDepth,
- _stageSize: StageSize
+ focus: ZoomFocus,
+ depth: ZoomDepth,
+ _stageSize: StageSize,
): ZoomFocus {
- const baseFocus = clampFocusToDepth(focus, depth);
- const bounds = getFocusBounds(depth);
+ const baseFocus = clampFocusToDepth(focus, depth);
+ const bounds = getFocusBounds(depth);
- return {
- cx: clamp(baseFocus.cx, bounds.minX, bounds.maxX),
- cy: clamp(baseFocus.cy, bounds.minY, bounds.maxY),
- };
+ return {
+ cx: clamp(baseFocus.cx, bounds.minX, bounds.maxX),
+ cy: clamp(baseFocus.cy, bounds.minY, bounds.maxY),
+ };
}
-export function clampFocusToScale(
- focus: ZoomFocus,
- zoomScale: number,
-): ZoomFocus {
- const baseFocus = {
- cx: clamp(focus.cx, 0, 1),
- cy: clamp(focus.cy, 0, 1),
- };
- const bounds = getFocusBoundsForScale(zoomScale);
-
- return {
- cx: clamp(baseFocus.cx, bounds.minX, bounds.maxX),
- cy: clamp(baseFocus.cy, bounds.minY, bounds.maxY),
- };
+export function clampFocusToScale(focus: ZoomFocus, zoomScale: number): ZoomFocus {
+ const baseFocus = {
+ cx: clamp(focus.cx, 0, 1),
+ cy: clamp(focus.cy, 0, 1),
+ };
+ const bounds = getFocusBoundsForScale(zoomScale);
+
+ return {
+ cx: clamp(baseFocus.cx, bounds.minX, bounds.maxX),
+ cy: clamp(baseFocus.cy, bounds.minY, bounds.maxY),
+ };
}
-export function softenFocusToScale(
- focus: ZoomFocus,
- zoomScale: number
-): ZoomFocus {
- const baseFocus = {
- cx: clamp(focus.cx, 0, 1),
- cy: clamp(focus.cy, 0, 1),
- };
- const bounds = getFocusBoundsForScale(zoomScale);
- const horizontalRange = bounds.maxX - bounds.minX;
- const verticalRange = bounds.maxY - bounds.minY;
- const horizontalSoftness = Math.min(0.12, horizontalRange * 0.35);
- const verticalSoftness = Math.min(0.12, verticalRange * 0.35);
-
- return {
- cx: softClampToRange(baseFocus.cx, bounds.minX, bounds.maxX, horizontalSoftness),
- cy: softClampToRange(baseFocus.cy, bounds.minY, bounds.maxY, verticalSoftness),
- };
+export function softenFocusToScale(focus: ZoomFocus, zoomScale: number): ZoomFocus {
+ const baseFocus = {
+ cx: clamp(focus.cx, 0, 1),
+ cy: clamp(focus.cy, 0, 1),
+ };
+ const bounds = getFocusBoundsForScale(zoomScale);
+ const horizontalRange = bounds.maxX - bounds.minX;
+ const verticalRange = bounds.maxY - bounds.minY;
+ const horizontalSoftness = Math.min(0.12, horizontalRange * 0.35);
+ const verticalSoftness = Math.min(0.12, verticalRange * 0.35);
+
+ return {
+ cx: softClampToRange(baseFocus.cx, bounds.minX, bounds.maxX, horizontalSoftness),
+ cy: softClampToRange(baseFocus.cy, bounds.minY, bounds.maxY, verticalSoftness),
+ };
}
/**
@@ -119,50 +113,63 @@ export function softenFocusToScale(
* @param snapToEdgesRatio 0.25 for manual zooms, 0.5 for system/auto zooms
*/
export function edgeSnapFocus(
- cursorFocus: ZoomFocus,
- zoomScale: number,
- snapToEdgesRatio: number,
+ cursorFocus: ZoomFocus,
+ zoomScale: number,
+ snapToEdgesRatio: number,
): ZoomFocus {
- const bounds = getFocusBoundsForScale(zoomScale);
-
- const snappedX = clampedInterpolate(cursorFocus.cx, snapToEdgesRatio, 1 - snapToEdgesRatio + 0.0001);
- const snappedY = clampedInterpolate(cursorFocus.cy, snapToEdgesRatio, 1 - snapToEdgesRatio + 0.0001);
-
- return {
- cx: bounds.minX + snappedX * (bounds.maxX - bounds.minX),
- cy: bounds.minY + snappedY * (bounds.maxY - bounds.minY),
- };
+ const bounds = getFocusBoundsForScale(zoomScale);
+
+ const snappedX = clampedInterpolate(
+ cursorFocus.cx,
+ snapToEdgesRatio,
+ 1 - snapToEdgesRatio + 0.0001,
+ );
+ const snappedY = clampedInterpolate(
+ cursorFocus.cy,
+ snapToEdgesRatio,
+ 1 - snapToEdgesRatio + 0.0001,
+ );
+
+ return {
+ cx: bounds.minX + snappedX * (bounds.maxX - bounds.minX),
+ cy: bounds.minY + snappedY * (bounds.maxY - bounds.minY),
+ };
}
/**
* Clamped linear interpolation: maps `value` from [inMin, inMax] → [0, 1].
*/
function clampedInterpolate(value: number, inMin: number, inMax: number): number {
- if (inMax <= inMin) return 0;
- const t = (value - inMin) / (inMax - inMin);
- return clamp(t, 0, 1);
+ if (inMax <= inMin) return 0;
+ const t = (value - inMin) / (inMax - inMin);
+ return clamp(t, 0, 1);
}
export function stageFocusToVideoSpace(
- focus: ZoomFocus,
- stageSize: StageSize,
- videoSize: { width: number; height: number },
- baseScale: number,
- baseOffset: { x: number; y: number }
+ focus: ZoomFocus,
+ stageSize: StageSize,
+ videoSize: { width: number; height: number },
+ baseScale: number,
+ baseOffset: { x: number; y: number },
): ZoomFocus {
- if (!stageSize.width || !stageSize.height || !videoSize.width || !videoSize.height || baseScale <= 0) {
- return focus;
- }
-
- const stageX = focus.cx * stageSize.width;
- const stageY = focus.cy * stageSize.height;
-
- const videoNormX = (stageX - baseOffset.x) / (videoSize.width * baseScale);
- const videoNormY = (stageY - baseOffset.y) / (videoSize.height * baseScale);
-
- return {
- cx: videoNormX,
- cy: videoNormY,
- };
+ if (
+ !stageSize.width ||
+ !stageSize.height ||
+ !videoSize.width ||
+ !videoSize.height ||
+ baseScale <= 0
+ ) {
+ return focus;
+ }
+
+ const stageX = focus.cx * stageSize.width;
+ const stageY = focus.cy * stageSize.height;
+
+ const videoNormX = (stageX - baseOffset.x) / (videoSize.width * baseScale);
+ const videoNormY = (stageY - baseOffset.y) / (videoSize.height * baseScale);
+
+ return {
+ cx: videoNormX,
+ cy: videoNormY,
+ };
}
-
diff --git a/src/components/video-editor/videoPlayback/index.ts b/src/components/video-editor/videoPlayback/index.ts
index 51263f26..06cf42ed 100644
--- a/src/components/video-editor/videoPlayback/index.ts
+++ b/src/components/video-editor/videoPlayback/index.ts
@@ -1,9 +1,8 @@
-export * from './constants';
-export * from './mathUtils';
-export * from './zoomRegionUtils';
-export * from './focusUtils';
-export * from './overlayUtils';
-export * from './layoutUtils';
-export * from './zoomTransform';
-export * from './videoEventHandlers';
-
+export * from "./constants";
+export * from "./focusUtils";
+export * from "./layoutUtils";
+export * from "./mathUtils";
+export * from "./overlayUtils";
+export * from "./videoEventHandlers";
+export * from "./zoomRegionUtils";
+export * from "./zoomTransform";
diff --git a/src/components/video-editor/videoPlayback/layoutUtils.ts b/src/components/video-editor/videoPlayback/layoutUtils.ts
index a80b8645..8c028e36 100644
--- a/src/components/video-editor/videoPlayback/layoutUtils.ts
+++ b/src/components/video-editor/videoPlayback/layoutUtils.ts
@@ -1,154 +1,161 @@
-import { Application, Sprite, Graphics } from 'pixi.js';
-import type { CropRegion } from '../types';
-import { drawSquircleOnGraphics } from '@/lib/geometry/squircle';
+import { Application, Graphics, Sprite } from "pixi.js";
+import { drawSquircleOnGraphics } from "@/lib/geometry/squircle";
+import type { CropRegion } from "../types";
interface LayoutParams {
- container: HTMLDivElement;
- app: Application;
- videoSprite: Sprite;
- maskGraphics: Graphics;
- videoElement: HTMLVideoElement;
- cropRegion?: CropRegion;
- lockedVideoDimensions?: { width: number; height: number } | null;
- borderRadius?: number;
- padding?: number;
- /** Screen insets from the active device frame, used to scale/center the full frame */
- frameInsets?: { top: number; right: number; bottom: number; left: number } | null;
+ container: HTMLDivElement;
+ app: Application;
+ videoSprite: Sprite;
+ maskGraphics: Graphics;
+ videoElement: HTMLVideoElement;
+ cropRegion?: CropRegion;
+ lockedVideoDimensions?: { width: number; height: number } | null;
+ borderRadius?: number;
+ padding?: number;
+ /** Screen insets from the active device frame, used to scale/center the full frame */
+ frameInsets?: { top: number; right: number; bottom: number; left: number } | null;
}
interface LayoutResult {
- stageSize: { width: number; height: number };
- videoSize: { width: number; height: number };
- baseScale: number;
- baseOffset: { x: number; y: number };
- maskRect: {
- x: number;
- y: number;
- width: number;
- height: number;
- sourceCrop?: CropRegion;
- };
- cropBounds: { startX: number; endX: number; startY: number; endY: number };
+ stageSize: { width: number; height: number };
+ videoSize: { width: number; height: number };
+ baseScale: number;
+ baseOffset: { x: number; y: number };
+ maskRect: {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+ sourceCrop?: CropRegion;
+ };
+ cropBounds: { startX: number; endX: number; startY: number; endY: number };
}
export function layoutVideoContent(params: LayoutParams): LayoutResult | null {
- const { container, app, videoSprite, maskGraphics, videoElement, cropRegion, lockedVideoDimensions, borderRadius = 0, padding = 0, frameInsets } = params;
-
- const videoWidth = lockedVideoDimensions?.width || videoElement.videoWidth;
- const videoHeight = lockedVideoDimensions?.height || videoElement.videoHeight;
-
- if (!videoWidth || !videoHeight) {
- return null;
- }
-
- const width = container.clientWidth;
- const height = container.clientHeight;
-
- if (!width || !height) {
- return null;
- }
-
- app.renderer.resize(width, height);
- app.canvas.style.width = '100%';
- app.canvas.style.height = '100%';
-
- // Apply crop region
- const crop = cropRegion || { x: 0, y: 0, width: 1, height: 1 };
-
- // Calculate the cropped dimensions
- const croppedVideoWidth = videoWidth * crop.width;
- const croppedVideoHeight = videoHeight * crop.height;
-
- const cropStartX = crop.x * videoWidth;
- const cropStartY = crop.y * videoHeight;
- const cropEndX = cropStartX + croppedVideoWidth;
- const cropEndY = cropStartY + croppedVideoHeight;
-
- // Calculate scale to fit the cropped area in the viewport
- // Padding is a percentage (0-100), where 50 matches the original VIEWPORT_SCALE of 0.8
- const paddingScale = 1.0 - (padding / 100) * 0.4;
- const maxDisplayWidth = width * paddingScale;
- const maxDisplayHeight = height * paddingScale;
-
- // When a device frame is active, the frame extends beyond the video area.
- // We need to scale so the ENTIRE frame (video + bezels) fits in the viewport,
- // then center the full frame, not just the video content.
- const insets = frameInsets;
- // Fraction of the full frame occupied by the screen area
- const screenFracW = insets ? (1 - insets.left - insets.right) : 1;
- const screenFracH = insets ? (1 - insets.top - insets.bottom) : 1;
- // Full frame dimensions in video pixels (the frame image is this large relative to the screen)
- const fullFrameVideoW = croppedVideoWidth / screenFracW;
- const fullFrameVideoH = croppedVideoHeight / screenFracH;
-
- const scale = Math.min(
- maxDisplayWidth / fullFrameVideoW,
- maxDisplayHeight / fullFrameVideoH,
- );
-
- videoSprite.scale.set(scale);
-
- // Calculate display size of the full video at this scale
- const fullVideoDisplayWidth = videoWidth * scale;
- const fullVideoDisplayHeight = videoHeight * scale;
-
- // Calculate display size of just the cropped region
- const croppedDisplayWidth = croppedVideoWidth * scale;
- const croppedDisplayHeight = croppedVideoHeight * scale;
-
- // Center the full frame (or just the video if no frame) in the container
- // Full frame display dimensions
- const fullFrameDisplayW = fullFrameVideoW * scale;
- const fullFrameDisplayH = fullFrameVideoH * scale;
- // The full frame's top-left, centered in the viewport
- const frameCenterX = (width - fullFrameDisplayW) / 2;
- const frameCenterY = (height - fullFrameDisplayH) / 2;
- // The screen area starts at frameCenterX + insets.left * fullFrameDisplayW
- const centerOffsetX = insets
- ? frameCenterX + insets.left * fullFrameDisplayW
- : (width - croppedDisplayWidth) / 2;
- const centerOffsetY = insets
- ? frameCenterY + insets.top * fullFrameDisplayH
- : (height - croppedDisplayHeight) / 2;
-
- // Position the full video sprite so that when we apply the mask,
- // the cropped region appears centered
- // The crop starts at (crop.x * videoWidth, crop.y * videoHeight) in video coordinates
- // In display coordinates, that's (crop.x * fullVideoDisplayWidth, crop.y * fullVideoDisplayHeight)
- // We want that point to be at centerOffsetX, centerOffsetY
- const spriteX = centerOffsetX - (crop.x * fullVideoDisplayWidth);
- const spriteY = centerOffsetY - (crop.y * fullVideoDisplayHeight);
-
- videoSprite.position.set(spriteX, spriteY);
-
- // Create a mask that only shows the cropped region (centered in container)
- const maskX = centerOffsetX;
- const maskY = centerOffsetY;
-
- // Apply border radius
- maskGraphics.clear();
- drawSquircleOnGraphics(maskGraphics, {
- x: maskX,
- y: maskY,
- width: croppedDisplayWidth,
- height: croppedDisplayHeight,
- radius: borderRadius,
- });
- maskGraphics.fill({ color: 0xffffff });
-
- return {
- stageSize: { width, height },
- videoSize: { width: croppedVideoWidth, height: croppedVideoHeight },
- baseScale: scale,
- baseOffset: { x: spriteX, y: spriteY },
- maskRect: {
- x: maskX,
- y: maskY,
- width: croppedDisplayWidth,
- height: croppedDisplayHeight,
- sourceCrop: crop,
- },
- cropBounds: { startX: cropStartX, endX: cropEndX, startY: cropStartY, endY: cropEndY },
- };
+ const {
+ container,
+ app,
+ videoSprite,
+ maskGraphics,
+ videoElement,
+ cropRegion,
+ lockedVideoDimensions,
+ borderRadius = 0,
+ padding = 0,
+ frameInsets,
+ } = params;
+
+ const videoWidth = lockedVideoDimensions?.width || videoElement.videoWidth;
+ const videoHeight = lockedVideoDimensions?.height || videoElement.videoHeight;
+
+ if (!videoWidth || !videoHeight) {
+ return null;
+ }
+
+ const width = container.clientWidth;
+ const height = container.clientHeight;
+
+ if (!width || !height) {
+ return null;
+ }
+
+ app.renderer.resize(width, height);
+ app.canvas.style.width = "100%";
+ app.canvas.style.height = "100%";
+
+ // Apply crop region
+ const crop = cropRegion || { x: 0, y: 0, width: 1, height: 1 };
+
+ // Calculate the cropped dimensions
+ const croppedVideoWidth = videoWidth * crop.width;
+ const croppedVideoHeight = videoHeight * crop.height;
+
+ const cropStartX = crop.x * videoWidth;
+ const cropStartY = crop.y * videoHeight;
+ const cropEndX = cropStartX + croppedVideoWidth;
+ const cropEndY = cropStartY + croppedVideoHeight;
+
+ // Calculate scale to fit the cropped area in the viewport
+ // Padding is a percentage (0-100), where 50 matches the original VIEWPORT_SCALE of 0.8
+ const paddingScale = 1.0 - (padding / 100) * 0.4;
+ const maxDisplayWidth = width * paddingScale;
+ const maxDisplayHeight = height * paddingScale;
+
+ // When a device frame is active, the frame extends beyond the video area.
+ // We need to scale so the ENTIRE frame (video + bezels) fits in the viewport,
+ // then center the full frame, not just the video content.
+ const insets = frameInsets;
+ // Fraction of the full frame occupied by the screen area
+ const screenFracW = insets ? 1 - insets.left - insets.right : 1;
+ const screenFracH = insets ? 1 - insets.top - insets.bottom : 1;
+ // Full frame dimensions in video pixels (the frame image is this large relative to the screen)
+ const fullFrameVideoW = croppedVideoWidth / screenFracW;
+ const fullFrameVideoH = croppedVideoHeight / screenFracH;
+
+ const scale = Math.min(maxDisplayWidth / fullFrameVideoW, maxDisplayHeight / fullFrameVideoH);
+
+ videoSprite.scale.set(scale);
+
+ // Calculate display size of the full video at this scale
+ const fullVideoDisplayWidth = videoWidth * scale;
+ const fullVideoDisplayHeight = videoHeight * scale;
+
+ // Calculate display size of just the cropped region
+ const croppedDisplayWidth = croppedVideoWidth * scale;
+ const croppedDisplayHeight = croppedVideoHeight * scale;
+
+ // Center the full frame (or just the video if no frame) in the container
+ // Full frame display dimensions
+ const fullFrameDisplayW = fullFrameVideoW * scale;
+ const fullFrameDisplayH = fullFrameVideoH * scale;
+ // The full frame's top-left, centered in the viewport
+ const frameCenterX = (width - fullFrameDisplayW) / 2;
+ const frameCenterY = (height - fullFrameDisplayH) / 2;
+ // The screen area starts at frameCenterX + insets.left * fullFrameDisplayW
+ const centerOffsetX = insets
+ ? frameCenterX + insets.left * fullFrameDisplayW
+ : (width - croppedDisplayWidth) / 2;
+ const centerOffsetY = insets
+ ? frameCenterY + insets.top * fullFrameDisplayH
+ : (height - croppedDisplayHeight) / 2;
+
+ // Position the full video sprite so that when we apply the mask,
+ // the cropped region appears centered
+ // The crop starts at (crop.x * videoWidth, crop.y * videoHeight) in video coordinates
+ // In display coordinates, that's (crop.x * fullVideoDisplayWidth, crop.y * fullVideoDisplayHeight)
+ // We want that point to be at centerOffsetX, centerOffsetY
+ const spriteX = centerOffsetX - crop.x * fullVideoDisplayWidth;
+ const spriteY = centerOffsetY - crop.y * fullVideoDisplayHeight;
+
+ videoSprite.position.set(spriteX, spriteY);
+
+ // Create a mask that only shows the cropped region (centered in container)
+ const maskX = centerOffsetX;
+ const maskY = centerOffsetY;
+
+ // Apply border radius
+ maskGraphics.clear();
+ drawSquircleOnGraphics(maskGraphics, {
+ x: maskX,
+ y: maskY,
+ width: croppedDisplayWidth,
+ height: croppedDisplayHeight,
+ radius: borderRadius,
+ });
+ maskGraphics.fill({ color: 0xffffff });
+
+ return {
+ stageSize: { width, height },
+ videoSize: { width: croppedVideoWidth, height: croppedVideoHeight },
+ baseScale: scale,
+ baseOffset: { x: spriteX, y: spriteY },
+ maskRect: {
+ x: maskX,
+ y: maskY,
+ width: croppedDisplayWidth,
+ height: croppedDisplayHeight,
+ sourceCrop: crop,
+ },
+ cropBounds: { startX: cropStartX, endX: cropEndX, startY: cropStartY, endY: cropEndY },
+ };
}
-
diff --git a/src/components/video-editor/videoPlayback/mathUtils.ts b/src/components/video-editor/videoPlayback/mathUtils.ts
index 58559200..dca5629d 100644
--- a/src/components/video-editor/videoPlayback/mathUtils.ts
+++ b/src/components/video-editor/videoPlayback/mathUtils.ts
@@ -1,74 +1,70 @@
export function clamp01(value: number) {
- return Math.max(0, Math.min(1, value));
+ return Math.max(0, Math.min(1, value));
}
function sampleCubicBezier(a1: number, a2: number, t: number) {
- const oneMinusT = 1 - t;
- return 3 * a1 * oneMinusT * oneMinusT * t
- + 3 * a2 * oneMinusT * t * t
- + t * t * t;
+ const oneMinusT = 1 - t;
+ return 3 * a1 * oneMinusT * oneMinusT * t + 3 * a2 * oneMinusT * t * t + t * t * t;
}
function sampleCubicBezierDerivative(a1: number, a2: number, t: number) {
- const oneMinusT = 1 - t;
- return 3 * a1 * oneMinusT * oneMinusT
- + 6 * (a2 - a1) * oneMinusT * t
- + 3 * (1 - a2) * t * t;
+ const oneMinusT = 1 - t;
+ return 3 * a1 * oneMinusT * oneMinusT + 6 * (a2 - a1) * oneMinusT * t + 3 * (1 - a2) * t * t;
}
export function cubicBezier(x1: number, y1: number, x2: number, y2: number, t: number) {
- const targetX = clamp01(t);
- let solvedT = targetX;
+ const targetX = clamp01(t);
+ let solvedT = targetX;
- for (let i = 0; i < 8; i += 1) {
- const currentX = sampleCubicBezier(x1, x2, solvedT) - targetX;
- const currentDerivative = sampleCubicBezierDerivative(x1, x2, solvedT);
+ for (let i = 0; i < 8; i += 1) {
+ const currentX = sampleCubicBezier(x1, x2, solvedT) - targetX;
+ const currentDerivative = sampleCubicBezierDerivative(x1, x2, solvedT);
- if (Math.abs(currentX) < 1e-6 || Math.abs(currentDerivative) < 1e-6) {
- break;
- }
+ if (Math.abs(currentX) < 1e-6 || Math.abs(currentDerivative) < 1e-6) {
+ break;
+ }
- solvedT -= currentX / currentDerivative;
- }
+ solvedT -= currentX / currentDerivative;
+ }
- let lower = 0;
- let upper = 1;
- solvedT = clamp01(solvedT);
+ let lower = 0;
+ let upper = 1;
+ solvedT = clamp01(solvedT);
- for (let i = 0; i < 10; i += 1) {
- const currentX = sampleCubicBezier(x1, x2, solvedT);
- if (Math.abs(currentX - targetX) < 1e-6) {
- break;
- }
+ for (let i = 0; i < 10; i += 1) {
+ const currentX = sampleCubicBezier(x1, x2, solvedT);
+ if (Math.abs(currentX - targetX) < 1e-6) {
+ break;
+ }
- if (currentX < targetX) {
- lower = solvedT;
- } else {
- upper = solvedT;
- }
+ if (currentX < targetX) {
+ lower = solvedT;
+ } else {
+ upper = solvedT;
+ }
- solvedT = (lower + upper) / 2;
- }
+ solvedT = (lower + upper) / 2;
+ }
- return sampleCubicBezier(y1, y2, solvedT);
+ return sampleCubicBezier(y1, y2, solvedT);
}
export function easeOutExpo(t: number) {
- const clamped = clamp01(t);
- if (clamped === 1) {
- return 1;
- }
+ const clamped = clamp01(t);
+ if (clamped === 1) {
+ return 1;
+ }
- return 1 - Math.pow(2, -7 * clamped);
+ return 1 - Math.pow(2, -7 * clamped);
}
export function easeOutZoom(t: number) {
- return cubicBezier(0.16, 1, 0.3, 1, t);
+ return cubicBezier(0.16, 1, 0.3, 1, t);
}
export function smoothStep(t: number) {
- const clamped = clamp01(t);
- return clamped * clamped * (3 - 2 * clamped);
+ const clamped = clamp01(t);
+ return clamped * clamped * (3 - 2 * clamped);
}
/**
@@ -76,10 +72,8 @@ export function smoothStep(t: number) {
* Used for zoom-in transitions.
*/
export function easeInOutCubic(t: number) {
- const x = clamp01(t);
- return x < 0.5
- ? 4 * x * x * x
- : 1 - Math.pow(-2 * x + 2, 3) / 2;
+ const x = clamp01(t);
+ return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;
}
/**
@@ -87,7 +81,6 @@ export function easeInOutCubic(t: number) {
* Used for zoom-out transitions so strength eases smoothly to zero.
*/
export function easeOutCubic(t: number) {
- const x = clamp01(t);
- return 1 - Math.pow(1 - x, 3);
+ const x = clamp01(t);
+ return 1 - Math.pow(1 - x, 3);
}
-
diff --git a/src/components/video-editor/videoPlayback/motionSmoothing.ts b/src/components/video-editor/videoPlayback/motionSmoothing.ts
index 3d78286b..4c15fd58 100644
--- a/src/components/video-editor/videoPlayback/motionSmoothing.ts
+++ b/src/components/video-editor/videoPlayback/motionSmoothing.ts
@@ -2,17 +2,17 @@
// Please use this code with the right attribution.
export interface SpringState {
- value: number;
- velocity: number;
- initialized: boolean;
+ value: number;
+ velocity: number;
+ initialized: boolean;
}
export interface SpringConfig {
- stiffness: number;
- damping: number;
- mass: number;
- restDelta?: number;
- restSpeed?: number;
+ stiffness: number;
+ damping: number;
+ mass: number;
+ restDelta?: number;
+ restSpeed?: number;
}
const CURSOR_SMOOTHING_MIN = 0;
@@ -20,28 +20,28 @@ const CURSOR_SMOOTHING_MAX = 2;
const CURSOR_SMOOTHING_LEGACY_MAX = 0.5;
export function createSpringState(initialValue = 0): SpringState {
- return {
- value: initialValue,
- velocity: 0,
- initialized: false,
- };
+ return {
+ value: initialValue,
+ velocity: 0,
+ initialized: false,
+ };
}
export function resetSpringState(state: SpringState, initialValue?: number) {
- if (typeof initialValue === 'number') {
- state.value = initialValue;
- }
+ if (typeof initialValue === "number") {
+ state.value = initialValue;
+ }
- state.velocity = 0;
- state.initialized = false;
+ state.velocity = 0;
+ state.initialized = false;
}
export function clampDeltaMs(deltaMs: number, fallbackMs = 1000 / 60) {
- if (!Number.isFinite(deltaMs) || deltaMs <= 0) {
- return fallbackMs;
- }
+ if (!Number.isFinite(deltaMs) || deltaMs <= 0) {
+ return fallbackMs;
+ }
- return Math.min(80, Math.max(1, deltaMs));
+ return Math.min(80, Math.max(1, deltaMs));
}
/**
@@ -59,205 +59,215 @@ export function clampDeltaMs(deltaMs: number, fallbackMs = 1000 / 60) {
*/
function msToSec(ms: number) {
- return ms / 1000;
+ return ms / 1000;
}
function resolveSpringPosition(
- t: number,
- target: number,
- initialDelta: number,
- initialVelocity: number,
- dampingRatio: number,
- undampedAngularFreq: number,
+ t: number,
+ target: number,
+ initialDelta: number,
+ initialVelocity: number,
+ dampingRatio: number,
+ undampedAngularFreq: number,
): number {
- if (dampingRatio < 1) {
- // Underdamped — oscillatory envelope
- const dampedFreq = undampedAngularFreq * Math.sqrt(1 - dampingRatio * dampingRatio);
- const envelope = Math.exp(-dampingRatio * undampedAngularFreq * t);
- return (
- target -
- envelope *
- (((initialVelocity + dampingRatio * undampedAngularFreq * initialDelta) / dampedFreq) *
- Math.sin(dampedFreq * t) +
- initialDelta * Math.cos(dampedFreq * t))
- );
- }
-
- if (dampingRatio === 1) {
- // Critically damped — no oscillation, fastest convergence
- return (
- target -
- Math.exp(-undampedAngularFreq * t) *
- (initialDelta + (initialVelocity + undampedAngularFreq * initialDelta) * t)
- );
- }
-
- // Overdamped — exponential decay, no oscillation
- const dampedFreq = undampedAngularFreq * Math.sqrt(dampingRatio * dampingRatio - 1);
- const envelope = Math.exp(-dampingRatio * undampedAngularFreq * t);
- const freqT = Math.min(dampedFreq * t, 300); // cap to avoid Infinity in sinh/cosh
- return (
- target -
- (envelope *
- ((initialVelocity + dampingRatio * undampedAngularFreq * initialDelta) * Math.sinh(freqT) +
- dampedFreq * initialDelta * Math.cosh(freqT))) /
- dampedFreq
- );
+ if (dampingRatio < 1) {
+ // Underdamped — oscillatory envelope
+ const dampedFreq = undampedAngularFreq * Math.sqrt(1 - dampingRatio * dampingRatio);
+ const envelope = Math.exp(-dampingRatio * undampedAngularFreq * t);
+ return (
+ target -
+ envelope *
+ (((initialVelocity + dampingRatio * undampedAngularFreq * initialDelta) /
+ dampedFreq) *
+ Math.sin(dampedFreq * t) +
+ initialDelta * Math.cos(dampedFreq * t))
+ );
+ }
+
+ if (dampingRatio === 1) {
+ // Critically damped — no oscillation, fastest convergence
+ return (
+ target -
+ Math.exp(-undampedAngularFreq * t) *
+ (initialDelta + (initialVelocity + undampedAngularFreq * initialDelta) * t)
+ );
+ }
+
+ // Overdamped — exponential decay, no oscillation
+ const dampedFreq = undampedAngularFreq * Math.sqrt(dampingRatio * dampingRatio - 1);
+ const envelope = Math.exp(-dampingRatio * undampedAngularFreq * t);
+ const freqT = Math.min(dampedFreq * t, 300); // cap to avoid Infinity in sinh/cosh
+ return (
+ target -
+ (envelope *
+ ((initialVelocity + dampingRatio * undampedAngularFreq * initialDelta) *
+ Math.sinh(freqT) +
+ dampedFreq * initialDelta * Math.cosh(freqT))) /
+ dampedFreq
+ );
}
export function stepSpringValue(
- state: SpringState,
- target: number,
- deltaMs: number,
- config: SpringConfig,
+ state: SpringState,
+ target: number,
+ deltaMs: number,
+ config: SpringConfig,
) {
- const safeDeltaMs = clampDeltaMs(deltaMs);
-
- if (!state.initialized || !Number.isFinite(state.value)) {
- state.value = target;
- state.velocity = 0;
- state.initialized = true;
- return state.value;
- }
-
- const restDelta = config.restDelta ?? 0.0005;
- const restSpeed = config.restSpeed ?? 0.02;
-
- if (Math.abs(target - state.value) <= restDelta && Math.abs(state.velocity) <= restSpeed) {
- state.value = target;
- state.velocity = 0;
- return state.value;
- }
-
- const { stiffness, damping, mass } = config;
- const undampedAngularFreq = Math.sqrt(stiffness / mass);
- const dampingRatio = damping / (2 * Math.sqrt(stiffness * mass));
- const initialDelta = target - state.value;
- const initialVelocity = -state.velocity;
- const tSec = msToSec(safeDeltaMs);
-
- const current = resolveSpringPosition(
- tSec,
- target,
- initialDelta,
- initialVelocity,
- dampingRatio,
- undampedAngularFreq,
- );
-
- // Overshoot guard for overdamped / critically-damped springs (ζ ≥ 1).
- // With a fixed target an overdamped spring never overshoots, but when
- // the target moves every frame (zoom easing curve) carried-over velocity
- // can push the value past the new target → wobble on reversal.
- // Clamping to target keeps the original animation speed while preventing
- // the jelly-like counter-oscillation.
- if (dampingRatio >= 1) {
- const crossed =
- (state.value <= target && current > target) ||
- (state.value >= target && current < target);
- if (crossed) {
- state.value = target;
- state.velocity = 0;
- return state.value;
- }
- }
-
- // Analytical velocity via forward-difference on the closed-form solution.
- const epsilon = 0.0001;
- const ahead = resolveSpringPosition(
- tSec + epsilon,
- target,
- initialDelta,
- initialVelocity,
- dampingRatio,
- undampedAngularFreq,
- );
- const analyticalVelocity = (ahead - current) / epsilon; // value per second
-
- const isBelowVelocityThreshold = Math.abs(analyticalVelocity) <= restSpeed;
- const isBelowDisplacementThreshold = Math.abs(target - current) <= restDelta;
- const isDone = isBelowVelocityThreshold && isBelowDisplacementThreshold;
-
- if (isDone) {
- state.value = target;
- state.velocity = 0;
- } else {
- state.value = current;
- state.velocity = analyticalVelocity;
- }
-
- return state.value;
+ const safeDeltaMs = clampDeltaMs(deltaMs);
+
+ if (!state.initialized || !Number.isFinite(state.value)) {
+ state.value = target;
+ state.velocity = 0;
+ state.initialized = true;
+ return state.value;
+ }
+
+ const restDelta = config.restDelta ?? 0.0005;
+ const restSpeed = config.restSpeed ?? 0.02;
+
+ if (Math.abs(target - state.value) <= restDelta && Math.abs(state.velocity) <= restSpeed) {
+ state.value = target;
+ state.velocity = 0;
+ return state.value;
+ }
+
+ const { stiffness, damping, mass } = config;
+ const undampedAngularFreq = Math.sqrt(stiffness / mass);
+ const dampingRatio = damping / (2 * Math.sqrt(stiffness * mass));
+ const initialDelta = target - state.value;
+ const initialVelocity = -state.velocity;
+ const tSec = msToSec(safeDeltaMs);
+
+ const current = resolveSpringPosition(
+ tSec,
+ target,
+ initialDelta,
+ initialVelocity,
+ dampingRatio,
+ undampedAngularFreq,
+ );
+
+ // Overshoot guard for overdamped / critically-damped springs (ζ ≥ 1).
+ // With a fixed target an overdamped spring never overshoots, but when
+ // the target moves every frame (zoom easing curve) carried-over velocity
+ // can push the value past the new target → wobble on reversal.
+ // Clamping to target keeps the original animation speed while preventing
+ // the jelly-like counter-oscillation.
+ if (dampingRatio >= 1) {
+ const crossed =
+ (state.value <= target && current > target) ||
+ (state.value >= target && current < target);
+ if (crossed) {
+ state.value = target;
+ state.velocity = 0;
+ return state.value;
+ }
+ }
+
+ // Analytical velocity via forward-difference on the closed-form solution.
+ const epsilon = 0.0001;
+ const ahead = resolveSpringPosition(
+ tSec + epsilon,
+ target,
+ initialDelta,
+ initialVelocity,
+ dampingRatio,
+ undampedAngularFreq,
+ );
+ const analyticalVelocity = (ahead - current) / epsilon; // value per second
+
+ const isBelowVelocityThreshold = Math.abs(analyticalVelocity) <= restSpeed;
+ const isBelowDisplacementThreshold = Math.abs(target - current) <= restDelta;
+ const isDone = isBelowVelocityThreshold && isBelowDisplacementThreshold;
+
+ if (isDone) {
+ state.value = target;
+ state.velocity = 0;
+ } else {
+ state.value = current;
+ state.velocity = analyticalVelocity;
+ }
+
+ return state.value;
}
export function getCursorSpringConfig(smoothingFactor: number): SpringConfig {
- const clamped = Math.min(CURSOR_SMOOTHING_MAX, Math.max(CURSOR_SMOOTHING_MIN, smoothingFactor));
-
- if (clamped <= 0) {
- return {
- stiffness: 1000,
- damping: 100,
- mass: 1,
- restDelta: 0.0001,
- restSpeed: 0.001,
- };
- }
-
- if (clamped <= CURSOR_SMOOTHING_LEGACY_MAX) {
- const legacyNormalized = Math.min(
- 1,
- Math.max(0, (clamped - CURSOR_SMOOTHING_MIN) / (CURSOR_SMOOTHING_LEGACY_MAX - CURSOR_SMOOTHING_MIN)),
- );
-
- return {
- stiffness: 760 - legacyNormalized * 420,
- damping: 34 + legacyNormalized * 24,
- mass: 0.55 + legacyNormalized * 0.45,
- restDelta: 0.0002,
- restSpeed: 0.01,
- };
- }
-
- const extendedNormalized = Math.min(
- 1,
- Math.max(0, (clamped - CURSOR_SMOOTHING_LEGACY_MAX) / (CURSOR_SMOOTHING_MAX - CURSOR_SMOOTHING_LEGACY_MAX)),
- );
-
- return {
- stiffness: 340 - extendedNormalized * 180,
- damping: 58 + extendedNormalized * 22,
- mass: 1 + extendedNormalized * 0.35,
- restDelta: 0.0002,
- restSpeed: 0.01,
- };
+ const clamped = Math.min(CURSOR_SMOOTHING_MAX, Math.max(CURSOR_SMOOTHING_MIN, smoothingFactor));
+
+ if (clamped <= 0) {
+ return {
+ stiffness: 1000,
+ damping: 100,
+ mass: 1,
+ restDelta: 0.0001,
+ restSpeed: 0.001,
+ };
+ }
+
+ if (clamped <= CURSOR_SMOOTHING_LEGACY_MAX) {
+ const legacyNormalized = Math.min(
+ 1,
+ Math.max(
+ 0,
+ (clamped - CURSOR_SMOOTHING_MIN) /
+ (CURSOR_SMOOTHING_LEGACY_MAX - CURSOR_SMOOTHING_MIN),
+ ),
+ );
+
+ return {
+ stiffness: 760 - legacyNormalized * 420,
+ damping: 34 + legacyNormalized * 24,
+ mass: 0.55 + legacyNormalized * 0.45,
+ restDelta: 0.0002,
+ restSpeed: 0.01,
+ };
+ }
+
+ const extendedNormalized = Math.min(
+ 1,
+ Math.max(
+ 0,
+ (clamped - CURSOR_SMOOTHING_LEGACY_MAX) /
+ (CURSOR_SMOOTHING_MAX - CURSOR_SMOOTHING_LEGACY_MAX),
+ ),
+ );
+
+ return {
+ stiffness: 340 - extendedNormalized * 180,
+ damping: 58 + extendedNormalized * 22,
+ mass: 1 + extendedNormalized * 0.35,
+ restDelta: 0.0002,
+ restSpeed: 0.01,
+ };
}
export function getZoomSpringConfig(smoothnessFactor = 0.5): SpringConfig {
- const clamped = Math.max(0, Math.min(1, smoothnessFactor));
-
- if (clamped <= 0) {
- return {
- stiffness: 1000,
- damping: 100,
- mass: 1,
- restDelta: 0.0001,
- restSpeed: 0.001,
- };
- }
-
- // Map 0-1 slider to the internal 0-2 spring range so that
- // smoothness=1 gives the same feel as the old smoothness=2.
- const scaled = clamped * 2;
-
- // Hooke's law spring: F = -kx - cv
- // Damping ratio ζ = c / (2√(km)) ≈ 1.05 — barely overdamped.
- // The overshoot clamp in stepSpringValue prevents wobble even at
- // this low damping, so animations stay fast and responsive.
- // Higher scaled → lower stiffness + higher mass → slower, floatier settle.
- return {
- stiffness: 100 / scaled,
- damping: 21,
- mass: 1.0 * scaled,
- restDelta: 0.0005,
- restSpeed: 0.015,
- };
+ const clamped = Math.max(0, Math.min(1, smoothnessFactor));
+
+ if (clamped <= 0) {
+ return {
+ stiffness: 1000,
+ damping: 100,
+ mass: 1,
+ restDelta: 0.0001,
+ restSpeed: 0.001,
+ };
+ }
+
+ // Map 0-1 slider to the internal 0-2 spring range so that
+ // smoothness=1 gives the same feel as the old smoothness=2.
+ const scaled = clamped * 2;
+
+ // Hooke's law spring: F = -kx - cv
+ // Damping ratio ζ = c / (2√(km)) ≈ 1.05 — barely overdamped.
+ // The overshoot clamp in stepSpringValue prevents wobble even at
+ // this low damping, so animations stay fast and responsive.
+ // Higher scaled → lower stiffness + higher mass → slower, floatier settle.
+ return {
+ stiffness: 100 / scaled,
+ damping: 21,
+ mass: 1.0 * scaled,
+ restDelta: 0.0005,
+ restSpeed: 0.015,
+ };
}
diff --git a/src/components/video-editor/videoPlayback/overlayUtils.ts b/src/components/video-editor/videoPlayback/overlayUtils.ts
index 24d2c6e5..22275782 100644
--- a/src/components/video-editor/videoPlayback/overlayUtils.ts
+++ b/src/components/video-editor/videoPlayback/overlayUtils.ts
@@ -1,65 +1,68 @@
-import { ZOOM_DEPTH_SCALES, type ZoomRegion, type ZoomFocus } from "../types";
+import { ZOOM_DEPTH_SCALES, type ZoomFocus, type ZoomRegion } from "../types";
import { clampFocusToStage } from "./focusUtils";
interface OverlayUpdateParams {
- overlayEl: HTMLDivElement;
- indicatorEl: HTMLDivElement;
- region: ZoomRegion | null;
- focusOverride?: ZoomFocus;
- baseMask: { x: number; y: number; width: number; height: number };
- isPlaying: boolean;
+ overlayEl: HTMLDivElement;
+ indicatorEl: HTMLDivElement;
+ region: ZoomRegion | null;
+ focusOverride?: ZoomFocus;
+ baseMask: { x: number; y: number; width: number; height: number };
+ isPlaying: boolean;
}
export function updateOverlayIndicator(params: OverlayUpdateParams) {
- const { overlayEl, indicatorEl, region, focusOverride, baseMask, isPlaying } = params;
+ const { overlayEl, indicatorEl, region, focusOverride, baseMask, isPlaying } = params;
- if (!region || region.mode === 'auto') {
- indicatorEl.style.display = 'none';
- overlayEl.style.pointerEvents = 'none';
- return;
- }
+ if (!region || region.mode === "auto") {
+ indicatorEl.style.display = "none";
+ overlayEl.style.pointerEvents = "none";
+ return;
+ }
- const stageWidth = overlayEl.clientWidth;
- const stageHeight = overlayEl.clientHeight;
-
- if (!stageWidth || !stageHeight) {
- indicatorEl.style.display = 'none';
- overlayEl.style.pointerEvents = 'none';
- return;
- }
+ const stageWidth = overlayEl.clientWidth;
+ const stageHeight = overlayEl.clientHeight;
- if (!baseMask.width || !baseMask.height) {
- indicatorEl.style.display = 'none';
- overlayEl.style.pointerEvents = isPlaying ? 'none' : 'auto';
- return;
- }
+ if (!stageWidth || !stageHeight) {
+ indicatorEl.style.display = "none";
+ overlayEl.style.pointerEvents = "none";
+ return;
+ }
- const zoomScale = ZOOM_DEPTH_SCALES[region.depth];
- const focus = clampFocusToStage(
- focusOverride ?? region.focus,
- region.depth,
- { width: stageWidth, height: stageHeight }
- );
+ if (!baseMask.width || !baseMask.height) {
+ indicatorEl.style.display = "none";
+ overlayEl.style.pointerEvents = isPlaying ? "none" : "auto";
+ return;
+ }
- const indicatorWidth = baseMask.width / zoomScale;
- const indicatorHeight = baseMask.height / zoomScale;
+ const zoomScale = ZOOM_DEPTH_SCALES[region.depth];
+ const focus = clampFocusToStage(focusOverride ?? region.focus, region.depth, {
+ width: stageWidth,
+ height: stageHeight,
+ });
- const rawLeft = baseMask.x + focus.cx * baseMask.width - indicatorWidth / 2;
- const rawTop = baseMask.y + focus.cy * baseMask.height - indicatorHeight / 2;
+ const indicatorWidth = baseMask.width / zoomScale;
+ const indicatorHeight = baseMask.height / zoomScale;
- const adjustedLeft = indicatorWidth >= baseMask.width
- ? baseMask.x + (baseMask.width - indicatorWidth) / 2
- : Math.max(baseMask.x, Math.min(baseMask.x + baseMask.width - indicatorWidth, rawLeft));
+ const rawLeft = baseMask.x + focus.cx * baseMask.width - indicatorWidth / 2;
+ const rawTop = baseMask.y + focus.cy * baseMask.height - indicatorHeight / 2;
- const adjustedTop = indicatorHeight >= baseMask.height
- ? baseMask.y + (baseMask.height - indicatorHeight) / 2
- : Math.max(baseMask.y, Math.min(baseMask.y + baseMask.height - indicatorHeight, rawTop));
+ const adjustedLeft =
+ indicatorWidth >= baseMask.width
+ ? baseMask.x + (baseMask.width - indicatorWidth) / 2
+ : Math.max(baseMask.x, Math.min(baseMask.x + baseMask.width - indicatorWidth, rawLeft));
- indicatorEl.style.display = 'block';
- indicatorEl.style.width = `${indicatorWidth}px`;
- indicatorEl.style.height = `${indicatorHeight}px`;
- indicatorEl.style.left = `${adjustedLeft}px`;
- indicatorEl.style.top = `${adjustedTop}px`;
- overlayEl.style.pointerEvents = isPlaying ? 'none' : 'auto';
-}
+ const adjustedTop =
+ indicatorHeight >= baseMask.height
+ ? baseMask.y + (baseMask.height - indicatorHeight) / 2
+ : Math.max(
+ baseMask.y,
+ Math.min(baseMask.y + baseMask.height - indicatorHeight, rawTop),
+ );
+ indicatorEl.style.display = "block";
+ indicatorEl.style.width = `${indicatorWidth}px`;
+ indicatorEl.style.height = `${indicatorHeight}px`;
+ indicatorEl.style.left = `${adjustedLeft}px`;
+ indicatorEl.style.top = `${adjustedTop}px`;
+ overlayEl.style.pointerEvents = isPlaying ? "none" : "auto";
+}
diff --git a/src/components/video-editor/videoPlayback/videoEventHandlers.ts b/src/components/video-editor/videoPlayback/videoEventHandlers.ts
index 0502581d..338538c3 100644
--- a/src/components/video-editor/videoPlayback/videoEventHandlers.ts
+++ b/src/components/video-editor/videoPlayback/videoEventHandlers.ts
@@ -1,139 +1,142 @@
-import type React from 'react';
-import type { TrimRegion, SpeedRegion } from '../types';
-import { extensionHost } from '@/lib/extensions';
+import type React from "react";
+import { extensionHost } from "@/lib/extensions";
+import type { SpeedRegion, TrimRegion } from "../types";
interface VideoEventHandlersParams {
- video: HTMLVideoElement;
- isSeekingRef: React.MutableRefObject<boolean>;
- isPlayingRef: React.MutableRefObject<boolean>;
- allowPlaybackRef: React.MutableRefObject<boolean>;
- currentTimeRef: React.MutableRefObject<number>;
- timeUpdateAnimationRef: React.MutableRefObject<number | null>;
- onPlayStateChange: (playing: boolean) => void;
- onTimeUpdate: (time: number) => void;
- trimRegionsRef: React.MutableRefObject<TrimRegion[]>;
- speedRegionsRef: React.MutableRefObject<SpeedRegion[]>;
+ video: HTMLVideoElement;
+ isSeekingRef: React.MutableRefObject<boolean>;
+ isPlayingRef: React.MutableRefObject<boolean>;
+ allowPlaybackRef: React.MutableRefObject<boolean>;
+ currentTimeRef: React.MutableRefObject<number>;
+ timeUpdateAnimationRef: React.MutableRefObject<number | null>;
+ onPlayStateChange: (playing: boolean) => void;
+ onTimeUpdate: (time: number) => void;
+ trimRegionsRef: React.MutableRefObject<TrimRegion[]>;
+ speedRegionsRef: React.MutableRefObject<SpeedRegion[]>;
}
export function createVideoEventHandlers(params: VideoEventHandlersParams) {
- const {
- video,
- isSeekingRef,
- isPlayingRef,
- allowPlaybackRef,
- currentTimeRef,
- timeUpdateAnimationRef,
- onPlayStateChange,
- onTimeUpdate,
- trimRegionsRef,
- speedRegionsRef,
- } = params;
-
- const emitTime = (timeValue: number) => {
- currentTimeRef.current = timeValue * 1000;
- onTimeUpdate(timeValue);
- extensionHost.emitEvent({ type: 'playback:timeupdate', timeMs: timeValue * 1000 });
- };
-
- // Helper function to check if current time is within a trim region
- const findActiveTrimRegion = (currentTimeMs: number): TrimRegion | null => {
- const trimRegions = trimRegionsRef.current;
- return trimRegions.find(
- (region) => currentTimeMs >= region.startMs && currentTimeMs < region.endMs
- ) || null;
- };
-
- // Helper function to find the active speed region at the current time
- const findActiveSpeedRegion = (currentTimeMs: number): SpeedRegion | null => {
- return speedRegionsRef.current.find(
- (region) => currentTimeMs >= region.startMs && currentTimeMs < region.endMs
- ) || null;
- };
-
- function updateTime() {
- if (!video) return;
-
- const currentTimeMs = video.currentTime * 1000;
- const activeTrimRegion = findActiveTrimRegion(currentTimeMs);
-
- // If we're in a trim region during playback, skip to the end of it
- if (activeTrimRegion && !video.paused && !video.ended) {
- const skipToTime = activeTrimRegion.endMs / 1000;
-
- // If the skip would take us past the video duration, pause instead
- if (skipToTime >= video.duration) {
- video.pause();
- } else {
- video.currentTime = skipToTime;
- emitTime(skipToTime);
- }
- } else {
- // Apply playback speed from active speed region
- const activeSpeedRegion = findActiveSpeedRegion(currentTimeMs);
- video.playbackRate = activeSpeedRegion ? activeSpeedRegion.speed : 1;
- emitTime(video.currentTime);
- }
-
- if (!video.paused && !video.ended) {
- timeUpdateAnimationRef.current = requestAnimationFrame(updateTime);
- }
- }
-
- const handlePlay = () => {
- if (!allowPlaybackRef.current) {
- video.pause();
- return;
- }
-
- isPlayingRef.current = true;
- onPlayStateChange(true);
- if (timeUpdateAnimationRef.current) {
- cancelAnimationFrame(timeUpdateAnimationRef.current);
- }
- timeUpdateAnimationRef.current = requestAnimationFrame(updateTime);
- };
-
- const handlePause = () => {
- isPlayingRef.current = false;
- onPlayStateChange(false);
- if (timeUpdateAnimationRef.current) {
- cancelAnimationFrame(timeUpdateAnimationRef.current);
- timeUpdateAnimationRef.current = null;
- }
- emitTime(video.currentTime);
- };
-
- const handleSeeked = () => {
- isSeekingRef.current = false;
-
- const currentTimeMs = video.currentTime * 1000;
- const activeTrimRegion = findActiveTrimRegion(currentTimeMs);
-
- // If we seeked into a trim region while playing, skip to the end
- if (activeTrimRegion && isPlayingRef.current && !video.paused) {
- const skipToTime = activeTrimRegion.endMs / 1000;
-
- if (skipToTime >= video.duration) {
- video.pause();
- } else {
- video.currentTime = skipToTime;
- emitTime(skipToTime);
- }
- } else {
- emitTime(video.currentTime);
- }
- };
-
- const handleSeeking = () => {
- isSeekingRef.current = true;
- emitTime(video.currentTime);
- };
-
- return {
- handlePlay,
- handlePause,
- handleSeeked,
- handleSeeking,
- };
+ const {
+ video,
+ isSeekingRef,
+ isPlayingRef,
+ allowPlaybackRef,
+ currentTimeRef,
+ timeUpdateAnimationRef,
+ onPlayStateChange,
+ onTimeUpdate,
+ trimRegionsRef,
+ speedRegionsRef,
+ } = params;
+
+ const emitTime = (timeValue: number) => {
+ currentTimeRef.current = timeValue * 1000;
+ onTimeUpdate(timeValue);
+ extensionHost.emitEvent({ type: "playback:timeupdate", timeMs: timeValue * 1000 });
+ };
+
+ // Helper function to check if current time is within a trim region
+ const findActiveTrimRegion = (currentTimeMs: number): TrimRegion | null => {
+ const trimRegions = trimRegionsRef.current;
+ return (
+ trimRegions.find(
+ (region) => currentTimeMs >= region.startMs && currentTimeMs < region.endMs,
+ ) || null
+ );
+ };
+
+ // Helper function to find the active speed region at the current time
+ const findActiveSpeedRegion = (currentTimeMs: number): SpeedRegion | null => {
+ return (
+ speedRegionsRef.current.find(
+ (region) => currentTimeMs >= region.startMs && currentTimeMs < region.endMs,
+ ) || null
+ );
+ };
+
+ function updateTime() {
+ if (!video) return;
+
+ const currentTimeMs = video.currentTime * 1000;
+ const activeTrimRegion = findActiveTrimRegion(currentTimeMs);
+
+ // If we're in a trim region during playback, skip to the end of it
+ if (activeTrimRegion && !video.paused && !video.ended) {
+ const skipToTime = activeTrimRegion.endMs / 1000;
+
+ // If the skip would take us past the video duration, pause instead
+ if (skipToTime >= video.duration) {
+ video.pause();
+ } else {
+ video.currentTime = skipToTime;
+ emitTime(skipToTime);
+ }
+ } else {
+ // Apply playback speed from active speed region
+ const activeSpeedRegion = findActiveSpeedRegion(currentTimeMs);
+ video.playbackRate = activeSpeedRegion ? activeSpeedRegion.speed : 1;
+ emitTime(video.currentTime);
+ }
+
+ if (!video.paused && !video.ended) {
+ timeUpdateAnimationRef.current = requestAnimationFrame(updateTime);
+ }
+ }
+
+ const handlePlay = () => {
+ if (!allowPlaybackRef.current) {
+ video.pause();
+ return;
+ }
+
+ isPlayingRef.current = true;
+ onPlayStateChange(true);
+ if (timeUpdateAnimationRef.current) {
+ cancelAnimationFrame(timeUpdateAnimationRef.current);
+ }
+ timeUpdateAnimationRef.current = requestAnimationFrame(updateTime);
+ };
+
+ const handlePause = () => {
+ isPlayingRef.current = false;
+ onPlayStateChange(false);
+ if (timeUpdateAnimationRef.current) {
+ cancelAnimationFrame(timeUpdateAnimationRef.current);
+ timeUpdateAnimationRef.current = null;
+ }
+ emitTime(video.currentTime);
+ };
+
+ const handleSeeked = () => {
+ isSeekingRef.current = false;
+
+ const currentTimeMs = video.currentTime * 1000;
+ const activeTrimRegion = findActiveTrimRegion(currentTimeMs);
+
+ // If we seeked into a trim region while playing, skip to the end
+ if (activeTrimRegion && isPlayingRef.current && !video.paused) {
+ const skipToTime = activeTrimRegion.endMs / 1000;
+
+ if (skipToTime >= video.duration) {
+ video.pause();
+ } else {
+ video.currentTime = skipToTime;
+ emitTime(skipToTime);
+ }
+ } else {
+ emitTime(video.currentTime);
+ }
+ };
+
+ const handleSeeking = () => {
+ isSeekingRef.current = true;
+ emitTime(video.currentTime);
+ };
+
+ return {
+ handlePlay,
+ handlePause,
+ handleSeeked,
+ handleSeeking,
+ };
}
-
diff --git a/src/components/video-editor/videoPlayback/zoomAnimation.test.ts b/src/components/video-editor/videoPlayback/zoomAnimation.test.ts
index 8ac2a92c..b65f12d5 100644
--- a/src/components/video-editor/videoPlayback/zoomAnimation.test.ts
+++ b/src/components/video-editor/videoPlayback/zoomAnimation.test.ts
@@ -1,73 +1,72 @@
import { describe, expect, it } from "vitest";
-
+import type { ZoomRegion } from "../types";
+import { clamp01, cubicBezier, easeOutExpo, easeOutZoom } from "./mathUtils";
import {
- createSpringState,
- resetSpringState,
- clampDeltaMs,
- stepSpringValue,
- getZoomSpringConfig,
- getCursorSpringConfig,
- type SpringState,
- type SpringConfig,
+ clampDeltaMs,
+ createSpringState,
+ getCursorSpringConfig,
+ getZoomSpringConfig,
+ resetSpringState,
+ type SpringConfig,
+ type SpringState,
+ stepSpringValue,
} from "./motionSmoothing";
import { computeRegionStrength, findDominantRegion } from "./zoomRegionUtils";
-import { clamp01, easeOutZoom, cubicBezier, easeOutExpo } from "./mathUtils";
-import type { ZoomRegion } from "../types";
// ---------------------------------------------------------------------------
// motionSmoothing — spring state helpers
// ---------------------------------------------------------------------------
describe("createSpringState", () => {
- it("returns uninitialized state at 0", () => {
- const s = createSpringState();
- expect(s).toEqual({ value: 0, velocity: 0, initialized: false });
- });
-
- it("accepts a custom initial value", () => {
- const s = createSpringState(5);
- expect(s.value).toBe(5);
- expect(s.initialized).toBe(false);
- });
+ it("returns uninitialized state at 0", () => {
+ const s = createSpringState();
+ expect(s).toEqual({ value: 0, velocity: 0, initialized: false });
+ });
+
+ it("accepts a custom initial value", () => {
+ const s = createSpringState(5);
+ expect(s.value).toBe(5);
+ expect(s.initialized).toBe(false);
+ });
});
describe("resetSpringState", () => {
- it("clears velocity and initialized flag", () => {
- const s: SpringState = { value: 3, velocity: 10, initialized: true };
- resetSpringState(s);
- expect(s.velocity).toBe(0);
- expect(s.initialized).toBe(false);
- expect(s.value).toBe(3); // kept
- });
-
- it("resets value when provided", () => {
- const s: SpringState = { value: 3, velocity: 10, initialized: true };
- resetSpringState(s, 7);
- expect(s.value).toBe(7);
- expect(s.velocity).toBe(0);
- });
+ it("clears velocity and initialized flag", () => {
+ const s: SpringState = { value: 3, velocity: 10, initialized: true };
+ resetSpringState(s);
+ expect(s.velocity).toBe(0);
+ expect(s.initialized).toBe(false);
+ expect(s.value).toBe(3); // kept
+ });
+
+ it("resets value when provided", () => {
+ const s: SpringState = { value: 3, velocity: 10, initialized: true };
+ resetSpringState(s, 7);
+ expect(s.value).toBe(7);
+ expect(s.velocity).toBe(0);
+ });
});
describe("clampDeltaMs", () => {
- it("passes through a normal 16ms frame", () => {
- expect(clampDeltaMs(16.67)).toBeCloseTo(16.67, 2);
- });
+ it("passes through a normal 16ms frame", () => {
+ expect(clampDeltaMs(16.67)).toBeCloseTo(16.67, 2);
+ });
- it("clamps negative delta to fallback", () => {
- expect(clampDeltaMs(-1)).toBeCloseTo(1000 / 60, 2);
- });
+ it("clamps negative delta to fallback", () => {
+ expect(clampDeltaMs(-1)).toBeCloseTo(1000 / 60, 2);
+ });
- it("clamps zero delta to fallback", () => {
- expect(clampDeltaMs(0)).toBeCloseTo(1000 / 60, 2);
- });
+ it("clamps zero delta to fallback", () => {
+ expect(clampDeltaMs(0)).toBeCloseTo(1000 / 60, 2);
+ });
- it("clamps NaN to fallback", () => {
- expect(clampDeltaMs(Number.NaN)).toBeCloseTo(1000 / 60, 2);
- });
+ it("clamps NaN to fallback", () => {
+ expect(clampDeltaMs(Number.NaN)).toBeCloseTo(1000 / 60, 2);
+ });
- it("clamps excessively large delta to 80ms", () => {
- expect(clampDeltaMs(500)).toBe(80);
- });
+ it("clamps excessively large delta to 80ms", () => {
+ expect(clampDeltaMs(500)).toBe(80);
+ });
});
// ---------------------------------------------------------------------------
@@ -75,91 +74,91 @@ describe("clampDeltaMs", () => {
// ---------------------------------------------------------------------------
describe("stepSpringValue", () => {
- const config: SpringConfig = {
- stiffness: 320,
- damping: 40,
- mass: 0.92,
- restDelta: 0.0005,
- restSpeed: 0.015,
- };
-
- it("snaps to target on first tick (initialization)", () => {
- const s = createSpringState();
- const result = stepSpringValue(s, 1, 16, config);
- expect(result).toBe(1);
- expect(s.initialized).toBe(true);
- });
-
- it("converges toward the target over many ticks", () => {
- const s = createSpringState();
- stepSpringValue(s, 0, 16, config); // init at 0
-
- // Drive toward 1 over many frames
- for (let i = 0; i < 200; i++) {
- stepSpringValue(s, 1, 16, config);
- }
-
- expect(s.value).toBeCloseTo(1, 3);
- });
-
- it("converges within reasonable time for default zoom config", () => {
- const zoomConfig = getZoomSpringConfig(1.0);
- const s = createSpringState();
- stepSpringValue(s, 0, 16, zoomConfig); // init at 0
-
- let frames = 0;
- while (Math.abs(s.value - 1) > 0.001 && frames < 500) {
- stepSpringValue(s, 1, 16, zoomConfig);
- frames++;
- }
-
- // Should converge well within 500 frames (~8 seconds)
- // High damping (ζ ≈ 2) settles without overshoot but takes longer
- expect(frames).toBeLessThan(400); // ~6.5s at 60fps
- expect(s.value).toBeCloseTo(1, 2);
- });
-
- it("handles target change mid-animation", () => {
- const s = createSpringState();
- stepSpringValue(s, 0.5, 16, config);
-
- // Animate toward 1
- for (let i = 0; i < 30; i++) {
- stepSpringValue(s, 1, 16, config);
- }
- const midway = s.value;
- expect(midway).toBeGreaterThan(0.5);
-
- // Reverse direction toward 0
- for (let i = 0; i < 200; i++) {
- stepSpringValue(s, 0, 16, config);
- }
- expect(s.value).toBeCloseTo(0, 2);
- });
-
- it("settles exactly at target when within rest thresholds", () => {
- const s = createSpringState();
- stepSpringValue(s, 0, 16, config);
-
- // Manually place near target
- s.value = 0.9999;
- s.velocity = 0;
- stepSpringValue(s, 1, 16, config);
-
- expect(s.value).toBe(1);
- expect(s.velocity).toBe(0);
- });
-
- it("does not produce NaN or Infinity values", () => {
- const s = createSpringState();
- stepSpringValue(s, 0, 16, config);
-
- for (let i = 0; i < 300; i++) {
- stepSpringValue(s, Math.sin(i * 0.1), 16, config);
- expect(Number.isFinite(s.value)).toBe(true);
- expect(Number.isFinite(s.velocity)).toBe(true);
- }
- });
+ const config: SpringConfig = {
+ stiffness: 320,
+ damping: 40,
+ mass: 0.92,
+ restDelta: 0.0005,
+ restSpeed: 0.015,
+ };
+
+ it("snaps to target on first tick (initialization)", () => {
+ const s = createSpringState();
+ const result = stepSpringValue(s, 1, 16, config);
+ expect(result).toBe(1);
+ expect(s.initialized).toBe(true);
+ });
+
+ it("converges toward the target over many ticks", () => {
+ const s = createSpringState();
+ stepSpringValue(s, 0, 16, config); // init at 0
+
+ // Drive toward 1 over many frames
+ for (let i = 0; i < 200; i++) {
+ stepSpringValue(s, 1, 16, config);
+ }
+
+ expect(s.value).toBeCloseTo(1, 3);
+ });
+
+ it("converges within reasonable time for default zoom config", () => {
+ const zoomConfig = getZoomSpringConfig(1.0);
+ const s = createSpringState();
+ stepSpringValue(s, 0, 16, zoomConfig); // init at 0
+
+ let frames = 0;
+ while (Math.abs(s.value - 1) > 0.001 && frames < 500) {
+ stepSpringValue(s, 1, 16, zoomConfig);
+ frames++;
+ }
+
+ // Should converge well within 500 frames (~8 seconds)
+ // High damping (ζ ≈ 2) settles without overshoot but takes longer
+ expect(frames).toBeLessThan(400); // ~6.5s at 60fps
+ expect(s.value).toBeCloseTo(1, 2);
+ });
+
+ it("handles target change mid-animation", () => {
+ const s = createSpringState();
+ stepSpringValue(s, 0.5, 16, config);
+
+ // Animate toward 1
+ for (let i = 0; i < 30; i++) {
+ stepSpringValue(s, 1, 16, config);
+ }
+ const midway = s.value;
+ expect(midway).toBeGreaterThan(0.5);
+
+ // Reverse direction toward 0
+ for (let i = 0; i < 200; i++) {
+ stepSpringValue(s, 0, 16, config);
+ }
+ expect(s.value).toBeCloseTo(0, 2);
+ });
+
+ it("settles exactly at target when within rest thresholds", () => {
+ const s = createSpringState();
+ stepSpringValue(s, 0, 16, config);
+
+ // Manually place near target
+ s.value = 0.9999;
+ s.velocity = 0;
+ stepSpringValue(s, 1, 16, config);
+
+ expect(s.value).toBe(1);
+ expect(s.velocity).toBe(0);
+ });
+
+ it("does not produce NaN or Infinity values", () => {
+ const s = createSpringState();
+ stepSpringValue(s, 0, 16, config);
+
+ for (let i = 0; i < 300; i++) {
+ stepSpringValue(s, Math.sin(i * 0.1), 16, config);
+ expect(Number.isFinite(s.value)).toBe(true);
+ expect(Number.isFinite(s.velocity)).toBe(true);
+ }
+ });
});
// ---------------------------------------------------------------------------
@@ -167,55 +166,59 @@ describe("stepSpringValue", () => {
// ---------------------------------------------------------------------------
describe("getZoomSpringConfig", () => {
- it("returns snap-like config at smoothness 0", () => {
- const c = getZoomSpringConfig(0);
- expect(c.stiffness).toBe(1000);
- expect(c.damping).toBe(100);
- });
-
- it("returns softer config at smoothness 1.0", () => {
- const c = getZoomSpringConfig(1.0);
- expect(c.stiffness).toBe(320);
- expect(c.mass).toBeCloseTo(0.92, 2);
- });
-
- it("returns even softer config at smoothness 2.0", () => {
- const c = getZoomSpringConfig(2.0);
- expect(c.stiffness).toBe(160);
- expect(c.mass).toBeCloseTo(1.84, 2);
- });
-
- it("clamps values above 2", () => {
- const c = getZoomSpringConfig(5);
- expect(c).toEqual(getZoomSpringConfig(2));
- });
-
- it("defaults to 1.0 when called without arguments", () => {
- expect(getZoomSpringConfig()).toEqual(getZoomSpringConfig(1.0));
- });
+ it("returns snap-like config at smoothness 0", () => {
+ const c = getZoomSpringConfig(0);
+ expect(c.stiffness).toBe(1000);
+ expect(c.damping).toBe(100);
+ });
+
+ it("returns the default mapped config at smoothness 0.5", () => {
+ const c = getZoomSpringConfig(0.5);
+ expect(c.stiffness).toBe(100);
+ expect(c.mass).toBeCloseTo(1, 2);
+ });
+
+ it("returns the max mapped config at smoothness 1.0", () => {
+ const c = getZoomSpringConfig(1.0);
+ expect(c.stiffness).toBe(50);
+ expect(c.mass).toBeCloseTo(2, 2);
+ });
+
+ it("clamps values above 1.0 to the max config", () => {
+ expect(getZoomSpringConfig(2.0)).toEqual(getZoomSpringConfig(1.0));
+ });
+
+ it("clamps values far above 1.0 to the max config", () => {
+ const c = getZoomSpringConfig(5);
+ expect(c).toEqual(getZoomSpringConfig(1.0));
+ });
+
+ it("defaults to 0.5 when called without arguments", () => {
+ expect(getZoomSpringConfig()).toEqual(getZoomSpringConfig(0.5));
+ });
});
describe("getCursorSpringConfig", () => {
- it("returns stiff config at 0 (no smoothing)", () => {
- const c = getCursorSpringConfig(0);
- expect(c.stiffness).toBe(1000);
- });
-
- it("returns config in legacy range at 0.25", () => {
- const c = getCursorSpringConfig(0.25);
- expect(c.stiffness).toBeLessThan(760);
- expect(c.stiffness).toBeGreaterThan(300);
- });
-
- it("returns config in extended range at 1.5", () => {
- const c = getCursorSpringConfig(1.5);
- expect(c.stiffness).toBeLessThan(340);
- expect(c.mass).toBeGreaterThan(1);
- });
-
- it("clamps at max smoothing", () => {
- expect(getCursorSpringConfig(999)).toEqual(getCursorSpringConfig(2));
- });
+ it("returns stiff config at 0 (no smoothing)", () => {
+ const c = getCursorSpringConfig(0);
+ expect(c.stiffness).toBe(1000);
+ });
+
+ it("returns config in legacy range at 0.25", () => {
+ const c = getCursorSpringConfig(0.25);
+ expect(c.stiffness).toBeLessThan(760);
+ expect(c.stiffness).toBeGreaterThan(300);
+ });
+
+ it("returns config in extended range at 1.5", () => {
+ const c = getCursorSpringConfig(1.5);
+ expect(c.stiffness).toBeLessThan(340);
+ expect(c.mass).toBeGreaterThan(1);
+ });
+
+ it("clamps at max smoothing", () => {
+ expect(getCursorSpringConfig(999)).toEqual(getCursorSpringConfig(2));
+ });
});
// ---------------------------------------------------------------------------
@@ -223,54 +226,54 @@ describe("getCursorSpringConfig", () => {
// ---------------------------------------------------------------------------
describe("clamp01", () => {
- it("passes values in [0,1]", () => {
- expect(clamp01(0.5)).toBe(0.5);
- });
- it("clamps below 0", () => {
- expect(clamp01(-0.1)).toBe(0);
- });
- it("clamps above 1", () => {
- expect(clamp01(1.5)).toBe(1);
- });
+ it("passes values in [0,1]", () => {
+ expect(clamp01(0.5)).toBe(0.5);
+ });
+ it("clamps below 0", () => {
+ expect(clamp01(-0.1)).toBe(0);
+ });
+ it("clamps above 1", () => {
+ expect(clamp01(1.5)).toBe(1);
+ });
});
describe("easeOutZoom", () => {
- it("starts at 0", () => {
- expect(easeOutZoom(0)).toBeCloseTo(0, 4);
- });
-
- it("ends at 1", () => {
- expect(easeOutZoom(1)).toBeCloseTo(1, 4);
- });
-
- it("is monotonically increasing", () => {
- let previous = 0;
- for (let t = 0.05; t <= 1; t += 0.05) {
- const current = easeOutZoom(t);
- expect(current).toBeGreaterThanOrEqual(previous);
- previous = current;
- }
- });
-
- it("has steep initial rise (ease-out character)", () => {
- // At t=0.25 the curve should already be well above linear (0.25)
- expect(easeOutZoom(0.25)).toBeGreaterThan(0.5);
- });
+ it("starts at 0", () => {
+ expect(easeOutZoom(0)).toBeCloseTo(0, 4);
+ });
+
+ it("ends at 1", () => {
+ expect(easeOutZoom(1)).toBeCloseTo(1, 4);
+ });
+
+ it("is monotonically increasing", () => {
+ let previous = 0;
+ for (let t = 0.05; t <= 1; t += 0.05) {
+ const current = easeOutZoom(t);
+ expect(current).toBeGreaterThanOrEqual(previous);
+ previous = current;
+ }
+ });
+
+ it("has steep initial rise (ease-out character)", () => {
+ // At t=0.25 the curve should already be well above linear (0.25)
+ expect(easeOutZoom(0.25)).toBeGreaterThan(0.5);
+ });
});
describe("easeOutExpo", () => {
- it("starts at 0 and ends at 1", () => {
- expect(easeOutExpo(0)).toBeCloseTo(0, 4);
- expect(easeOutExpo(1)).toBe(1);
- });
+ it("starts at 0 and ends at 1", () => {
+ expect(easeOutExpo(0)).toBeCloseTo(0, 4);
+ expect(easeOutExpo(1)).toBe(1);
+ });
});
describe("cubicBezier", () => {
- it("linear bezier returns identity", () => {
- for (let t = 0; t <= 1; t += 0.1) {
- expect(cubicBezier(0.333, 0.333, 0.666, 0.666, t)).toBeCloseTo(t, 1);
- }
- });
+ it("linear bezier returns identity", () => {
+ for (let t = 0; t <= 1; t += 0.1) {
+ expect(cubicBezier(0.333, 0.333, 0.666, 0.666, t)).toBeCloseTo(t, 1);
+ }
+ });
});
// ---------------------------------------------------------------------------
@@ -278,43 +281,43 @@ describe("cubicBezier", () => {
// ---------------------------------------------------------------------------
describe("computeRegionStrength", () => {
- const region: ZoomRegion = {
- id: "z1",
- startMs: 2000,
- endMs: 5000,
- depth: 2,
- focus: { cx: 0.5, cy: 0.5 },
- };
-
- it("returns 0 well before the region", () => {
- expect(computeRegionStrength(region, 0)).toBe(0);
- });
-
- it("returns 0 well after the region", () => {
- expect(computeRegionStrength(region, 10000)).toBe(0);
- });
-
- it("reaches full strength during the hold phase", () => {
- // Mid-region: after zoom-in completes, before zoom-out starts
- expect(computeRegionStrength(region, 3500)).toBe(1);
- });
-
- it("rises smoothly during zoom-in", () => {
- // Zoom-in transitions from leadInStart .. zoomInEnd
- // zoomInEnd = startMs + 500, leadInStart = zoomInEnd - 1500 = startMs - 1000
- // So at startMs the transition is partially done
- const s = computeRegionStrength(region, region.startMs);
- expect(s).toBeGreaterThan(0);
- expect(s).toBeLessThan(1);
- });
-
- it("falls smoothly during zoom-out", () => {
- // zoomOutStart = endMs - 150, leadOutEnd = zoomOutStart + 1000
- const zoomOutStart = region.endMs - 150;
- const s = computeRegionStrength(region, zoomOutStart + 500);
- expect(s).toBeGreaterThan(0);
- expect(s).toBeLessThan(1);
- });
+ const region: ZoomRegion = {
+ id: "z1",
+ startMs: 2000,
+ endMs: 5000,
+ depth: 2,
+ focus: { cx: 0.5, cy: 0.5 },
+ };
+
+ it("returns 0 well before the region", () => {
+ expect(computeRegionStrength(region, 0)).toBe(0);
+ });
+
+ it("returns 0 well after the region", () => {
+ expect(computeRegionStrength(region, 10000)).toBe(0);
+ });
+
+ it("reaches full strength during the hold phase", () => {
+ // Mid-region: after zoom-in completes, before zoom-out starts
+ expect(computeRegionStrength(region, 3500)).toBe(1);
+ });
+
+ it("rises smoothly during zoom-in", () => {
+ // Zoom-in transitions from leadInStart .. zoomInEnd
+ // zoomInEnd = startMs + 500, leadInStart = zoomInEnd - 1500 = startMs - 1000
+ // So at startMs the transition is partially done
+ const s = computeRegionStrength(region, region.startMs);
+ expect(s).toBeGreaterThan(0);
+ expect(s).toBeLessThan(1);
+ });
+
+ it("falls smoothly during zoom-out", () => {
+ // Zoom-out now starts 200ms later than the original timing.
+ const zoomOutStart = region.endMs - 150;
+ const s = computeRegionStrength(region, zoomOutStart + 700);
+ expect(s).toBeGreaterThan(0);
+ expect(s).toBeLessThan(1);
+ });
});
// ---------------------------------------------------------------------------
@@ -322,70 +325,70 @@ describe("computeRegionStrength", () => {
// ---------------------------------------------------------------------------
describe("findDominantRegion", () => {
- it("returns null region when no regions exist", () => {
- const result = findDominantRegion([], 1000);
- expect(result.region).toBeNull();
- expect(result.strength).toBe(0);
- });
-
- it("returns the active region at its hold phase", () => {
- const regions: ZoomRegion[] = [
- { id: "a", startMs: 1000, endMs: 4000, depth: 2, focus: { cx: 0.3, cy: 0.3 } },
- ];
- const result = findDominantRegion(regions, 2500);
- expect(result.region).not.toBeNull();
- expect(result.region!.id).toBe("a");
- expect(result.strength).toBe(1);
- });
-
- it("returns null region outside all regions", () => {
- const regions: ZoomRegion[] = [
- { id: "a", startMs: 1000, endMs: 2000, depth: 2, focus: { cx: 0.5, cy: 0.5 } },
- ];
- const result = findDominantRegion(regions, 10000);
- expect(result.region).toBeNull();
- });
-
- it("connects chained zooms when connectZooms is true", () => {
- const regions: ZoomRegion[] = [
- { id: "a", startMs: 1000, endMs: 3000, depth: 2, focus: { cx: 0.2, cy: 0.2 } },
- { id: "b", startMs: 3500, endMs: 6000, depth: 3, focus: { cx: 0.8, cy: 0.8 } },
- ];
-
- // During the connected transition (between a.endMs and a.endMs + 1000)
- const result = findDominantRegion(regions, 3200, { connectZooms: true });
- expect(result.strength).toBe(1);
- expect(result.transition).not.toBeNull();
-
- // Focus should be blending between the two
- if (result.region) {
- expect(result.region.focus.cx).toBeGreaterThan(0.2);
- }
- });
-
- it("does NOT connect zooms with a large gap", () => {
- const regions: ZoomRegion[] = [
- { id: "a", startMs: 1000, endMs: 3000, depth: 2, focus: { cx: 0.2, cy: 0.2 } },
- { id: "b", startMs: 8000, endMs: 10000, depth: 3, focus: { cx: 0.8, cy: 0.8 } },
- ];
-
- // In the gap — should be no active region
- const result = findDominantRegion(regions, 5000, { connectZooms: true });
- expect(result.region).toBeNull();
- });
-
- it("holds the next region's focus between connected-transition end and next start", () => {
- const regions: ZoomRegion[] = [
- { id: "a", startMs: 1000, endMs: 3000, depth: 2, focus: { cx: 0.2, cy: 0.2 } },
- { id: "b", startMs: 4200, endMs: 7000, depth: 3, focus: { cx: 0.7, cy: 0.7 } },
- ];
-
- // After transition end (3000+1000=4000) but before b starts (4200)
- const result = findDominantRegion(regions, 4100, { connectZooms: true });
- expect(result.strength).toBe(1);
- expect(result.region).not.toBeNull();
- expect(result.region!.id).toBe("b");
- });
+ it("returns null region when no regions exist", () => {
+ const result = findDominantRegion([], 1000);
+ expect(result.region).toBeNull();
+ expect(result.strength).toBe(0);
+ });
+
+ it("returns the active region at its hold phase", () => {
+ const regions: ZoomRegion[] = [
+ { id: "a", startMs: 1000, endMs: 4000, depth: 2, focus: { cx: 0.3, cy: 0.3 } },
+ ];
+ const result = findDominantRegion(regions, 2500);
+ expect(result.region).not.toBeNull();
+ expect(result.region!.id).toBe("a");
+ expect(result.strength).toBe(1);
+ });
+
+ it("returns null region outside all regions", () => {
+ const regions: ZoomRegion[] = [
+ { id: "a", startMs: 1000, endMs: 2000, depth: 2, focus: { cx: 0.5, cy: 0.5 } },
+ ];
+ const result = findDominantRegion(regions, 10000);
+ expect(result.region).toBeNull();
+ });
+
+ it("connects chained zooms when connectZooms is true", () => {
+ const regions: ZoomRegion[] = [
+ { id: "a", startMs: 1000, endMs: 3000, depth: 2, focus: { cx: 0.2, cy: 0.2 } },
+ { id: "b", startMs: 3500, endMs: 6000, depth: 3, focus: { cx: 0.8, cy: 0.8 } },
+ ];
+
+ // During the connected transition (between a.endMs + 200 and a.endMs + 1200)
+ const result = findDominantRegion(regions, 3200, { connectZooms: true });
+ expect(result.strength).toBe(1);
+ expect(result.transition).not.toBeNull();
+
+ // Focus should be blending between the two
+ if (result.region) {
+ expect(result.region.focus.cx).toBeGreaterThan(0.2);
+ }
+ });
+
+ it("does NOT connect zooms with a large gap", () => {
+ const regions: ZoomRegion[] = [
+ { id: "a", startMs: 1000, endMs: 3000, depth: 2, focus: { cx: 0.2, cy: 0.2 } },
+ { id: "b", startMs: 8000, endMs: 10000, depth: 3, focus: { cx: 0.8, cy: 0.8 } },
+ ];
+
+ // In the gap — should be no active region
+ const result = findDominantRegion(regions, 5000, { connectZooms: true });
+ expect(result.region).toBeNull();
+ });
+
+ it("holds the next region's focus between connected-transition end and next start", () => {
+ const regions: ZoomRegion[] = [
+ { id: "a", startMs: 1000, endMs: 3000, depth: 2, focus: { cx: 0.2, cy: 0.2 } },
+ { id: "b", startMs: 4200, endMs: 7000, depth: 3, focus: { cx: 0.7, cy: 0.7 } },
+ ];
+
+ // After transition end (3000+200+1000=4200) but before b starts (4200)
+ const result = findDominantRegion(regions, 4100, { connectZooms: true });
+ expect(result.strength).toBe(1);
+ expect(result.region).not.toBeNull();
+ expect(result.region!.id).toBe("b");
+ });
});
// ---------------------------------------------------------------------------
@@ -393,40 +396,40 @@ describe("findDominantRegion", () => {
// ---------------------------------------------------------------------------
describe("ZoomRegion mode field", () => {
- it("accepts manual mode", () => {
- const r: ZoomRegion = {
- id: "m1",
- startMs: 0,
- endMs: 1000,
- depth: 2,
- focus: { cx: 0.5, cy: 0.5 },
- mode: "manual",
- };
- expect(r.mode).toBe("manual");
- });
-
- it("accepts auto mode", () => {
- const r: ZoomRegion = {
- id: "a1",
- startMs: 0,
- endMs: 1000,
- depth: 2,
- focus: { cx: 0.5, cy: 0.5 },
- mode: "auto",
- };
- expect(r.mode).toBe("auto");
- });
-
- it("mode is optional", () => {
- const r: ZoomRegion = {
- id: "x1",
- startMs: 0,
- endMs: 1000,
- depth: 2,
- focus: { cx: 0.5, cy: 0.5 },
- };
- expect(r.mode).toBeUndefined();
- });
+ it("accepts manual mode", () => {
+ const r: ZoomRegion = {
+ id: "m1",
+ startMs: 0,
+ endMs: 1000,
+ depth: 2,
+ focus: { cx: 0.5, cy: 0.5 },
+ mode: "manual",
+ };
+ expect(r.mode).toBe("manual");
+ });
+
+ it("accepts auto mode", () => {
+ const r: ZoomRegion = {
+ id: "a1",
+ startMs: 0,
+ endMs: 1000,
+ depth: 2,
+ focus: { cx: 0.5, cy: 0.5 },
+ mode: "auto",
+ };
+ expect(r.mode).toBe("auto");
+ });
+
+ it("mode is optional", () => {
+ const r: ZoomRegion = {
+ id: "x1",
+ startMs: 0,
+ endMs: 1000,
+ depth: 2,
+ focus: { cx: 0.5, cy: 0.5 },
+ };
+ expect(r.mode).toBeUndefined();
+ });
});
// ---------------------------------------------------------------------------
@@ -434,73 +437,73 @@ describe("ZoomRegion mode field", () => {
// ---------------------------------------------------------------------------
describe("spring damping regimes", () => {
- it("underdamped spring overshoots then converges", () => {
- // Low damping → expect overshoot
- const config: SpringConfig = {
- stiffness: 300,
- damping: 10,
- mass: 1,
- restDelta: 0.0005,
- restSpeed: 0.015,
- };
- const s = createSpringState();
- stepSpringValue(s, 0, 16, config);
-
- let maxValue = 0;
- for (let i = 0; i < 500; i++) {
- stepSpringValue(s, 1, 16, config);
- if (s.value > maxValue) maxValue = s.value;
- }
-
- // Should have overshot past 1 at some point
- expect(maxValue).toBeGreaterThan(1.01);
- // But should converge
- expect(s.value).toBeCloseTo(1, 1);
- });
-
- it("overdamped spring converges without overshoot", () => {
- const config: SpringConfig = {
- stiffness: 100,
- damping: 200,
- mass: 1,
- restDelta: 0.0005,
- restSpeed: 0.015,
- };
- const s = createSpringState();
- stepSpringValue(s, 0, 16, config);
-
- let maxValue = 0;
- for (let i = 0; i < 1000; i++) {
- stepSpringValue(s, 1, 16, config);
- if (s.value > maxValue) maxValue = s.value;
- }
-
- // Overdamped should not overshoot
- expect(maxValue).toBeLessThanOrEqual(1.001);
- // Should still converge (overdamped converges slowly)
- expect(s.value).toBeCloseTo(1, 2);
- });
-
- it("critically damped spring converges without oscillation", () => {
- // ζ = c / (2√(km)) = 1 → c = 2√(km)
- const stiffness = 200;
- const mass = 1;
- const criticalDamping = 2 * Math.sqrt(stiffness * mass);
-
- const config: SpringConfig = {
- stiffness,
- damping: criticalDamping,
- mass,
- restDelta: 0.0005,
- restSpeed: 0.015,
- };
- const s = createSpringState();
- stepSpringValue(s, 0, 16, config);
-
- for (let i = 0; i < 300; i++) {
- stepSpringValue(s, 1, 16, config);
- }
-
- expect(s.value).toBeCloseTo(1, 2);
- });
+ it("underdamped spring overshoots then converges", () => {
+ // Low damping → expect overshoot
+ const config: SpringConfig = {
+ stiffness: 300,
+ damping: 10,
+ mass: 1,
+ restDelta: 0.0005,
+ restSpeed: 0.015,
+ };
+ const s = createSpringState();
+ stepSpringValue(s, 0, 16, config);
+
+ let maxValue = 0;
+ for (let i = 0; i < 500; i++) {
+ stepSpringValue(s, 1, 16, config);
+ if (s.value > maxValue) maxValue = s.value;
+ }
+
+ // Should have overshot past 1 at some point
+ expect(maxValue).toBeGreaterThan(1.01);
+ // But should converge
+ expect(s.value).toBeCloseTo(1, 1);
+ });
+
+ it("overdamped spring converges without overshoot", () => {
+ const config: SpringConfig = {
+ stiffness: 100,
+ damping: 200,
+ mass: 1,
+ restDelta: 0.0005,
+ restSpeed: 0.015,
+ };
+ const s = createSpringState();
+ stepSpringValue(s, 0, 16, config);
+
+ let maxValue = 0;
+ for (let i = 0; i < 1000; i++) {
+ stepSpringValue(s, 1, 16, config);
+ if (s.value > maxValue) maxValue = s.value;
+ }
+
+ // Overdamped should not overshoot
+ expect(maxValue).toBeLessThanOrEqual(1.001);
+ // Should still converge (overdamped converges slowly)
+ expect(s.value).toBeCloseTo(1, 2);
+ });
+
+ it("critically damped spring converges without oscillation", () => {
+ // ζ = c / (2√(km)) = 1 → c = 2√(km)
+ const stiffness = 200;
+ const mass = 1;
+ const criticalDamping = 2 * Math.sqrt(stiffness * mass);
+
+ const config: SpringConfig = {
+ stiffness,
+ damping: criticalDamping,
+ mass,
+ restDelta: 0.0005,
+ restSpeed: 0.015,
+ };
+ const s = createSpringState();
+ stepSpringValue(s, 0, 16, config);
+
+ for (let i = 0; i < 300; i++) {
+ stepSpringValue(s, 1, 16, config);
+ }
+
+ expect(s.value).toBeCloseTo(1, 2);
+ });
});
diff --git a/src/components/video-editor/videoPlayback/zoomRegionUtils.ts b/src/components/video-editor/videoPlayback/zoomRegionUtils.ts
index 0bc470ee..a3cc06d5 100644
--- a/src/components/video-editor/videoPlayback/zoomRegionUtils.ts
+++ b/src/components/video-editor/videoPlayback/zoomRegionUtils.ts
@@ -1,232 +1,232 @@
import type { ZoomFocus, ZoomRegion } from "../types";
+import { ZOOM_DEPTH_SCALES } from "../types";
import {
- ZOOM_DEPTH_SCALES,
-} from "../types";
-import { TRANSITION_WINDOW_MS, ZOOM_IN_TRANSITION_WINDOW_MS, ZOOM_OUT_EARLY_START_MS } from "./constants";
+ TRANSITION_WINDOW_MS,
+ ZOOM_IN_TRANSITION_WINDOW_MS,
+ ZOOM_OUT_EARLY_START_MS,
+} from "./constants";
import { clampFocusToScale } from "./focusUtils";
import { clamp01, cubicBezier, easeOutZoom } from "./mathUtils";
const CHAINED_ZOOM_PAN_GAP_MS = 1350;
const CONNECTED_ZOOM_PAN_DURATION_MS = 1000;
-const ZOOM_IN_OVERLAP_MS = 500;
+const ZOOM_IN_OVERLAP_MS = 1000;
+const ZOOM_ANIMATION_LEAD_MS = 200;
type DominantRegionOptions = {
- connectZooms?: boolean;
+ connectZooms?: boolean;
};
type ConnectedRegionPair = {
- currentRegion: ZoomRegion;
- nextRegion: ZoomRegion;
- transitionStart: number;
- transitionEnd: number;
+ currentRegion: ZoomRegion;
+ nextRegion: ZoomRegion;
+ transitionStart: number;
+ transitionEnd: number;
};
type ConnectedPanTransition = {
- progress: number;
- startFocus: ZoomFocus;
- endFocus: ZoomFocus;
- startScale: number;
- endScale: number;
+ progress: number;
+ startFocus: ZoomFocus;
+ endFocus: ZoomFocus;
+ startScale: number;
+ endScale: number;
};
function lerp(start: number, end: number, amount: number) {
- return start + (end - start) * amount;
+ return start + (end - start) * amount;
}
function easeConnectedPan(value: number) {
- return cubicBezier(0.1, 0.0, 0.2, 1.0, value);
+ return cubicBezier(0.1, 0.0, 0.2, 1.0, value);
}
-export function computeRegionStrength(
- region: ZoomRegion,
- timeMs: number,
-) {
- const zoomInEnd = region.startMs + ZOOM_IN_OVERLAP_MS;
- const leadInStart = zoomInEnd - ZOOM_IN_TRANSITION_WINDOW_MS;
- const zoomOutStart = region.endMs - ZOOM_OUT_EARLY_START_MS;
- const leadOutEnd = zoomOutStart + TRANSITION_WINDOW_MS;
-
- if (timeMs < leadInStart || timeMs > leadOutEnd) {
- return 0;
- }
-
- if (timeMs < zoomInEnd) {
- const progress = (timeMs - leadInStart) / ZOOM_IN_TRANSITION_WINDOW_MS;
- return easeOutZoom(progress);
- }
-
- if (timeMs <= zoomOutStart) {
- return 1;
- }
-
- const progress = clamp01((timeMs - zoomOutStart) / TRANSITION_WINDOW_MS);
- return 1 - easeOutZoom(progress);
+export function computeRegionStrength(region: ZoomRegion, timeMs: number) {
+ const adjustedTimeMs = timeMs - ZOOM_ANIMATION_LEAD_MS;
+ const zoomInEnd = region.startMs + ZOOM_IN_OVERLAP_MS;
+ const leadInStart = zoomInEnd - ZOOM_IN_TRANSITION_WINDOW_MS;
+ const zoomOutStart = region.endMs - ZOOM_OUT_EARLY_START_MS;
+ const leadOutEnd = zoomOutStart + TRANSITION_WINDOW_MS;
+
+ if (adjustedTimeMs < leadInStart || adjustedTimeMs > leadOutEnd) {
+ return 0;
+ }
+
+ if (adjustedTimeMs < zoomInEnd) {
+ const progress = (adjustedTimeMs - leadInStart) / ZOOM_IN_TRANSITION_WINDOW_MS;
+ return easeOutZoom(progress);
+ }
+
+ if (adjustedTimeMs <= zoomOutStart) {
+ return 1;
+ }
+
+ const progress = clamp01((adjustedTimeMs - zoomOutStart) / TRANSITION_WINDOW_MS);
+ return 1 - easeOutZoom(progress);
}
function getLinearFocus(start: ZoomFocus, end: ZoomFocus, amount: number): ZoomFocus {
- return {
- cx: lerp(start.cx, end.cx, amount),
- cy: lerp(start.cy, end.cy, amount),
- };
+ return {
+ cx: lerp(start.cx, end.cx, amount),
+ cy: lerp(start.cy, end.cy, amount),
+ };
}
function getResolvedFocus(region: ZoomRegion, zoomScale: number): ZoomFocus {
- return clampFocusToScale(region.focus, zoomScale);
+ return clampFocusToScale(region.focus, zoomScale);
}
function getConnectedRegionPairs(regions: ZoomRegion[]) {
- const sortedRegions = [...regions].sort((a, b) => a.startMs - b.startMs);
- const pairs: ConnectedRegionPair[] = [];
-
- for (let index = 0; index < sortedRegions.length - 1; index += 1) {
- const currentRegion = sortedRegions[index];
- const nextRegion = sortedRegions[index + 1];
- const gapMs = nextRegion.startMs - currentRegion.endMs;
-
- if (gapMs > CHAINED_ZOOM_PAN_GAP_MS) {
- continue;
- }
-
- pairs.push({
- currentRegion,
- nextRegion,
- transitionStart: currentRegion.endMs,
- transitionEnd: currentRegion.endMs + CONNECTED_ZOOM_PAN_DURATION_MS,
- });
- }
-
- return pairs;
+ const sortedRegions = [...regions].sort((a, b) => a.startMs - b.startMs);
+ const pairs: ConnectedRegionPair[] = [];
+
+ for (let index = 0; index < sortedRegions.length - 1; index += 1) {
+ const currentRegion = sortedRegions[index];
+ const nextRegion = sortedRegions[index + 1];
+ const gapMs = nextRegion.startMs - currentRegion.endMs;
+
+ if (gapMs > CHAINED_ZOOM_PAN_GAP_MS) {
+ continue;
+ }
+
+ pairs.push({
+ currentRegion,
+ nextRegion,
+ transitionStart: currentRegion.endMs + ZOOM_ANIMATION_LEAD_MS,
+ transitionEnd:
+ currentRegion.endMs + ZOOM_ANIMATION_LEAD_MS + CONNECTED_ZOOM_PAN_DURATION_MS,
+ });
+ }
+
+ return pairs;
}
function getActiveRegion(
- regions: ZoomRegion[],
- timeMs: number,
- connectedPairs: ConnectedRegionPair[],
+ regions: ZoomRegion[],
+ timeMs: number,
+ connectedPairs: ConnectedRegionPair[],
) {
- const activeRegions = regions
- .map((region) => {
- const outgoingPair = connectedPairs.find((pair) => pair.currentRegion.id === region.id);
- if (outgoingPair && timeMs > outgoingPair.currentRegion.endMs) {
- return { region, strength: 0 };
- }
-
- const incomingPair = connectedPairs.find((pair) => pair.nextRegion.id === region.id);
- if (incomingPair && timeMs < incomingPair.transitionEnd) {
- return { region, strength: 0 };
- }
-
- return { region, strength: computeRegionStrength(region, timeMs) };
- })
- .filter((entry) => entry.strength > 0)
- .sort((left, right) => {
- if (right.strength !== left.strength) {
- return right.strength - left.strength;
- }
-
- return right.region.startMs - left.region.startMs;
- });
-
- if (activeRegions.length === 0) {
- return null;
- }
-
- const activeRegion = activeRegions[0].region;
- const activeScale = ZOOM_DEPTH_SCALES[activeRegion.depth];
-
- return {
- region: {
- ...activeRegion,
- focus: getResolvedFocus(activeRegion, activeScale),
- },
- strength: activeRegions[0].strength,
- blendedScale: null,
- };
+ const activeRegions = regions
+ .map((region) => {
+ const outgoingPair = connectedPairs.find((pair) => pair.currentRegion.id === region.id);
+ if (outgoingPair && timeMs > outgoingPair.currentRegion.endMs) {
+ return { region, strength: 0 };
+ }
+
+ const incomingPair = connectedPairs.find((pair) => pair.nextRegion.id === region.id);
+ if (incomingPair && timeMs < incomingPair.transitionEnd) {
+ return { region, strength: 0 };
+ }
+
+ return { region, strength: computeRegionStrength(region, timeMs) };
+ })
+ .filter((entry) => entry.strength > 0)
+ .sort((left, right) => {
+ if (right.strength !== left.strength) {
+ return right.strength - left.strength;
+ }
+
+ return right.region.startMs - left.region.startMs;
+ });
+
+ if (activeRegions.length === 0) {
+ return null;
+ }
+
+ const activeRegion = activeRegions[0].region;
+ const activeScale = ZOOM_DEPTH_SCALES[activeRegion.depth];
+
+ return {
+ region: {
+ ...activeRegion,
+ focus: getResolvedFocus(activeRegion, activeScale),
+ },
+ strength: activeRegions[0].strength,
+ blendedScale: null,
+ };
}
function getConnectedRegionHold(timeMs: number, connectedPairs: ConnectedRegionPair[]) {
- for (const pair of connectedPairs) {
- if (timeMs > pair.transitionEnd && timeMs < pair.nextRegion.startMs) {
- const nextScale = ZOOM_DEPTH_SCALES[pair.nextRegion.depth];
- return {
- region: {
- ...pair.nextRegion,
- focus: getResolvedFocus(pair.nextRegion, nextScale),
- },
- strength: 1,
- blendedScale: null,
- };
- }
- }
-
- return null;
+ for (const pair of connectedPairs) {
+ if (timeMs > pair.transitionEnd && timeMs < pair.nextRegion.startMs) {
+ const nextScale = ZOOM_DEPTH_SCALES[pair.nextRegion.depth];
+ return {
+ region: {
+ ...pair.nextRegion,
+ focus: getResolvedFocus(pair.nextRegion, nextScale),
+ },
+ strength: 1,
+ blendedScale: null,
+ };
+ }
+ }
+
+ return null;
}
-function getConnectedRegionTransition(
- connectedPairs: ConnectedRegionPair[],
- timeMs: number,
-) {
- for (const pair of connectedPairs) {
- const { currentRegion, nextRegion, transitionStart, transitionEnd } = pair;
-
- if (timeMs < transitionStart || timeMs > transitionEnd) {
- continue;
- }
-
- const transitionProgress = easeConnectedPan(
- clamp01((timeMs - transitionStart) / Math.max(1, transitionEnd - transitionStart)),
- );
- const currentScale = ZOOM_DEPTH_SCALES[currentRegion.depth];
- const nextScale = ZOOM_DEPTH_SCALES[nextRegion.depth];
- const transitionScale = lerp(currentScale, nextScale, transitionProgress);
- const currentFocus = getResolvedFocus(currentRegion, currentScale);
- const nextFocus = getResolvedFocus(nextRegion, nextScale);
- const transitionFocus = getLinearFocus(currentFocus, nextFocus, transitionProgress);
-
- return {
- region: {
- ...nextRegion,
- focus: transitionFocus,
- },
- strength: 1,
- blendedScale: transitionScale,
- transition: {
- progress: transitionProgress,
- startFocus: currentFocus,
- endFocus: nextFocus,
- startScale: currentScale,
- endScale: nextScale,
- },
- };
- }
-
- return null;
+function getConnectedRegionTransition(connectedPairs: ConnectedRegionPair[], timeMs: number) {
+ for (const pair of connectedPairs) {
+ const { currentRegion, nextRegion, transitionStart, transitionEnd } = pair;
+
+ if (timeMs < transitionStart || timeMs > transitionEnd) {
+ continue;
+ }
+
+ const transitionProgress = easeConnectedPan(
+ clamp01((timeMs - transitionStart) / Math.max(1, transitionEnd - transitionStart)),
+ );
+ const currentScale = ZOOM_DEPTH_SCALES[currentRegion.depth];
+ const nextScale = ZOOM_DEPTH_SCALES[nextRegion.depth];
+ const transitionScale = lerp(currentScale, nextScale, transitionProgress);
+ const currentFocus = getResolvedFocus(currentRegion, currentScale);
+ const nextFocus = getResolvedFocus(nextRegion, nextScale);
+ const transitionFocus = getLinearFocus(currentFocus, nextFocus, transitionProgress);
+
+ return {
+ region: {
+ ...nextRegion,
+ focus: transitionFocus,
+ },
+ strength: 1,
+ blendedScale: transitionScale,
+ transition: {
+ progress: transitionProgress,
+ startFocus: currentFocus,
+ endFocus: nextFocus,
+ startScale: currentScale,
+ endScale: nextScale,
+ },
+ };
+ }
+
+ return null;
}
-export function findDominantRegion(regions: ZoomRegion[], timeMs: number, options: DominantRegionOptions = {}): {
- region: ZoomRegion | null;
- strength: number;
- blendedScale: number | null;
- transition: ConnectedPanTransition | null;
+export function findDominantRegion(
+ regions: ZoomRegion[],
+ timeMs: number,
+ options: DominantRegionOptions = {},
+): {
+ region: ZoomRegion | null;
+ strength: number;
+ blendedScale: number | null;
+ transition: ConnectedPanTransition | null;
} {
- const connectedPairs = options.connectZooms
- ? getConnectedRegionPairs(regions)
- : [];
-
- if (options.connectZooms) {
- const connectedTransition = getConnectedRegionTransition(connectedPairs, timeMs);
- if (connectedTransition) {
- return connectedTransition;
- }
-
- const connectedHold = getConnectedRegionHold(timeMs, connectedPairs);
- if (connectedHold) {
- return { ...connectedHold, transition: null };
- }
- }
-
- const activeRegion = getActiveRegion(regions, timeMs, connectedPairs);
- return activeRegion
- ? { ...activeRegion, transition: null }
- : { region: null, strength: 0, blendedScale: null, transition: null };
+ const connectedPairs = options.connectZooms ? getConnectedRegionPairs(regions) : [];
+
+ if (options.connectZooms) {
+ const connectedTransition = getConnectedRegionTransition(connectedPairs, timeMs);
+ if (connectedTransition) {
+ return connectedTransition;
+ }
+
+ const connectedHold = getConnectedRegionHold(timeMs, connectedPairs);
+ if (connectedHold) {
+ return { ...connectedHold, transition: null };
+ }
+ }
+
+ const activeRegion = getActiveRegion(regions, timeMs, connectedPairs);
+ return activeRegion
+ ? { ...activeRegion, transition: null }
+ : { region: null, strength: 0, blendedScale: null, transition: null };
}
-
diff --git a/src/components/video-editor/videoPlayback/zoomTransform.ts b/src/components/video-editor/videoPlayback/zoomTransform.ts
index a6efe92c..9bcd3b4d 100644
--- a/src/components/video-editor/videoPlayback/zoomTransform.ts
+++ b/src/components/video-editor/videoPlayback/zoomTransform.ts
@@ -1,239 +1,246 @@
-import { Container, BlurFilter } from 'pixi.js';
-import { MotionBlurFilter } from 'pixi-filters/motion-blur';
+import { BlurFilter, Container } from "pixi.js";
+import { MotionBlurFilter } from "pixi-filters/motion-blur";
const PEAK_VELOCITY_PPS = 2000;
const MAX_BLUR_PX = 8;
const VELOCITY_THRESHOLD_PPS = 15;
export interface MotionBlurState {
- lastFrameTimeMs: number;
- prevCamX: number;
- prevCamY: number;
- prevCamScale: number;
- initialized: boolean;
+ lastFrameTimeMs: number;
+ prevCamX: number;
+ prevCamY: number;
+ prevCamScale: number;
+ initialized: boolean;
}
export function createMotionBlurState(): MotionBlurState {
- return {
- lastFrameTimeMs: 0,
- prevCamX: 0,
- prevCamY: 0,
- prevCamScale: 1,
- initialized: false,
- };
+ return {
+ lastFrameTimeMs: 0,
+ prevCamX: 0,
+ prevCamY: 0,
+ prevCamScale: 1,
+ initialized: false,
+ };
}
interface TransformParams {
- cameraContainer: Container;
- blurFilter: BlurFilter | null;
- motionBlurFilter?: MotionBlurFilter | null;
- stageSize: { width: number; height: number };
- baseMask: { x: number; y: number; width: number; height: number };
- zoomScale: number;
- zoomProgress?: number;
- focusX: number;
- focusY: number;
- motionIntensity: number;
- motionVector?: { x: number; y: number };
- isPlaying: boolean;
- motionBlurAmount?: number;
- transformOverride?: AppliedTransform;
- motionBlurState?: MotionBlurState;
- frameTimeMs?: number;
+ cameraContainer: Container;
+ blurFilter: BlurFilter | null;
+ motionBlurFilter?: MotionBlurFilter | null;
+ stageSize: { width: number; height: number };
+ baseMask: { x: number; y: number; width: number; height: number };
+ zoomScale: number;
+ zoomProgress?: number;
+ focusX: number;
+ focusY: number;
+ motionIntensity: number;
+ motionVector?: { x: number; y: number };
+ isPlaying: boolean;
+ motionBlurAmount?: number;
+ transformOverride?: AppliedTransform;
+ motionBlurState?: MotionBlurState;
+ frameTimeMs?: number;
}
interface AppliedTransform {
- scale: number;
- x: number;
- y: number;
+ scale: number;
+ x: number;
+ y: number;
}
interface FocusFromTransformGeometry {
- stageSize: { width: number; height: number };
- baseMask: { x: number; y: number; width: number; height: number };
- zoomScale: number;
- x: number;
- y: number;
+ stageSize: { width: number; height: number };
+ baseMask: { x: number; y: number; width: number; height: number };
+ zoomScale: number;
+ x: number;
+ y: number;
}
interface ZoomTransformGeometry {
- stageSize: { width: number; height: number };
- baseMask: { x: number; y: number; width: number; height: number };
- zoomScale: number;
- zoomProgress?: number;
- focusX: number;
- focusY: number;
+ stageSize: { width: number; height: number };
+ baseMask: { x: number; y: number; width: number; height: number };
+ zoomScale: number;
+ zoomProgress?: number;
+ focusX: number;
+ focusY: number;
}
export function computeZoomTransform({
- stageSize,
- baseMask,
- zoomScale,
- zoomProgress = 1,
- focusX,
- focusY,
+ stageSize,
+ baseMask,
+ zoomScale,
+ zoomProgress = 1,
+ focusX,
+ focusY,
}: ZoomTransformGeometry): AppliedTransform {
- if (
- stageSize.width <= 0 ||
- stageSize.height <= 0 ||
- baseMask.width <= 0 ||
- baseMask.height <= 0
- ) {
- return { scale: 1, x: 0, y: 0 };
- }
-
- const progress = Math.min(1, Math.max(0, zoomProgress));
- const focusStagePxX = baseMask.x + focusX * baseMask.width;
- const focusStagePxY = baseMask.y + focusY * baseMask.height;
- const stageCenterX = stageSize.width / 2;
- const stageCenterY = stageSize.height / 2;
- const scale = 1 + (zoomScale - 1) * progress;
- const finalX = stageCenterX - focusStagePxX * zoomScale;
- const finalY = stageCenterY - focusStagePxY * zoomScale;
-
- return {
- scale,
- x: finalX * progress,
- y: finalY * progress,
- };
+ if (
+ stageSize.width <= 0 ||
+ stageSize.height <= 0 ||
+ baseMask.width <= 0 ||
+ baseMask.height <= 0
+ ) {
+ return { scale: 1, x: 0, y: 0 };
+ }
+
+ const progress = Math.min(1, Math.max(0, zoomProgress));
+ const focusStagePxX = baseMask.x + focusX * baseMask.width;
+ const focusStagePxY = baseMask.y + focusY * baseMask.height;
+ const stageCenterX = stageSize.width / 2;
+ const stageCenterY = stageSize.height / 2;
+ const scale = 1 + (zoomScale - 1) * progress;
+ const finalX = stageCenterX - focusStagePxX * zoomScale;
+ const finalY = stageCenterY - focusStagePxY * zoomScale;
+
+ return {
+ scale,
+ x: finalX * progress,
+ y: finalY * progress,
+ };
}
export function computeFocusFromTransform({
- stageSize,
- baseMask,
- zoomScale,
- x,
- y,
+ stageSize,
+ baseMask,
+ zoomScale,
+ x,
+ y,
}: FocusFromTransformGeometry) {
- if (
- stageSize.width <= 0 ||
- stageSize.height <= 0 ||
- baseMask.width <= 0 ||
- baseMask.height <= 0 ||
- zoomScale <= 0
- ) {
- return { cx: 0.5, cy: 0.5 };
- }
-
- const stageCenterX = stageSize.width / 2;
- const stageCenterY = stageSize.height / 2;
- const focusStagePxX = (stageCenterX - x) / zoomScale;
- const focusStagePxY = (stageCenterY - y) / zoomScale;
-
- return {
- cx: (focusStagePxX - baseMask.x) / baseMask.width,
- cy: (focusStagePxY - baseMask.y) / baseMask.height,
- };
+ if (
+ stageSize.width <= 0 ||
+ stageSize.height <= 0 ||
+ baseMask.width <= 0 ||
+ baseMask.height <= 0 ||
+ zoomScale <= 0
+ ) {
+ return { cx: 0.5, cy: 0.5 };
+ }
+
+ const stageCenterX = stageSize.width / 2;
+ const stageCenterY = stageSize.height / 2;
+ const focusStagePxX = (stageCenterX - x) / zoomScale;
+ const focusStagePxY = (stageCenterY - y) / zoomScale;
+
+ return {
+ cx: (focusStagePxX - baseMask.x) / baseMask.width,
+ cy: (focusStagePxY - baseMask.y) / baseMask.height,
+ };
}
export function applyZoomTransform({
- cameraContainer,
- blurFilter,
- motionBlurFilter,
- stageSize,
- baseMask,
- zoomScale,
- zoomProgress = 1,
- focusX,
- focusY,
- motionIntensity: _motionIntensity,
- motionVector: _motionVector,
- isPlaying,
- motionBlurAmount = 0,
- transformOverride,
- motionBlurState,
- frameTimeMs,
+ cameraContainer,
+ blurFilter,
+ motionBlurFilter,
+ stageSize,
+ baseMask,
+ zoomScale,
+ zoomProgress = 1,
+ focusX,
+ focusY,
+ motionIntensity: _motionIntensity,
+ motionVector: _motionVector,
+ isPlaying,
+ motionBlurAmount = 0,
+ transformOverride,
+ motionBlurState,
+ frameTimeMs,
}: TransformParams): AppliedTransform {
- if (
- stageSize.width <= 0 ||
- stageSize.height <= 0 ||
- baseMask.width <= 0 ||
- baseMask.height <= 0
- ) {
- return { scale: 1, x: 0, y: 0 };
- }
-
- const transform = transformOverride ?? computeZoomTransform({
- stageSize,
- baseMask,
- zoomScale,
- zoomProgress,
- focusX,
- focusY,
- });
-
- // Apply position & scale to camera container
- cameraContainer.scale.set(transform.scale);
- cameraContainer.position.set(transform.x, transform.y);
-
- if (motionBlurState && motionBlurFilter && motionBlurAmount > 0 && isPlaying) {
- const now = frameTimeMs ?? performance.now();
-
- if (!motionBlurState.initialized) {
- motionBlurState.prevCamX = transform.x;
- motionBlurState.prevCamY = transform.y;
- motionBlurState.prevCamScale = transform.scale;
- motionBlurState.lastFrameTimeMs = now;
- motionBlurState.initialized = true;
- motionBlurFilter.velocity = { x: 0, y: 0 };
- motionBlurFilter.kernelSize = 5;
- motionBlurFilter.offset = 0;
- if (blurFilter) blurFilter.blur = 0;
- } else {
- const dtMs = Math.min(80, Math.max(1, now - motionBlurState.lastFrameTimeMs));
- const dtSeconds = dtMs / 1000;
- motionBlurState.lastFrameTimeMs = now;
-
- // Camera displacement this frame (stage-px)
- const dx = transform.x - motionBlurState.prevCamX;
- const dy = transform.y - motionBlurState.prevCamY;
- const dScale = transform.scale - motionBlurState.prevCamScale;
-
- motionBlurState.prevCamX = transform.x;
- motionBlurState.prevCamY = transform.y;
- motionBlurState.prevCamScale = transform.scale;
-
- // Velocity in px/s (translation + scale-change contribution)
- const velocityX = dx / dtSeconds;
- const velocityY = dy / dtSeconds;
- const scaleVelocity = Math.abs(dScale / dtSeconds) * Math.max(stageSize.width, stageSize.height) * 0.5;
- const speed = Math.sqrt(velocityX * velocityX + velocityY * velocityY) + scaleVelocity;
-
- const normalised = Math.min(1, speed / PEAK_VELOCITY_PPS);
- const targetBlur = speed < VELOCITY_THRESHOLD_PPS
- ? 0
- : normalised * normalised * MAX_BLUR_PX * motionBlurAmount;
-
- const dirMag = Math.sqrt(velocityX * velocityX + velocityY * velocityY) || 1;
- const velocityScale = targetBlur * 1.2;
- motionBlurFilter.velocity = targetBlur > 0
- ? { x: (velocityX / dirMag) * velocityScale, y: (velocityY / dirMag) * velocityScale }
- : { x: 0, y: 0 };
- motionBlurFilter.kernelSize = targetBlur > 4 ? 11 : targetBlur > 1.5 ? 9 : 5;
- motionBlurFilter.offset = targetBlur > 0.5 ? -0.2 : 0;
-
- if (blurFilter) {
- blurFilter.blur = 0;
- }
- }
- } else {
- if (motionBlurFilter) {
- motionBlurFilter.velocity = { x: 0, y: 0 };
- motionBlurFilter.kernelSize = 5;
- motionBlurFilter.offset = 0;
- }
- if (blurFilter) {
- blurFilter.blur = 0;
- }
- if (motionBlurState) {
- motionBlurState.initialized = false;
- }
- }
-
- return {
- scale: transform.scale,
- x: transform.x,
- y: transform.y,
- };
+ if (
+ stageSize.width <= 0 ||
+ stageSize.height <= 0 ||
+ baseMask.width <= 0 ||
+ baseMask.height <= 0
+ ) {
+ return { scale: 1, x: 0, y: 0 };
+ }
+
+ const transform =
+ transformOverride ??
+ computeZoomTransform({
+ stageSize,
+ baseMask,
+ zoomScale,
+ zoomProgress,
+ focusX,
+ focusY,
+ });
+
+ // Apply position & scale to camera container
+ cameraContainer.scale.set(transform.scale);
+ cameraContainer.position.set(transform.x, transform.y);
+
+ if (motionBlurState && motionBlurFilter && motionBlurAmount > 0 && isPlaying) {
+ const now = frameTimeMs ?? performance.now();
+
+ if (!motionBlurState.initialized) {
+ motionBlurState.prevCamX = transform.x;
+ motionBlurState.prevCamY = transform.y;
+ motionBlurState.prevCamScale = transform.scale;
+ motionBlurState.lastFrameTimeMs = now;
+ motionBlurState.initialized = true;
+ motionBlurFilter.velocity = { x: 0, y: 0 };
+ motionBlurFilter.kernelSize = 5;
+ motionBlurFilter.offset = 0;
+ if (blurFilter) blurFilter.blur = 0;
+ } else {
+ const dtMs = Math.min(80, Math.max(1, now - motionBlurState.lastFrameTimeMs));
+ const dtSeconds = dtMs / 1000;
+ motionBlurState.lastFrameTimeMs = now;
+
+ // Camera displacement this frame (stage-px)
+ const dx = transform.x - motionBlurState.prevCamX;
+ const dy = transform.y - motionBlurState.prevCamY;
+ const dScale = transform.scale - motionBlurState.prevCamScale;
+
+ motionBlurState.prevCamX = transform.x;
+ motionBlurState.prevCamY = transform.y;
+ motionBlurState.prevCamScale = transform.scale;
+
+ // Velocity in px/s (translation + scale-change contribution)
+ const velocityX = dx / dtSeconds;
+ const velocityY = dy / dtSeconds;
+ const scaleVelocity =
+ Math.abs(dScale / dtSeconds) * Math.max(stageSize.width, stageSize.height) * 0.5;
+ const speed = Math.sqrt(velocityX * velocityX + velocityY * velocityY) + scaleVelocity;
+
+ const normalised = Math.min(1, speed / PEAK_VELOCITY_PPS);
+ const targetBlur =
+ speed < VELOCITY_THRESHOLD_PPS
+ ? 0
+ : normalised * normalised * MAX_BLUR_PX * motionBlurAmount;
+
+ const dirMag = Math.sqrt(velocityX * velocityX + velocityY * velocityY) || 1;
+ const velocityScale = targetBlur * 1.2;
+ motionBlurFilter.velocity =
+ targetBlur > 0
+ ? {
+ x: (velocityX / dirMag) * velocityScale,
+ y: (velocityY / dirMag) * velocityScale,
+ }
+ : { x: 0, y: 0 };
+ motionBlurFilter.kernelSize = targetBlur > 4 ? 11 : targetBlur > 1.5 ? 9 : 5;
+ motionBlurFilter.offset = targetBlur > 0.5 ? -0.2 : 0;
+
+ if (blurFilter) {
+ blurFilter.blur = 0;
+ }
+ }
+ } else {
+ if (motionBlurFilter) {
+ motionBlurFilter.velocity = { x: 0, y: 0 };
+ motionBlurFilter.kernelSize = 5;
+ motionBlurFilter.offset = 0;
+ }
+ if (blurFilter) {
+ blurFilter.blur = 0;
+ }
+ if (motionBlurState) {
+ motionBlurState.initialized = false;
+ }
+ }
+
+ return {
+ scale: transform.scale,
+ x: transform.x,
+ y: transform.y,
+ };
}
-
diff --git a/src/hooks/useAudioLevelMeter.ts b/src/hooks/useAudioLevelMeter.ts
index 98dbeb1d..9ad26995 100644
--- a/src/hooks/useAudioLevelMeter.ts
+++ b/src/hooks/useAudioLevelMeter.ts
@@ -1,106 +1,106 @@
-import { useEffect, useRef, useState } from 'react'
+import { useEffect, useRef, useState } from "react";
export interface AudioLevelMeterOptions {
- enabled: boolean
- deviceId?: string
- smoothingFactor?: number
+ enabled: boolean;
+ deviceId?: string;
+ smoothingFactor?: number;
}
export function useAudioLevelMeter(options: AudioLevelMeterOptions) {
- const [level, setLevel] = useState(0)
- const audioContextRef = useRef<AudioContext | null>(null)
- const analyserRef = useRef<AnalyserNode | null>(null)
- const streamRef = useRef<MediaStream | null>(null)
- const animationFrameRef = useRef<number | null>(null)
-
- useEffect(() => {
- const cleanup = () => {
- if (animationFrameRef.current) {
- cancelAnimationFrame(animationFrameRef.current)
- animationFrameRef.current = null
- }
- if (streamRef.current) {
- streamRef.current.getTracks().forEach((track) => track.stop())
- streamRef.current = null
- }
- if (audioContextRef.current) {
- audioContextRef.current.close().catch(() => {})
- audioContextRef.current = null
- }
- analyserRef.current = null
- }
-
- if (!options.enabled) {
- cleanup()
- setLevel(0)
- return cleanup
- }
-
- let mounted = true
-
- const startMonitoring = async () => {
- try {
- const constraints: MediaStreamConstraints = {
- audio: options.deviceId ? { deviceId: { exact: options.deviceId } } : true,
- video: false,
- }
-
- const stream = await navigator.mediaDevices.getUserMedia(constraints)
- if (!mounted) {
- stream.getTracks().forEach((track) => track.stop())
- return
- }
-
- streamRef.current = stream
-
- const audioContext = new AudioContext()
- if (audioContext.state === 'suspended') {
- await audioContext.resume()
- }
- audioContextRef.current = audioContext
-
- const analyser = audioContext.createAnalyser()
- analyser.fftSize = 256
- analyser.smoothingTimeConstant = options.smoothingFactor ?? 0.8
- analyserRef.current = analyser
-
- const source = audioContext.createMediaStreamSource(stream)
- source.connect(analyser)
-
- const dataArray = new Uint8Array(analyser.frequencyBinCount)
-
- const updateLevel = () => {
- if (!mounted || !analyserRef.current) return
-
- analyser.getByteFrequencyData(dataArray)
-
- let sum = 0
- for (let index = 0; index < dataArray.length; index++) {
- sum += dataArray[index] * dataArray[index]
- }
-
- const rms = Math.sqrt(sum / dataArray.length)
- const normalizedLevel = Math.min(100, (rms / 255) * 100 * 2)
- setLevel(normalizedLevel)
- animationFrameRef.current = requestAnimationFrame(updateLevel)
- }
-
- updateLevel()
- } catch (error) {
- console.error('Error starting audio level monitoring:', error)
- if (mounted) {
- setLevel(0)
- }
- }
- }
-
- void startMonitoring()
-
- return () => {
- mounted = false
- cleanup()
- }
- }, [options.deviceId, options.enabled, options.smoothingFactor])
-
- return { level }
+ const [level, setLevel] = useState(0);
+ const audioContextRef = useRef<AudioContext | null>(null);
+ const analyserRef = useRef<AnalyserNode | null>(null);
+ const streamRef = useRef<MediaStream | null>(null);
+ const animationFrameRef = useRef<number | null>(null);
+
+ useEffect(() => {
+ const cleanup = () => {
+ if (animationFrameRef.current) {
+ cancelAnimationFrame(animationFrameRef.current);
+ animationFrameRef.current = null;
+ }
+ if (streamRef.current) {
+ streamRef.current.getTracks().forEach((track) => track.stop());
+ streamRef.current = null;
+ }
+ if (audioContextRef.current) {
+ audioContextRef.current.close().catch(() => undefined);
+ audioContextRef.current = null;
+ }
+ analyserRef.current = null;
+ };
+
+ if (!options.enabled) {
+ cleanup();
+ setLevel(0);
+ return cleanup;
+ }
+
+ let mounted = true;
+
+ const startMonitoring = async () => {
+ try {
+ const constraints: MediaStreamConstraints = {
+ audio: options.deviceId ? { deviceId: { exact: options.deviceId } } : true,
+ video: false,
+ };
+
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
+ if (!mounted) {
+ stream.getTracks().forEach((track) => track.stop());
+ return;
+ }
+
+ streamRef.current = stream;
+
+ const audioContext = new AudioContext();
+ if (audioContext.state === "suspended") {
+ await audioContext.resume();
+ }
+ audioContextRef.current = audioContext;
+
+ const analyser = audioContext.createAnalyser();
+ analyser.fftSize = 256;
+ analyser.smoothingTimeConstant = options.smoothingFactor ?? 0.8;
+ analyserRef.current = analyser;
+
+ const source = audioContext.createMediaStreamSource(stream);
+ source.connect(analyser);
+
+ const dataArray = new Uint8Array(analyser.frequencyBinCount);
+
+ const updateLevel = () => {
+ if (!mounted || !analyserRef.current) return;
+
+ analyser.getByteFrequencyData(dataArray);
+
+ let sum = 0;
+ for (let index = 0; index < dataArray.length; index++) {
+ sum += dataArray[index] * dataArray[index];
+ }
+
+ const rms = Math.sqrt(sum / dataArray.length);
+ const normalizedLevel = Math.min(100, (rms / 255) * 100 * 2);
+ setLevel(normalizedLevel);
+ animationFrameRef.current = requestAnimationFrame(updateLevel);
+ };
+
+ updateLevel();
+ } catch (error) {
+ console.error("Error starting audio level monitoring:", error);
+ if (mounted) {
+ setLevel(0);
+ }
+ }
+ };
+
+ void startMonitoring();
+
+ return () => {
+ mounted = false;
+ cleanup();
+ };
+ }, [options.deviceId, options.enabled, options.smoothingFactor]);
+
+ return { level };
}
diff --git a/src/hooks/useMicrophoneDevices.ts b/src/hooks/useMicrophoneDevices.ts
index f47710cf..0d251b6c 100644
--- a/src/hooks/useMicrophoneDevices.ts
+++ b/src/hooks/useMicrophoneDevices.ts
@@ -57,7 +57,11 @@ export function useMicrophoneDevices(enabled: boolean = true, preferredDeviceId?
setDevices(audioInputs);
setSelectedDeviceId((currentDeviceId) => {
const normalizedPreferredDeviceId = preferredDeviceId ?? "default";
- if (audioInputs.some((device) => device.deviceId === normalizedPreferredDeviceId)) {
+ if (
+ audioInputs.some(
+ (device) => device.deviceId === normalizedPreferredDeviceId,
+ )
+ ) {
return normalizedPreferredDeviceId;
}
@@ -79,7 +83,9 @@ export function useMicrophoneDevices(enabled: boolean = true, preferredDeviceId?
} catch (error) {
if (mounted) {
const message =
- error instanceof Error ? error.message : "Failed to enumerate audio devices";
+ error instanceof Error
+ ? error.message
+ : "Failed to enumerate audio devices";
setError(message);
setIsLoading(false);
console.error("Error loading microphone devices:", error);
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index b86ca81d..84ece28c 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -34,1264 +34,1352 @@ const WEBCAM_FRAME_RATE = 30;
const WEBCAM_SUFFIX = "-webcam";
type PauseSegment = {
- startMs: number;
- endMs: number;
+ startMs: number;
+ endMs: number;
+};
+
+type DesktopCaptureMediaDevices = {
+ getUserMedia: (constraints: unknown) => Promise<MediaStream>;
+ getDisplayMedia: (constraints: unknown) => Promise<MediaStream>;
};
type UseScreenRecorderReturn = {
- recording: boolean;
- paused: boolean;
- countdownActive: boolean;
- toggleRecording: () => void;
- pauseRecording: () => void;
- resumeRecording: () => void;
- cancelRecording: () => void;
- preparePermissions: (options?: { startup?: boolean }) => Promise<boolean>;
- isMacOS: boolean;
- microphoneEnabled: boolean;
- setMicrophoneEnabled: (enabled: boolean) => void;
- microphoneDeviceId: string | undefined;
- setMicrophoneDeviceId: (deviceId: string | undefined) => void;
- systemAudioEnabled: boolean;
- setSystemAudioEnabled: (enabled: boolean) => void;
- webcamEnabled: boolean;
- setWebcamEnabled: (enabled: boolean) => void;
- webcamDeviceId: string | undefined;
- setWebcamDeviceId: (deviceId: string | undefined) => void;
- countdownDelay: number;
- setCountdownDelay: (delay: number) => void;
+ recording: boolean;
+ paused: boolean;
+ countdownActive: boolean;
+ toggleRecording: () => void;
+ pauseRecording: () => void;
+ resumeRecording: () => void;
+ cancelRecording: () => void;
+ preparePermissions: (options?: { startup?: boolean }) => Promise<boolean>;
+ isMacOS: boolean;
+ microphoneEnabled: boolean;
+ setMicrophoneEnabled: (enabled: boolean) => void;
+ microphoneDeviceId: string | undefined;
+ setMicrophoneDeviceId: (deviceId: string | undefined) => void;
+ systemAudioEnabled: boolean;
+ setSystemAudioEnabled: (enabled: boolean) => void;
+ webcamEnabled: boolean;
+ setWebcamEnabled: (enabled: boolean) => void;
+ webcamDeviceId: string | undefined;
+ setWebcamDeviceId: (deviceId: string | undefined) => void;
+ countdownDelay: number;
+ setCountdownDelay: (delay: number) => void;
};
export function useScreenRecorder(): UseScreenRecorderReturn {
- const [recording, setRecording] = useState(false);
- const [paused, setPaused] = useState(false);
- const [starting, setStarting] = useState(false);
- const [countdownActive, setCountdownActive] = useState(false);
- const [isMacOS, setIsMacOS] = useState(false);
- const [microphoneEnabled, setMicrophoneEnabled] = useState(false);
- const [microphoneDeviceId, setMicrophoneDeviceId] = useState<string | undefined>(undefined);
- const [systemAudioEnabled, setSystemAudioEnabled] = useState(false);
- const [webcamEnabled, setWebcamEnabled] = useState(false);
- const [webcamDeviceId, setWebcamDeviceId] = useState<string | undefined>(undefined);
- const [countdownDelay, setCountdownDelayState] = useState(3);
- const mediaRecorder = useRef<MediaRecorder | null>(null);
- const webcamRecorder = useRef<MediaRecorder | null>(null);
- const stream = useRef<MediaStream | null>(null);
- const screenStream = useRef<MediaStream | null>(null);
- const microphoneStream = useRef<MediaStream | null>(null);
- const webcamStream = useRef<MediaStream | null>(null);
- const mixingContext = useRef<AudioContext | null>(null);
- const chunks = useRef<Blob[]>([]);
- const webcamChunks = useRef<Blob[]>([]);
- const startTime = useRef<number>(0);
- const webcamStartTime = useRef<number | null>(null);
- const webcamTimeOffsetMs = useRef(0);
- const recordingSessionTimestamp = useRef<number | null>(null);
- const nativeScreenRecording = useRef(false);
- const nativeWindowsRecording = useRef(false);
- const startInFlight = useRef(false);
- const hasPromptedForReselect = useRef(false);
- const hasShownNativeWindowsFallbackToast = useRef(false);
- const countdownDelayLoaded = useRef(false);
- const recordingPrefsLoaded = useRef(false);
- const pendingWebcamPathPromise = useRef<Promise<string | null> | null>(null);
- const webcamStopPromise = useRef<Promise<string | null> | null>(null);
- const webcamStopResolver = useRef<((path: string | null) => void) | null>(null);
- const resolvedWebcamPath = useRef<string | null>(null);
- const accumulatedPausedDurationMs = useRef(0);
- const pauseStartedAtMs = useRef<number | null>(null);
- const pauseSegmentsRef = useRef<PauseSegment[]>([]);
- const recordingFinalizationToastId = useRef<string | number | null>(null);
- const micFallbackRecorder = useRef<MediaRecorder | null>(null);
- const micFallbackChunks = useRef<Blob[]>([]);
-
- const showRecordingFinalizationToast = useCallback((message = "Preparing recording...") => {
- recordingFinalizationToastId.current = toast.loading(message, {
- id: recordingFinalizationToastId.current ?? undefined,
- duration: Number.POSITIVE_INFINITY,
- });
- }, []);
-
- const clearRecordingFinalizationToast = useCallback(() => {
- const toastId = recordingFinalizationToastId.current;
- if (toastId === null) {
- return;
- }
-
- toast.dismiss(toastId);
- recordingFinalizationToastId.current = null;
- }, []);
-
- const notifyRecordingFinalizationFailure = useCallback(
- async (message: string) => {
- clearRecordingFinalizationToast();
- toast.error(message, { duration: 10000 });
- },
- [clearRecordingFinalizationToast],
- );
-
- const logNativeCaptureDiagnostics = useCallback(async (context: string) => {
- if (typeof window.electronAPI?.getLastNativeCaptureDiagnostics !== "function") {
- return;
- }
-
- try {
- const result = await window.electronAPI.getLastNativeCaptureDiagnostics();
- if (result.success && result.diagnostics) {
- console.warn(`[NativeCaptureDiagnostics:${context}]`, result.diagnostics);
- }
- } catch (error) {
- console.warn("Failed to load native capture diagnostics:", error);
- }
- }, []);
-
- const buildNativeCaptureFailureMessage = useCallback(
- async (context: string, fallbackMessage: string) => {
- if (typeof window.electronAPI?.getLastNativeCaptureDiagnostics !== "function") {
- return fallbackMessage;
- }
-
- try {
- const result = await window.electronAPI.getLastNativeCaptureDiagnostics();
- const diagnostics = result.success ? result.diagnostics ?? null : null;
- if (!diagnostics) {
- return fallbackMessage;
- }
-
- console.warn(`[NativeCaptureDiagnostics:${context}]`, diagnostics);
-
- const details: string[] = [];
- if (diagnostics.error) {
- details.push(diagnostics.error);
- }
- if (diagnostics.outputPath) {
- details.push(`Saved file: ${diagnostics.outputPath}`);
- }
-
- return details.length > 0
- ? `${fallbackMessage} ${details.join(". ")}`
- : fallbackMessage;
- } catch (error) {
- console.warn("Failed to load native capture diagnostics:", error);
- return fallbackMessage;
- }
- },
- [],
- );
-
- const resetRecordingClock = useCallback((startedAt: number) => {
- startTime.current = startedAt;
- accumulatedPausedDurationMs.current = 0;
- pauseStartedAtMs.current = null;
- pauseSegmentsRef.current = [];
- }, []);
-
- const markRecordingPaused = useCallback((pausedAt: number) => {
- if (pauseStartedAtMs.current === null) {
- pauseStartedAtMs.current = pausedAt;
- }
- }, []);
-
- const markRecordingResumed = useCallback((resumedAt: number) => {
- if (pauseStartedAtMs.current === null) {
- return;
- }
-
- const pauseStart = pauseStartedAtMs.current;
- const pauseDurationMs = Math.max(0, resumedAt - pauseStart);
- accumulatedPausedDurationMs.current += pauseDurationMs;
- if (pauseDurationMs > 0) {
- pauseSegmentsRef.current.push({ startMs: pauseStart, endMs: resumedAt });
- }
- pauseStartedAtMs.current = null;
- }, []);
-
- const getRecordingDurationMs = useCallback((endedAt: number) => {
- return getEffectiveRecordingDurationMs({
- startTimeMs: startTime.current,
- endTimeMs: endedAt,
- accumulatedPausedDurationMs: accumulatedPausedDurationMs.current,
- pauseStartedAtMs: pauseStartedAtMs.current,
- });
- }, []);
-
- const preparePermissions = useCallback(async (options: { startup?: boolean } = {}) => {
- const platform = await window.electronAPI.getPlatform();
- if (platform !== "darwin") {
- return true;
- }
-
- const screenPermission = await window.electronAPI.getScreenRecordingPermissionStatus();
- if (!screenPermission.success || screenPermission.status !== "granted") {
- await window.electronAPI.openScreenRecordingPreferences();
- alert(
- options.startup
- ? "Recordly needs Screen Recording permission before you start. System Settings has been opened. After enabling it, quit and reopen Recordly."
- : "Screen Recording permission is still missing. System Settings has been opened again. Enable it, then quit and reopen Recordly before recording.",
- );
- return false;
- }
-
- const accessibilityPermission = await window.electronAPI.getAccessibilityPermissionStatus();
- if (!accessibilityPermission.success) {
- return false;
- }
-
- if (accessibilityPermission.trusted) {
- return true;
- }
-
- const requestedAccessibility = await window.electronAPI.requestAccessibilityPermission();
- if (requestedAccessibility.success && requestedAccessibility.trusted) {
- return true;
- }
-
- await window.electronAPI.openAccessibilityPreferences();
- alert(
- options.startup
- ? "Recordly also needs Accessibility permission for cursor tracking. System Settings has been opened. After enabling it, quit and reopen Recordly."
- : "Accessibility permission is still missing. System Settings has been opened again. Enable it, then quit and reopen Recordly before recording.",
- );
-
- return false;
- }, []);
-
- const selectMimeType = () => {
- const preferred = [
- "video/webm;codecs=av1",
- "video/webm;codecs=h264",
- "video/webm;codecs=vp9",
- "video/webm;codecs=vp8",
- "video/webm",
- ];
-
- return preferred.find((type) => MediaRecorder.isTypeSupported(type)) ?? "video/webm";
- };
-
- const computeBitrate = (width: number, height: number) => {
- const pixels = width * height;
- const highFrameRateBoost =
- TARGET_FRAME_RATE >= HIGH_FRAME_RATE_THRESHOLD ? HIGH_FRAME_RATE_BOOST : 1;
-
- if (pixels >= FOUR_K_PIXELS) {
- return Math.round(BITRATE_4K * highFrameRateBoost);
- }
-
- if (pixels >= QHD_PIXELS) {
- return Math.round(BITRATE_QHD * highFrameRateBoost);
- }
-
- return Math.round(BITRATE_BASE * highFrameRateBoost);
- };
-
- const cleanupCapturedMedia = useCallback(() => {
- if (stream.current) {
- stream.current.getTracks().forEach((track) => track.stop());
- stream.current = null;
- }
-
- if (screenStream.current) {
- screenStream.current.getTracks().forEach((track) => track.stop());
- screenStream.current = null;
- }
-
- if (microphoneStream.current) {
- microphoneStream.current.getTracks().forEach((track) => track.stop());
- microphoneStream.current = null;
- }
-
- if (webcamStream.current) {
- webcamStream.current.getTracks().forEach((track) => track.stop());
- webcamStream.current = null;
- }
-
- if (mixingContext.current) {
- mixingContext.current.close().catch(() => {});
- mixingContext.current = null;
- }
-
- if (micFallbackRecorder.current) {
- try {
- if (micFallbackRecorder.current.state !== "inactive") {
- micFallbackRecorder.current.stop();
- }
- micFallbackRecorder.current.stream?.getTracks().forEach((track) => track.stop());
- } catch { /* ignore */ }
- micFallbackRecorder.current = null;
- micFallbackChunks.current = [];
- }
- }, []);
-
- const resolveBrowserCaptureSource = useCallback(async (source: any) => {
- if (!source?.id?.startsWith("screen:")) {
- return source;
- }
-
- try {
- const liveSources = await window.electronAPI.getSources({
- types: ["screen"],
- thumbnailSize: { width: 1, height: 1 },
- fetchWindowIcons: false,
- });
-
- const exactMatch = liveSources.find((candidate) => candidate.id === source.id);
- if (exactMatch) {
- return {
- ...source,
- id: exactMatch.id,
- name: exactMatch.name ?? source.name,
- display_id: exactMatch.display_id ?? source.display_id,
- };
- }
-
- const displayMatch = liveSources.find(
- (candidate) => String(candidate.display_id ?? "") === String(source.display_id ?? ""),
- );
- if (displayMatch) {
- return {
- ...source,
- id: displayMatch.id,
- name: displayMatch.name ?? source.name,
- display_id: displayMatch.display_id ?? source.display_id,
- };
- }
- } catch (error) {
- console.warn("Failed to resolve browser capture source:", error);
- }
-
- return source;
- }, []);
-
- const finalizeRecordingSession = useCallback(async (videoPath: string, webcamPath: string | null) => {
- try {
- if (webcamPath) {
- await window.electronAPI.setCurrentRecordingSession({
- videoPath,
- webcamPath,
- timeOffsetMs: webcamTimeOffsetMs.current,
- });
- } else {
- await window.electronAPI.setCurrentVideoPath(videoPath);
- }
- } catch (error) {
- console.error("Failed to persist recording session metadata:", error);
-
- try {
- await window.electronAPI.setCurrentVideoPath(videoPath);
- } catch (fallbackError) {
- console.error("Failed to persist fallback video path:", fallbackError);
- }
- }
-
- clearRecordingFinalizationToast();
- await window.electronAPI.switchToEditor();
- }, [clearRecordingFinalizationToast]);
-
- const stopWebcamRecorder = useCallback(async () => {
- const recorder = webcamRecorder.current;
- const pending = webcamStopPromise.current;
-
- if (!recorder) {
- return null;
- }
-
- if (recorder.state !== "inactive") {
- recorder.stop();
- }
-
- const result = pending ? await pending : null;
- resolvedWebcamPath.current = result;
- pendingWebcamPathPromise.current = null;
- return result;
- }, []);
-
- const recoverNativeRecordingSession = useCallback(async () => {
- if (typeof window.electronAPI?.recoverNativeScreenRecording !== "function") {
- return null;
- }
-
- const result = await window.electronAPI.recoverNativeScreenRecording();
- if (!result.success || !result.path) {
- return null;
- }
-
- const webcamPath = await stopWebcamRecorder();
- await finalizeRecordingSession(result.path, webcamPath);
- return result.path;
- }, [finalizeRecordingSession, stopWebcamRecorder]);
-
- /**
- * Acquire the webcam stream and prepare the MediaRecorder, but do NOT start
- * recording yet. Call {@link beginWebcamCapture} after the main recording
- * has started so both begin at approximately the same time.
- */
- const prepareWebcamRecorder = useCallback(async () => {
- if (!webcamEnabled) {
- resolvedWebcamPath.current = null;
- pendingWebcamPathPromise.current = Promise.resolve(null);
- webcamStartTime.current = null;
- webcamTimeOffsetMs.current = 0;
- return;
- }
-
- try {
- webcamStream.current = await navigator.mediaDevices.getUserMedia({
- video: webcamDeviceId
- ? {
- deviceId: { exact: webcamDeviceId },
- width: { ideal: WEBCAM_WIDTH },
- height: { ideal: WEBCAM_HEIGHT },
- frameRate: { ideal: WEBCAM_FRAME_RATE, max: WEBCAM_FRAME_RATE },
- }
- : {
- width: { ideal: WEBCAM_WIDTH },
- height: { ideal: WEBCAM_HEIGHT },
- frameRate: { ideal: WEBCAM_FRAME_RATE, max: WEBCAM_FRAME_RATE },
- },
- audio: false,
- });
-
- const mimeType = selectMimeType();
- webcamChunks.current = [];
- resolvedWebcamPath.current = null;
- webcamStopPromise.current = new Promise((resolve) => {
- webcamStopResolver.current = resolve;
- });
- pendingWebcamPathPromise.current = webcamStopPromise.current;
-
- const recorder = new MediaRecorder(webcamStream.current, {
- mimeType,
- videoBitsPerSecond: WEBCAM_BITRATE,
- });
-
- webcamRecorder.current = recorder;
- recorder.ondataavailable = (event) => {
- if (event.data && event.data.size > 0) {
- webcamChunks.current.push(event.data);
- }
- };
- recorder.onerror = () => {
- webcamStopResolver.current?.(null);
- webcamStopResolver.current = null;
- };
- recorder.onstop = async () => {
- const sessionTimestamp = recordingSessionTimestamp.current ?? Date.now();
- const webcamFileName = `${RECORDING_FILE_PREFIX}${sessionTimestamp}${WEBCAM_SUFFIX}${VIDEO_FILE_EXTENSION}`;
-
- try {
- if (webcamChunks.current.length === 0) {
- webcamStopResolver.current?.(null);
- return;
- }
-
- const duration = Math.max(
- 0,
- getRecordingDurationMs(Date.now()) - webcamTimeOffsetMs.current,
- );
- const webcamBlob = new Blob(webcamChunks.current, { type: mimeType });
- webcamChunks.current = [];
- const fixedBlob = await fixWebmDuration(webcamBlob, duration);
- const arrayBuffer = await fixedBlob.arrayBuffer();
- const result = await window.electronAPI.storeRecordedVideo(arrayBuffer, webcamFileName);
- webcamStopResolver.current?.(result.success ? result.path ?? null : null);
- } catch (error) {
- console.error("Error saving webcam recording:", error);
- webcamStopResolver.current?.(null);
- } finally {
- webcamStopResolver.current = null;
- webcamRecorder.current = null;
- webcamStartTime.current = null;
- if (webcamStream.current) {
- webcamStream.current.getTracks().forEach((track) => track.stop());
- webcamStream.current = null;
- }
- }
- };
- } catch (error) {
- console.warn("Failed to start webcam recording; continuing without webcam layer:", error);
- resolvedWebcamPath.current = null;
- pendingWebcamPathPromise.current = Promise.resolve(null);
- webcamStopPromise.current = Promise.resolve(null);
- webcamRecorder.current = null;
- webcamStartTime.current = null;
- webcamTimeOffsetMs.current = 0;
- if (webcamStream.current) {
- webcamStream.current.getTracks().forEach((track) => track.stop());
- webcamStream.current = null;
- }
- }
- }, [getRecordingDurationMs, webcamDeviceId, webcamEnabled]);
-
- /** Start the prepared webcam MediaRecorder. Call after main recording begins. */
- const beginWebcamCapture = useCallback(() => {
- const recorder = webcamRecorder.current;
- if (recorder && recorder.state === "inactive") {
- webcamStartTime.current = Date.now();
- recorder.start(RECORDER_TIMESLICE_MS);
- }
- }, []);
-
- const stopRecording = useRef(() => {
- setPaused(false);
- if (nativeScreenRecording.current) {
- nativeScreenRecording.current = false;
- setRecording(false);
-
- void (async () => {
- showRecordingFinalizationToast();
- const micFallbackBlobPromise = stopMicFallbackRecorder();
- const webcamPath = await stopWebcamRecorder();
- const isNativeWindows = nativeWindowsRecording.current;
- markRecordingResumed(Date.now());
- const pauseSegments = pauseSegmentsRef.current.slice();
- nativeWindowsRecording.current = false;
-
- const result = await window.electronAPI.stopNativeScreenRecording();
- await window.electronAPI?.setRecordingState(false);
-
- if (!result.success || !result.path) {
- console.error("Failed to stop native screen recording:", result.error ?? result.message);
- void logNativeCaptureDiagnostics("stop-native-screen-recording");
- try {
- const recoveredPath = await recoverNativeRecordingSession();
- if (recoveredPath) {
- return;
- }
- } catch (recoveryError) {
- console.error("Failed to recover native screen recording:", recoveryError);
- }
-
- const failureMessage = await buildNativeCaptureFailureMessage(
- "stop-native-screen-recording",
- isMacOS
- ? "Failed to finish the macOS recording, so the editor was not opened."
- : "Failed to finish the recording, so the editor was not opened.",
- );
- await notifyRecordingFinalizationFailure(failureMessage);
- return;
- }
-
- let finalPath = result.path;
-
- if (isNativeWindows) {
- const muxResult = await window.electronAPI.muxNativeWindowsRecording(pauseSegments);
- if (!muxResult?.success) {
- void logNativeCaptureDiagnostics("mux-native-windows-recording");
- }
- finalPath = muxResult?.path ?? result.path;
- }
-
- // Save the browser-captured microphone sidecar if one was recorded.
- const micFallbackBlob = await micFallbackBlobPromise;
- if (micFallbackBlob && finalPath) {
- try {
- const arrayBuffer = await micFallbackBlob.arrayBuffer();
- await window.electronAPI.storeMicrophoneSidecar(arrayBuffer, finalPath);
- } catch (e) {
- console.warn("Failed to store microphone sidecar:", e);
- }
- }
-
- await finalizeRecordingSession(finalPath, webcamPath);
- })();
- return;
- }
-
- const recorder = mediaRecorder.current;
- const recorderState = recorder?.state;
- if (recorder && (recorderState === "recording" || recorderState === "paused")) {
- if (recorderState === "paused") {
- markRecordingResumed(Date.now());
- recorder.resume();
- }
- pendingWebcamPathPromise.current = stopWebcamRecorder();
- cleanupCapturedMedia();
- recorder.stop();
- setRecording(false);
- window.electronAPI?.setRecordingState(false);
- }
- });
-
- useEffect(() => {
- void (async () => {
- const platform = await window.electronAPI.getPlatform();
- setIsMacOS(platform === "darwin");
- })();
- }, []);
-
- useEffect(() => {
- if (countdownDelayLoaded.current) return;
- countdownDelayLoaded.current = true;
-
- void (async () => {
- const result = await window.electronAPI.getCountdownDelay();
- if (result.success && typeof result.delay === "number") {
- setCountdownDelayState(result.delay);
- }
- })();
- }, []);
-
- const setCountdownDelay = useCallback((delay: number) => {
- setCountdownDelayState(delay);
- void window.electronAPI.setCountdownDelay(delay);
- }, []);
-
- useEffect(() => {
- if (recordingPrefsLoaded.current) return;
- recordingPrefsLoaded.current = true;
-
- void (async () => {
- const result = await window.electronAPI.getRecordingPreferences();
- if (result.success) {
- setMicrophoneEnabled(result.microphoneEnabled);
- if (result.microphoneDeviceId) {
- setMicrophoneDeviceId(result.microphoneDeviceId);
- }
- setSystemAudioEnabled(result.systemAudioEnabled);
- }
- })();
- }, []);
-
- const persistMicrophoneEnabled = useCallback((enabled: boolean) => {
- setMicrophoneEnabled(enabled);
- void window.electronAPI.setRecordingPreferences({ microphoneEnabled: enabled });
- }, []);
-
- const persistMicrophoneDeviceId = useCallback((deviceId: string | undefined) => {
- setMicrophoneDeviceId(deviceId);
- void window.electronAPI.setRecordingPreferences({ microphoneDeviceId: deviceId });
- }, []);
-
- const persistSystemAudioEnabled = useCallback((enabled: boolean) => {
- setSystemAudioEnabled(enabled);
- void window.electronAPI.setRecordingPreferences({ systemAudioEnabled: enabled });
- }, []);
-
- useEffect(() => {
- let cleanup: (() => void) | undefined;
-
- if (window.electronAPI?.onStopRecordingFromTray) {
- cleanup = window.electronAPI.onStopRecordingFromTray(() => {
- stopRecording.current();
- });
- }
-
- const removeRecordingStateListener = window.electronAPI?.onRecordingStateChanged?.((state) => {
- setRecording(state.recording);
- });
-
- const removeRecordingInterruptedListener = window.electronAPI?.onRecordingInterrupted?.((state) => {
- void (async () => {
- setRecording(false);
- nativeScreenRecording.current = false;
- cleanupCapturedMedia();
- await window.electronAPI.setRecordingState(false);
-
- if (state.reason !== "window-unavailable") {
- try {
- const recoveredPath = await recoverNativeRecordingSession();
- if (recoveredPath) {
- return;
- }
- } catch (recoveryError) {
- console.error("Failed to recover interrupted native screen recording:", recoveryError);
- }
- }
-
- if (state.reason === "window-unavailable" && !hasPromptedForReselect.current) {
- hasPromptedForReselect.current = true;
- alert(state.message);
- await window.electronAPI.openSourceSelector();
- } else {
- console.error(state.message);
- toast.error(state.message);
- }
- })();
- });
-
- return () => {
- cleanup?.();
- removeRecordingStateListener?.();
- removeRecordingInterruptedListener?.();
-
- if (nativeScreenRecording.current) {
- nativeScreenRecording.current = false;
- void window.electronAPI.stopNativeScreenRecording();
- }
-
- if (mediaRecorder.current?.state === "recording") {
- mediaRecorder.current.stop();
- }
-
- cleanupCapturedMedia();
- };
- }, [cleanupCapturedMedia, recoverNativeRecordingSession]);
-
- const stopMicFallbackRecorder = useCallback((): Promise<Blob | null> => {
- return new Promise((resolve) => {
- const recorder = micFallbackRecorder.current;
- if (!recorder || recorder.state === "inactive") {
- micFallbackRecorder.current = null;
- resolve(null);
- return;
- }
- recorder.ondataavailable = (event) => {
- if (event.data.size > 0) {
- micFallbackChunks.current.push(event.data);
- }
- };
- recorder.onstop = () => {
- const blob = micFallbackChunks.current.length > 0
- ? new Blob(micFallbackChunks.current, { type: recorder.mimeType })
- : null;
- micFallbackChunks.current = [];
- micFallbackRecorder.current = null;
- resolve(blob);
- };
- recorder.stop();
- });
- }, []);
-
- const startRecording = async () => {
- if (startInFlight.current) {
- return;
- }
-
- hasPromptedForReselect.current = false;
- startInFlight.current = true;
- setStarting(true);
-
- try {
- const selectedSource = await window.electronAPI.getSelectedSource();
- if (!selectedSource) {
- alert("Please select a source to record");
- return;
- }
-
- const permissionsReady = await preparePermissions();
- if (!permissionsReady) {
- return;
- }
-
- recordingSessionTimestamp.current = Date.now();
- resetRecordingClock(recordingSessionTimestamp.current);
- await prepareWebcamRecorder();
-
- const platform = await window.electronAPI.getPlatform();
- const useNativeMacScreenCapture =
- platform === "darwin" &&
- (selectedSource.id?.startsWith("screen:") || selectedSource.id?.startsWith("window:")) &&
- typeof window.electronAPI.startNativeScreenRecording === "function";
-
- let useNativeWindowsCapture = false;
- if (
- platform === "win32" &&
- (selectedSource.id?.startsWith("screen:") || selectedSource.id?.startsWith("window:")) &&
- typeof window.electronAPI.isNativeWindowsCaptureAvailable === "function"
- ) {
- try {
- const nativeWindowsResult = await window.electronAPI.isNativeWindowsCaptureAvailable();
- useNativeWindowsCapture = nativeWindowsResult.available;
- if (!useNativeWindowsCapture && !hasShownNativeWindowsFallbackToast.current) {
- void logNativeCaptureDiagnostics("is-native-windows-capture-available");
- hasShownNativeWindowsFallbackToast.current = true;
- toast.info(
- "Native Windows capture is unavailable. Falling back to browser capture.",
- );
- }
- } catch {
- useNativeWindowsCapture = false;
- if (!hasShownNativeWindowsFallbackToast.current) {
- hasShownNativeWindowsFallbackToast.current = true;
- toast.info(
- "Unable to check native Windows capture. Falling back to browser capture.",
- );
- }
- }
- }
-
- if (useNativeMacScreenCapture || useNativeWindowsCapture) {
- // Resolve the selected mic label for native capture backends.
- let micLabel: string | undefined;
- if (microphoneEnabled) {
- try {
- const devices = await navigator.mediaDevices.enumerateDevices();
- const mic = devices.find(
- (d) => d.deviceId === microphoneDeviceId && d.kind === "audioinput",
- );
- micLabel = mic?.label || undefined;
- } catch {
- // Fall through — native process will use the default mic
- }
- }
-
- const nativeResult = await window.electronAPI.startNativeScreenRecording(selectedSource, {
- capturesSystemAudio: systemAudioEnabled,
- capturesMicrophone: microphoneEnabled,
- microphoneDeviceId,
- microphoneLabel: micLabel,
- });
- if (!nativeResult.success) {
- if (useNativeWindowsCapture) {
- console.warn("Native Windows capture failed, falling back to browser capture:", nativeResult.error ?? nativeResult.message);
- void logNativeCaptureDiagnostics("start-native-screen-recording");
- if (!hasShownNativeWindowsFallbackToast.current) {
- hasShownNativeWindowsFallbackToast.current = true;
- toast.warning(
- "Native Windows capture failed to start. Falling back to browser capture.",
- );
- }
- } else if (!nativeResult.userNotified) {
- throw new Error(
- nativeResult.error ?? nativeResult.message ?? "Failed to start native screen recording",
- );
- } else {
- setRecording(false);
- cleanupCapturedMedia();
- await stopWebcamRecorder();
- return;
- }
- }
-
- if (nativeResult.success) {
- const mainStartedAt = Date.now();
- beginWebcamCapture();
- nativeScreenRecording.current = true;
- nativeWindowsRecording.current = useNativeWindowsCapture;
- resetRecordingClock(mainStartedAt);
- webcamTimeOffsetMs.current = webcamStartTime.current === null
- ? 0
- : webcamStartTime.current - mainStartedAt;
-
- // When native mic capture is unavailable (macOS < 14), record mic
- // via browser getUserMedia so it can be saved as a sidecar file.
- if (nativeResult.microphoneFallbackRequired && microphoneEnabled) {
- try {
- const micStream = await navigator.mediaDevices.getUserMedia({
- audio: microphoneDeviceId
- ? { deviceId: { exact: microphoneDeviceId }, echoCancellation: true, noiseSuppression: true, autoGainControl: true }
- : { echoCancellation: true, noiseSuppression: true, autoGainControl: true },
- video: false,
- });
- micFallbackChunks.current = [];
- const recorder = new MediaRecorder(micStream, { mimeType: "audio/webm;codecs=opus" });
- recorder.ondataavailable = (event) => {
- if (event.data.size > 0) {
- micFallbackChunks.current.push(event.data);
- }
- };
- recorder.start(1000);
- micFallbackRecorder.current = recorder;
- } catch (micError) {
- console.warn("Browser microphone fallback failed:", micError);
- }
- }
-
- setRecording(true);
- window.electronAPI?.setRecordingState(true);
-
- return;
- }
- }
-
- const wantsAudioCapture = microphoneEnabled || systemAudioEnabled;
- const browserCaptureSource = await resolveBrowserCaptureSource(selectedSource);
-
- if (
- browserCaptureSource?.id?.startsWith("screen:fallback:") ||
- browserCaptureSource?.id?.startsWith("window:fallback:")
- ) {
- throw new Error("Selected display is not available for browser capture on this system.");
- }
-
- try {
- await window.electronAPI.hideOsCursor?.();
- } catch {
- console.warn("Could not hide OS cursor before recording.");
- }
-
- let videoTrack: MediaStreamTrack | undefined;
- let systemAudioIncluded = false;
- const browserScreenVideoConstraints = {
- mandatory: {
- chromeMediaSource: CHROME_MEDIA_SOURCE,
- chromeMediaSourceId: browserCaptureSource.id,
- maxWidth: TARGET_WIDTH,
- maxHeight: TARGET_HEIGHT,
- maxFrameRate: TARGET_FRAME_RATE,
- minFrameRate: MIN_FRAME_RATE,
- googCaptureCursor: false,
- },
- cursor: "never" as const,
- };
-
- if (wantsAudioCapture) {
- let screenMediaStream: MediaStream;
-
- if (systemAudioEnabled) {
- try {
- screenMediaStream = await (navigator.mediaDevices as any).getUserMedia({
- audio: {
- mandatory: {
- chromeMediaSource: CHROME_MEDIA_SOURCE,
- chromeMediaSourceId: browserCaptureSource.id,
- },
- },
- video: browserScreenVideoConstraints,
- });
- } catch (audioError) {
- console.warn("System audio capture failed, falling back to video-only:", audioError);
- alert("System audio is not available for this source. Recording will continue without system audio.");
- screenMediaStream = await (navigator.mediaDevices as any).getUserMedia({
- audio: false,
- video: browserScreenVideoConstraints,
- });
- }
- } else {
- screenMediaStream = await (navigator.mediaDevices as any).getUserMedia({
- audio: false,
- video: browserScreenVideoConstraints,
- });
- }
-
- screenStream.current = screenMediaStream;
- stream.current = new MediaStream();
-
- videoTrack = screenMediaStream.getVideoTracks()[0];
- if (!videoTrack) {
- throw new Error("Video track is not available.");
- }
-
- stream.current.addTrack(videoTrack);
-
- if (microphoneEnabled) {
- try {
- microphoneStream.current = await navigator.mediaDevices.getUserMedia({
- audio: microphoneDeviceId
- ? {
- deviceId: { exact: microphoneDeviceId },
- echoCancellation: true,
- noiseSuppression: true,
- autoGainControl: true,
- }
- : {
- echoCancellation: true,
- noiseSuppression: true,
- autoGainControl: true,
- },
- video: false,
- });
- } catch (audioError) {
- console.warn("Failed to get microphone access:", audioError);
- alert("Microphone access was denied. Recording will continue without microphone audio.");
- setMicrophoneEnabled(false);
- }
- }
-
- const systemAudioTrack = screenMediaStream.getAudioTracks()[0];
- const micAudioTrack = microphoneStream.current?.getAudioTracks()[0];
-
- if (systemAudioTrack && micAudioTrack) {
- const context = new AudioContext();
- mixingContext.current = context;
- const systemSource = context.createMediaStreamSource(new MediaStream([systemAudioTrack]));
- const micSource = context.createMediaStreamSource(new MediaStream([micAudioTrack]));
- const micGain = context.createGain();
- micGain.gain.value = MIC_GAIN_BOOST;
- const destination = context.createMediaStreamDestination();
-
- systemSource.connect(destination);
- micSource.connect(micGain).connect(destination);
-
- const mixedTrack = destination.stream.getAudioTracks()[0];
- if (mixedTrack) {
- stream.current.addTrack(mixedTrack);
- systemAudioIncluded = true;
- }
- } else if (systemAudioTrack) {
- stream.current.addTrack(systemAudioTrack);
- systemAudioIncluded = true;
- } else if (micAudioTrack) {
- stream.current.addTrack(micAudioTrack);
- }
- } else {
- const mediaStream = await navigator.mediaDevices.getDisplayMedia({
- audio: false,
- video: {
- displaySurface: selectedSource.id?.startsWith("window:") ? "window" : "monitor",
- width: { ideal: TARGET_WIDTH, max: TARGET_WIDTH },
- height: { ideal: TARGET_HEIGHT, max: TARGET_HEIGHT },
- frameRate: { ideal: TARGET_FRAME_RATE, max: TARGET_FRAME_RATE },
- cursor: "never",
- },
- selfBrowserSurface: "exclude",
- surfaceSwitching: "exclude",
- } as any);
-
- stream.current = mediaStream;
- videoTrack = mediaStream.getVideoTracks()[0];
- }
-
- if (!stream.current || !videoTrack) {
- throw new Error("Media stream is not available.");
- }
-
- try {
- await videoTrack.applyConstraints({
- frameRate: { ideal: TARGET_FRAME_RATE, max: TARGET_FRAME_RATE },
- width: { ideal: TARGET_WIDTH, max: TARGET_WIDTH },
- height: { ideal: TARGET_HEIGHT, max: TARGET_HEIGHT },
- } as MediaTrackConstraints);
- } catch (error) {
- console.warn(
- "Unable to lock 4K/60fps constraints, using best available track settings.",
- error,
- );
- }
-
- let {
- width = DEFAULT_WIDTH,
- height = DEFAULT_HEIGHT,
- frameRate = TARGET_FRAME_RATE,
- } = videoTrack.getSettings();
-
- width = Math.floor(width / CODEC_ALIGNMENT) * CODEC_ALIGNMENT;
- height = Math.floor(height / CODEC_ALIGNMENT) * CODEC_ALIGNMENT;
-
- const videoBitsPerSecond = computeBitrate(width, height);
- const mimeType = selectMimeType();
-
- console.log(
- `Recording at ${width}x${height} @ ${frameRate ?? TARGET_FRAME_RATE}fps using ${mimeType} / ${Math.round(
- videoBitsPerSecond / BITS_PER_MEGABIT,
- )} Mbps`,
- );
-
- chunks.current = [];
- const hasAudio = stream.current.getAudioTracks().length > 0;
- const recorder = new MediaRecorder(stream.current, {
- mimeType,
- videoBitsPerSecond,
- ...(hasAudio
- ? { audioBitsPerSecond: systemAudioIncluded ? AUDIO_BITRATE_SYSTEM : AUDIO_BITRATE_VOICE }
- : {}),
- });
-
- mediaRecorder.current = recorder;
- recorder.ondataavailable = (event) => {
- if (event.data && event.data.size > 0) chunks.current.push(event.data);
- };
- recorder.onstop = async () => {
- cleanupCapturedMedia();
- if (chunks.current.length === 0) return;
-
- showRecordingFinalizationToast();
-
- const duration = getRecordingDurationMs(Date.now());
- const recordedChunks = chunks.current;
- const buggyBlob = new Blob(recordedChunks, { type: mimeType });
- chunks.current = [];
- const timestamp = recordingSessionTimestamp.current ?? Date.now();
- const videoFileName = `${RECORDING_FILE_PREFIX}${timestamp}${VIDEO_FILE_EXTENSION}`;
-
- try {
- const videoBlob = await fixWebmDuration(buggyBlob, duration);
- const arrayBuffer = await videoBlob.arrayBuffer();
- const videoResult = await window.electronAPI.storeRecordedVideo(arrayBuffer, videoFileName);
- if (!videoResult.success) {
- console.error("Failed to store video:", videoResult.message);
- await notifyRecordingFinalizationFailure(videoResult.message || "Failed to store the recording.");
- return;
- }
-
- if (videoResult.path) {
- const webcamPath = pendingWebcamPathPromise.current
- ? await pendingWebcamPathPromise.current
- : resolvedWebcamPath.current;
- await finalizeRecordingSession(videoResult.path, webcamPath);
- } else {
- await notifyRecordingFinalizationFailure("Failed to save the recording.");
- }
- } catch (error) {
- console.error("Error saving recording:", error);
- const message = error instanceof Error ? error.message : String(error);
- await notifyRecordingFinalizationFailure(`Failed to finalize the recording. ${message}`);
- }
- };
- recorder.onerror = () => {
- setRecording(false);
- };
- const mainStartedAt = Date.now();
- beginWebcamCapture();
- resetRecordingClock(mainStartedAt);
- webcamTimeOffsetMs.current = webcamStartTime.current === null
- ? 0
- : webcamStartTime.current - mainStartedAt;
- recorder.start(RECORDER_TIMESLICE_MS);
- setRecording(true);
- window.electronAPI?.setRecordingState(true);
- } catch (error) {
- console.error("Failed to start recording:", error);
- alert(error instanceof Error ? `Failed to start recording: ${error.message}` : "Failed to start recording");
- setRecording(false);
- cleanupCapturedMedia();
- await stopWebcamRecorder();
- } finally {
- startInFlight.current = false;
- setStarting(false);
- }
- };
-
- const pauseRecording = useCallback(() => {
- if (!recording || paused) return;
- if (nativeScreenRecording.current) {
- void (async () => {
- const result = await window.electronAPI.pauseNativeScreenRecording();
- if (!result.success) {
- console.error("Failed to pause native screen recording:", result.error ?? result.message);
- return;
- }
-
- if (webcamRecorder.current?.state === "recording") {
- webcamRecorder.current.pause();
- }
- markRecordingPaused(Date.now());
- setPaused(true);
- })();
- return;
- }
- if (mediaRecorder.current?.state === "recording") {
- mediaRecorder.current.pause();
- if (webcamRecorder.current?.state === "recording") {
- webcamRecorder.current.pause();
- }
- markRecordingPaused(Date.now());
- setPaused(true);
- }
- }, [markRecordingPaused, paused, recording]);
-
- const resumeRecording = useCallback(() => {
- if (!recording || !paused) return;
- if (nativeScreenRecording.current) {
- void (async () => {
- const result = await window.electronAPI.resumeNativeScreenRecording();
- if (!result.success) {
- console.error("Failed to resume native screen recording:", result.error ?? result.message);
- return;
- }
-
- if (webcamRecorder.current?.state === "paused") {
- webcamRecorder.current.resume();
- }
- markRecordingResumed(Date.now());
- setPaused(false);
- })();
- return;
- }
- if (mediaRecorder.current?.state === "paused") {
- mediaRecorder.current.resume();
- if (webcamRecorder.current?.state === "paused") {
- webcamRecorder.current.resume();
- }
- markRecordingResumed(Date.now());
- setPaused(false);
- }
- }, [markRecordingResumed, paused, recording]);
-
- const cancelRecording = useCallback(() => {
- if (!recording) return;
- setPaused(false);
- markRecordingResumed(Date.now());
-
- // Discard webcam recording regardless of recording mode
- webcamChunks.current = [];
- if (webcamRecorder.current && webcamRecorder.current.state !== "inactive") {
- webcamRecorder.current.stop();
- }
- webcamRecorder.current = null;
- webcamStartTime.current = null;
- webcamTimeOffsetMs.current = 0;
- webcamStream.current?.getTracks().forEach((t) => t.stop());
- webcamStream.current = null;
- pendingWebcamPathPromise.current = null;
- resolvedWebcamPath.current = null;
-
- if (nativeScreenRecording.current) {
- nativeScreenRecording.current = false;
- nativeWindowsRecording.current = false;
- setRecording(false);
- window.electronAPI?.setRecordingState(false);
- void (async () => {
- try {
- const result = await window.electronAPI.stopNativeScreenRecording();
- if (result?.path) {
- await window.electronAPI.deleteRecordingFile(result.path);
- }
- } catch {
- // Best-effort cleanup
- }
- })();
- return;
- }
-
- if (mediaRecorder.current) {
- chunks.current = [];
- cleanupCapturedMedia();
- if (mediaRecorder.current.state !== "inactive") {
- mediaRecorder.current.stop();
- }
- setRecording(false);
- window.electronAPI?.setRecordingState(false);
- }
- }, [cleanupCapturedMedia, markRecordingResumed, recording]);
-
- const toggleRecording = async () => {
- if (starting || countdownActive) {
- return;
- }
-
- if (recording) {
- stopRecording.current();
- return;
- }
-
- // Start recording with optional countdown
- if (countdownDelay > 0) {
- setCountdownActive(true);
- try {
- const result = await window.electronAPI.startCountdown(countdownDelay);
- if (!result.success || result.cancelled) {
- return;
- }
- } finally {
- setCountdownActive(false);
- }
- }
-
- startRecording();
- };
-
- return {
- recording,
- paused,
- countdownActive,
- toggleRecording,
- pauseRecording,
- resumeRecording,
- cancelRecording,
- preparePermissions,
- isMacOS,
- microphoneEnabled,
- setMicrophoneEnabled: persistMicrophoneEnabled,
- microphoneDeviceId,
- setMicrophoneDeviceId: persistMicrophoneDeviceId,
- systemAudioEnabled,
- setSystemAudioEnabled: persistSystemAudioEnabled,
- webcamEnabled,
- setWebcamEnabled,
- webcamDeviceId,
- setWebcamDeviceId,
- countdownDelay,
- setCountdownDelay,
- };
+ const [recording, setRecording] = useState(false);
+ const [paused, setPaused] = useState(false);
+ const [starting, setStarting] = useState(false);
+ const [countdownActive, setCountdownActive] = useState(false);
+ const [isMacOS, setIsMacOS] = useState(false);
+ const [microphoneEnabled, setMicrophoneEnabled] = useState(false);
+ const [microphoneDeviceId, setMicrophoneDeviceId] = useState<string | undefined>(undefined);
+ const [systemAudioEnabled, setSystemAudioEnabled] = useState(false);
+ const [webcamEnabled, setWebcamEnabled] = useState(false);
+ const [webcamDeviceId, setWebcamDeviceId] = useState<string | undefined>(undefined);
+ const [countdownDelay, setCountdownDelayState] = useState(3);
+ const mediaRecorder = useRef<MediaRecorder | null>(null);
+ const webcamRecorder = useRef<MediaRecorder | null>(null);
+ const stream = useRef<MediaStream | null>(null);
+ const screenStream = useRef<MediaStream | null>(null);
+ const microphoneStream = useRef<MediaStream | null>(null);
+ const webcamStream = useRef<MediaStream | null>(null);
+ const mixingContext = useRef<AudioContext | null>(null);
+ const chunks = useRef<Blob[]>([]);
+ const webcamChunks = useRef<Blob[]>([]);
+ const startTime = useRef<number>(0);
+ const webcamStartTime = useRef<number | null>(null);
+ const webcamTimeOffsetMs = useRef(0);
+ const recordingSessionTimestamp = useRef<number | null>(null);
+ const nativeScreenRecording = useRef(false);
+ const nativeWindowsRecording = useRef(false);
+ const startInFlight = useRef(false);
+ const hasPromptedForReselect = useRef(false);
+ const hasShownNativeWindowsFallbackToast = useRef(false);
+ const countdownDelayLoaded = useRef(false);
+ const recordingPrefsLoaded = useRef(false);
+ const pendingWebcamPathPromise = useRef<Promise<string | null> | null>(null);
+ const webcamStopPromise = useRef<Promise<string | null> | null>(null);
+ const webcamStopResolver = useRef<((path: string | null) => void) | null>(null);
+ const resolvedWebcamPath = useRef<string | null>(null);
+ const accumulatedPausedDurationMs = useRef(0);
+ const pauseStartedAtMs = useRef<number | null>(null);
+ const pauseSegmentsRef = useRef<PauseSegment[]>([]);
+ const recordingFinalizationToastId = useRef<string | number | null>(null);
+ const micFallbackRecorder = useRef<MediaRecorder | null>(null);
+ const micFallbackChunks = useRef<Blob[]>([]);
+
+ const showRecordingFinalizationToast = useCallback((message = "Preparing recording...") => {
+ recordingFinalizationToastId.current = toast.loading(message, {
+ id: recordingFinalizationToastId.current ?? undefined,
+ duration: Number.POSITIVE_INFINITY,
+ });
+ }, []);
+
+ const clearRecordingFinalizationToast = useCallback(() => {
+ const toastId = recordingFinalizationToastId.current;
+ if (toastId === null) {
+ return;
+ }
+
+ toast.dismiss(toastId);
+ recordingFinalizationToastId.current = null;
+ }, []);
+
+ const notifyRecordingFinalizationFailure = useCallback(
+ async (message: string) => {
+ clearRecordingFinalizationToast();
+ toast.error(message, { duration: 10000 });
+ },
+ [clearRecordingFinalizationToast],
+ );
+
+ const logNativeCaptureDiagnostics = useCallback(async (context: string) => {
+ if (typeof window.electronAPI?.getLastNativeCaptureDiagnostics !== "function") {
+ return;
+ }
+
+ try {
+ const result = await window.electronAPI.getLastNativeCaptureDiagnostics();
+ if (result.success && result.diagnostics) {
+ console.warn(`[NativeCaptureDiagnostics:${context}]`, result.diagnostics);
+ }
+ } catch (error) {
+ console.warn("Failed to load native capture diagnostics:", error);
+ }
+ }, []);
+
+ const buildNativeCaptureFailureMessage = useCallback(
+ async (context: string, fallbackMessage: string) => {
+ if (typeof window.electronAPI?.getLastNativeCaptureDiagnostics !== "function") {
+ return fallbackMessage;
+ }
+
+ try {
+ const result = await window.electronAPI.getLastNativeCaptureDiagnostics();
+ const diagnostics = result.success ? (result.diagnostics ?? null) : null;
+ if (!diagnostics) {
+ return fallbackMessage;
+ }
+
+ console.warn(`[NativeCaptureDiagnostics:${context}]`, diagnostics);
+
+ const details: string[] = [];
+ if (diagnostics.error) {
+ details.push(diagnostics.error);
+ }
+ if (diagnostics.outputPath) {
+ details.push(`Saved file: ${diagnostics.outputPath}`);
+ }
+
+ return details.length > 0
+ ? `${fallbackMessage} ${details.join(". ")}`
+ : fallbackMessage;
+ } catch (error) {
+ console.warn("Failed to load native capture diagnostics:", error);
+ return fallbackMessage;
+ }
+ },
+ [],
+ );
+
+ const resetRecordingClock = useCallback((startedAt: number) => {
+ startTime.current = startedAt;
+ accumulatedPausedDurationMs.current = 0;
+ pauseStartedAtMs.current = null;
+ pauseSegmentsRef.current = [];
+ }, []);
+
+ const markRecordingPaused = useCallback((pausedAt: number) => {
+ if (pauseStartedAtMs.current === null) {
+ pauseStartedAtMs.current = pausedAt;
+ }
+ }, []);
+
+ const markRecordingResumed = useCallback((resumedAt: number) => {
+ if (pauseStartedAtMs.current === null) {
+ return;
+ }
+
+ const pauseStart = pauseStartedAtMs.current;
+ const pauseDurationMs = Math.max(0, resumedAt - pauseStart);
+ accumulatedPausedDurationMs.current += pauseDurationMs;
+ if (pauseDurationMs > 0) {
+ pauseSegmentsRef.current.push({ startMs: pauseStart, endMs: resumedAt });
+ }
+ pauseStartedAtMs.current = null;
+ }, []);
+
+ const getRecordingDurationMs = useCallback((endedAt: number) => {
+ return getEffectiveRecordingDurationMs({
+ startTimeMs: startTime.current,
+ endTimeMs: endedAt,
+ accumulatedPausedDurationMs: accumulatedPausedDurationMs.current,
+ pauseStartedAtMs: pauseStartedAtMs.current,
+ });
+ }, []);
+
+ const preparePermissions = useCallback(async (options: { startup?: boolean } = {}) => {
+ const platform = await window.electronAPI.getPlatform();
+ if (platform !== "darwin") {
+ return true;
+ }
+
+ const screenPermission = await window.electronAPI.getScreenRecordingPermissionStatus();
+ if (!screenPermission.success || screenPermission.status !== "granted") {
+ await window.electronAPI.openScreenRecordingPreferences();
+ alert(
+ options.startup
+ ? "Recordly needs Screen Recording permission before you start. System Settings has been opened. After enabling it, quit and reopen Recordly."
+ : "Screen Recording permission is still missing. System Settings has been opened again. Enable it, then quit and reopen Recordly before recording.",
+ );
+ return false;
+ }
+
+ const accessibilityPermission = await window.electronAPI.getAccessibilityPermissionStatus();
+ if (!accessibilityPermission.success) {
+ return false;
+ }
+
+ if (accessibilityPermission.trusted) {
+ return true;
+ }
+
+ const requestedAccessibility = await window.electronAPI.requestAccessibilityPermission();
+ if (requestedAccessibility.success && requestedAccessibility.trusted) {
+ return true;
+ }
+
+ await window.electronAPI.openAccessibilityPreferences();
+ alert(
+ options.startup
+ ? "Recordly also needs Accessibility permission for cursor tracking. System Settings has been opened. After enabling it, quit and reopen Recordly."
+ : "Accessibility permission is still missing. System Settings has been opened again. Enable it, then quit and reopen Recordly before recording.",
+ );
+
+ return false;
+ }, []);
+
+ const selectMimeType = () => {
+ const preferred = [
+ "video/webm;codecs=av1",
+ "video/webm;codecs=h264",
+ "video/webm;codecs=vp9",
+ "video/webm;codecs=vp8",
+ "video/webm",
+ ];
+
+ return preferred.find((type) => MediaRecorder.isTypeSupported(type)) ?? "video/webm";
+ };
+
+ const computeBitrate = (width: number, height: number) => {
+ const pixels = width * height;
+ const highFrameRateBoost =
+ TARGET_FRAME_RATE >= HIGH_FRAME_RATE_THRESHOLD ? HIGH_FRAME_RATE_BOOST : 1;
+
+ if (pixels >= FOUR_K_PIXELS) {
+ return Math.round(BITRATE_4K * highFrameRateBoost);
+ }
+
+ if (pixels >= QHD_PIXELS) {
+ return Math.round(BITRATE_QHD * highFrameRateBoost);
+ }
+
+ return Math.round(BITRATE_BASE * highFrameRateBoost);
+ };
+
+ const cleanupCapturedMedia = useCallback(() => {
+ if (stream.current) {
+ stream.current.getTracks().forEach((track) => track.stop());
+ stream.current = null;
+ }
+
+ if (screenStream.current) {
+ screenStream.current.getTracks().forEach((track) => track.stop());
+ screenStream.current = null;
+ }
+
+ if (microphoneStream.current) {
+ microphoneStream.current.getTracks().forEach((track) => track.stop());
+ microphoneStream.current = null;
+ }
+
+ if (webcamStream.current) {
+ webcamStream.current.getTracks().forEach((track) => track.stop());
+ webcamStream.current = null;
+ }
+
+ if (mixingContext.current) {
+ mixingContext.current.close().catch(() => undefined);
+ mixingContext.current = null;
+ }
+
+ if (micFallbackRecorder.current) {
+ try {
+ if (micFallbackRecorder.current.state !== "inactive") {
+ micFallbackRecorder.current.stop();
+ }
+ micFallbackRecorder.current.stream?.getTracks().forEach((track) => track.stop());
+ } catch {
+ /* ignore */
+ }
+ micFallbackRecorder.current = null;
+ micFallbackChunks.current = [];
+ }
+ }, []);
+
+ const resolveBrowserCaptureSource = useCallback(async (source: ProcessedDesktopSource) => {
+ if (!source?.id?.startsWith("screen:")) {
+ return source;
+ }
+
+ try {
+ const liveSources = await window.electronAPI.getSources({
+ types: ["screen"],
+ thumbnailSize: { width: 1, height: 1 },
+ fetchWindowIcons: false,
+ });
+
+ const exactMatch = liveSources.find((candidate) => candidate.id === source.id);
+ if (exactMatch) {
+ return {
+ ...source,
+ id: exactMatch.id,
+ name: exactMatch.name ?? source.name,
+ display_id: exactMatch.display_id ?? source.display_id,
+ };
+ }
+
+ const displayMatch = liveSources.find(
+ (candidate) =>
+ String(candidate.display_id ?? "") === String(source.display_id ?? ""),
+ );
+ if (displayMatch) {
+ return {
+ ...source,
+ id: displayMatch.id,
+ name: displayMatch.name ?? source.name,
+ display_id: displayMatch.display_id ?? source.display_id,
+ };
+ }
+ } catch (error) {
+ console.warn("Failed to resolve browser capture source:", error);
+ }
+
+ return source;
+ }, []);
+
+ const finalizeRecordingSession = useCallback(
+ async (videoPath: string, webcamPath: string | null) => {
+ try {
+ if (webcamPath) {
+ await window.electronAPI.setCurrentRecordingSession({
+ videoPath,
+ webcamPath,
+ timeOffsetMs: webcamTimeOffsetMs.current,
+ });
+ } else {
+ await window.electronAPI.setCurrentVideoPath(videoPath);
+ }
+ } catch (error) {
+ console.error("Failed to persist recording session metadata:", error);
+
+ try {
+ await window.electronAPI.setCurrentVideoPath(videoPath);
+ } catch (fallbackError) {
+ console.error("Failed to persist fallback video path:", fallbackError);
+ }
+ }
+
+ clearRecordingFinalizationToast();
+ await window.electronAPI.switchToEditor();
+ },
+ [clearRecordingFinalizationToast],
+ );
+
+ const stopWebcamRecorder = useCallback(async () => {
+ const recorder = webcamRecorder.current;
+ const pending = webcamStopPromise.current;
+
+ if (!recorder) {
+ return null;
+ }
+
+ if (recorder.state !== "inactive") {
+ recorder.stop();
+ }
+
+ const result = pending ? await pending : null;
+ resolvedWebcamPath.current = result;
+ pendingWebcamPathPromise.current = null;
+ return result;
+ }, []);
+
+ const recoverNativeRecordingSession = useCallback(async () => {
+ if (typeof window.electronAPI?.recoverNativeScreenRecording !== "function") {
+ return null;
+ }
+
+ const result = await window.electronAPI.recoverNativeScreenRecording();
+ if (!result.success || !result.path) {
+ return null;
+ }
+
+ const webcamPath = await stopWebcamRecorder();
+ await finalizeRecordingSession(result.path, webcamPath);
+ return result.path;
+ }, [finalizeRecordingSession, stopWebcamRecorder]);
+
+ /**
+ * Acquire the webcam stream and prepare the MediaRecorder, but do NOT start
+ * recording yet. Call {@link beginWebcamCapture} after the main recording
+ * has started so both begin at approximately the same time.
+ */
+ const prepareWebcamRecorder = useCallback(async () => {
+ if (!webcamEnabled) {
+ resolvedWebcamPath.current = null;
+ pendingWebcamPathPromise.current = Promise.resolve(null);
+ webcamStartTime.current = null;
+ webcamTimeOffsetMs.current = 0;
+ return;
+ }
+
+ try {
+ webcamStream.current = await navigator.mediaDevices.getUserMedia({
+ video: webcamDeviceId
+ ? {
+ deviceId: { exact: webcamDeviceId },
+ width: { ideal: WEBCAM_WIDTH },
+ height: { ideal: WEBCAM_HEIGHT },
+ frameRate: { ideal: WEBCAM_FRAME_RATE, max: WEBCAM_FRAME_RATE },
+ }
+ : {
+ width: { ideal: WEBCAM_WIDTH },
+ height: { ideal: WEBCAM_HEIGHT },
+ frameRate: { ideal: WEBCAM_FRAME_RATE, max: WEBCAM_FRAME_RATE },
+ },
+ audio: false,
+ });
+
+ const mimeType = selectMimeType();
+ webcamChunks.current = [];
+ resolvedWebcamPath.current = null;
+ webcamStopPromise.current = new Promise((resolve) => {
+ webcamStopResolver.current = resolve;
+ });
+ pendingWebcamPathPromise.current = webcamStopPromise.current;
+
+ const recorder = new MediaRecorder(webcamStream.current, {
+ mimeType,
+ videoBitsPerSecond: WEBCAM_BITRATE,
+ });
+
+ webcamRecorder.current = recorder;
+ recorder.ondataavailable = (event) => {
+ if (event.data && event.data.size > 0) {
+ webcamChunks.current.push(event.data);
+ }
+ };
+ recorder.onerror = () => {
+ webcamStopResolver.current?.(null);
+ webcamStopResolver.current = null;
+ };
+ recorder.onstop = async () => {
+ const sessionTimestamp = recordingSessionTimestamp.current ?? Date.now();
+ const webcamFileName = `${RECORDING_FILE_PREFIX}${sessionTimestamp}${WEBCAM_SUFFIX}${VIDEO_FILE_EXTENSION}`;
+
+ try {
+ if (webcamChunks.current.length === 0) {
+ webcamStopResolver.current?.(null);
+ return;
+ }
+
+ const duration = Math.max(
+ 0,
+ getRecordingDurationMs(Date.now()) - webcamTimeOffsetMs.current,
+ );
+ const webcamBlob = new Blob(webcamChunks.current, { type: mimeType });
+ webcamChunks.current = [];
+ const fixedBlob = await fixWebmDuration(webcamBlob, duration);
+ const arrayBuffer = await fixedBlob.arrayBuffer();
+ const result = await window.electronAPI.storeRecordedVideo(
+ arrayBuffer,
+ webcamFileName,
+ );
+ webcamStopResolver.current?.(result.success ? (result.path ?? null) : null);
+ } catch (error) {
+ console.error("Error saving webcam recording:", error);
+ webcamStopResolver.current?.(null);
+ } finally {
+ webcamStopResolver.current = null;
+ webcamRecorder.current = null;
+ webcamStartTime.current = null;
+ if (webcamStream.current) {
+ webcamStream.current.getTracks().forEach((track) => track.stop());
+ webcamStream.current = null;
+ }
+ }
+ };
+ } catch (error) {
+ console.warn(
+ "Failed to start webcam recording; continuing without webcam layer:",
+ error,
+ );
+ resolvedWebcamPath.current = null;
+ pendingWebcamPathPromise.current = Promise.resolve(null);
+ webcamStopPromise.current = Promise.resolve(null);
+ webcamRecorder.current = null;
+ webcamStartTime.current = null;
+ webcamTimeOffsetMs.current = 0;
+ if (webcamStream.current) {
+ webcamStream.current.getTracks().forEach((track) => track.stop());
+ webcamStream.current = null;
+ }
+ }
+ }, [getRecordingDurationMs, webcamDeviceId, webcamEnabled]);
+
+ /** Start the prepared webcam MediaRecorder. Call after main recording begins. */
+ const beginWebcamCapture = useCallback(() => {
+ const recorder = webcamRecorder.current;
+ if (recorder && recorder.state === "inactive") {
+ webcamStartTime.current = Date.now();
+ recorder.start(RECORDER_TIMESLICE_MS);
+ }
+ }, []);
+
+ const stopRecording = useRef(() => {
+ setPaused(false);
+ if (nativeScreenRecording.current) {
+ nativeScreenRecording.current = false;
+ setRecording(false);
+
+ void (async () => {
+ showRecordingFinalizationToast();
+ const micFallbackBlobPromise = stopMicFallbackRecorder();
+ const webcamPath = await stopWebcamRecorder();
+ const isNativeWindows = nativeWindowsRecording.current;
+ markRecordingResumed(Date.now());
+ const pauseSegments = pauseSegmentsRef.current.slice();
+ nativeWindowsRecording.current = false;
+
+ const result = await window.electronAPI.stopNativeScreenRecording();
+ await window.electronAPI?.setRecordingState(false);
+
+ if (!result.success || !result.path) {
+ console.error(
+ "Failed to stop native screen recording:",
+ result.error ?? result.message,
+ );
+ void logNativeCaptureDiagnostics("stop-native-screen-recording");
+ try {
+ const recoveredPath = await recoverNativeRecordingSession();
+ if (recoveredPath) {
+ return;
+ }
+ } catch (recoveryError) {
+ console.error("Failed to recover native screen recording:", recoveryError);
+ }
+
+ const failureMessage = await buildNativeCaptureFailureMessage(
+ "stop-native-screen-recording",
+ isMacOS
+ ? "Failed to finish the macOS recording, so the editor was not opened."
+ : "Failed to finish the recording, so the editor was not opened.",
+ );
+ await notifyRecordingFinalizationFailure(failureMessage);
+ return;
+ }
+
+ let finalPath = result.path;
+
+ if (isNativeWindows) {
+ const muxResult =
+ await window.electronAPI.muxNativeWindowsRecording(pauseSegments);
+ if (!muxResult?.success) {
+ void logNativeCaptureDiagnostics("mux-native-windows-recording");
+ }
+ finalPath = muxResult?.path ?? result.path;
+ }
+
+ // Save the browser-captured microphone sidecar if one was recorded.
+ const micFallbackBlob = await micFallbackBlobPromise;
+ if (micFallbackBlob && finalPath) {
+ try {
+ const arrayBuffer = await micFallbackBlob.arrayBuffer();
+ await window.electronAPI.storeMicrophoneSidecar(arrayBuffer, finalPath);
+ } catch (e) {
+ console.warn("Failed to store microphone sidecar:", e);
+ }
+ }
+
+ await finalizeRecordingSession(finalPath, webcamPath);
+ })();
+ return;
+ }
+
+ const recorder = mediaRecorder.current;
+ const recorderState = recorder?.state;
+ if (recorder && (recorderState === "recording" || recorderState === "paused")) {
+ if (recorderState === "paused") {
+ markRecordingResumed(Date.now());
+ recorder.resume();
+ }
+ pendingWebcamPathPromise.current = stopWebcamRecorder();
+ cleanupCapturedMedia();
+ recorder.stop();
+ setRecording(false);
+ window.electronAPI?.setRecordingState(false);
+ }
+ });
+
+ useEffect(() => {
+ void (async () => {
+ const platform = await window.electronAPI.getPlatform();
+ setIsMacOS(platform === "darwin");
+ })();
+ }, []);
+
+ useEffect(() => {
+ if (countdownDelayLoaded.current) return;
+ countdownDelayLoaded.current = true;
+
+ void (async () => {
+ const result = await window.electronAPI.getCountdownDelay();
+ if (result.success && typeof result.delay === "number") {
+ setCountdownDelayState(result.delay);
+ }
+ })();
+ }, []);
+
+ const setCountdownDelay = useCallback((delay: number) => {
+ setCountdownDelayState(delay);
+ void window.electronAPI.setCountdownDelay(delay);
+ }, []);
+
+ useEffect(() => {
+ if (recordingPrefsLoaded.current) return;
+ recordingPrefsLoaded.current = true;
+
+ void (async () => {
+ const result = await window.electronAPI.getRecordingPreferences();
+ if (result.success) {
+ setMicrophoneEnabled(result.microphoneEnabled);
+ if (result.microphoneDeviceId) {
+ setMicrophoneDeviceId(result.microphoneDeviceId);
+ }
+ setSystemAudioEnabled(result.systemAudioEnabled);
+ }
+ })();
+ }, []);
+
+ const persistMicrophoneEnabled = useCallback((enabled: boolean) => {
+ setMicrophoneEnabled(enabled);
+ void window.electronAPI.setRecordingPreferences({ microphoneEnabled: enabled });
+ }, []);
+
+ const persistMicrophoneDeviceId = useCallback((deviceId: string | undefined) => {
+ setMicrophoneDeviceId(deviceId);
+ void window.electronAPI.setRecordingPreferences({ microphoneDeviceId: deviceId });
+ }, []);
+
+ const persistSystemAudioEnabled = useCallback((enabled: boolean) => {
+ setSystemAudioEnabled(enabled);
+ void window.electronAPI.setRecordingPreferences({ systemAudioEnabled: enabled });
+ }, []);
+
+ useEffect(() => {
+ let cleanup: (() => void) | undefined;
+
+ if (window.electronAPI?.onStopRecordingFromTray) {
+ cleanup = window.electronAPI.onStopRecordingFromTray(() => {
+ stopRecording.current();
+ });
+ }
+
+ const removeRecordingStateListener = window.electronAPI?.onRecordingStateChanged?.(
+ (state) => {
+ setRecording(state.recording);
+ },
+ );
+
+ const removeRecordingInterruptedListener = window.electronAPI?.onRecordingInterrupted?.(
+ (state) => {
+ void (async () => {
+ setRecording(false);
+ nativeScreenRecording.current = false;
+ cleanupCapturedMedia();
+ await window.electronAPI.setRecordingState(false);
+
+ if (state.reason !== "window-unavailable") {
+ try {
+ const recoveredPath = await recoverNativeRecordingSession();
+ if (recoveredPath) {
+ return;
+ }
+ } catch (recoveryError) {
+ console.error(
+ "Failed to recover interrupted native screen recording:",
+ recoveryError,
+ );
+ }
+ }
+
+ if (state.reason === "window-unavailable" && !hasPromptedForReselect.current) {
+ hasPromptedForReselect.current = true;
+ alert(state.message);
+ await window.electronAPI.openSourceSelector();
+ } else {
+ console.error(state.message);
+ toast.error(state.message);
+ }
+ })();
+ },
+ );
+
+ return () => {
+ cleanup?.();
+ removeRecordingStateListener?.();
+ removeRecordingInterruptedListener?.();
+
+ if (nativeScreenRecording.current) {
+ nativeScreenRecording.current = false;
+ void window.electronAPI.stopNativeScreenRecording();
+ }
+
+ if (mediaRecorder.current?.state === "recording") {
+ mediaRecorder.current.stop();
+ }
+
+ cleanupCapturedMedia();
+ };
+ }, [cleanupCapturedMedia, recoverNativeRecordingSession]);
+
+ const stopMicFallbackRecorder = useCallback((): Promise<Blob | null> => {
+ return new Promise((resolve) => {
+ const recorder = micFallbackRecorder.current;
+ if (!recorder || recorder.state === "inactive") {
+ micFallbackRecorder.current = null;
+ resolve(null);
+ return;
+ }
+ recorder.ondataavailable = (event) => {
+ if (event.data.size > 0) {
+ micFallbackChunks.current.push(event.data);
+ }
+ };
+ recorder.onstop = () => {
+ const blob =
+ micFallbackChunks.current.length > 0
+ ? new Blob(micFallbackChunks.current, { type: recorder.mimeType })
+ : null;
+ micFallbackChunks.current = [];
+ micFallbackRecorder.current = null;
+ resolve(blob);
+ };
+ recorder.stop();
+ });
+ }, []);
+
+ const startRecording = async () => {
+ if (startInFlight.current) {
+ return;
+ }
+
+ hasPromptedForReselect.current = false;
+ startInFlight.current = true;
+ setStarting(true);
+
+ try {
+ const selectedSource = await window.electronAPI.getSelectedSource();
+ if (!selectedSource) {
+ alert("Please select a source to record");
+ return;
+ }
+
+ const permissionsReady = await preparePermissions();
+ if (!permissionsReady) {
+ return;
+ }
+
+ recordingSessionTimestamp.current = Date.now();
+ resetRecordingClock(recordingSessionTimestamp.current);
+ await prepareWebcamRecorder();
+
+ const platform = await window.electronAPI.getPlatform();
+ const useNativeMacScreenCapture =
+ platform === "darwin" &&
+ (selectedSource.id?.startsWith("screen:") ||
+ selectedSource.id?.startsWith("window:")) &&
+ typeof window.electronAPI.startNativeScreenRecording === "function";
+
+ let useNativeWindowsCapture = false;
+ if (
+ platform === "win32" &&
+ (selectedSource.id?.startsWith("screen:") ||
+ selectedSource.id?.startsWith("window:")) &&
+ typeof window.electronAPI.isNativeWindowsCaptureAvailable === "function"
+ ) {
+ try {
+ const nativeWindowsResult =
+ await window.electronAPI.isNativeWindowsCaptureAvailable();
+ useNativeWindowsCapture = nativeWindowsResult.available;
+ if (!useNativeWindowsCapture && !hasShownNativeWindowsFallbackToast.current) {
+ void logNativeCaptureDiagnostics("is-native-windows-capture-available");
+ hasShownNativeWindowsFallbackToast.current = true;
+ toast.info(
+ "Native Windows capture is unavailable. Falling back to browser capture.",
+ );
+ }
+ } catch {
+ useNativeWindowsCapture = false;
+ if (!hasShownNativeWindowsFallbackToast.current) {
+ hasShownNativeWindowsFallbackToast.current = true;
+ toast.info(
+ "Unable to check native Windows capture. Falling back to browser capture.",
+ );
+ }
+ }
+ }
+
+ if (useNativeMacScreenCapture || useNativeWindowsCapture) {
+ // Resolve the selected mic label for native capture backends.
+ let micLabel: string | undefined;
+ if (microphoneEnabled) {
+ try {
+ const devices = await navigator.mediaDevices.enumerateDevices();
+ const mic = devices.find(
+ (d) => d.deviceId === microphoneDeviceId && d.kind === "audioinput",
+ );
+ micLabel = mic?.label || undefined;
+ } catch {
+ // Fall through — native process will use the default mic
+ }
+ }
+
+ const nativeResult = await window.electronAPI.startNativeScreenRecording(
+ selectedSource,
+ {
+ capturesSystemAudio: systemAudioEnabled,
+ capturesMicrophone: microphoneEnabled,
+ microphoneDeviceId,
+ microphoneLabel: micLabel,
+ },
+ );
+ if (!nativeResult.success) {
+ if (useNativeWindowsCapture) {
+ console.warn(
+ "Native Windows capture failed, falling back to browser capture:",
+ nativeResult.error ?? nativeResult.message,
+ );
+ void logNativeCaptureDiagnostics("start-native-screen-recording");
+ if (!hasShownNativeWindowsFallbackToast.current) {
+ hasShownNativeWindowsFallbackToast.current = true;
+ toast.warning(
+ "Native Windows capture failed to start. Falling back to browser capture.",
+ );
+ }
+ } else if (!nativeResult.userNotified) {
+ throw new Error(
+ nativeResult.error ??
+ nativeResult.message ??
+ "Failed to start native screen recording",
+ );
+ } else {
+ setRecording(false);
+ cleanupCapturedMedia();
+ await stopWebcamRecorder();
+ return;
+ }
+ }
+
+ if (nativeResult.success) {
+ const mainStartedAt = Date.now();
+ beginWebcamCapture();
+ nativeScreenRecording.current = true;
+ nativeWindowsRecording.current = useNativeWindowsCapture;
+ resetRecordingClock(mainStartedAt);
+ webcamTimeOffsetMs.current =
+ webcamStartTime.current === null
+ ? 0
+ : webcamStartTime.current - mainStartedAt;
+
+ // When native mic capture is unavailable (macOS < 14), record mic
+ // via browser getUserMedia so it can be saved as a sidecar file.
+ if (nativeResult.microphoneFallbackRequired && microphoneEnabled) {
+ try {
+ const micStream = await navigator.mediaDevices.getUserMedia({
+ audio: microphoneDeviceId
+ ? {
+ deviceId: { exact: microphoneDeviceId },
+ echoCancellation: true,
+ noiseSuppression: true,
+ autoGainControl: true,
+ }
+ : {
+ echoCancellation: true,
+ noiseSuppression: true,
+ autoGainControl: true,
+ },
+ video: false,
+ });
+ micFallbackChunks.current = [];
+ const recorder = new MediaRecorder(micStream, {
+ mimeType: "audio/webm;codecs=opus",
+ });
+ recorder.ondataavailable = (event) => {
+ if (event.data.size > 0) {
+ micFallbackChunks.current.push(event.data);
+ }
+ };
+ recorder.start(1000);
+ micFallbackRecorder.current = recorder;
+ } catch (micError) {
+ console.warn("Browser microphone fallback failed:", micError);
+ }
+ }
+
+ setRecording(true);
+ window.electronAPI?.setRecordingState(true);
+
+ return;
+ }
+ }
+
+ const wantsAudioCapture = microphoneEnabled || systemAudioEnabled;
+ const browserCaptureSource = await resolveBrowserCaptureSource(selectedSource);
+
+ if (
+ browserCaptureSource?.id?.startsWith("screen:fallback:") ||
+ browserCaptureSource?.id?.startsWith("window:fallback:")
+ ) {
+ throw new Error(
+ "Selected display is not available for browser capture on this system.",
+ );
+ }
+
+ try {
+ await window.electronAPI.hideOsCursor?.();
+ } catch {
+ console.warn("Could not hide OS cursor before recording.");
+ }
+
+ let videoTrack: MediaStreamTrack | undefined;
+ let systemAudioIncluded = false;
+ const mediaDevices = navigator.mediaDevices as DesktopCaptureMediaDevices;
+ const browserScreenVideoConstraints = {
+ mandatory: {
+ chromeMediaSource: CHROME_MEDIA_SOURCE,
+ chromeMediaSourceId: browserCaptureSource.id,
+ maxWidth: TARGET_WIDTH,
+ maxHeight: TARGET_HEIGHT,
+ maxFrameRate: TARGET_FRAME_RATE,
+ minFrameRate: MIN_FRAME_RATE,
+ googCaptureCursor: false,
+ },
+ cursor: "never" as const,
+ };
+
+ if (wantsAudioCapture) {
+ let screenMediaStream: MediaStream;
+
+ if (systemAudioEnabled) {
+ try {
+ screenMediaStream = await mediaDevices.getUserMedia({
+ audio: {
+ mandatory: {
+ chromeMediaSource: CHROME_MEDIA_SOURCE,
+ chromeMediaSourceId: browserCaptureSource.id,
+ },
+ },
+ video: browserScreenVideoConstraints,
+ });
+ } catch (audioError) {
+ console.warn(
+ "System audio capture failed, falling back to video-only:",
+ audioError,
+ );
+ alert(
+ "System audio is not available for this source. Recording will continue without system audio.",
+ );
+ screenMediaStream = await mediaDevices.getUserMedia({
+ audio: false,
+ video: browserScreenVideoConstraints,
+ });
+ }
+ } else {
+ screenMediaStream = await mediaDevices.getUserMedia({
+ audio: false,
+ video: browserScreenVideoConstraints,
+ });
+ }
+
+ screenStream.current = screenMediaStream;
+ stream.current = new MediaStream();
+
+ videoTrack = screenMediaStream.getVideoTracks()[0];
+ if (!videoTrack) {
+ throw new Error("Video track is not available.");
+ }
+
+ stream.current.addTrack(videoTrack);
+
+ if (microphoneEnabled) {
+ try {
+ microphoneStream.current = await navigator.mediaDevices.getUserMedia({
+ audio: microphoneDeviceId
+ ? {
+ deviceId: { exact: microphoneDeviceId },
+ echoCancellation: true,
+ noiseSuppression: true,
+ autoGainControl: true,
+ }
+ : {
+ echoCancellation: true,
+ noiseSuppression: true,
+ autoGainControl: true,
+ },
+ video: false,
+ });
+ } catch (audioError) {
+ console.warn("Failed to get microphone access:", audioError);
+ alert(
+ "Microphone access was denied. Recording will continue without microphone audio.",
+ );
+ setMicrophoneEnabled(false);
+ }
+ }
+
+ const systemAudioTrack = screenMediaStream.getAudioTracks()[0];
+ const micAudioTrack = microphoneStream.current?.getAudioTracks()[0];
+
+ if (systemAudioTrack && micAudioTrack) {
+ const context = new AudioContext();
+ mixingContext.current = context;
+ const systemSource = context.createMediaStreamSource(
+ new MediaStream([systemAudioTrack]),
+ );
+ const micSource = context.createMediaStreamSource(
+ new MediaStream([micAudioTrack]),
+ );
+ const micGain = context.createGain();
+ micGain.gain.value = MIC_GAIN_BOOST;
+ const destination = context.createMediaStreamDestination();
+
+ systemSource.connect(destination);
+ micSource.connect(micGain).connect(destination);
+
+ const mixedTrack = destination.stream.getAudioTracks()[0];
+ if (mixedTrack) {
+ stream.current.addTrack(mixedTrack);
+ systemAudioIncluded = true;
+ }
+ } else if (systemAudioTrack) {
+ stream.current.addTrack(systemAudioTrack);
+ systemAudioIncluded = true;
+ } else if (micAudioTrack) {
+ stream.current.addTrack(micAudioTrack);
+ }
+ } else {
+ const mediaStream = await mediaDevices.getDisplayMedia({
+ audio: false,
+ video: {
+ displaySurface: selectedSource.id?.startsWith("window:")
+ ? "window"
+ : "monitor",
+ width: { ideal: TARGET_WIDTH, max: TARGET_WIDTH },
+ height: { ideal: TARGET_HEIGHT, max: TARGET_HEIGHT },
+ frameRate: { ideal: TARGET_FRAME_RATE, max: TARGET_FRAME_RATE },
+ cursor: "never",
+ },
+ selfBrowserSurface: "exclude",
+ surfaceSwitching: "exclude",
+ });
+
+ stream.current = mediaStream;
+ videoTrack = mediaStream.getVideoTracks()[0];
+ }
+
+ if (!stream.current || !videoTrack) {
+ throw new Error("Media stream is not available.");
+ }
+
+ try {
+ await videoTrack.applyConstraints({
+ frameRate: { ideal: TARGET_FRAME_RATE, max: TARGET_FRAME_RATE },
+ width: { ideal: TARGET_WIDTH, max: TARGET_WIDTH },
+ height: { ideal: TARGET_HEIGHT, max: TARGET_HEIGHT },
+ } as MediaTrackConstraints);
+ } catch (error) {
+ console.warn(
+ "Unable to lock 4K/60fps constraints, using best available track settings.",
+ error,
+ );
+ }
+
+ let {
+ width = DEFAULT_WIDTH,
+ height = DEFAULT_HEIGHT,
+ frameRate = TARGET_FRAME_RATE,
+ } = videoTrack.getSettings();
+
+ width = Math.floor(width / CODEC_ALIGNMENT) * CODEC_ALIGNMENT;
+ height = Math.floor(height / CODEC_ALIGNMENT) * CODEC_ALIGNMENT;
+
+ const videoBitsPerSecond = computeBitrate(width, height);
+ const mimeType = selectMimeType();
+
+ console.log(
+ `Recording at ${width}x${height} @ ${frameRate ?? TARGET_FRAME_RATE}fps using ${mimeType} / ${Math.round(
+ videoBitsPerSecond / BITS_PER_MEGABIT,
+ )} Mbps`,
+ );
+
+ chunks.current = [];
+ const hasAudio = stream.current.getAudioTracks().length > 0;
+ const recorder = new MediaRecorder(stream.current, {
+ mimeType,
+ videoBitsPerSecond,
+ ...(hasAudio
+ ? {
+ audioBitsPerSecond: systemAudioIncluded
+ ? AUDIO_BITRATE_SYSTEM
+ : AUDIO_BITRATE_VOICE,
+ }
+ : {}),
+ });
+
+ mediaRecorder.current = recorder;
+ recorder.ondataavailable = (event) => {
+ if (event.data && event.data.size > 0) chunks.current.push(event.data);
+ };
+ recorder.onstop = async () => {
+ cleanupCapturedMedia();
+ if (chunks.current.length === 0) return;
+
+ showRecordingFinalizationToast();
+
+ const duration = getRecordingDurationMs(Date.now());
+ const recordedChunks = chunks.current;
+ const buggyBlob = new Blob(recordedChunks, { type: mimeType });
+ chunks.current = [];
+ const timestamp = recordingSessionTimestamp.current ?? Date.now();
+ const videoFileName = `${RECORDING_FILE_PREFIX}${timestamp}${VIDEO_FILE_EXTENSION}`;
+
+ try {
+ const videoBlob = await fixWebmDuration(buggyBlob, duration);
+ const arrayBuffer = await videoBlob.arrayBuffer();
+ const videoResult = await window.electronAPI.storeRecordedVideo(
+ arrayBuffer,
+ videoFileName,
+ );
+ if (!videoResult.success) {
+ console.error("Failed to store video:", videoResult.message);
+ await notifyRecordingFinalizationFailure(
+ videoResult.message || "Failed to store the recording.",
+ );
+ return;
+ }
+
+ if (videoResult.path) {
+ const webcamPath = pendingWebcamPathPromise.current
+ ? await pendingWebcamPathPromise.current
+ : resolvedWebcamPath.current;
+ await finalizeRecordingSession(videoResult.path, webcamPath);
+ } else {
+ await notifyRecordingFinalizationFailure("Failed to save the recording.");
+ }
+ } catch (error) {
+ console.error("Error saving recording:", error);
+ const message = error instanceof Error ? error.message : String(error);
+ await notifyRecordingFinalizationFailure(
+ `Failed to finalize the recording. ${message}`,
+ );
+ }
+ };
+ recorder.onerror = () => {
+ setRecording(false);
+ };
+ const mainStartedAt = Date.now();
+ beginWebcamCapture();
+ resetRecordingClock(mainStartedAt);
+ webcamTimeOffsetMs.current =
+ webcamStartTime.current === null ? 0 : webcamStartTime.current - mainStartedAt;
+ recorder.start(RECORDER_TIMESLICE_MS);
+ setRecording(true);
+ window.electronAPI?.setRecordingState(true);
+ } catch (error) {
+ console.error("Failed to start recording:", error);
+ alert(
+ error instanceof Error
+ ? `Failed to start recording: ${error.message}`
+ : "Failed to start recording",
+ );
+ setRecording(false);
+ cleanupCapturedMedia();
+ await stopWebcamRecorder();
+ } finally {
+ startInFlight.current = false;
+ setStarting(false);
+ }
+ };
+
+ const pauseRecording = useCallback(() => {
+ if (!recording || paused) return;
+ if (nativeScreenRecording.current) {
+ void (async () => {
+ const result = await window.electronAPI.pauseNativeScreenRecording();
+ if (!result.success) {
+ console.error(
+ "Failed to pause native screen recording:",
+ result.error ?? result.message,
+ );
+ return;
+ }
+
+ if (webcamRecorder.current?.state === "recording") {
+ webcamRecorder.current.pause();
+ }
+ markRecordingPaused(Date.now());
+ setPaused(true);
+ })();
+ return;
+ }
+ if (mediaRecorder.current?.state === "recording") {
+ mediaRecorder.current.pause();
+ if (webcamRecorder.current?.state === "recording") {
+ webcamRecorder.current.pause();
+ }
+ markRecordingPaused(Date.now());
+ setPaused(true);
+ }
+ }, [markRecordingPaused, paused, recording]);
+
+ const resumeRecording = useCallback(() => {
+ if (!recording || !paused) return;
+ if (nativeScreenRecording.current) {
+ void (async () => {
+ const result = await window.electronAPI.resumeNativeScreenRecording();
+ if (!result.success) {
+ console.error(
+ "Failed to resume native screen recording:",
+ result.error ?? result.message,
+ );
+ return;
+ }
+
+ if (webcamRecorder.current?.state === "paused") {
+ webcamRecorder.current.resume();
+ }
+ markRecordingResumed(Date.now());
+ setPaused(false);
+ })();
+ return;
+ }
+ if (mediaRecorder.current?.state === "paused") {
+ mediaRecorder.current.resume();
+ if (webcamRecorder.current?.state === "paused") {
+ webcamRecorder.current.resume();
+ }
+ markRecordingResumed(Date.now());
+ setPaused(false);
+ }
+ }, [markRecordingResumed, paused, recording]);
+
+ const cancelRecording = useCallback(() => {
+ if (!recording) return;
+ setPaused(false);
+ markRecordingResumed(Date.now());
+
+ // Discard webcam recording regardless of recording mode
+ webcamChunks.current = [];
+ if (webcamRecorder.current && webcamRecorder.current.state !== "inactive") {
+ webcamRecorder.current.stop();
+ }
+ webcamRecorder.current = null;
+ webcamStartTime.current = null;
+ webcamTimeOffsetMs.current = 0;
+ webcamStream.current?.getTracks().forEach((t) => t.stop());
+ webcamStream.current = null;
+ pendingWebcamPathPromise.current = null;
+ resolvedWebcamPath.current = null;
+
+ if (nativeScreenRecording.current) {
+ nativeScreenRecording.current = false;
+ nativeWindowsRecording.current = false;
+ setRecording(false);
+ window.electronAPI?.setRecordingState(false);
+ void (async () => {
+ try {
+ const result = await window.electronAPI.stopNativeScreenRecording();
+ if (result?.path) {
+ await window.electronAPI.deleteRecordingFile(result.path);
+ }
+ } catch {
+ // Best-effort cleanup
+ }
+ })();
+ return;
+ }
+
+ if (mediaRecorder.current) {
+ chunks.current = [];
+ cleanupCapturedMedia();
+ if (mediaRecorder.current.state !== "inactive") {
+ mediaRecorder.current.stop();
+ }
+ setRecording(false);
+ window.electronAPI?.setRecordingState(false);
+ }
+ }, [cleanupCapturedMedia, markRecordingResumed, recording]);
+
+ const toggleRecording = async () => {
+ if (starting || countdownActive) {
+ return;
+ }
+
+ if (recording) {
+ stopRecording.current();
+ return;
+ }
+
+ // Start recording with optional countdown
+ if (countdownDelay > 0) {
+ setCountdownActive(true);
+ try {
+ const result = await window.electronAPI.startCountdown(countdownDelay);
+ if (!result.success || result.cancelled) {
+ return;
+ }
+ } finally {
+ setCountdownActive(false);
+ }
+ }
+
+ startRecording();
+ };
+
+ return {
+ recording,
+ paused,
+ countdownActive,
+ toggleRecording,
+ pauseRecording,
+ resumeRecording,
+ cancelRecording,
+ preparePermissions,
+ isMacOS,
+ microphoneEnabled,
+ setMicrophoneEnabled: persistMicrophoneEnabled,
+ microphoneDeviceId,
+ setMicrophoneDeviceId: persistMicrophoneDeviceId,
+ systemAudioEnabled,
+ setSystemAudioEnabled: persistSystemAudioEnabled,
+ webcamEnabled,
+ setWebcamEnabled,
+ webcamDeviceId,
+ setWebcamDeviceId,
+ countdownDelay,
+ setCountdownDelay,
+ };
}
diff --git a/src/hooks/useVideoDevices.ts b/src/hooks/useVideoDevices.ts
index 228e51f5..3a1ebd81 100644
--- a/src/hooks/useVideoDevices.ts
+++ b/src/hooks/useVideoDevices.ts
@@ -1,107 +1,113 @@
-import { useEffect, useState } from 'react'
+import { useEffect, useState } from "react";
export interface VideoDevice {
- deviceId: string
- label: string
- groupId: string
+ deviceId: string;
+ label: string;
+ groupId: string;
}
-let hasRequestedVideoLabels = false
+let hasRequestedVideoLabels = false;
export function useVideoDevices(enabled: boolean = true) {
- const [devices, setDevices] = useState<VideoDevice[]>([])
- const [selectedDeviceId, setSelectedDeviceId] = useState<string>('default')
- const [isLoading, setIsLoading] = useState(false)
- const [error, setError] = useState<string | null>(null)
-
- useEffect(() => {
- if (!enabled) {
- return
- }
-
- let mounted = true
-
- const loadDevices = async () => {
- let permissionStream: MediaStream | null = null
-
- try {
- setIsLoading(true)
- setError(null)
-
- let allDevices = await navigator.mediaDevices.enumerateDevices()
- let videoInputs = allDevices
- .filter((device) => device.kind === 'videoinput')
- .map((device, index) => ({
- deviceId: device.deviceId,
- label: device.label || `Camera ${index + 1}`,
- groupId: device.groupId,
- }))
-
- const needsLabelPermission =
- videoInputs.length > 0 && videoInputs.every((device) => !device.label.trim())
-
- if (needsLabelPermission && !hasRequestedVideoLabels) {
- hasRequestedVideoLabels = true
- permissionStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false })
- allDevices = await navigator.mediaDevices.enumerateDevices()
- videoInputs = allDevices
- .filter((device) => device.kind === 'videoinput')
- .map((device, index) => ({
- deviceId: device.deviceId,
- label: device.label || `Camera ${index + 1}`,
- groupId: device.groupId,
- }))
- }
-
- if (mounted) {
- setDevices(videoInputs)
- setSelectedDeviceId((currentDeviceId) => {
- if (currentDeviceId === 'default' && videoInputs.length > 0) {
- return videoInputs[0].deviceId
- }
-
- if (
- currentDeviceId !== 'default' &&
- videoInputs.some((device) => device.deviceId === currentDeviceId)
- ) {
- return currentDeviceId
- }
-
- return videoInputs[0]?.deviceId ?? 'default'
- })
- setIsLoading(false)
- }
- } catch (error) {
- if (mounted) {
- const message = error instanceof Error ? error.message : 'Failed to enumerate video devices'
- setError(message)
- setIsLoading(false)
- console.error('Error loading video devices:', error)
- }
- } finally {
- permissionStream?.getTracks().forEach((track) => track.stop())
- }
- }
-
- void loadDevices()
-
- const handleDeviceChange = () => {
- void loadDevices()
- }
-
- navigator.mediaDevices.addEventListener('devicechange', handleDeviceChange)
-
- return () => {
- mounted = false
- navigator.mediaDevices.removeEventListener('devicechange', handleDeviceChange)
- }
- }, [enabled])
-
- return {
- devices,
- selectedDeviceId,
- setSelectedDeviceId,
- isLoading,
- error,
- }
+ const [devices, setDevices] = useState<VideoDevice[]>([]);
+ const [selectedDeviceId, setSelectedDeviceId] = useState<string>("default");
+ const [isLoading, setIsLoading] = useState(false);
+ const [error, setError] = useState<string | null>(null);
+
+ useEffect(() => {
+ if (!enabled) {
+ return;
+ }
+
+ let mounted = true;
+
+ const loadDevices = async () => {
+ let permissionStream: MediaStream | null = null;
+
+ try {
+ setIsLoading(true);
+ setError(null);
+
+ let allDevices = await navigator.mediaDevices.enumerateDevices();
+ let videoInputs = allDevices
+ .filter((device) => device.kind === "videoinput")
+ .map((device, index) => ({
+ deviceId: device.deviceId,
+ label: device.label || `Camera ${index + 1}`,
+ groupId: device.groupId,
+ }));
+
+ const needsLabelPermission =
+ videoInputs.length > 0 && videoInputs.every((device) => !device.label.trim());
+
+ if (needsLabelPermission && !hasRequestedVideoLabels) {
+ hasRequestedVideoLabels = true;
+ permissionStream = await navigator.mediaDevices.getUserMedia({
+ video: true,
+ audio: false,
+ });
+ allDevices = await navigator.mediaDevices.enumerateDevices();
+ videoInputs = allDevices
+ .filter((device) => device.kind === "videoinput")
+ .map((device, index) => ({
+ deviceId: device.deviceId,
+ label: device.label || `Camera ${index + 1}`,
+ groupId: device.groupId,
+ }));
+ }
+
+ if (mounted) {
+ setDevices(videoInputs);
+ setSelectedDeviceId((currentDeviceId) => {
+ if (currentDeviceId === "default" && videoInputs.length > 0) {
+ return videoInputs[0].deviceId;
+ }
+
+ if (
+ currentDeviceId !== "default" &&
+ videoInputs.some((device) => device.deviceId === currentDeviceId)
+ ) {
+ return currentDeviceId;
+ }
+
+ return videoInputs[0]?.deviceId ?? "default";
+ });
+ setIsLoading(false);
+ }
+ } catch (error) {
+ if (mounted) {
+ const message =
+ error instanceof Error
+ ? error.message
+ : "Failed to enumerate video devices";
+ setError(message);
+ setIsLoading(false);
+ console.error("Error loading video devices:", error);
+ }
+ } finally {
+ permissionStream?.getTracks().forEach((track) => track.stop());
+ }
+ };
+
+ void loadDevices();
+
+ const handleDeviceChange = () => {
+ void loadDevices();
+ };
+
+ navigator.mediaDevices.addEventListener("devicechange", handleDeviceChange);
+
+ return () => {
+ mounted = false;
+ navigator.mediaDevices.removeEventListener("devicechange", handleDeviceChange);
+ };
+ }, [enabled]);
+
+ return {
+ devices,
+ selectedDeviceId,
+ setSelectedDeviceId,
+ isLoading,
+ error,
+ };
}