🎥 Make videos programmatically with React
`@remotion/studio`: Reduce inspector and timeline re-renders during playback
Jonny Burger54373591e1bc
diff --git a/packages/studio/src/components/InspectorSequenceSection.tsx b/packages/studio/src/components/InspectorSequenceSection.tsxindex 6c028db0ac..bcda488be6 100644--- a/packages/studio/src/components/InspectorSequenceSection.tsx+++ b/packages/studio/src/components/InspectorSequenceSection.tsx@@ -21,6 +21,10 @@ import {type SchemaFieldGroupInfo,type TimelineTreeNode,} from '../helpers/timeline-layout';+import {+ useRuntimeValue,+ useRuntimeValueSelector,+} from '../helpers/use-runtime-values';import {BorderRadiusIcon} from '../icons/border-radius';import {CubeIcon} from '../icons/cube';import {FullscreenIcon} from '../icons/fullscreen';@@ -318,7 +322,7 @@ export const InspectorSequenceSection: React.FC<{sequenceFrameOffset,renderTransformControls,}) => {- const {tree, propStatuses, runtimeValues} = useTimelineExpandedTree({+ const {tree, propStatuses} = useTimelineExpandedTree({sequence,nodePathInfo,includeTextContent: true,@@ -381,10 +385,15 @@ export const InspectorSequenceSection: React.FC<{const {setPropStatuses} = useContext(Internals.VisualModeSettersContext);const {setSelectedModal} = useContext(SetSelectedModalContext);const selectAsset = useSelectAsset();- const mediaSrc = getTimelineAssetSrcFromSchema(- sequence.controls,- runtimeValues,+ const selectMediaSrc = useCallback(+ (values: Readonly<Record<string, unknown>>) =>+ getTimelineAssetSrcFromSchema(sequence.controls, values),+ [sequence.controls],);+ const mediaSrc = useRuntimeValueSelector({+ controls: sequence.controls,+ selector: selectMediaSrc,+ });const getSourceAction = useCallback((src: string): InspectorSourceAction | null => {const linkInfo = getTimelineAssetLinkInfo(src);@@ -612,20 +621,26 @@ export const InspectorSequenceSection: React.FC<{propStatuses,nodePathInfo.sequenceSubscriptionKey,);- const automaticallyEnabled3DTransform = useMemo(() => {- return [- 'style.translate',- 'style.scale',- 'style.rotate',- 'style.transformOrigin',- ].some((fieldKey) =>- propStatusHas3DTransformValue({- fieldKey,- propStatus: sequencePropStatuses?.[fieldKey],- runtimeValue: runtimeValues[fieldKey],- }),- );- }, [runtimeValues, sequencePropStatuses]);+ const selectAutomaticallyEnabled3DTransform = useCallback(+ (values: Readonly<Record<string, unknown>>) =>+ [+ 'style.translate',+ 'style.scale',+ 'style.rotate',+ 'style.transformOrigin',+ ].some((fieldKey) =>+ propStatusHas3DTransformValue({+ fieldKey,+ propStatus: sequencePropStatuses?.[fieldKey],+ runtimeValue: values[fieldKey],+ }),+ ),+ [sequencePropStatuses],+ );+ const automaticallyEnabled3DTransform = useRuntimeValueSelector({+ controls: sequence.controls,+ selector: selectAutomaticallyEnabled3DTransform,+ });const show3DTransformControls =automaticallyEnabled3DTransform || manuallyEnabled3DTransform;const onToggle3DTransform = useCallback(() => {@@ -803,14 +818,18 @@ export const InspectorSequenceSection: React.FC<{getDragOverrides(nodePathInfo.sequenceSubscriptionKey),);const hasCaptionsSchema = schema.captions?.type === 'remotion-captions';+ const runtimeCaptions = useRuntimeValue(+ hasCaptionsSchema ? sequence.controls : null,+ 'captions',+ );const inlineCaptions = useMemo(() =>hasCaptionsSchema- ? Array.isArray(runtimeValues.captions)- ? (runtimeValues.captions as Caption[])+ ? Array.isArray(runtimeCaptions)+ ? (runtimeCaptions as Caption[]): []: null,- [hasCaptionsSchema, runtimeValues.captions],+ [hasCaptionsSchema, runtimeCaptions],);const {getCurrentFrame: getCommittedSequenceFrame,diff --git a/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx b/packages/studio/src/components/Timeline/TimelineExpandedRow.tsxindex 081df123a3..640525373a 100644--- a/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx+++ b/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx@@ -165,7 +165,7 @@ const TimelineExpandedRowInner: React.FC<TimelineExpandedRowProps> = ({keyframeDisplayOffset={keyframeDisplayOffset}keyframePlaybackRate={keyframePlaybackRate}keyframeControlsMode={keyframeControlsMode}- runtimeValue={node.runtimeValue}+ runtimeValueStore={node.runtimeValueStore}/>);}diff --git a/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx b/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsxindex 611b27914b..b3cde3c6a0 100644--- a/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx+++ b/packages/studio/src/components/Timeline/TimelineSequencePropItem.tsx@@ -7,9 +7,11 @@ import React, {useCallback, useContext, useMemo, useRef} from 'react';import {unstable_batchedUpdates} from 'react-dom';import type {CanUpdateSequencePropStatus,+ CanUpdateSequencePropStatusFalse,CanUpdateSequencePropStatusKeyframed,CanUpdateSequencePropStatusStatic,InteractivitySchema,+ RuntimeValueStore,SequencePropsSubscriptionKey,} from 'remotion';import {Internals} from 'remotion';@@ -23,6 +25,7 @@ import type {TimelineFieldOnDragValueChange,TimelineFieldOnSave,} from '../../helpers/timeline-layout';+import {useRuntimeStoreValue} from '../../helpers/use-runtime-values';import {ContextMenu} from '../ContextMenu';import {INSPECTOR_PANEL_HORIZONTAL_PADDING} from '../InspectorPanelLayout';import type {ComboboxValue} from '../NewComposition/ComboBox';@@ -617,6 +620,24 @@ export const TimelineSequenceKeyframedValue = React.memo(TimelineSequenceKeyframedValueUnmemoized,);+const TimelineComputedSequencePropValue: React.FC<{+ readonly field: SchemaFieldInfo;+ readonly propStatus: CanUpdateSequencePropStatusFalse;+ readonly runtimeValueStore: RuntimeValueStore | null;+ readonly validatedLocation: CodePosition;+}> = ({field, propStatus, runtimeValueStore, validatedLocation}) => {+ const runtimeValue = useRuntimeStoreValue(runtimeValueStore, field.key);++ return (+ <TimelineNonEditableStatus+ propStatus={propStatus}+ field={field}+ runtimeValue={runtimeValue}+ validatedLocation={validatedLocation}+ />+ );+};+export const TimelineSequencePropItem: React.FC<{readonly field: SchemaFieldInfo;readonly validatedLocation: CodePosition;@@ -627,7 +648,7 @@ export const TimelineSequencePropItem: React.FC<{readonly keyframeDisplayOffset: number;readonly keyframePlaybackRate: number;readonly keyframeControlsMode: TimelineKeyframeControlsMode;- readonly runtimeValue: unknown;+ readonly runtimeValueStore: RuntimeValueStore | null;}> = ({field,validatedLocation,@@ -638,7 +659,7 @@ export const TimelineSequencePropItem: React.FC<{keyframeDisplayOffset,keyframePlaybackRate,keyframeControlsMode,- runtimeValue,+ runtimeValueStore,}) => {const {propStatuses: visualModePropStatuses} = useContext(Internals.VisualModePropStatusesContext,@@ -850,10 +871,10 @@ export const TimelineSequencePropItem: React.FC<{propStatus={propStatus}/>) : (- <TimelineNonEditableStatus+ <TimelineComputedSequencePropValuepropStatus={propStatus}field={field}- runtimeValue={runtimeValue}+ runtimeValueStore={runtimeValueStore}validatedLocation={validatedLocation}/>);diff --git a/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts b/packages/studio/src/components/Timeline/use-timeline-expanded-tree.tsindex c12dae3ec1..90c57acca5 100644--- a/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts+++ b/packages/studio/src/components/Timeline/use-timeline-expanded-tree.ts@@ -1,12 +1,9 @@import {stringifySequenceExpandedRowKey} from '@remotion/studio-shared';import {useCallback, useContext, useMemo} from 'react';-import {Internals, type TSequence} from 'remotion';+import {Internals, type InteractivitySchema, type TSequence} from 'remotion';import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';import {buildTimelineTree} from '../../helpers/timeline-layout';-import {- useRuntimeValues,- useRuntimeValueSelector,-} from '../../helpers/use-runtime-values';+import {useRuntimeValueSelector} from '../../helpers/use-runtime-values';import {ExpandedTracksGetterContext,ExpandedTracksSetterContext,@@ -169,7 +166,50 @@ export const useTimelineExpandedTree = ({Internals.VisualModeDragOverridesContext,);const {selectedItems} = useTimelineSelection();- const runtimeValues = useRuntimeValues(sequence.controls);+ // Runtime values only affect the tree through enum variants and array row+ // heights. Animated field values subscribe in their editors instead.+ const structureKeys = useMemo(() => {+ const keys = new Set<string>();+ const schemas: InteractivitySchema[] = sequence.controls+ ? [sequence.controls.schema]+ : [];+ for (const schema of schemas) {+ for (const [key, field] of Object.entries(schema)) {+ if (field.type === 'enum') {+ keys.add(key);+ schemas.push(...Object.values(field.variants));+ } else if (field.type === 'array') {+ keys.add(key);+ }+ }+ }++ return [...keys];+ }, [sequence.controls]);+ const selectStructureValues = useCallback(+ (values: Readonly<Record<string, unknown>>) =>+ Object.fromEntries(structureKeys.map((key) => [key, values[key]])),+ [structureKeys],+ );+ const structureValuesEqual = useCallback(+ (+ previous: Readonly<Record<string, unknown>>,+ next: Readonly<Record<string, unknown>>,+ ) =>+ structureKeys.every((key) => {+ const previousValue = previous[key];+ const nextValue = next[key];+ return Array.isArray(previousValue) && Array.isArray(nextValue)+ ? previousValue.length === nextValue.length+ : Object.is(previousValue, nextValue);+ }),+ [structureKeys],+ );+ const runtimeValues = useRuntimeValueSelector({+ controls: structureKeys.length > 0 ? sequence.controls : null,+ selector: selectStructureValues,+ isEqual: structureValuesEqual,+ });const tree = useMemo(() =>@@ -229,7 +269,6 @@ export const useTimelineExpandedTree = ({filteredTree,getIsExpanded,propStatuses: visualModePropStatuses,- runtimeValues,toggleTrack,tree,};diff --git a/packages/studio/src/helpers/timeline-layout.ts b/packages/studio/src/helpers/timeline-layout.tsindex 78a146592f..2bce54a389 100644--- a/packages/studio/src/helpers/timeline-layout.ts+++ b/packages/studio/src/helpers/timeline-layout.ts@@ -82,7 +82,6 @@ export type TimelineTreeNode =readonly nodePathInfo: SequenceNodePathInfo;readonly label: string;readonly field: AnySchemaFieldInfo | null;- readonly runtimeValue: unknown;readonly runtimeValueStore: RuntimeValueStore | null;};@@ -138,8 +137,7 @@ export const buildTimelineTree = ({},label: f.description ?? f.key,field: f,- runtimeValue: runtimeValues?.[f.key],- runtimeValueStore: null,+ runtimeValueStore: sequence.controls!.runtimeValues,});}}@@ -196,7 +194,6 @@ export const buildTimelineTree = ({},label: f.description ?? f.key,field: f,- runtimeValue: null,runtimeValueStore: sequence.effectRuntimeValues?.[i] ?? null,}),),diff --git a/packages/studio/src/test/timeline-keyframes.test.ts b/packages/studio/src/test/timeline-keyframes.test.tsindex a64d92bdb5..e7fa0e7f99 100644--- a/packages/studio/src/test/timeline-keyframes.test.ts+++ b/packages/studio/src/test/timeline-keyframes.test.ts@@ -127,7 +127,6 @@ const makeSequenceFieldNode = (key: string): TimelineTreeNode => ({fieldSchema: numberFieldSchema,group: getSchemaFieldGroup(key),},- runtimeValue: null,runtimeValueStore: null,});@@ -157,7 +156,6 @@ const makeEffectFieldNode = (},group: getSchemaFieldGroup(key),},- runtimeValue: null,runtimeValueStore: null,});