diff --git a/packages/studio/src/player/components/TimelineCanvas.tsx b/packages/studio/src/player/components/TimelineCanvas.tsx index 6161f646b0..add029101f 100644 --- a/packages/studio/src/player/components/TimelineCanvas.tsx +++ b/packages/studio/src/player/components/TimelineCanvas.tsx @@ -26,7 +26,7 @@ const DROP_PREVIEW_SECONDS = 3; export const TimelineCanvas = memo(function TimelineCanvas() { const { state } = useTimelineContext(); - const props = state.canvasProps; + const props = state.canvas; const { draggedClip, resizingClip, scrollRef, selectedElementIds, displayTrackOrder } = props; const snapGuide = resolveSnapGuide(draggedClip, resizingClip); const draggedRowIndex = diff --git a/packages/studio/src/player/components/TimelineOverlays.test.ts b/packages/studio/src/player/components/TimelineOverlays.test.ts index a53a263aea..b214b91bb7 100644 --- a/packages/studio/src/player/components/TimelineOverlays.test.ts +++ b/packages/studio/src/player/components/TimelineOverlays.test.ts @@ -6,6 +6,11 @@ import type { TimelineElement } from "../store/playerStore"; import { usePlayerStore } from "../store/playerStore"; import { type KeyframeDiamondContextMenuState } from "./KeyframeDiamondContextMenu"; import { TimelineOverlays, resolveTimelineContextElement } from "./TimelineOverlays"; +import { + TimelineContextProvider, + type TimelineContextValue, + type TimelineOverlaysState, +} from "./TimelineProvider"; import { defaultTimelineTheme } from "./timelineTheme"; (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; @@ -90,6 +95,42 @@ function renderKeyframeOverlay(options: { percentage: 50, animationId: "child-position", }; + const overlays = { + elements, + elementsRef: { current: elements }, + theme: defaultTimelineTheme, + showShortcutHint: false, + showPopover: false, + rangeSelection: null, + setShowPopover: vi.fn(), + setRangeSelection: vi.fn(), + kfContextMenu: menu, + setKfContextMenu, + onDeleteKeyframe: vi.fn(), + onDeleteAllKeyframes, + onMoveKeyframeToPlayhead: vi.fn(), + clipContextMenu: null, + setClipContextMenu: vi.fn(), + currentTime: 0, + onSplitElement: vi.fn(), + pinZoomBeforeEdit: vi.fn(), + onDeleteElement: vi.fn(), + gapContextMenu: null, + onDismissGapContextMenu: vi.fn(), + onCloseTrackGap: vi.fn(), + onCloseAllTrackGaps: vi.fn(), + onHoverGapAction: vi.fn(), + } satisfies TimelineOverlaysState; + const contextValue = { + state: { + timelineReady: true, + elements, + canvas: {} as TimelineContextValue["state"]["canvas"], + overlays, + }, + actions: {}, + meta: {} as TimelineContextValue["meta"], + } satisfies TimelineContextValue; act(() => { usePlayerStore.setState({ @@ -97,32 +138,11 @@ function renderKeyframeOverlay(options: { timelineSessionEpoch: 2, }); root.render( - createElement(TimelineOverlays, { - elements, - elementsRef: { current: elements }, - theme: defaultTimelineTheme, - showShortcutHint: false, - showPopover: false, - rangeSelection: null, - setShowPopover: vi.fn(), - setRangeSelection: vi.fn(), - kfContextMenu: menu, - setKfContextMenu, - onDeleteKeyframe: vi.fn(), - onDeleteAllKeyframes, - onMoveKeyframeToPlayhead: vi.fn(), - clipContextMenu: null, - setClipContextMenu: vi.fn(), - currentTime: 0, - onSplitElement: vi.fn(), - pinZoomBeforeEdit: vi.fn(), - onDeleteElement: vi.fn(), - gapContextMenu: null, - onDismissGapContextMenu: vi.fn(), - onCloseTrackGap: vi.fn(), - onCloseAllTrackGaps: vi.fn(), - onHoverGapAction: vi.fn(), - }), + createElement( + TimelineContextProvider, + { value: contextValue }, + createElement(TimelineOverlays), + ), ); }); return { setKfContextMenu, onDeleteAllKeyframes }; diff --git a/packages/studio/src/player/components/TimelineOverlays.tsx b/packages/studio/src/player/components/TimelineOverlays.tsx index de0738edf1..131e7b3527 100644 --- a/packages/studio/src/player/components/TimelineOverlays.tsx +++ b/packages/studio/src/player/components/TimelineOverlays.tsx @@ -1,9 +1,6 @@ -import { useEffect, type MutableRefObject } from "react"; +import { useEffect } from "react"; import type { TimelineElement } from "../store/playerStore"; import { usePlayerStore } from "../store/playerStore"; -import type { TimelineTheme } from "./timelineTheme"; -import type { TimelineRangeSelection } from "./timelineEditing"; -import type { TimelineEditCallbacks } from "./timelineCallbacks"; import { EditPopover } from "./EditModal"; import { KeyframeDiamondContextMenu, @@ -14,55 +11,8 @@ import { TrackGapContextMenu } from "./TrackGapContextMenu"; import { TimelineShortcutHint } from "./TimelineShortcutHint"; import { copyTextToClipboard } from "../../utils/clipboard"; import { trackStudioSegmentEaseEdit } from "../../telemetry/events"; -import { useTimelineContextOptional } from "./TimelineProvider"; - -export interface ClipContextMenuState { - x: number; - y: number; - element: TimelineElement; - sessionEpoch: number; -} - -/** Resolved model for the empty-lane-space (track gap) context menu. */ -interface TrackGapContextMenuState { - x: number; - y: number; - gapWidth: number | null; - canCloseGap: boolean; - canCloseAllGaps: boolean; - hasAnyGaps: boolean; -} - -export interface TimelineOverlaysProps { - elements: readonly TimelineElement[]; - elementsRef: MutableRefObject; - theme: TimelineTheme; - showShortcutHint: boolean; - showPopover: boolean; - rangeSelection: TimelineRangeSelection | null; - setShowPopover: (value: boolean) => void; - setRangeSelection: (value: TimelineRangeSelection | null) => void; - kfContextMenu: KeyframeDiamondContextMenuState | null; - setKfContextMenu: (value: KeyframeDiamondContextMenuState | null) => void; - onDeleteKeyframe: TimelineEditCallbacks["onDeleteKeyframe"]; - onDeleteAllKeyframes: TimelineEditCallbacks["onDeleteAllKeyframes"]; - onMoveKeyframeToPlayhead: TimelineEditCallbacks["onMoveKeyframeToPlayhead"]; - clipContextMenu: ClipContextMenuState | null; - setClipContextMenu: (value: ClipContextMenuState | null) => void; - currentTime: number; - onSplitElement: TimelineEditCallbacks["onSplitElement"]; - pinZoomBeforeEdit: () => void; - onDeleteElement?: (element: TimelineElement) => Promise | void; - onCopyClip?: () => boolean; - onPasteClip?: () => Promise; - onDuplicateClip?: () => Promise; - canPasteClip?: () => boolean; - gapContextMenu: TrackGapContextMenuState | null; - onDismissGapContextMenu: () => void; - onCloseTrackGap: () => void; - onCloseAllTrackGaps: () => void; - onHoverGapAction: (action: "close-gap" | "close-all" | null) => void; -} +import { useTimelineContext } from "./TimelineProvider"; +import type { ClipContextMenuState } from "./TimelineProvider"; interface TimelineContextTargetInput { capturedElement: TimelineElement; @@ -104,12 +54,9 @@ function readTimelineContextElement( // The timeline's floating overlays, rendered as siblings above the scroll area: // the shortcut hint, the range-edit popover, the keyframe-diamond context menu, // and the clip context menu. -export function TimelineOverlays(props?: TimelineOverlaysProps) { - const context = useTimelineContextOptional(); - const overlayProps = context?.state.overlaysProps ?? props; - if (overlayProps === undefined) { - throw new Error("TimelineOverlays requires TimelineProvider or props"); - } +export function TimelineOverlays() { + const { state } = useTimelineContext(); + const overlayProps = state.overlays; const { elements, elementsRef, diff --git a/packages/studio/src/player/components/TimelineProvider.tsx b/packages/studio/src/player/components/TimelineProvider.tsx index fc5bdbd00f..5c85678f5d 100644 --- a/packages/studio/src/player/components/TimelineProvider.tsx +++ b/packages/studio/src/player/components/TimelineProvider.tsx @@ -1,5 +1,14 @@ -import { createContext, useContext, type ComponentProps, type ReactNode } from "react"; -import type { TimelineOverlaysProps } from "./TimelineOverlays"; +import { + createContext, + useContext, + type ComponentProps, + type CSSProperties, + type MouseEvent, + type PointerEvent, + type ReactNode, + type RefCallback, + type UIEvent, +} from "react"; import type { TimelineEmptyState } from "./TimelineEmptyState"; import type { TimelineProps } from "./TimelineTypes"; import type { TimelineRangeSelection } from "./timelineEditing"; @@ -8,6 +17,10 @@ import type { Rect } from "../../utils/marqueeGeometry"; import type { ResizingClipState } from "./useTimelineClipDrag"; import type { TimelineLaneBaseProps } from "./timelineLaneProps"; import type { TimelineLaneGapStrips } from "./useTimelineGapHighlights"; +import type { TimelineElement } from "../store/playerStore"; +import type { TimelineTheme } from "./timelineTheme"; +import type { TimelineEditCallbacks } from "./timelineCallbacks"; +import type { KeyframeDiamondContextMenuState } from "./KeyframeDiamondContextMenu"; import { useTimelineProviderState } from "./useTimelineProviderState"; export { @@ -28,7 +41,7 @@ export { getTimelineVisibleTimeRange, } from "./timelineViewportGeometry"; -type TimelineCanvasProps = Omit< +export type TimelineCanvasState = Omit< TimelineLaneBaseProps, "setRangeSelection" | "setResizingClip" | "setDraggedClip" > & { @@ -49,22 +62,104 @@ type TimelineCanvasProps = Omit< setDraggedClip: (value: TimelineLaneBaseProps["draggedClip"]) => void; }; +export interface ClipContextMenuState { + x: number; + y: number; + element: TimelineElement; + sessionEpoch: number; +} + +export interface TrackGapContextMenuState { + x: number; + y: number; + gapWidth: number | null; + canCloseGap: boolean; + canCloseAllGaps: boolean; + hasAnyGaps: boolean; +} + +export interface TimelineOverlaysState { + elements: readonly TimelineElement[]; + elementsRef: { current: readonly TimelineElement[] }; + theme: TimelineTheme; + showShortcutHint: boolean; + showPopover: boolean; + rangeSelection: TimelineRangeSelection | null; + setShowPopover: (value: boolean) => void; + setRangeSelection: (value: TimelineRangeSelection | null) => void; + kfContextMenu: KeyframeDiamondContextMenuState | null; + setKfContextMenu: (value: KeyframeDiamondContextMenuState | null) => void; + onDeleteKeyframe: TimelineEditCallbacks["onDeleteKeyframe"]; + onDeleteAllKeyframes: TimelineEditCallbacks["onDeleteAllKeyframes"]; + onMoveKeyframeToPlayhead: TimelineEditCallbacks["onMoveKeyframeToPlayhead"]; + clipContextMenu: ClipContextMenuState | null; + setClipContextMenu: (value: ClipContextMenuState | null) => void; + currentTime: number; + onSplitElement: TimelineEditCallbacks["onSplitElement"]; + pinZoomBeforeEdit: () => void; + onDeleteElement?: (element: TimelineElement) => Promise | void; + onCopyClip?: () => boolean; + onPasteClip?: () => Promise; + onDuplicateClip?: () => Promise; + canPasteClip?: () => boolean; + gapContextMenu: TrackGapContextMenuState | null; + onDismissGapContextMenu: () => void; + onCloseTrackGap: () => void; + onCloseAllTrackGaps: () => void; + onHoverGapAction: (action: "close-gap" | "close-all" | null) => void; +} + +export interface TimelineContainerProps { + ref: RefCallback; + "aria-label": string; + "data-timeline-element-count": number; + className: string; + onMouseMove: (event: MouseEvent) => void; + onMouseLeave: () => void; + style: CSSProperties; +} + +export interface TimelineViewportProps { + ref: RefCallback; + "data-timeline-scroll-viewport": boolean; + "data-timeline-auto-scroll-left-inset": number; + tabIndex: number; + className: string; + onScroll: (event: UIEvent) => void; + onFocus: (event: React.FocusEvent) => void; + onBlur: (event: React.FocusEvent) => void; + onDragOver: (event: React.DragEvent) => void; + onDragLeave: (event: React.DragEvent) => void; + onDrop: (event: React.DragEvent) => void; + onPointerDown: (event: PointerEvent) => void; + onPointerMove: (event: PointerEvent) => void; + onPointerUp: (event: PointerEvent) => void; + onPointerCancel: (event: PointerEvent) => void; + onLostPointerCapture: (event: PointerEvent) => void; +} + +export interface TimelineState { + timelineReady: boolean; + elements: readonly TimelineElement[]; + canvas: TimelineCanvasState; + overlays: TimelineOverlaysState; +} + +export interface TimelineActions {} + +export interface TimelineMeta { + renderClipContent: TimelineCanvasState["renderClipContent"]; + renderClipOverlay: TimelineCanvasState["renderClipOverlay"]; + emptyState: ComponentProps; + containerProps: TimelineContainerProps; + viewportProps: TimelineViewportProps; + razorGuide: ReactNode; +} + export interface TimelineContextValue { - state: { - timelineReady: boolean; - elements: readonly unknown[]; - canvasProps: TimelineCanvasProps; - overlaysProps: TimelineOverlaysProps; - }; - actions: Record; - meta: { - renderClipContent: TimelineCanvasProps["renderClipContent"]; - renderClipOverlay: TimelineCanvasProps["renderClipOverlay"]; - emptyState: ComponentProps; - containerProps: ComponentProps<"div">; - viewportProps: ComponentProps<"div">; - razorGuide: ReactNode; - }; + state: TimelineState; + actions: TimelineActions; + meta: TimelineMeta; } const TimelineContext = createContext(null); @@ -74,6 +169,16 @@ export function TimelineProvider({ children, ...props }: TimelineProps & { child return {children}; } +export function TimelineContextProvider({ + value, + children, +}: { + value: TimelineContextValue; + children: ReactNode; +}) { + return {children}; +} + export function useTimelineContext(): TimelineContextValue { const value = useTimelineContextOptional(); if (value === null) throw new Error("useTimelineContext must be used inside TimelineProvider"); diff --git a/packages/studio/src/player/components/useTimelineClipContextMenu.ts b/packages/studio/src/player/components/useTimelineClipContextMenu.ts index 28c545df2e..25a47f369d 100644 --- a/packages/studio/src/player/components/useTimelineClipContextMenu.ts +++ b/packages/studio/src/player/components/useTimelineClipContextMenu.ts @@ -1,6 +1,6 @@ import { useCallback } from "react"; import { usePlayerStore, type TimelineElement } from "../store/playerStore"; -import type { ClipContextMenuState } from "./TimelineOverlays"; +import type { ClipContextMenuState } from "./TimelineProvider"; /** A clicked member of an active multi-selection keeps the whole group * selected, so the context menu it opens acts on all of it. */ diff --git a/packages/studio/src/player/components/useTimelineProviderState.tsx b/packages/studio/src/player/components/useTimelineProviderState.tsx index 20b8bfdb4d..c74bc9a6fb 100644 --- a/packages/studio/src/player/components/useTimelineProviderState.tsx +++ b/packages/studio/src/player/components/useTimelineProviderState.tsx @@ -9,7 +9,7 @@ import { useTimelineZoom } from "./useTimelineZoom"; import { useTimelineAssetDrop } from "./timelineDragDrop"; import { type KeyframeDiamondContextMenuState } from "./KeyframeDiamondContextMenu"; import { useTimelineClipDrag } from "./useTimelineClipDrag"; -import { type ClipContextMenuState } from "./TimelineOverlays"; +import type { ClipContextMenuState, TimelineContextValue } from "./TimelineProvider"; import { useTimelineEditPinning } from "./useTimelineEditPinning"; import { useTimelineStackingSync } from "./useTimelineStackingSync"; import { useTimelineGeometry } from "./useTimelineGeometry"; @@ -42,7 +42,6 @@ import { useTimelineActiveClips } from "./useTimelineActiveClips"; import { useTimelineLaneMoveRefresh } from "./useTimelineLaneMoveRefresh"; import { useTimelineLogicalFocus } from "./useTimelineLogicalFocus"; import { useClipContextMenu } from "./useTimelineClipContextMenu"; -import type { TimelineContextValue } from "./TimelineProvider"; export function useTimelineProviderState({ onSeek, @@ -519,7 +518,12 @@ export function useTimelineProviderState({ onHoverGapAction: setHoveredGapAction, }; const contextValue: TimelineContextValue = { - state: { timelineReady, elements: timelineElements, canvasProps, overlaysProps }, + state: { + timelineReady, + elements: timelineElements, + canvas: canvasProps, + overlays: overlaysProps, + }, actions: {}, meta: { renderClipContent,