Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Reduce timeline row renders during selection

Jonny Burger3c954f7be9fa
diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsx
index eeddeeb608..48c80f2b80 100644
--- a/packages/studio/src/components/Timeline/Timeline.tsx
+++ b/packages/studio/src/components/Timeline/Timeline.tsx
@@ -56,7 +56,7 @@ import {TimelineScrollable} from './TimelineScrollable';
import {
TimelineSelectableItemsProvider,
TimelineSelectAllKeybindings,
- useTimelineSelection,
+ useCurrentTimelineSelectionStateAsRef,
} from './TimelineSelection';
import {TimelineSequenceMediaDurationDragLimitsProvider} from './TimelineSequenceRightEdgeDragHandle';
import {TimelineSlider} from './TimelineSlider';
@@ -356,7 +356,7 @@ const TimelineInner: React.FC = () => {
fastRefreshes: number;
existingSequenceIds: Set<string>;
} | null>(null);
- const {selectItems} = useTimelineSelection();
+ const currentSelection = useCurrentTimelineSelectionStateAsRef();
useLayoutEffect(() => {
if (pendingInsertedElementSelection === null) {
pendingSelectionStart.current = null;
@@ -420,7 +420,7 @@ const TimelineInner: React.FC = () => {
return;
}
- selectItems(
+ currentSelection.current.selectItems(
[{type: 'sequence', nodePathInfo: insertedTrack.nodePathInfo}],
{reveal: true},
);
@@ -430,10 +430,10 @@ const TimelineInner: React.FC = () => {
}
}, [
canvasContent,
+ currentSelection,
fastRefreshes,
layerChildrenValue,
pendingInsertedElementSelection,
- selectItems,
timeline,
]);
diff --git a/packages/studio/src/components/Timeline/TimelineList.tsx b/packages/studio/src/components/Timeline/TimelineList.tsx
index df731e8f20..c117e2105e 100644
--- a/packages/studio/src/components/Timeline/TimelineList.tsx
+++ b/packages/studio/src/components/Timeline/TimelineList.tsx
@@ -12,16 +12,20 @@ const container: React.CSSProperties = {
position: 'relative',
};
+const noConnectedCompositions: readonly never[] = [];
+
const TimelineListTrack: React.FC<{
readonly row: TimelineVirtualRow;
-}> = ({row}) => {
+}> = React.memo(({row}) => {
const {afterDropLineOffset, siblingIndex, track} = row;
return (
<TimelineSequenceItem
afterDropLineOffset={afterDropLineOffset}
siblingIndex={siblingIndex}
- connectedCompositions={track.connectedCompositions ?? []}
+ connectedCompositions={
+ track.connectedCompositions ?? noConnectedCompositions
+ }
nestedDepth={track.depth}
sequence={track.sequence}
nodePathInfo={track.nodePathInfo}
@@ -29,7 +33,7 @@ const TimelineListTrack: React.FC<{
sequenceFrameOffset={track.sequenceFrameOffset}
/>
);
-};
+});
export const TimelineList: React.FC = () => {
const {rows, tracksEnd, virtualItems} = useTimelineVirtualization();
diff --git a/packages/studio/src/components/Timeline/TimelineSelection.tsx b/packages/studio/src/components/Timeline/TimelineSelection.tsx
index 609236d951..14cbbc49af 100644
--- a/packages/studio/src/components/Timeline/TimelineSelection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSelection.tsx
@@ -62,6 +62,7 @@ import {
} from '../../helpers/timeline-layout';
import {timelineNodePathInfoToKey} from '../../helpers/timeline-node-path-key';
import {useKeybinding} from '../../helpers/use-keybinding';
+import {useSyncExternalStore} from '../../helpers/use-sync-external-store';
import {useZIndex} from '../../state/z-index';
import {
ExpandedTracksGetterContext,
@@ -486,6 +487,17 @@ const TimelineSelectionContext = createContext<TimelineSelectionContextValue>(
defaultTimelineSelectionContextValue,
);
+type TimelineRowSelectionContextValue = Pick<
+ TimelineSelectionContextValue,
+ 'canSelect' | 'registerMarqueeSelectableItem' | 'selectItem'
+> & {
+ readonly getSnapshot: () => TimelineSelectionState;
+ readonly subscribe: (listener: () => void) => () => void;
+};
+
+const TimelineRowSelectionContext =
+ createContext<TimelineRowSelectionContextValue | null>(null);
+
const EMPTY_SELECTABLE_TIMELINE_ITEMS: readonly TimelineSelection[] = [];
const SelectableTimelineItemsContext = createContext<
@@ -995,6 +1007,13 @@ export const TimelineSelectionProvider: React.FC<{
? selectionState
: EMPTY_TIMELINE_SELECTION_STATE;
const availableSelectedItems = availableSelectionState.selectedItems;
+ const getAvailableSelectionState = useCallback(
+ () =>
+ selectionScope.current === timelineSelectionScope
+ ? selectionController.getSnapshot()
+ : EMPTY_TIMELINE_SELECTION_STATE,
+ [selectionController, timelineSelectionScope],
+ );
const requestRevealSelectionItem = useCallback((item: TimelineSelection) => {
setRevealRequest((previousRequest) => ({
@@ -1318,19 +1337,37 @@ export const TimelineSelectionProvider: React.FC<{
);
const currentSelection = useRef(value);
currentSelection.current = value;
+ const rowSelectionContextValue = useMemo(
+ (): TimelineRowSelectionContextValue => ({
+ canSelect,
+ getSnapshot: getAvailableSelectionState,
+ registerMarqueeSelectableItem,
+ selectItem,
+ subscribe: selectionController.subscribe,
+ }),
+ [
+ canSelect,
+ getAvailableSelectionState,
+ registerMarqueeSelectableItem,
+ selectItem,
+ selectionController.subscribe,
+ ],
+ );
return (
<CurrentTimelineSelectionContext.Provider value={currentSelection}>
- <TimelineSelectionContext.Provider value={value}>
- {children}
- <TimelineEscapeKeybindings />
- {isStudioInteractivityEnabled() ? (
- <>
- <TimelineClipboardKeybindings />
- <TimelineDeleteKeybindings />
- </>
- ) : null}
- </TimelineSelectionContext.Provider>
+ <TimelineRowSelectionContext.Provider value={rowSelectionContextValue}>
+ <TimelineSelectionContext.Provider value={value}>
+ {children}
+ <TimelineEscapeKeybindings />
+ {isStudioInteractivityEnabled() ? (
+ <>
+ <TimelineClipboardKeybindings />
+ <TimelineDeleteKeybindings />
+ </>
+ ) : null}
+ </TimelineSelectionContext.Provider>
+ </TimelineRowSelectionContext.Provider>
</CurrentTimelineSelectionContext.Provider>
);
};
@@ -1559,7 +1596,14 @@ export const useTimelineMarqueeSelectableItem = (
item: TimelineSelection | null,
ref: React.RefObject<Element | null>,
) => {
- const {registerMarqueeSelectableItem} = useTimelineSelection();
+ const selectionContext = useContext(TimelineRowSelectionContext);
+ if (selectionContext === null) {
+ throw new Error(
+ 'useTimelineMarqueeSelectableItem must be used inside TimelineSelectionProvider',
+ );
+ }
+
+ const {registerMarqueeSelectableItem} = selectionContext;
useEffect(() => {
if (item === null) {
@@ -1577,15 +1621,41 @@ export const useTimelineRowSelection = (
nodePathInfo: SequenceNodePathInfo | null,
revealInInspector = false,
) => {
- const {canSelect, isSelected, selectItem} = useTimelineSelection();
+ const selectionContext = useContext(TimelineRowSelectionContext);
+ if (selectionContext === null) {
+ throw new Error(
+ 'useTimelineRowSelection must be used inside TimelineSelectionProvider',
+ );
+ }
+
const selectableTimelineItemsRef = useContext(SelectableTimelineItemsContext);
const selectionItem = useMemo(
(): TimelineSelection | null =>
getTimelineSelectionFromNodePathInfo(nodePathInfo),
[nodePathInfo],
);
+ const selectionItemKey = useMemo(
+ () =>
+ selectionItem === null ? null : getTimelineSelectionKey(selectionItem),
+ [selectionItem],
+ );
+ const getSelectedState = useCallback((): TimelineSelectionState | null => {
+ if (selectionItemKey === null) {
+ return null;
+ }
- const selected = selectionItem === null ? false : isSelected(selectionItem);
+ const snapshot = selectionContext.getSnapshot();
+ return snapshot.selectedItems.some(
+ (item) => getTimelineSelectionKey(item) === selectionItemKey,
+ )
+ ? snapshot
+ : null;
+ }, [selectionContext, selectionItemKey]);
+ const selectedState = useSyncExternalStore(
+ selectionContext.subscribe,
+ getSelectedState,
+ getSelectedState,
+ );
const onSelect = useCallback(
(interaction?: TimelineSelectionInteraction) => {
@@ -1593,21 +1663,30 @@ export const useTimelineRowSelection = (
return;
}
- selectItem(
+ selectionContext.selectItem(
selectionItem,
interaction,
selectableTimelineItemsRef.current,
{revealInInspector},
);
},
- [revealInInspector, selectItem, selectableTimelineItemsRef, selectionItem],
+ [
+ revealInInspector,
+ selectableTimelineItemsRef,
+ selectionContext,
+ selectionItem,
+ ],
);
return {
onSelect,
- selectable: canSelect && selectionItem !== null,
+ selectable: selectionContext.canSelect && selectionItem !== null,
+ selectedItems:
+ selectedState?.selectedItems ??
+ EMPTY_TIMELINE_SELECTION_STATE.selectedItems,
+ selectItem: selectionContext.selectItem,
selectionItem,
- selected,
+ selected: selectedState !== null,
};
};
@@ -1721,12 +1800,32 @@ export const useTimelineGuideSelection = (guideId: string) => {
export const useTimelineRowContainsSelection = (
nodePathInfo: SequenceNodePathInfo | null,
): boolean => {
- const {containsSelection} = useTimelineSelection();
- if (nodePathInfo === null) {
- return false;
+ const selectionContext = useContext(TimelineRowSelectionContext);
+ if (selectionContext === null) {
+ throw new Error(
+ 'useTimelineRowContainsSelection must be used inside TimelineSelectionProvider',
+ );
}
- return containsSelection(nodePathInfo);
+ const getContainsSelection = useCallback(() => {
+ if (nodePathInfo === null) {
+ return false;
+ }
+
+ return selectionContext
+ .getSnapshot()
+ .selectedItems.some(
+ (selected) =>
+ selected.type !== 'guide' &&
+ nodePathDescendsFrom(selected.nodePathInfo, nodePathInfo),
+ );
+ }, [nodePathInfo, selectionContext]);
+
+ return useSyncExternalStore(
+ selectionContext.subscribe,
+ getContainsSelection,
+ getContainsSelection,
+ );
};
export const useTimelineRowHighlightBackground = (
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index 2f31599f90..92075307e6 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -68,7 +68,6 @@ import {
TIMELINE_MARQUEE_ITEM_ATTR,
useTimelineMarqueeSelectableItem,
useTimelineRowSelection,
- useTimelineSelection,
} from './TimelineSelection';
import {TimelineSequenceFrame} from './TimelineSequenceFrame';
import {
@@ -535,9 +534,8 @@ const TimelineSequenceInner: React.FC<{
const selectComposition = useSelectComposition();
const confirm = useConfirmationDialog();
const deleteTimelineItems = useDeleteTimelineItems();
- const {onSelect, selectable, selected} =
+ const {onSelect, selectable, selected, selectedItems} =
useTimelineRowSelection(nodePathInfo);
- const {selectedItems} = useTimelineSelection();
const selectedSequenceNodePathInfos = useMemo(() => {
if (
!selected ||
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
index 33b9cb7293..f6a919c95d 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
@@ -102,9 +102,9 @@ import {
getTimelineColor,
getTimelineSequenceSelectionKey,
isTimelineSelectionModifierEvent,
+ type TimelineSelection,
useTimelineRowContainsSelection,
useTimelineRowSelection,
- useTimelineSelection,
} from './TimelineSelection';
import {TimelineSequenceName} from './TimelineSequenceName';
import {TIMELINE_TIME_INDICATOR_HEIGHT} from './TimelineTimeIndicators';
@@ -148,16 +148,18 @@ type TimelineSequenceExpandArrowProps = {
readonly disabled: boolean;
readonly isExpanded: boolean;
readonly nodePathInfo: SequenceNodePathInfo;
+ readonly selectedItems: readonly TimelineSelection[];
readonly sequence: TSequence;
};
const TimelineSequenceExpandArrowInner: React.FC<
TimelineSequenceExpandArrowProps
-> = ({disabled, isExpanded, nodePathInfo, sequence}) => {
+> = ({disabled, isExpanded, nodePathInfo, selectedItems, sequence}) => {
const {toggleTrack} = useContext(ExpandedTracksSetterContext);
const hasExpandableContent = useTimelineSequenceHasExpandableContent({
sequence,
nodePathInfo,
+ selectedItems,
});
const onToggleExpand = useCallback(
() => toggleTrack(nodePathInfo),
@@ -185,6 +187,7 @@ const areTimelineSequenceExpandArrowPropsEqual = (
return (
first.disabled === second.disabled &&
first.isExpanded === second.isExpanded &&
+ first.selectedItems === second.selectedItems &&
first.sequence.controls?.schema === second.sequence.controls?.schema &&
first.sequence.controls?.runtimeValues ===
second.sequence.controls?.runtimeValues &&
@@ -323,9 +326,8 @@ const TimelineSequenceItemInner: React.FC<{
const assetContextMenu = useAssetTimelineContextMenu();
const selectComposition = useSelectComposition();
const deleteTimelineItems = useDeleteTimelineItems();
- const {onSelect, selectable, selected} =
+ const {onSelect, selectable, selected, selectedItems, selectItem} =
useTimelineRowSelection(nodePathInfo);
- const {selectItem, selectedItems} = useTimelineSelection();
const selectedSequenceNodePathInfos = useMemo(() => {
if (
!selected ||
@@ -1464,6 +1466,7 @@ const TimelineSequenceItemInner: React.FC<{
disabled={!previewInteractive}
isExpanded={isExpanded}
nodePathInfo={nodePathInfo}
+ selectedItems={selectedItems}
sequence={sequence}
/>
</>
diff --git a/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts b/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts
index d5ccfcb92c..f21aa04203 100644
--- a/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts
+++ b/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts
@@ -16,14 +16,19 @@ import {
getSelectedTimelineExpandedRowKeys,
isTimelineExpandedNodeSelected,
} from './timeline-expanded-filter';
-import {useTimelineSelection} from './TimelineSelection';
+import {
+ type TimelineSelection,
+ useTimelineSelection,
+} from './TimelineSelection';
export const useTimelineSequenceHasExpandableContent = ({
sequence,
nodePathInfo,
+ selectedItems,
}: {
readonly sequence: TSequence;
readonly nodePathInfo: SequenceNodePathInfo;
+ readonly selectedItems: readonly TimelineSelection[];
}) => {
const {propStatuses: visualModePropStatuses} = useContext(
Internals.VisualModePropStatusesContext,
@@ -31,7 +36,6 @@ export const useTimelineSequenceHasExpandableContent = ({
const {getDragOverrides, getEffectDragOverrides} = useContext(
Internals.VisualModeDragOverridesContext,
);
- const {selectedItems} = useTimelineSelection();
const selectedRowKeys = useMemo(
() => getSelectedTimelineExpandedRowKeys(selectedItems),
[selectedItems],
diff --git a/packages/studio/src/test/sequence-props-observer.test.tsx b/packages/studio/src/test/sequence-props-observer.test.tsx
index 7334788185..6d772145c5 100644
--- a/packages/studio/src/test/sequence-props-observer.test.tsx
+++ b/packages/studio/src/test/sequence-props-observer.test.tsx
@@ -7,10 +7,12 @@ import {subscribeToSequencePropsRefresh} from '../components/Timeline/sequence-p
import {SequencePropsObserver} from '../components/Timeline/SequencePropsObserver';
import {
TimelineSelectionProvider,
+ useTimelineRowSelection,
useTimelineSelection,
} from '../components/Timeline/TimelineSelection';
import {FastRefreshContext} from '../fast-refresh-context';
import {StudioServerConnectionCtx} from '../helpers/client-id';
+import type {SequenceNodePathInfo} from '../helpers/get-timeline-sequence-sort-key';
import {queueSequenceNodePathMutation} from '../helpers/sequence-node-path-mutations';
import {KeybindingContextProvider} from '../state/keybindings';
@@ -263,3 +265,106 @@ test('keeps the selected sequence selected after its node path changes', () => {
: null,
).toEqual(['body', 1]);
});
+
+test('only rerenders timeline rows whose selected state changes', () => {
+ const makeNodePathInfo = (index: number): SequenceNodePathInfo => ({
+ sequenceSubscriptionKey: {
+ absolutePath: '/project/src/BarChart.tsx',
+ effectKeys: [],
+ nodePath: ['body', index],
+ sequenceKeys: ['hidden', 'name'],
+ videoConfigValues: null,
+ },
+ auxiliaryKeys: [],
+ index,
+ numberOfSequencesWithThisNodePath: 1,
+ supportsEffects: true,
+ });
+ const firstNodePathInfo = makeNodePathInfo(0);
+ const secondNodePathInfo = makeNodePathInfo(1);
+ const renderCounts = {first: 0, second: 0};
+ const selectedStates = {first: false, second: false};
+ const selectionRef: {
+ current: ReturnType<typeof useTimelineSelection> | null;
+ } = {current: null};
+ const CaptureSelection = () => {
+ selectionRef.current = useTimelineSelection();
+ return null;
+ };
+
+ const Row = ({
+ name,
+ nodePathInfo,
+ }: {
+ readonly name: keyof typeof renderCounts;
+ readonly nodePathInfo: SequenceNodePathInfo;
+ }) => {
+ const {selected} = useTimelineRowSelection(nodePathInfo);
+ renderCounts[name] += 1;
+ selectedStates[name] = selected;
+ return null;
+ };
+
+ render(
+ <ObserverTestProviders
+ values={{
+ fastRefresh: {
+ fastRefreshes: 0,
+ manualRefreshes: 0,
+ increaseManualRefreshes: () => undefined,
+ },
+ expandedTracksSetter: {
+ expandParentTracks: () => undefined,
+ toggleTrack: () => undefined,
+ migrateExpandedTracksForSubscriptionKey: () => undefined,
+ } as never,
+ overrideIdsGetter: {overrideIdToNodePathMappings: {}},
+ overrideIdsSetter: {setOverrideIdToNodePath: () => undefined},
+ propStatusesRef: {current: {}},
+ visualModeSetters: {
+ remapPropStatuses: () => undefined,
+ setPropStatuses: () => undefined,
+ } as never,
+ }}
+ >
+ <KeybindingContextProvider>
+ <TimelineSelectionProvider>
+ <CaptureSelection />
+ <Row name="first" nodePathInfo={firstNodePathInfo} />
+ <Row name="second" nodePathInfo={secondNodePathInfo} />
+ </TimelineSelectionProvider>
+ </KeybindingContextProvider>
+ </ObserverTestProviders>,
+ );
+
+ expect(renderCounts).toEqual({first: 1, second: 1});
+ expect(selectedStates).toEqual({first: false, second: false});
+
+ act(() => {
+ selectionRef.current?.selectItems([
+ {type: 'sequence', nodePathInfo: firstNodePathInfo},
+ ]);
+ });
+
+ expect(renderCounts).toEqual({first: 2, second: 1});
+ expect(selectedStates).toEqual({first: true, second: false});
+
+ act(() => {
+ selectionRef.current?.selectItems([
+ {type: 'sequence', nodePathInfo: secondNodePathInfo},
+ ]);
+ });
+
+ expect(renderCounts).toEqual({first: 3, second: 2});
+ expect(selectedStates).toEqual({first: false, second: true});
+
+ act(() => {
+ selectionRef.current?.selectItems([
+ {type: 'sequence', nodePathInfo: firstNodePathInfo},
+ {type: 'sequence', nodePathInfo: secondNodePathInfo},
+ ]);
+ });
+
+ expect(renderCounts).toEqual({first: 4, second: 3});
+ expect(selectedStates).toEqual({first: true, second: true});
+});