Files
hyperframes/packages/studio/src/player/components/timelineZoom.ts
T
Miguel Ángel 723d3381c4 fix(studio): keep dense keyframes readable (#2925)
* perf(studio): define timeline viewport budgets and fixtures

* test(studio): gate timeline viewport performance in Chromium

* refactor(studio): isolate clip drag lifecycle

* refactor(studio): extract timeline render contracts

* perf(studio): centralize timeline viewport geometry

* perf(studio): follow playhead across virtualized rows

* perf(studio): add timeline clip-window index primitive

* perf(studio): virtualize timeline clip windows

* perf(studio): stop timeline scroll work when row virtualization is off

The row virtualization stack made the timeline publish a viewport snapshot
on every scroll frame and swap `renderClipContent` across every mounted clip
at gesture start and settle. Both are windowing concessions, and neither was
gated on the flag, so the build users actually run paid for them while
mounting all 1,000 clips anyway. Measured on a 3,000-clip project: median
scroll step 16.6ms to 76.9ms, p95 17.9ms to 189.4ms, 40 long tasks to 247.

Gate both on the row virtualization flag. The scroll path now stops at the
door when the flag is off, so `isScrolling` stays false and resize-driven
and programmatic syncs still publish through the immediate path.

The flag moves into its own module: the scroll-viewport hook needs to read
it, and the virtualization hook already imports the viewport snapshot type
back, which would have closed an import cycle.

Also release the perf fixture lease from the fixture rather than from the
test-hook effect. Loading a fixture writes player state, which changed that
effect's dependency identities and tore it down on the next frame, so the
lease was revoked moments after it was taken and live iframe discovery
overwrote the fixture before the gate could measure it.

The e2e gate gains a flag-off arm (`test:timeline-default`, 1,000 elements)
next to the existing flag-on one. It refuses the 50,000-element combination,
verifies from the mounted DOM that the server under test matches the
requested flag, and skips the DOM-size budgets for the unvirtualized build
rather than relaxing them, so a skipped budget never reads as a passed one.

Verified against a live Studio dev server on the fixture project:

  flag off, before: interactionP95 303.1ms, longest task 194ms, 0/5 runs pass
  flag off, after:  interactionP95  33.6ms, longest task   0ms, 5/5 runs pass
  flag on,  after:  interactionP95  33.2ms, 4/5 runs pass, exit 0

The flag-on arm's fourth run reproducibly reports a 55-58ms long task
against a 50ms budget. That is the residual tail of the window swap itself,
tracked separately and not addressed here.

* ci(studio): run the timeline viewport gate on studio changes

The gate has existed since the row virtualization stack landed but nothing
under `.github/` referenced it, so it only ever ran when someone ran it by
hand. That is how the flag-off scroll regression reached eight merged-ready
PRs without anything noticing.

Adds a `studio-timeline-viewport` job that boots two Studio dev servers, one
per flag state, and runs both arms of the gate against them. Two servers are
needed because row virtualization is read from `import.meta.env` at module
load, so one process cannot serve both builds.

Scoped to a new `studio` paths filter rather than the broad `code` one: the
gate only says anything about `packages/studio`, `packages/core` and
`packages/studio-server`.

Adds a `ci` tier. It applies the constrained budgets without any emulation,
because a hosted runner is already slower and noisier than the machine the
strict numbers were recorded on, while the existing `low-resource` tier would
throttle it a further 4x and measure the throttle rather than the build.

The fixture composition is tracked under `tests/e2e/fixtures` but Studio
resolves projects from the gitignored `data/projects`, so the job copies it
into place instead of a project directory being committed.

Both arms run in about 7 seconds each locally, so the job cost is almost
entirely dependency install and the workspace build it shares with
`studio-load-smoke`.

* fix(ci): preserve both timeline gate evidence arms

* ci(studio): report timeline gate arm statuses

* ci(studio): require timeline gate evidence artifacts

* fix(studio): keep dense keyframes readable

* fix(ci): resolve timeline stack audit findings
2026-07-31 18:05:22 +02:00

122 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { ZoomMode } from "../store/playerStore";
import { STUDIO_PREVIEW_FPS } from "../lib/time";
export const MIN_TIMELINE_ZOOM_PERCENT = 10;
const MAX_TIMELINE_FRAME_WIDTH_PX = 48;
// CapCut-strength steps: one button press / pinch gesture moves the zoom
// meaningfully (user feedback, twice-doubled: 1.25×/0.8× + 0.0035 felt like
// "zooming several times to get anywhere", then 1.5× + 0.007 still too soft).
// Kept reciprocal (2 × 0.5 = 1) so in+out round-trips.
const ZOOM_OUT_FACTOR = 0.5;
const ZOOM_IN_FACTOR = 2;
const PINCH_ZOOM_SENSITIVITY = 0.014;
export function getMaxTimelineZoomPercent(fitPixelsPerSecond: number): number {
if (!Number.isFinite(fitPixelsPerSecond) || fitPixelsPerSecond <= 0) return 100;
const frameLevelPixelsPerSecond = STUDIO_PREVIEW_FPS * MAX_TIMELINE_FRAME_WIDTH_PX;
return Math.max(100, Math.round((frameLevelPixelsPerSecond / fitPixelsPerSecond) * 100));
}
export function clampTimelineZoomPercent(percent: number, fitPixelsPerSecond: number): number {
if (!Number.isFinite(percent)) return 100;
const maxZoomPercent = getMaxTimelineZoomPercent(fitPixelsPerSecond);
return Math.max(MIN_TIMELINE_ZOOM_PERCENT, Math.min(maxZoomPercent, Math.round(percent)));
}
export function getTimelineZoomPercent(
zoomMode: ZoomMode,
manualZoomPercent: number,
fitPixelsPerSecond: number,
): number {
return zoomMode === "fit" ? 100 : clampTimelineZoomPercent(manualZoomPercent, fitPixelsPerSecond);
}
/**
* The manual-zoom percent that, applied to `fitPixelsPerSecond`, reproduces the
* CURRENT on-screen pixels-per-second exactly. Used to PIN the timeline zoom on
* the first edit so a duration change (which recomputes fit-pps) no longer
* rescales every clip: we switch `zoomMode` to "manual" with this percent, so
* `getTimelinePixelsPerSecond` keeps returning today's pps regardless of the new
* fit basis.
*
* Since `pps = fitPps * (percent / 100)` in manual mode, and while fitting
* `pps === fitPps`, the pinned percent is `currentPps / fitPps * 100`. Clamped to
* the manual-zoom range so the pin can't land outside the slider's bounds; falls
* back to 100 (a no-op pin at the current fit) when either input is unusable.
*/
export function computePinnedZoomPercent(
currentPixelsPerSecond: number,
fitPixelsPerSecond: number,
): number {
if (
!Number.isFinite(currentPixelsPerSecond) ||
currentPixelsPerSecond <= 0 ||
!Number.isFinite(fitPixelsPerSecond) ||
fitPixelsPerSecond <= 0
) {
return 100;
}
return clampTimelineZoomPercent(
(currentPixelsPerSecond / fitPixelsPerSecond) * 100,
fitPixelsPerSecond,
);
}
export function getTimelinePixelsPerSecond(
fitPixelsPerSecond: number,
zoomMode: ZoomMode,
manualZoomPercent: number,
): number {
if (!Number.isFinite(fitPixelsPerSecond) || fitPixelsPerSecond <= 0) return 100;
const zoomPercent = getTimelineZoomPercent(zoomMode, manualZoomPercent, fitPixelsPerSecond);
return zoomMode === "fit" ? fitPixelsPerSecond : fitPixelsPerSecond * (zoomPercent / 100);
}
export function getNextTimelineZoomPercent(
direction: "in" | "out",
zoomMode: ZoomMode,
manualZoomPercent: number,
fitPixelsPerSecond: number,
): number {
const current = getTimelineZoomPercent(zoomMode, manualZoomPercent, fitPixelsPerSecond);
const next = direction === "in" ? current * ZOOM_IN_FACTOR : current * ZOOM_OUT_FACTOR;
return clampTimelineZoomPercent(next, fitPixelsPerSecond);
}
export function getPinchTimelineZoomPercent(
deltaY: number,
zoomMode: ZoomMode,
manualZoomPercent: number,
fitPixelsPerSecond: number,
): number {
const current = getTimelineZoomPercent(zoomMode, manualZoomPercent, fitPixelsPerSecond);
if (!Number.isFinite(deltaY) || deltaY === 0) return current;
return clampTimelineZoomPercent(
current * Math.exp(-deltaY * PINCH_ZOOM_SENSITIVITY),
fitPixelsPerSecond,
);
}
const LOG_MIN = Math.log(MIN_TIMELINE_ZOOM_PERCENT);
/**
* Maps the frame-level zoom range to a slider position (0100) using a log scale.
* Linear would compress the useful low end into a tiny sliver of the slider.
*/
export function timelineZoomPercentToSlider(percent: number, fitPixelsPerSecond: number): number {
const clamped = clampTimelineZoomPercent(percent, fitPixelsPerSecond);
const logMax = Math.log(getMaxTimelineZoomPercent(fitPixelsPerSecond));
return ((Math.log(clamped) - LOG_MIN) / (logMax - LOG_MIN)) * 100;
}
/**
* Maps a slider position (0100) to the frame-level zoom range using a log scale.
* Inverse of `timelineZoomPercentToSlider`.
*/
export function timelineSliderToZoomPercent(slider: number, fitPixelsPerSecond: number): number {
const clampedSlider = Math.max(0, Math.min(100, slider));
const logMax = Math.log(getMaxTimelineZoomPercent(fitPixelsPerSecond));
const logValue = LOG_MIN + (clampedSlider / 100) * (logMax - LOG_MIN);
return clampTimelineZoomPercent(Math.exp(logValue), fitPixelsPerSecond);
}