diff --git a/src/components/timeline/components/clip/clip-component.ts b/src/components/timeline/components/clip/clip-component.ts index 96e71b96..ec11fb11 100644 --- a/src/components/timeline/components/clip/clip-component.ts +++ b/src/components/timeline/components/clip/clip-component.ts @@ -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); }); } diff --git a/src/components/timeline/interaction/interaction-controller.ts b/src/components/timeline/interaction/interaction-controller.ts index 610278a6..30a2ec61 100644 --- a/src/components/timeline/interaction/interaction-controller.ts +++ b/src/components/timeline/interaction/interaction-controller.ts @@ -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(); } @@ -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(); diff --git a/src/components/timeline/interaction/timeline-pinch.ts b/src/components/timeline/interaction/timeline-pinch.ts new file mode 100644 index 00000000..b59b3984 --- /dev/null +++ b/src/components/timeline/interaction/timeline-pinch.ts @@ -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; + } +} diff --git a/src/components/timeline/timeline.ts b/src/components/timeline/timeline.ts index e4c60fad..faa9be85 100644 --- a/src/components/timeline/timeline.ts +++ b/src/components/timeline/timeline.ts @@ -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"; @@ -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; @@ -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(); @@ -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; diff --git a/src/styles/timeline/timeline.css b/src/styles/timeline/timeline.css index d04d7fcd..6ab5e7b8 100644 --- a/src/styles/timeline/timeline.css +++ b/src/styles/timeline/timeline.css @@ -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; @@ -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); diff --git a/tests/timeline-pinch.test.ts b/tests/timeline-pinch.test.ts new file mode 100644 index 00000000..eb69b128 --- /dev/null +++ b/tests/timeline-pinch.test.ts @@ -0,0 +1,235 @@ +/** @jest-environment jsdom */ +import { AssetLoadTracker } from "@core/events/asset-load-tracker"; +import { EventEmitter } from "@core/events/event-emitter"; +import { sec } from "@core/timing/types"; +import { Timeline } from "@timeline/timeline"; + +// @ts-expect-error - Jest transforms this via moduleNameMapper to css-inline.js mock +import styles from "../src/styles/index.css?inline"; + +async function mountTimeline() { + const events = new EventEmitter(); + const config = { timeline: { tracks: [{ clips: [{ id: "clip-1", asset: { type: "html5", html: "
Title
" }, start: 0, length: 5 }] }] } }; + const edit = { + events, + assetLoader: { loadTracker: new AssetLoadTracker() }, + getInternalEvents: () => events, + getResolvedEdit: () => config, + getEdit: () => config, + isClipSelected: () => false, + selectClip: jest.fn(), + clearSelection: jest.fn(), + getClipGenerationState: () => undefined, + getClipError: () => null, + getPendingCaptures: () => ({ pending: 0, total: 0, progress: 1 }), + getClipCaptureProgress: () => null, + playbackTime: sec(0), + totalDuration: sec(5), + isPlaying: false + }; + const container = document.createElement("div"); + document.body.appendChild(container); + const timeline = new Timeline(edit as never, container); + timeline.registerClipRenderer("html5", { render: () => {} }); + await timeline.load(); + + const root = container.querySelector