Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 78 additions & 6 deletions src/components/timeline/interaction/interaction-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
buildTrackYPositions,
determineDragBehavior,
determineDropAction,
distance,
exceedsDragThreshold,
findContentClipAtPosition,
findNearestSnapPoint,
Expand Down Expand Up @@ -83,6 +84,11 @@ const DEFAULT_CONFIG: ResolvedConfig = {
resizeZone: 12
};

/** A touch held this long without moving lifts the clip under it */
const LIFT_HOLD_MS = 450;
/** Movement past this before the hold completes is a pan or a drag, not a lift */
const LIFT_SLOP_PX = 8;

// ─── Lifecycle Interface ───────────────────────────────────────────────────

/**
Expand All @@ -101,6 +107,7 @@ export interface TimelineInteractionRegistration {
export class InteractionController implements TimelineInteractionRegistration {
private state: InteractionState = IDLE_STATE;
private activePointerId = -1;
private activePointerType = "";
private readonly config: ResolvedConfig;
private snapPoints: SnapPoint[] = [];

Expand All @@ -113,6 +120,15 @@ export class InteractionController implements TimelineInteractionRegistration {
private readonly handlePointerUp: (e: PointerEvent) => void;
private readonly handlePointerCancel: (e: PointerEvent) => void;
private readonly handleTouchTap: (e: PointerEvent) => void;
private readonly handleLiftedTouchMove: (e: TouchEvent) => void;

private liftTimer: ReturnType<typeof setTimeout> | null = null;
/** The touch's latest event while a lift is pending, where the lifted clip picks up */
private liftEvent: PointerEvent | null = null;
/** The held clip's id, so a lift never picks up a clip removed or reordered during the hold */
private liftClipId: string | null = null;
/** Where the clip was lifted; set only while a lifted clip is held */
private liftPoint: { x: number; y: number } | null = null;

