Skip to content

🎥 Make videos programmatically with React

Make drag-aware double-click handling deterministic

JonnyBurger859e6dfa2e6c
diff --git a/packages/example/e2e/double-click-drag.test.mts b/packages/example/e2e/double-click-drag.test.mts
deleted file mode 100644
index 0e591e53c4..0000000000
--- a/packages/example/e2e/double-click-drag.test.mts
+++ /dev/null
@@ -1,77 +0,0 @@
-import fs from 'fs';
-import path from 'path';
-import {expect, test} from '@playwright/test';
-import {EXPANDED_SIDEBAR_STATE, STUDIO_URL, exampleDir} from './constants.mts';
-import {startStudio, stopStudio} from './studio-server.mts';
-
-test.use({storageState: EXPANDED_SIDEBAR_STATE});
-
-const doubleClickDragFile = path.join(
- exampleDir,
- 'src',
- 'VisualModeTests',
- 'DoubleClickDragRepro.tsx',
-);
-
-test.describe('double-clicking a timeline sequence', () => {
- let sourceBefore: string;
-
- test.beforeEach(async () => {
- sourceBefore = fs.readFileSync(doubleClickDragFile, 'utf-8');
- await startStudio();
- });
-
- test.afterEach(async () => {
- await stopStudio();
- fs.writeFileSync(doubleClickDragFile, sourceBefore);
- });
-
- test('a double-click whose second press drags the sequence moves it without opening the connected composition', async ({
- page,
- }) => {
- await page.goto(`${STUDIO_URL}/double-click-drag-repro`);
- await page.waitForFunction(
- () => !document.body.innerText.includes('Loading...'),
- {timeout: 30_000},
- );
-
- const sequence = page.locator(
- '[data-timeline-marquee-item][title="Double click drag target"]',
- );
- await expect(sequence).toBeVisible({timeout: 15_000});
- const box = await sequence.boundingBox();
- if (box === null) {
- throw new Error('Expected the sequence to have a bounding box');
- }
-
- // The sequence is 30 frames wide, so box.width / 30 is one frame.
- const fourFrames = (box.width / 30) * 4;
- const centerX = box.x + box.width / 2;
- const centerY = box.y + box.height / 2;
-
- // A double-click where the second press turns into a drag: the browser
- // still fires `dblclick` on the second release.
- await page.mouse.move(centerX, centerY);
- await page.mouse.down();
- await page.mouse.up();
- await page.mouse.down({clickCount: 2});
- await page.mouse.move(centerX + fourFrames, centerY, {steps: 5});
- await page.mouse.up({clickCount: 2});
-
- // The drag committed...
- await expect
- .poll(() => fs.readFileSync(doubleClickDragFile, 'utf-8'), {
- timeout: 15_000,
- })
- .toContain('from={6}');
- // ...but the gesture did not open the connected composition.
- await expect(page).toHaveURL(/double-click-drag-repro/);
-
- // A plain double-click still opens the connected composition.
- await expect(sequence).toBeVisible({timeout: 15_000});
- await sequence.dblclick();
- await expect(page).toHaveURL(/double-click-drag-child/, {
- timeout: 15_000,
- });
- });
-});
diff --git a/packages/example/src/E2eTestRoot.tsx b/packages/example/src/E2eTestRoot.tsx
index b2ba1ee859..b5a0bbcb4e 100644
--- a/packages/example/src/E2eTestRoot.tsx
+++ b/packages/example/src/E2eTestRoot.tsx
@@ -19,10 +19,6 @@ import {TimelineVirtualizationTestbed} from './TimelineVirtualizationTestbed';
import {VisualControls} from './VisualControls';
import {VisualMode3D} from './VisualMode3D';
import {AffineFrameClock} from './VisualModeTests/AffineFrameClock';
-import {
- DoubleClickDragChild,
- DoubleClickDragRepro,
-} from './VisualModeTests/DoubleClickDragRepro';
import {OutlineSelectionCases} from './VisualModeTests/OutlineSelectionCases';
import {SequenceShiftRepro} from './VisualModeTests/SequenceShiftRepro';
@@ -212,22 +208,6 @@ export const E2eTestRoot: React.FC = () => {
fps={30}
durationInFrames={60}
/>
- <Composition
- id="double-click-drag-repro"
- component={DoubleClickDragRepro}
- width={1280}
- height={720}
- fps={30}
- durationInFrames={60}
- />
- <Composition
- id="double-click-drag-child"
- component={DoubleClickDragChild}
- width={1280}
- height={720}
- fps={30}
- durationInFrames={30}
- />
<Composition
id="timeline-virtualization-testbed"
component={TimelineVirtualizationTestbed}
diff --git a/packages/example/src/VisualModeTests/DoubleClickDragRepro.tsx b/packages/example/src/VisualModeTests/DoubleClickDragRepro.tsx
deleted file mode 100644
index e61cf23b0e..0000000000
--- a/packages/example/src/VisualModeTests/DoubleClickDragRepro.tsx
+++ /dev/null
@@ -1,15 +0,0 @@
-import {AbsoluteFill, Sequence} from 'remotion';
-
-export const DoubleClickDragChild = () => {
- return <AbsoluteFill style={{backgroundColor: 'seagreen'}} />;
-};
-
-export const DoubleClickDragRepro = () => {
- return (
- <AbsoluteFill style={{backgroundColor: 'black'}}>
- <Sequence name="Double click drag target" from={2} durationInFrames={30}>
- <DoubleClickDragChild />
- </Sequence>
- </AbsoluteFill>
- );
-};
diff --git a/packages/studio/src/components/SelectedOutlineElement.tsx b/packages/studio/src/components/SelectedOutlineElement.tsx
index 29b1f82604..81dbf5cf44 100644
--- a/packages/studio/src/components/SelectedOutlineElement.tsx
+++ b/packages/studio/src/components/SelectedOutlineElement.tsx
@@ -136,7 +136,11 @@ const SelectedOutlineElementUnmemoized: React.FC<
);
const onDoubleClickTarget = React.useCallback(
- (doubleClickTarget: SelectedOutlineTarget, button: number) => {
+ (
+ doubleClickTarget: SelectedOutlineTarget,
+ button: number,
+ sequenceWasDragged: boolean,
+ ) => {
const connectedCompositions = getConnectedCompositions({
compositions,
singleChildComponent: doubleClickTarget.sequence.singleChildComponent,
@@ -145,7 +149,7 @@ const SelectedOutlineElementUnmemoized: React.FC<
button,
canOpenInEditor: editorAvailable,
numberOfConnectedCompositions: connectedCompositions.length,
- sequenceWasDragged: false,
+ sequenceWasDragged,
});
if (action === null) {
diff --git a/packages/studio/src/components/SelectedOutlinePolygon.tsx b/packages/studio/src/components/SelectedOutlinePolygon.tsx
index 6de311162a..533e840ed0 100644
--- a/packages/studio/src/components/SelectedOutlinePolygon.tsx
+++ b/packages/studio/src/components/SelectedOutlinePolygon.tsx
@@ -6,6 +6,7 @@ import {
TIMELINE_DROP_BLUE_ALPHA_12,
TRANSPARENT,
} from '../helpers/colors';
+import {createDragAwareDoubleClickTracker} from '../helpers/drag-aware-double-click';
import {isStudioInteractivityEnabled} from '../helpers/interactivity-enabled';
import {
startCapturedPointerSession,
@@ -89,6 +90,7 @@ const SelectedOutlinePolygonUnmemoized: React.FC<{
readonly onDoubleClickTarget: (
target: SelectedOutlineTarget,
button: number,
+ sequenceWasDragged: boolean,
) => boolean;
readonly scale: number;
readonly showSelectedOutline: boolean;
@@ -116,6 +118,10 @@ const SelectedOutlinePolygonUnmemoized: React.FC<{
showSelectedOutline,
}) => {
const {previewServerState} = useContext(StudioServerConnectionCtx);
+ const dragAwareDoubleClick = useMemo(
+ () => createDragAwareDoubleClickTracker(),
+ [],
+ );
const {canvasContent} = useContext(Internals.CompositionManager);
const {getDragOverrides} = useContext(
Internals.VisualModeDragOverridesContext,
@@ -373,6 +379,7 @@ const SelectedOutlinePolygonUnmemoized: React.FC<{
};
const onPointerUp = (reason: PointerSessionEndReason) => {
+ dragAwareDoubleClick.endPointerGesture(dragStarted);
window.removeEventListener('keydown', onKeyChange);
window.removeEventListener('keyup', onKeyChange);
if (dragStarted) {
@@ -457,6 +464,7 @@ const SelectedOutlinePolygonUnmemoized: React.FC<{
compositionHeight,
compositionWidth,
containsSelection,
+ dragAwareDoubleClick,
editorShowGuides,
editorSnapping,
getAllDragOutlines,
@@ -481,14 +489,20 @@ const SelectedOutlinePolygonUnmemoized: React.FC<{
return;
}
- if (!onDoubleClickTarget(target, event.button)) {
+ if (
+ !onDoubleClickTarget(
+ target,
+ event.button,
+ dragAwareDoubleClick.consumePointerGestureWasDragged(),
+ )
+ ) {
return;
}
event.preventDefault();
event.stopPropagation();
},
- [getTarget, onDoubleClickTarget],
+ [dragAwareDoubleClick, getTarget, onDoubleClickTarget],
);
const onEffectDragOver = React.useCallback(
@@ -584,6 +598,7 @@ const SelectedOutlinePolygonUnmemoized: React.FC<{
}
}}
onPointerDown={onPointerDown}
+ onPointerDownCapture={dragAwareDoubleClick.beginPointerGesture}
onDoubleClick={onDoubleClick}
onDragOver={onEffectDragOver}
onDragLeave={onEffectDragLeave}
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index f4a2ec777e..7a20134af3 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -12,6 +12,7 @@ import {
WHITE_ALPHA_20,
WHITE_ALPHA_50,
} from '../../helpers/colors';
+import {createDragAwareDoubleClickTracker} from '../../helpers/drag-aware-double-click';
import {
getConnectedCompositionFrame,
getSequenceDoubleClickAction,
@@ -57,7 +58,6 @@ import {
import {TimelineSequenceFrame} from './TimelineSequenceFrame';
import {
canResizeTimelineSequenceDuration,
- didTimelineSequenceDragJustMove,
isCascadingSequence,
isTimelineSequenceDurationDraggable,
isTimelineSequenceLeftEdgeDraggable,
@@ -108,6 +108,7 @@ const TimelineSequenceCurrentFrame: React.FC<{
readonly onMoveDragPointerDown: (
e: React.PointerEvent<HTMLDivElement>,
) => void;
+ readonly onPointerDownCapture: () => void;
readonly onDoubleClick?: (e: React.MouseEvent<HTMLDivElement>) => void;
}> = ({
s,
@@ -121,6 +122,7 @@ const TimelineSequenceCurrentFrame: React.FC<{
fromCanUpdate,
frozenFrame,
onMoveDragPointerDown,
+ onPointerDownCapture,
onDoubleClick,
}) => {
const ref = useRef<HTMLDivElement>(null);
@@ -191,6 +193,7 @@ const TimelineSequenceCurrentFrame: React.FC<{
{...{[TIMELINE_MARQUEE_ITEM_ATTR]: true}}
style={actualStyle}
title={s.displayName}
+ onPointerDownCapture={onPointerDownCapture}
onPointerDown={selectable ? onPointerDown : undefined}
onDoubleClick={onDoubleClick}
>
@@ -276,6 +279,10 @@ const TimelineSequenceInner: React.FC<{
const video = Internals.useVideo();
const renderWindow = useContext(TimelineViewportContext);
+ const dragAwareDoubleClick = useMemo(
+ () => createDragAwareDoubleClickTracker(),
+ [],
+ );
const maxMediaDuration = useMaxMediaDuration(s, video?.fps ?? 30);
const effectiveMaxMediaDuration = s.loopDisplay ? null : maxMediaDuration;
@@ -352,7 +359,8 @@ const TimelineSequenceInner: React.FC<{
button: e.button,
canOpenInEditor,
numberOfConnectedCompositions: connectedCompositions.length,
- sequenceWasDragged: didTimelineSequenceDragJustMove(),
+ sequenceWasDragged:
+ dragAwareDoubleClick.consumePointerGestureWasDragged(),
});
if (action === null) {
return;
@@ -378,6 +386,7 @@ const TimelineSequenceInner: React.FC<{
[
canOpenInEditor,
connectedCompositions,
+ dragAwareDoubleClick,
openInEditor,
s,
selectComposition,
@@ -561,6 +570,7 @@ const TimelineSequenceInner: React.FC<{
nodePathInfo,
windowWidth,
timelineDurationInFrames: video?.durationInFrames ?? 1,
+ onDragEnd: dragAwareDoubleClick.endPointerGesture,
});
if (!video) {
@@ -680,6 +690,7 @@ const TimelineSequenceInner: React.FC<{
fromCanUpdate={fromCanUpdate}
frozenFrame={frozenFrame}
onMoveDragPointerDown={onMoveDragPointerDown}
+ onPointerDownCapture={dragAwareDoubleClick.beginPointerGesture}
onDoubleClick={
canHandleSequenceDoubleClick ? onSequenceDoubleClick : undefined
}
@@ -746,6 +757,7 @@ const TimelineSequenceInner: React.FC<{
nodePathInfo={nodePathInfo}
windowWidth={windowWidth}
timelineDurationInFrames={video.durationInFrames ?? 1}
+ onDragEnd={dragAwareDoubleClick.endPointerGesture}
/>
) : null}
{showRightEdgeDragHandle &&
@@ -756,6 +768,7 @@ const TimelineSequenceInner: React.FC<{
nodePathInfo={nodePathInfo}
windowWidth={windowWidth}
timelineDurationInFrames={video.durationInFrames ?? 1}
+ onDragEnd={dragAwareDoubleClick.endPointerGesture}
/>
) : null}
</TimelineSequenceCurrentFrame>
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
index 468d745072..f93c096440 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceRightEdgeDragHandle.tsx
@@ -49,18 +49,6 @@ import {
const HANDLE_WIDTH = 6;
export const timelineSequenceFromDragSnapThresholdPx = 10;
-// The browser fires `dblclick` even if the second press of a double-click
-// turned into a drag. Double-click handlers consult this to ignore those
-// gestures.
-let lastSequenceDragEnd = {endedAt: 0, moved: false};
-
-const markSequenceDragEnd = (moved: boolean) => {
- lastSequenceDragEnd = {endedAt: Date.now(), moved};
-};
-
-export const didTimelineSequenceDragJustMove = () =>
- lastSequenceDragEnd.moved && Date.now() - lastSequenceDragEnd.endedAt < 500;
-
const baseStyle: React.CSSProperties = {
position: 'absolute',
top: 0,
@@ -1089,7 +1077,8 @@ const TimelineSequenceLeftEdgeDragHandleInner: React.FC<{
readonly nodePathInfo: SequenceNodePathInfo;
readonly windowWidth: number;
readonly timelineDurationInFrames: number;
-}> = ({nodePathInfo, windowWidth, timelineDurationInFrames}) => {
+ readonly onDragEnd: (wasDragged: boolean) => void;
+}> = ({nodePathInfo, windowWidth, timelineDurationInFrames, onDragEnd}) => {
const {setPropStatuses, setDragOverrides, clearDragOverrides} = useContext(
Internals.VisualModeSettersContext,
);
@@ -1122,6 +1111,7 @@ const TimelineSequenceLeftEdgeDragHandleInner: React.FC<{
clearDragOverrides,
previewServerState,
overrideIdToNodePathMappings,
+ onDragEnd,
});
latestRef.current = {
nodePathInfo,
@@ -1130,6 +1120,7 @@ const TimelineSequenceLeftEdgeDragHandleInner: React.FC<{
clearDragOverrides,
previewServerState,
overrideIdToNodePathMappings,
+ onDragEnd,
};
const finishDrag = useCallback((commit: boolean) => {
@@ -1139,7 +1130,7 @@ const TimelineSequenceLeftEdgeDragHandleInner: React.FC<{
}
dragStateRef.current = null;
- markSequenceDragEnd(dragState.didMove);
+ latestRef.current.onDragEnd(dragState.didMove);
document.body.style.userSelect = '';
document.body.style.webkitUserSelect = '';
stopForcingSpecificCursor();
@@ -1372,10 +1363,12 @@ export const useTimelineSequenceFromDrag = ({
nodePathInfo,
windowWidth,
timelineDurationInFrames,
+ onDragEnd,
}: {
readonly nodePathInfo: SequenceNodePathInfo | null;
readonly windowWidth: number;
readonly timelineDurationInFrames: number;
+ readonly onDragEnd: (wasDragged: boolean) => void;
}) => {
const {
setPropStatuses,
@@ -1417,6 +1410,7 @@ export const useTimelineSequenceFromDrag = ({
previewServerState,
overrideIdToNodePathMappings,
editorSnapping,
+ onDragEnd,
});
latestRef.current = {
nodePathInfo,
@@ -1428,6 +1422,7 @@ export const useTimelineSequenceFromDrag = ({
previewServerState,
overrideIdToNodePathMappings,
editorSnapping,
+ onDragEnd,
};
const finishDrag = useCallback((commit: boolean) => {
@@ -1437,7 +1432,7 @@ export const useTimelineSequenceFromDrag = ({
}
dragStateRef.current = null;
- markSequenceDragEnd(dragState.didMove);
+ latestRef.current.onDragEnd(dragState.didMove);
document.body.style.userSelect = '';
document.body.style.webkitUserSelect = '';
setDragging(false);
@@ -1686,7 +1681,8 @@ const TimelineSequenceRightEdgeDragHandleInner: React.FC<{
readonly nodePathInfo: SequenceNodePathInfo;
readonly windowWidth: number;
readonly timelineDurationInFrames: number;
-}> = ({nodePathInfo, windowWidth, timelineDurationInFrames}) => {
+ readonly onDragEnd: (wasDragged: boolean) => void;
+}> = ({nodePathInfo, windowWidth, timelineDurationInFrames, onDragEnd}) => {
const {setPropStatuses, setDragOverrides, clearDragOverrides} = useContext(
Internals.VisualModeSettersContext,
);
@@ -1720,6 +1716,7 @@ const TimelineSequenceRightEdgeDragHandleInner: React.FC<{
clearDragOverrides,
previewServerState,
overrideIdToNodePathMappings,
+ onDragEnd,
});
latestRef.current = {
nodePathInfo,
@@ -1728,6 +1725,7 @@ const TimelineSequenceRightEdgeDragHandleInner: React.FC<{
clearDragOverrides,
previewServerState,
overrideIdToNodePathMappings,
+ onDragEnd,
};
const finishDrag = useCallback((commit: boolean) => {
@@ -1737,7 +1735,7 @@ const TimelineSequenceRightEdgeDragHandleInner: React.FC<{
}
dragStateRef.current = null;
- markSequenceDragEnd(dragState.didMove);
+ latestRef.current.onDragEnd(dragState.didMove);
document.body.style.userSelect = '';
document.body.style.webkitUserSelect = '';
stopForcingSpecificCursor();
diff --git a/packages/studio/src/helpers/drag-aware-double-click.ts b/packages/studio/src/helpers/drag-aware-double-click.ts
new file mode 100644
index 0000000000..f73924f008
--- /dev/null
+++ b/packages/studio/src/helpers/drag-aware-double-click.ts
@@ -0,0 +1,24 @@
+export type DragAwareDoubleClickTracker = {
+ readonly beginPointerGesture: () => void;
+ readonly endPointerGesture: (wasDragged: boolean) => void;
+ readonly consumePointerGestureWasDragged: () => boolean;
+};
+
+export const createDragAwareDoubleClickTracker =
+ (): DragAwareDoubleClickTracker => {
+ let pointerGestureWasDragged = false;
+
+ return {
+ beginPointerGesture: () => {
+ pointerGestureWasDragged = false;
+ },
+ endPointerGesture: (wasDragged) => {
+ pointerGestureWasDragged = wasDragged;
+ },
+ consumePointerGestureWasDragged: () => {
+ const wasDragged = pointerGestureWasDragged;
+ pointerGestureWasDragged = false;
+ return wasDragged;
+ },
+ };
+ };
diff --git a/packages/studio/src/test/drag-aware-double-click.test.ts b/packages/studio/src/test/drag-aware-double-click.test.ts
new file mode 100644
index 0000000000..fdf8bcc8a0
--- /dev/null
+++ b/packages/studio/src/test/drag-aware-double-click.test.ts
@@ -0,0 +1,17 @@
+import {expect, test} from 'bun:test';
+import {createDragAwareDoubleClickTracker} from '../helpers/drag-aware-double-click';
+
+test('Drag-aware double clicks consume the drag from the current pointer gesture', () => {
+ const tracker = createDragAwareDoubleClickTracker();
+
+ tracker.beginPointerGesture();
+ tracker.endPointerGesture(true);
+ expect(tracker.consumePointerGestureWasDragged()).toBe(true);
+ expect(tracker.consumePointerGestureWasDragged()).toBe(false);
+
+ tracker.beginPointerGesture();
+ tracker.endPointerGesture(true);
+ tracker.beginPointerGesture();
+ tracker.endPointerGesture(false);
+ expect(tracker.consumePointerGestureWasDragged()).toBe(false);
+});