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
3 changes: 2 additions & 1 deletion src/components/timeline/components/clip/clip-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ export class ClipComponent {

// Once the browser takes a touch as a pan it sends pointercancel, so pointerup means a tap
this.element.addEventListener("pointerup", e => {
if (!isTouchPointer(e) || !this.currentState) return;
// A pinch cancels its fingers' lifts
if (!isTouchPointer(e) || e.defaultPrevented || !this.currentState) return;
this.options.onSelect(this.currentState.trackIndex, this.currentState.clipIndex, false);
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/timeline/interaction/interaction-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ export class InteractionController implements TimelineInteractionRegistration {
/** Once the browser takes a touch as a pan it sends pointercancel, so a pointerup here is a tap */
private onTouchTap(e: PointerEvent): void {
// A drag or resize of the selected clip also ends here, on the container that captured it
if (!isTouchPointer(e) || this.state.type !== "idle" || (e.target as HTMLElement).closest(".ss-clip")) return;
// A pinch cancels its fingers' lifts
if (!isTouchPointer(e) || e.defaultPrevented || this.state.type !== "idle" || (e.target as HTMLElement).closest(".ss-clip")) return;
this.stateManager.clearSelection();
}

Expand All @@ -623,7 +624,7 @@ export class InteractionController implements TimelineInteractionRegistration {
}

/** Abandon the current interaction without executing any command */
private cancelInteraction(): void {
public cancelInteraction(): void {
if (this.state.type === "dragging") this.cancelDrag(this.state);
else if (this.state.type === "resizing") this.cancelResize(this.state);
this.setIdle();
Expand Down
111 changes: 111 additions & 0 deletions src/components/timeline/interaction/timeline-pinch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import type { Seconds } from "@core/timing/types";

import { distance, timeToViewX, viewXToTime } from "./interaction-calculations";

interface TimelinePinchOptions {
/** The scroller whose scrollLeft keeps the pinched time under the fingers */
scroller: HTMLElement;
getPixelsPerSecond: () => number;
setZoom: (pixelsPerSecond: number) => void;
/** Ends any lift or drag the first finger started */
onPinchStart: () => void;
}

type Pair = [Touch, Touch];
const span = ([a, b]: Pair) => distance(b.clientX - a.clientX, b.clientY - a.clientY);
const midX = ([a, b]: Pair) => (a.clientX + b.clientX) / 2;

/** Two fingers on the timeline zoom it around their midpoint; a single finger keeps the browser's own touch handling. */
export class TimelinePinch {
private pinch: { span: number; pixelsPerSecond: number; time: Seconds } | null = null;
/**
* From a pinch's start until its last finger lifts: touch presses and moves don't reach the timeline, and lifts are
* cancelled so they don't count as taps. The second finger's press lands before the pinch is known, so on the ruler it seeks.
*/
private gestureActive = false;

constructor(
private readonly root: HTMLElement,
private readonly options: TimelinePinchOptions
) {}

public mount(): void {
this.root.addEventListener("touchstart", this.onTouchStart, { passive: false });
this.root.addEventListener("touchmove", this.onTouchMove, { passive: false });
this.root.addEventListener("touchend", this.onTouchEnd);
this.root.addEventListener("touchcancel", this.onTouchEnd);
// iOS Safari zooms the page from its own gesture events unless they are cancelled too
this.root.addEventListener("gesturestart", this.onGesture);
this.root.addEventListener("gesturechange", this.onGesture);
["pointerdown", "pointermove"].forEach(type => this.root.addEventListener(type, this.swallowPointer, true));
// Lifts still reach every handler, as drags such as the playhead's only end on them
this.root.addEventListener("pointerup", this.markLift, true);
}

public dispose(): void {
this.root.removeEventListener("touchstart", this.onTouchStart);
this.root.removeEventListener("touchmove", this.onTouchMove);
this.root.removeEventListener("touchend", this.onTouchEnd);
this.root.removeEventListener("touchcancel", this.onTouchEnd);
this.root.removeEventListener("gesturestart", this.onGesture);
this.root.removeEventListener("gesturechange", this.onGesture);
["pointerdown", "pointermove"].forEach(type => this.root.removeEventListener(type, this.swallowPointer, true));
this.root.removeEventListener("pointerup", this.markLift, true);
}

private readonly onTouchStart = (e: TouchEvent): void => {
const pair = this.pairOnTimeline(e);
if (!pair) return;
e.preventDefault();
this.gestureActive = true;
this.options.onPinchStart();

const pixelsPerSecond = this.options.getPixelsPerSecond();
this.pinch = { span: Math.max(1, span(pair)), pixelsPerSecond, time: viewXToTime(this.contentX(midX(pair)), pixelsPerSecond) };
};

private readonly onTouchMove = (e: TouchEvent): void => {
const pair = this.pairOnTimeline(e);
if (!pair || !this.pinch) return;
e.preventDefault();

this.options.setZoom(this.pinch.pixelsPerSecond * (span(pair) / this.pinch.span));
// Read back after the timeline clamps the zoom
const pixelsPerSecond = this.options.getPixelsPerSecond();
const { scroller } = this.options;
scroller.scrollLeft = timeToViewX(this.pinch.time, pixelsPerSecond) - (midX(pair) - scroller.getBoundingClientRect().left);
};

private readonly onTouchEnd = (e: TouchEvent): void => {
const left = this.onTimeline(e).length;
if (left < 2) this.pinch = null;
if (left === 0) this.gestureActive = false;
};

private readonly onGesture = (e: Event): void => {
if (this.gestureActive) e.preventDefault();
};

private readonly swallowPointer = (e: Event): void => {
if (this.gestureActive && (e as PointerEvent).pointerType === "touch") e.stopPropagation();
};

private readonly markLift = (e: Event): void => {
if (this.gestureActive && (e as PointerEvent).pointerType === "touch") e.preventDefault();
};

/** `touches` spans the whole document, and a finger elsewhere on the page is no part of a timeline pinch */
private onTimeline(e: TouchEvent): Touch[] {
return Array.from(e.touches).filter(touch => this.root.contains(touch.target as Node));
}

private pairOnTimeline(e: TouchEvent): Pair | null {
const touches = this.onTimeline(e);
return touches.length === 2 ? [touches[0], touches[1]] : null;
}

private contentX(clientX: number): number {
const { scroller } = this.options;
return clientX - scroller.getBoundingClientRect().left + scroller.scrollLeft;
}
}
17 changes: 17 additions & 0 deletions src/components/timeline/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RulerComponent } from "./components/ruler/ruler-component";
import { ToolbarComponent } from "./components/toolbar/toolbar-component";
import { TrackListComponent } from "./components/track/track-list";
import { InteractionController } from "./interaction/interaction-controller";
import { TimelinePinch } from "./interaction/timeline-pinch";
import { MediaThumbnailRenderer } from "./media-thumbnail-renderer";
import { ThumbnailGenerator } from "./thumbnail-generator";
import { createTimelineResizeHandle, type TimelineResizeHandle } from "./timeline-resize-handle";
Expand Down Expand Up @@ -47,6 +48,7 @@ export class Timeline {
private playheadGhost: HTMLElement | null = null;
private feedbackLayer: HTMLElement | null = null;
private interactionController: InteractionController | null = null;
private pinch: TimelinePinch | null = null;
private clipContextMenu: ClipContextMenu | null = null;
private resizeHandle: TimelineResizeHandle | null = null;

Expand Down Expand Up @@ -493,6 +495,18 @@ export class Timeline {
});
this.interactionController.mount();

this.pinch = new TimelinePinch(this.element, {
scroller: this.trackList.element,
getPixelsPerSecond: () => this.stateManager.getViewport().pixelsPerSecond,
setZoom: pps => {
this.setZoom(pps);
// Playback defers rendering a frame, which would hold the pinch's scroll to the pre-zoom width
this.draw();
},
onPinchStart: () => this.interactionController?.cancelInteraction()
});
this.pinch.mount();

this.clipContextMenu = new ClipContextMenu(this.edit, this.trackList.element);
this.clipContextMenu.mount();

Expand All @@ -509,6 +523,9 @@ export class Timeline {
this.interactionController?.dispose();
this.interactionController = null;

this.pinch?.dispose();
this.pinch = null;

this.clipContextMenu?.dispose();
this.clipContextMenu = null;

Expand Down
8 changes: 7 additions & 1 deletion src/styles/timeline/timeline.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
}

/* Double-tap zoom and the tap delay get in the way of editing by touch */
.ss-html-timeline,
.ss-clip-context-menu {
touch-action: manipulation;
}

/* A pinch zooms the timeline, never the page; this also rules out double-tap zoom */
.ss-html-timeline {
touch-action: pan-x pan-y;
}

/* Toolbar */
.ss-timeline-toolbar {
display: flex;
Expand Down Expand Up @@ -185,6 +189,8 @@
}

.ss-ruler-content {
/* A zoom rebuilds the ticks, and a finger whose target is rebuilt away never delivers its touchend */
pointer-events: none;
position: relative;
height: 100%;
margin-left: var(--ss-timeline-padding);
Expand Down
Loading
Loading