constructor(
private readonly edit: Edit,
Expand All @@ -131,6 +147,9 @@ export class InteractionController implements TimelineInteractionRegistration {
this.handlePointerUp = this.onPointerUp.bind(this);
this.handlePointerCancel = this.onPointerCancel.bind(this);
this.handleTouchTap = this.onTouchTap.bind(this);
this.handleLiftedTouchMove = e => {
if (this.liftPoint && e.cancelable) e.preventDefault();
};
}

// ═══════════════════════════════════════════════════════════════════════════
Expand All @@ -143,6 +162,8 @@ export class InteractionController implements TimelineInteractionRegistration {
document.addEventListener("pointermove", this.handlePointerMove);
document.addEventListener("pointerup", this.handlePointerUp);
document.addEventListener("pointercancel", this.handlePointerCancel);
// Browsers fix which listeners can cancel a touch when it starts, so this one is in place before any lift
this.tracksContainer.addEventListener("touchmove", this.handleLiftedTouchMove, { passive: false });
}

public update(_deltaTime: number): void {
Expand All @@ -156,6 +177,11 @@ export class InteractionController implements TimelineInteractionRegistration {

private onPointerDown(e: PointerEvent): void {
if (e.button !== 0) return;
// A second finger ends a touch interaction (a pinch or a stray touch) and starts nothing; a palm leaves a pen drag alone
if (this.state.type !== "idle" && e.pointerType === "touch" && e.pointerId !== this.activePointerId) {
if (this.activePointerType === "touch") this.cancelInteraction();
return;
}
if (this.state.type !== "idle") this.cancelInteraction();

const target = e.target as HTMLElement;
Expand All @@ -171,12 +197,15 @@ export class InteractionController implements TimelineInteractionRegistration {
return;
}

// Only the selected clip has touch-action: none; a touch anywhere else is the browser's to scroll
if (isTouchPointer(e) && !clipEl.classList.contains("selected")) return;

const trackIndex = parseInt(clipEl.dataset["trackIndex"] || "0", 10);
const clipIndex = parseInt(clipEl.dataset["clipIndex"] || "0", 10);

// Only the selected clip has touch-action: none; a touch anywhere else is the browser's to scroll unless a still hold lifts it
if (isTouchPointer(e) && !clipEl.classList.contains("selected")) {
if (e.pointerType === "touch") this.startPending(e, { trackIndex, clipIndex }, true);
return;
}

// Check if clicking on resize handle
if (target.classList.contains("ss-clip-resize-handle")) {
const edge = target.classList.contains("left") ? "left" : "right";
Expand All @@ -188,15 +217,41 @@ export class InteractionController implements TimelineInteractionRegistration {
this.startPending(e, { trackIndex, clipIndex });
}

private startPending(e: PointerEvent, clipRef: ClipRef): void {
private startPending(e: PointerEvent, clipRef: ClipRef, liftOnly = false): void {
const clip = this.stateManager.getClipAt(clipRef.trackIndex, clipRef.clipIndex);
if (!clip) return;

// No pointer capture yet: capturing retargets the eventual click event to
// the tracks container, which would break click handlers on clip children
// (e.g. the mask badge). Capture happens if this becomes a real drag.
this.activePointerId = e.pointerId;
this.state = createPendingState({ x: e.clientX, y: e.clientY }, clipRef, clip.config.start);
this.activePointerType = e.pointerType;
const pending = createPendingState({ x: e.clientX, y: e.clientY }, clipRef, clip.config.start, liftOnly);
this.state = pending;

if (e.pointerType === "touch") {
this.liftEvent = e;
this.liftClipId = clip.id;
this.liftTimer = setTimeout(() => this.lift(pending), LIFT_HOLD_MS);
}
}

private lift(pending: PendingState): void {
const e = this.liftEvent;
this.liftTimer = null;
if (this.state !== pending || !e) return;
if (this.stateManager.getClipAt(pending.clipRef.trackIndex, pending.clipRef.clipIndex)?.id !== this.liftClipId) {
this.setIdle();
return;
}

if (pending.liftOnly) {
this.stateManager.selectClip(pending.clipRef.trackIndex, pending.clipRef.clipIndex, false);
this.config.onRequestRender?.();
}
// touch-action is fixed when a touch starts, so only cancelling its touchmoves stops the browser scrolling a lifted clip
this.liftPoint = { x: e.clientX, y: e.clientY };
this.transitionToDragging(e, pending);
}

private startResize(e: PointerEvent, clipRef: ClipRef, edge: "left" | "right"): void {
Expand All @@ -223,6 +278,7 @@ export class InteractionController implements TimelineInteractionRegistration {
/** Track the pointer driving this interaction and capture it so its pointerup cannot be lost */
private beginPointerInteraction(e: PointerEvent): void {
this.activePointerId = e.pointerId;
this.activePointerType = e.pointerType;
try {
this.tracksContainer.setPointerCapture(e.pointerId);
} catch {
Expand Down Expand Up @@ -258,6 +314,13 @@ export class InteractionController implements TimelineInteractionRegistration {
private handlePendingMove(e: PointerEvent, state: PendingState): void {
const dx = e.clientX - state.startPoint.x;
const dy = e.clientY - state.startPoint.y;
if (this.liftEvent) this.liftEvent = e;

if (state.liftOnly) {
// Past the slop the touch is a pan, which the browser already owns
if (distance(dx, dy) > LIFT_SLOP_PX) this.setIdle();
return;
}

if (exceedsDragThreshold(dx, dy, this.config.dragThreshold)) {
this.transitionToDragging(e, state);
Expand Down Expand Up @@ -535,7 +598,9 @@ export class InteractionController implements TimelineInteractionRegistration {
this.setIdle();
break;
case "dragging":
this.completeDrag(e, this.state);
// A lifted clip let go where it was lifted stays put; layout rounding would otherwise read as a small move
if (this.liftPoint && distance(e.clientX - this.liftPoint.x, e.clientY - this.liftPoint.y) <= LIFT_SLOP_PX) this.cancelInteraction();
else this.completeDrag(e, this.state);
break;
case "resizing":
this.completeResize(e, this.state);
Expand Down Expand Up @@ -581,6 +646,12 @@ export class InteractionController implements TimelineInteractionRegistration {
private setIdle(): void {
this.state = IDLE_STATE;
this.activePointerId = -1;
this.activePointerType = "";
if (this.liftTimer) clearTimeout(this.liftTimer);
this.liftTimer = null;
this.liftEvent = null;
this.liftClipId = null;
this.liftPoint = null;
}

private completeDrag(_e: PointerEvent, state: DraggingState): void {
Expand Down Expand Up @@ -952,6 +1023,7 @@ export class InteractionController implements TimelineInteractionRegistration {
document.removeEventListener("pointermove", this.handlePointerMove);
document.removeEventListener("pointerup", this.handlePointerUp);
document.removeEventListener("pointercancel", this.handlePointerCancel);
this.tracksContainer.removeEventListener("touchmove", this.handleLiftedTouchMove);

// Dispose all feedback elements (stateless, idempotent)
disposeFeedbackElements(this.feedbackElements);
Expand Down
6 changes: 4 additions & 2 deletions src/components/timeline/interaction/interaction-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface PendingState {
readonly startPoint: Point;
readonly clipRef: ClipRef;
readonly originalTime: Seconds;
/** A touch on an unselected clip: the browser owns its pan, so only a still hold lifts it into a drag */
readonly liftOnly: boolean;
}

export interface DraggingState {
Expand Down Expand Up @@ -80,8 +82,8 @@ export const IDLE_STATE: IdleState = { type: "idle" };

// ─── State Factory Functions ───────────────────────────────────────────────

export function createPendingState(startPoint: Point, clipRef: ClipRef, originalTime: Seconds): PendingState {
return { type: "pending", startPoint, clipRef, originalTime };
export function createPendingState(startPoint: Point, clipRef: ClipRef, originalTime: Seconds, liftOnly = false): PendingState {
return { type: "pending", startPoint, clipRef, originalTime, liftOnly };
}

export function createDraggingState(
Expand Down
Loading
Loading