From 7bd053a919869e6ee1e87557c9fc8cc350833534 Mon Sep 17 00:00:00 2001 From: Vance Ingalls Date: Wed, 12 Aug 2026 16:54:12 -0700 Subject: [PATCH 1/2] fix(studio): pull TimelineLanes under the 600-line cap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TimelineLanes.tsx hit 620 lines. Extracted the three per-clip pointer gestures (resize-start, pointer-down move-arm, click/razor-split) into createClipGestureHandlers — one factory call per rendered clip instead of ~120 lines of inline handler bodies in the render loop. 529 lines now. --- .../src/player/components/TimelineLanes.tsx | 149 +++------------- .../components/timelineClipGestureHandlers.ts | 166 ++++++++++++++++++ 2 files changed, 195 insertions(+), 120 deletions(-) create mode 100644 packages/studio/src/player/components/timelineClipGestureHandlers.ts diff --git a/packages/studio/src/player/components/TimelineLanes.tsx b/packages/studio/src/player/components/TimelineLanes.tsx index d9ddf38429..2cd537b46b 100644 --- a/packages/studio/src/player/components/TimelineLanes.tsx +++ b/packages/studio/src/player/components/TimelineLanes.tsx @@ -9,8 +9,8 @@ import { TimelineTrackHeader } from "./TimelineTrackHeader"; import { resolveTrackKeyframeClip } from "./useTimelineTrackLayout"; import { trackDisplayNumber, trackDisplaySuffix } from "./timelineTrackDisplay"; import { clipTimingStart } from "../../hooks/gsapShared"; -import { getTimelineEditCapabilities, resolveBlockedTimelineEditIntent } from "./timelineEditing"; -import { CLIP_Y, CLIP_HANDLE_W, TRACK_H } from "./timelineLayout"; +import { getTimelineEditCapabilities } from "./timelineEditing"; +import { CLIP_Y, TRACK_H } from "./timelineLayout"; import { usePlayerStore, type TimelineElement } from "../store/playerStore"; import { isMultiDragActive, @@ -22,8 +22,8 @@ import { import type { TimelineLaneBaseProps } from "./timelineLaneProps"; import type { TimelineEditCallbacks } from "./timelineCallbacks"; import { trackStudioKeyframeLaneExpand } from "../../telemetry/events"; -import { SPLIT_BOUNDARY_EPSILON_S } from "../../utils/timelineElementSplit"; import { isAudioTimelineElement, isMusicTrack } from "../../utils/timelineInspector"; +import { createClipGestureHandlers } from "./timelineClipGestureHandlers"; import { renderClipChildren, resolveClipRenderContext } from "./timelineClipChildren"; import { TimelineTrackRow } from "./TimelineTrackRow"; import { isTimelineClipActive } from "./useTimelineActiveClips"; @@ -347,6 +347,29 @@ export function TimelineLanes({ const passengerOffsetPx = isPassenger ? multiDragPassengerOffsetPx(clipKey, pps, multiDragPreview) : 0; + const clipGestures = createClipGestureHandlers( + el, + elementKey, + previewElement, + capabilities, + { + pps, + onResizeElement, + onMoveElement, + onRazorSplit, + onRazorSplitAll, + blockedClipRef, + shiftClickClipRef, + suppressClickRef, + scrollRef, + setShowPopover, + setRangeSelection, + setResizingClip, + setDraggedClip, + setSelectedElementId, + onSelectElement, + }, + ); const clip = ( setHoveredClip(clipKey)} onHoverEnd={() => setHoveredClip(null)} - onResizeStart={ - // fallow-ignore-next-line complexity - (edge, e) => { - if (e.button !== 0 || e.shiftKey || !onResizeElement) return; - if (edge === "start" && !capabilities.canTrimStart) return; - if (edge === "end" && !capabilities.canTrimEnd) return; - e.stopPropagation(); - blockedClipRef.current = null; - setShowPopover(false); - setRangeSelection(null); - setResizingClip({ - pointerId: e.pointerId, - element: el, - edge, - originClientX: e.clientX, - originScrollLeft: scrollRef.current?.scrollLeft ?? 0, - previewStart: el.start, - previewDuration: el.duration, - previewPlaybackStart: el.playbackStart, - started: false, - }); - } - } - onPointerDown={ - // fallow-ignore-next-line complexity - (e) => { - if (e.button !== 0) return; - if (usePlayerStore.getState().activeTool === "razor") return; - if (e.shiftKey) { - shiftClickClipRef.current = { - element: el, - anchorX: e.clientX, - anchorY: e.clientY, - }; - return; - } - const target = e.currentTarget as HTMLElement; - const rect = target.getBoundingClientRect(); - const blockedIntent = resolveBlockedTimelineEditIntent({ - width: rect.width, - offsetX: e.clientX - rect.left, - handleWidth: CLIP_HANDLE_W, - capabilities, - }); - if ( - blockedIntent && - ((blockedIntent === "move" && onMoveElement) || - (blockedIntent !== "move" && onResizeElement)) - ) { - blockedClipRef.current = { - pointerId: e.pointerId, - element: el, - intent: blockedIntent, - originClientX: e.clientX, - originClientY: e.clientY, - started: false, - }; - return; - } - if (!onMoveElement || !capabilities.canMove) return; - blockedClipRef.current = null; - setShowPopover(false); - setRangeSelection(null); - setDraggedClip({ - pointerId: e.pointerId, - element: el, - originClientX: e.clientX, - originClientY: e.clientY, - originScrollLeft: scrollRef.current?.scrollLeft ?? 0, - originScrollTop: scrollRef.current?.scrollTop ?? 0, - pointerClientX: e.clientX, - pointerClientY: e.clientY, - pointerOffsetX: e.clientX - rect.left, - pointerOffsetY: e.clientY - rect.top, - previewStart: el.start, - previewTrack: el.track, - desiredTrack: el.track, - insertRow: null, - snapTime: null, - snapType: null, - started: false, - }); - } - } - onClick={ - // fallow-ignore-next-line complexity - (e) => { - e.stopPropagation(); - if (suppressClickRef.current) return; - const { activeTool } = usePlayerStore.getState(); - if (activeTool === "razor" && onRazorSplit) { - const clipRect = ( - e.currentTarget as HTMLElement - ).getBoundingClientRect(); - const clickOffsetX = e.clientX - clipRect.left; - const splitTime = previewElement.start + clickOffsetX / pps; - const clampedTime = Math.max( - previewElement.start + SPLIT_BOUNDARY_EPSILON_S, - Math.min( - previewElement.start + - previewElement.duration - - SPLIT_BOUNDARY_EPSILON_S, - splitTime, - ), - ); - if (e.shiftKey && onRazorSplitAll) { - onRazorSplitAll(clampedTime); - } else { - onRazorSplit(el, clampedTime); - } - return; - } - // Clip selection is idempotent; empty timeline space owns deselection. - setSelectedElementId(elementKey); - onSelectElement?.(el); - } - } + onResizeStart={clipGestures.onResizeStart} + onPointerDown={clipGestures.onPointerDown} + onClick={clipGestures.onClick} onDoubleClick={(e) => { e.stopPropagation(); if (suppressClickRef.current) return; diff --git a/packages/studio/src/player/components/timelineClipGestureHandlers.ts b/packages/studio/src/player/components/timelineClipGestureHandlers.ts new file mode 100644 index 0000000000..7cba6a9a5a --- /dev/null +++ b/packages/studio/src/player/components/timelineClipGestureHandlers.ts @@ -0,0 +1,166 @@ +import type { PointerEvent as ReactPointerEvent, MouseEvent as ReactMouseEvent } from "react"; +import { usePlayerStore, type TimelineElement } from "../store/playerStore"; +import type { DraggedClipState, ResizingClipState, BlockedClipState } from "./useTimelineClipDrag"; +import { resolveBlockedTimelineEditIntent } from "./timelineEditing"; +import type { TimelineEditCapabilities } from "./timelineEditCapabilities"; +import type { TimelineEditCallbacks } from "./timelineCallbacks"; +import { CLIP_HANDLE_W } from "./timelineLayout"; +import { SPLIT_BOUNDARY_EPSILON_S } from "../../utils/timelineElementSplit"; + +export interface ClipGestureDeps { + pps: number; + onResizeElement?: TimelineEditCallbacks["onResizeElement"]; + onMoveElement?: TimelineEditCallbacks["onMoveElement"]; + onRazorSplit?: (element: TimelineElement, splitTime: number) => Promise | void; + onRazorSplitAll?: (splitTime: number) => Promise | void; + blockedClipRef: React.RefObject; + shiftClickClipRef: React.RefObject<{ + element: TimelineElement; + anchorX: number; + anchorY: number; + } | null>; + suppressClickRef: React.RefObject; + scrollRef: React.RefObject; + setShowPopover(v: boolean): void; + setRangeSelection(v: null): void; + setResizingClip(v: ResizingClipState | null): void; + setDraggedClip(v: DraggedClipState | null): void; + setSelectedElementId(id: string | null): void; + onSelectElement?: (element: TimelineElement | null) => void; +} + +/** + * The three pointer/click gestures a clip bar answers to. Factored out of + * TimelineLanes' render loop — one call per rendered clip, closing over that + * clip's own `el`/`elementKey`/`previewElement`/`capabilities` — so the huge + * per-track map body reads as JSX instead of ~120 lines of gesture logic. + */ +export function createClipGestureHandlers( + el: TimelineElement, + elementKey: string, + previewElement: TimelineElement, + capabilities: TimelineEditCapabilities, + deps: ClipGestureDeps, +) { + const { + pps, + onResizeElement, + onMoveElement, + onRazorSplit, + onRazorSplitAll, + blockedClipRef, + shiftClickClipRef, + suppressClickRef, + scrollRef, + setShowPopover, + setRangeSelection, + setResizingClip, + setDraggedClip, + setSelectedElementId, + onSelectElement, + } = deps; + + const onResizeStart = (edge: "start" | "end", e: ReactPointerEvent): void => { + if (e.button !== 0 || e.shiftKey || !onResizeElement) return; + if (edge === "start" && !capabilities.canTrimStart) return; + if (edge === "end" && !capabilities.canTrimEnd) return; + e.stopPropagation(); + blockedClipRef.current = null; + setShowPopover(false); + setRangeSelection(null); + setResizingClip({ + pointerId: e.pointerId, + element: el, + edge, + originClientX: e.clientX, + originScrollLeft: scrollRef.current?.scrollLeft ?? 0, + previewStart: el.start, + previewDuration: el.duration, + previewPlaybackStart: el.playbackStart, + started: false, + }); + }; + + const onPointerDown = (e: ReactPointerEvent): void => { + if (e.button !== 0) return; + if (usePlayerStore.getState().activeTool === "razor") return; + if (e.shiftKey) { + shiftClickClipRef.current = { element: el, anchorX: e.clientX, anchorY: e.clientY }; + return; + } + const target = e.currentTarget as HTMLElement; + const rect = target.getBoundingClientRect(); + const blockedIntent = resolveBlockedTimelineEditIntent({ + width: rect.width, + offsetX: e.clientX - rect.left, + handleWidth: CLIP_HANDLE_W, + capabilities, + }); + if ( + blockedIntent && + ((blockedIntent === "move" && onMoveElement) || (blockedIntent !== "move" && onResizeElement)) + ) { + blockedClipRef.current = { + pointerId: e.pointerId, + element: el, + intent: blockedIntent, + originClientX: e.clientX, + originClientY: e.clientY, + started: false, + }; + return; + } + if (!onMoveElement || !capabilities.canMove) return; + blockedClipRef.current = null; + setShowPopover(false); + setRangeSelection(null); + setDraggedClip({ + pointerId: e.pointerId, + element: el, + originClientX: e.clientX, + originClientY: e.clientY, + originScrollLeft: scrollRef.current?.scrollLeft ?? 0, + originScrollTop: scrollRef.current?.scrollTop ?? 0, + pointerClientX: e.clientX, + pointerClientY: e.clientY, + pointerOffsetX: e.clientX - rect.left, + pointerOffsetY: e.clientY - rect.top, + previewStart: el.start, + previewTrack: el.track, + desiredTrack: el.track, + insertRow: null, + snapTime: null, + snapType: null, + started: false, + }); + }; + + const onClick = (e: ReactMouseEvent): void => { + e.stopPropagation(); + if (suppressClickRef.current) return; + const { activeTool } = usePlayerStore.getState(); + if (activeTool === "razor" && onRazorSplit) { + const clipRect = (e.currentTarget as HTMLElement).getBoundingClientRect(); + const clickOffsetX = e.clientX - clipRect.left; + const splitTime = previewElement.start + clickOffsetX / pps; + const clampedTime = Math.max( + previewElement.start + SPLIT_BOUNDARY_EPSILON_S, + Math.min( + previewElement.start + previewElement.duration - SPLIT_BOUNDARY_EPSILON_S, + splitTime, + ), + ); + if (e.shiftKey && onRazorSplitAll) { + onRazorSplitAll(clampedTime); + } else { + onRazorSplit(el, clampedTime); + } + return; + } + // Clip selection is idempotent; empty timeline space owns deselection. + setSelectedElementId(elementKey); + onSelectElement?.(el); + }; + + return { onResizeStart, onPointerDown, onClick }; +} From c03fd4a3d92cdbd59d6df4c04eb37acd695a174b Mon Sep 17 00:00:00 2001 From: Vance Ingalls Date: Wed, 12 Aug 2026 17:06:33 -0700 Subject: [PATCH 2/2] fix(studio): split the extracted pointerdown handler under the CRAP threshold Moving the ~120-line gesture logic into timelineClipGestureHandlers.ts concentrated it into two functions fallow flagged (onPointerDown at CRAP 63.6, onResizeStart at 31.6). Split the decision logic (which gesture a pointerdown implies) into a pure resolvePointerDownAction, then split its own intent-blocking check into isIntentBlocked. onResizeStart's guard moved into canStartResize. Every function now scores under 30. --- .../components/timelineClipGestureHandlers.ts | 99 +++++++++++++++---- 1 file changed, 78 insertions(+), 21 deletions(-) diff --git a/packages/studio/src/player/components/timelineClipGestureHandlers.ts b/packages/studio/src/player/components/timelineClipGestureHandlers.ts index 7cba6a9a5a..42d4f4dd56 100644 --- a/packages/studio/src/player/components/timelineClipGestureHandlers.ts +++ b/packages/studio/src/player/components/timelineClipGestureHandlers.ts @@ -1,7 +1,10 @@ import type { PointerEvent as ReactPointerEvent, MouseEvent as ReactMouseEvent } from "react"; import { usePlayerStore, type TimelineElement } from "../store/playerStore"; import type { DraggedClipState, ResizingClipState, BlockedClipState } from "./useTimelineClipDrag"; -import { resolveBlockedTimelineEditIntent } from "./timelineEditing"; +import { + resolveBlockedTimelineEditIntent, + type BlockedTimelineEditIntent, +} from "./timelineEditing"; import type { TimelineEditCapabilities } from "./timelineEditCapabilities"; import type { TimelineEditCallbacks } from "./timelineCallbacks"; import { CLIP_HANDLE_W } from "./timelineLayout"; @@ -29,6 +32,70 @@ export interface ClipGestureDeps { onSelectElement?: (element: TimelineElement | null) => void; } +/** Whether a resize-handle drag on `edge` is allowed to begin at all. */ +function canStartResize( + edge: "start" | "end", + e: ReactPointerEvent, + capabilities: TimelineEditCapabilities, + onResizeElement: ClipGestureDeps["onResizeElement"], +): boolean { + if (e.button !== 0 || e.shiftKey || !onResizeElement) return false; + if (edge === "start") return capabilities.canTrimStart; + return capabilities.canTrimEnd; +} + +type PointerDownAction = + | { kind: "ignore" } + | { kind: "arm-shift-click" } + | { kind: "block"; intent: BlockedTimelineEditIntent; rect: DOMRect } + | { kind: "move"; rect: DOMRect }; + +/** + * A hit-tested intent is "blocked" when the pointer landed on a smaller + * target than the gesture it implies would need — e.g. a resize handle too + * narrow to grab reliably — AND that gesture is actually wired up. An intent + * with nowhere to go (no matching callback) is not blocking anything. + */ +function isIntentBlocked( + intent: BlockedTimelineEditIntent | null, + onResizeElement: ClipGestureDeps["onResizeElement"], + onMoveElement: ClipGestureDeps["onMoveElement"], +): intent is BlockedTimelineEditIntent { + if (!intent) return false; + return intent === "move" ? Boolean(onMoveElement) : Boolean(onResizeElement); +} + +/** + * What a pointerdown on a clip bar means, before any state is touched. Kept + * pure (no refs, no store writes) so the branching that decides move vs. + * resize-handle vs. blocked-by-a-smaller-hit-target lives in one place, + * separate from the side effects `onPointerDown` below performs for it. + */ +function resolvePointerDownAction( + e: ReactPointerEvent, + capabilities: TimelineEditCapabilities, + onResizeElement: ClipGestureDeps["onResizeElement"], + onMoveElement: ClipGestureDeps["onMoveElement"], +): PointerDownAction { + if (e.button !== 0) return { kind: "ignore" }; + if (usePlayerStore.getState().activeTool === "razor") return { kind: "ignore" }; + if (e.shiftKey) return { kind: "arm-shift-click" }; + + const rect = (e.currentTarget as HTMLElement).getBoundingClientRect(); + const intent = resolveBlockedTimelineEditIntent({ + width: rect.width, + offsetX: e.clientX - rect.left, + handleWidth: CLIP_HANDLE_W, + capabilities, + }); + if (isIntentBlocked(intent, onResizeElement, onMoveElement)) { + return { kind: "block", intent, rect }; + } + + if (!onMoveElement || !capabilities.canMove) return { kind: "ignore" }; + return { kind: "move", rect }; +} + /** * The three pointer/click gestures a clip bar answers to. Factored out of * TimelineLanes' render loop — one call per rendered clip, closing over that @@ -61,9 +128,7 @@ export function createClipGestureHandlers( } = deps; const onResizeStart = (edge: "start" | "end", e: ReactPointerEvent): void => { - if (e.button !== 0 || e.shiftKey || !onResizeElement) return; - if (edge === "start" && !capabilities.canTrimStart) return; - if (edge === "end" && !capabilities.canTrimEnd) return; + if (!canStartResize(edge, e, capabilities, onResizeElement)) return; e.stopPropagation(); blockedClipRef.current = null; setShowPopover(false); @@ -82,35 +147,27 @@ export function createClipGestureHandlers( }; const onPointerDown = (e: ReactPointerEvent): void => { - if (e.button !== 0) return; - if (usePlayerStore.getState().activeTool === "razor") return; - if (e.shiftKey) { + const action = resolvePointerDownAction(e, capabilities, onResizeElement, onMoveElement); + if (action.kind === "ignore") return; + + if (action.kind === "arm-shift-click") { shiftClickClipRef.current = { element: el, anchorX: e.clientX, anchorY: e.clientY }; return; } - const target = e.currentTarget as HTMLElement; - const rect = target.getBoundingClientRect(); - const blockedIntent = resolveBlockedTimelineEditIntent({ - width: rect.width, - offsetX: e.clientX - rect.left, - handleWidth: CLIP_HANDLE_W, - capabilities, - }); - if ( - blockedIntent && - ((blockedIntent === "move" && onMoveElement) || (blockedIntent !== "move" && onResizeElement)) - ) { + + if (action.kind === "block") { blockedClipRef.current = { pointerId: e.pointerId, element: el, - intent: blockedIntent, + intent: action.intent, originClientX: e.clientX, originClientY: e.clientY, started: false, }; return; } - if (!onMoveElement || !capabilities.canMove) return; + + const { rect } = action; blockedClipRef.current = null; setShowPopover(false); setRangeSelection(null);