Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Restore initial video viewer transitions

Anand Baburajana40c2b5e3e3a
diff --git a/web/apps/space/src/components/FileViewer.tsx b/web/apps/space/src/components/FileViewer.tsx
index 31b2d70fec..e6f57b8063 100644
--- a/web/apps/space/src/components/FileViewer.tsx
+++ b/web/apps/space/src/components/FileViewer.tsx
@@ -37,6 +37,7 @@ import {
playViewerOpen,
viewerCornerRadii,
viewerOriginKey,
+ viewerVideoFrame,
} from "components/viewer-transition";
import log from "ente-base/log";
import { useCommentCount } from "hooks/use-comment-count";
@@ -266,12 +267,8 @@ interface ViewerMotion {
reveal?: () => void;
}
-const endOpenFlight = (
- motion: ViewerMotion,
- root: HTMLElement,
- fadeOut = false,
-) => {
- motion.flight?.release(fadeOut);
+const endOpenFlight = (motion: ViewerMotion, root: HTMLElement) => {
+ motion.flight?.release();
motion.flight = undefined;
delete root.dataset.spaceViewerFlying;
delete root.dataset.spaceViewerWaiting;
@@ -290,15 +287,13 @@ const handOverOpenFlight = (
const image = content?.element;
const isShown =
content?.isError() ||
- (content?.data.type == "video" &&
- content.state == "loaded" &&
- image?.isConnected) ||
+ (content?.data.type == "video" && image?.isConnected) ||
(image instanceof HTMLImageElement &&
image.complete &&
image.naturalWidth > 0 &&
image.isConnected &&
!content?.isDecoding);
- if (isShown) endOpenFlight(motion, root, content?.data.type == "video");
+ if (isShown) endOpenFlight(motion, root);
};
const viewerTouchPoint = (event: Event) => {
@@ -810,11 +805,6 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
viewerPhotosRef.current[pswp?.currIndex ?? activePhotoIndex];
const element = pswp?.currSlide?.content.element;
const video = element?.querySelector("video");
- const preview =
- element instanceof HTMLImageElement
- ? element
- : element?.querySelector("img");
- const fadeFromVideo = Boolean(video && !motion.flight);
let from: ViewerPhotoState | undefined = motion.flight?.current();
let src = from ? motion.flight?.src : undefined;
const rect = element?.getBoundingClientRect();
@@ -825,8 +815,10 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
rect &&
rect.width > 0 &&
rect.height > 0 &&
- preview?.complete &&
- preview.naturalWidth > 0
+ ((element instanceof HTMLImageElement &&
+ element.complete &&
+ element.naturalWidth > 0) ||
+ video)
) {
const scale = Number(pswp.currSlide?.container.style.scale) || 1;
from = shownPhotoState(
@@ -834,7 +826,10 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
{ x: 0, y: 0, w: window.innerWidth, h: window.innerHeight },
viewerCornerRadii(element, scale),
);
- src = preview.currentSrc || preview.src;
+ src =
+ element instanceof HTMLImageElement
+ ? element.currentSrc || element.src
+ : (viewerVideoFrame(video) ?? video?.poster);
}
window.scrollTo({ ...pageScrollRef.current, behavior: "instant" });
const origin =
@@ -859,7 +854,6 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
{
origin,
src,
- fadeFromVideo,
photo: {
width: item.width ?? defaultPhotoWidth,
height: item.height ?? defaultPhotoHeight,
@@ -868,11 +862,9 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
from,
throwVelocity,
startBackground,
- () => {
- if (root.isConnected) onCloseRef.current();
- },
);
- } else onCloseRef.current();
+ }
+ onCloseRef.current();
};
React.useImperativeHandle(
@@ -1547,15 +1539,13 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
pswp.on("contentLoad", (event) => {
if (event.content.data.type != "video") return;
event.preventDefault();
- event.content.state = "loading";
videos.get(event.content)?.destroy();
const item = viewerPhotosRef.current[event.content.index]!;
- const video = createSpaceVideoContent(
- item,
- (asset) => videoLoaderRef.current!(asset),
- { onReady: () => event.content.onLoaded() },
+ const video = createSpaceVideoContent(item, (asset) =>
+ videoLoaderRef.current!(asset),
);
event.content.element = video.element;
+ event.content.onLoaded();
videos.set(event.content, video);
});
pswp.on("contentActivate", ({ content }) =>
diff --git a/web/apps/space/src/components/PostVideoContent.ts b/web/apps/space/src/components/PostVideoContent.ts
index 61d2058d86..3f48864201 100644
--- a/web/apps/space/src/components/PostVideoContent.ts
+++ b/web/apps/space/src/components/PostVideoContent.ts
@@ -10,13 +10,11 @@ export const createSpaceVideoContent = (
inline = false,
onPlay,
onPause,
- onReady,
onTogglePlayback,
}: {
inline?: boolean;
onPlay?: () => Promise<void> | void;
onPause?: () => void;
- onReady?: () => void;
onTogglePlayback?: (play: boolean) => void;
} = {},
) => {
@@ -109,33 +107,12 @@ export const createSpaceVideoContent = (
}
supportMessage.style.marginTop = "2px";
errorMessage.append(errorTitle, supportMessage);
- const cover =
- !inline && item.imageUrl ? document.createElement("img") : undefined;
- if (cover) {
- video.style.position = "relative";
- cover.alt = "";
- cover.src = item.imageUrl!;
- Object.assign(cover.style, {
- position: "absolute",
- inset: "0",
- width: "100%",
- height: "100%",
- objectFit: "contain",
- pointerEvents: "none",
- });
- element.append(cover);
- }
element.append(video, button, loading, errorMessage);
let generation = 0;
let sourceGeneration = 0;
let pendingLoad: Promise<void> | undefined;
let ownedURL: string | undefined;
let disposed = false;
- void (cover?.decode() ?? Promise.resolve())
- .catch(() => cover?.remove())
- .then(() => {
- if (!disposed) onReady?.();
- });
let active = false;
let autoplayBlocked = false;
let failure: "error" | "rate-limit" | undefined;
diff --git a/web/apps/space/src/components/viewer-transition.ts b/web/apps/space/src/components/viewer-transition.ts
index f2fc09a1c0..a04fa44a80 100644
--- a/web/apps/space/src/components/viewer-transition.ts
+++ b/web/apps/space/src/components/viewer-transition.ts
@@ -14,7 +14,6 @@ const openDuration = 0.48;
const openBounce = 0.26;
const closeDuration = 0.36;
const swapFadeMs = 220;
-const coverFadeMs = 150;
const closingFlights = new WeakMap<HTMLElement, () => void>();
export const viewerOriginKey = (spaceId?: string, postId?: number) =>
@@ -29,11 +28,21 @@ export const findViewerOrigin = (key: string | undefined) =>
interface ViewerFlightTarget {
origin: HTMLElement;
- src: string;
- fadeFromVideo?: boolean;
+ src: string | HTMLCanvasElement;
photo: { width: number; height: number };
}
+export const viewerVideoFrame = (
+ video: HTMLVideoElement | null | undefined,
+) => {
+ if (!video || video.readyState < video.HAVE_CURRENT_DATA) return undefined;
+ const frame = document.createElement("canvas");
+ frame.width = video.videoWidth;
+ frame.height = video.videoHeight;
+ frame.getContext("2d")!.drawImage(video, 0, 0);
+ return frame;
+};
+
const rectOf = (element: Element): ViewerRect => {
const { left, top, width, height } = element.getBoundingClientRect();
return { x: left, y: top, w: width, h: height };
@@ -115,7 +124,11 @@ const runSpring = ({
};
// A cropping window and the photo inside it, both moved with transforms.
-const createFlyer = (root: HTMLElement, src: string, base: ViewerRect) => {
+const createFlyer = (
+ root: HTMLElement,
+ src: string | HTMLCanvasElement,
+ base: ViewerRect,
+) => {
const flyer = document.createElement("div");
flyer.setAttribute("aria-hidden", "true");
Object.assign(flyer.style, {
@@ -130,9 +143,10 @@ const createFlyer = (root: HTMLElement, src: string, base: ViewerRect) => {
willChange: "transform",
zIndex: "1",
});
- const image = document.createElement("img");
- image.alt = "";
- image.src = src;
+ const image =
+ typeof src == "string"
+ ? Object.assign(document.createElement("img"), { alt: "", src })
+ : src;
Object.assign(image.style, {
display: "block",
height: `${base.h}px`,
@@ -295,13 +309,14 @@ export const playViewerOpen = (
tileImage?.complete && tileImage.naturalWidth > 0
? tileImage
: thumbnail;
- const flightSrc = preview?.currentSrc || src;
+ const flightSrc =
+ viewerVideoFrame(origin.querySelector("video")) ??
+ (preview?.currentSrc || src);
const { flyer, image, draw } = createFlyer(root, flightSrc, to);
let rider: ReturnType<typeof createRider>;
let started = false;
let disposed = false;
let stopSpring: () => void = () => undefined;
- let fade: Animation | undefined;
const drawAt = (value: number) => {
const state = mixPhotoState(from, end, value);
draw(state);
@@ -312,58 +327,38 @@ export const playViewerOpen = (
drawAt(0);
setProgress(0);
- const release = (fadeOut = false) => {
+ const release = () => {
disposed = true;
- fade?.cancel();
+ flyer.remove();
rider?.remove();
- if (fadeOut) {
- fade = flyer.animate([{ opacity: 1 }, { opacity: 0 }], {
- duration: coverFadeMs,
- easing: "ease",
- fill: "forwards",
- });
- fade.addEventListener("finish", () => flyer.remove(), {
- once: true,
- });
- } else flyer.remove();
};
const reveal = () => {
origin.style.opacity = "";
rider?.reveal();
};
- const start = () => {
- if (disposed) return;
- origin.style.opacity = "0";
- delete root.dataset.spaceViewerWaiting;
- stopSpring = runSpring({
- from: 0,
- to: 1,
- velocity: 0,
- duration: openDuration,
- bounce: openBounce,
- restDelta: 1.5 / Math.max(1, photoStateTravel(from, end)),
- onUpdate: (value) => {
- t = value;
- drawAt(t);
- setProgress(t);
- },
- onDone: onLanded,
- });
- };
- void image.decode().then(
+ void ("decode" in image ? image.decode() : Promise.resolve()).then(
() => {
if (disposed) return;
started = true;
flyer.style.visibility = "";
+ origin.style.opacity = "0";
+ delete root.dataset.spaceViewerWaiting;
rider = createRider(flyer, origin);
drawAt(0);
- if (origin.querySelector("video")) {
- fade = image.animate([{ opacity: 0 }, { opacity: 1 }], {
- duration: coverFadeMs,
- easing: "ease",
- });
- fade.addEventListener("finish", start, { once: true });
- } else start();
+ stopSpring = runSpring({
+ from: 0,
+ to: 1,
+ velocity: 0,
+ duration: openDuration,
+ bounce: openBounce,
+ restDelta: 1.5 / Math.max(1, photoStateTravel(from, end)),
+ onUpdate: (value) => {
+ t = value;
+ drawAt(t);
+ setProgress(t);
+ },
+ onDone: onLanded,
+ });
},
() => {
if (!disposed) onLanded();
@@ -389,16 +384,12 @@ export const playViewerOpen = (
// throwVelocity is in px per ms. A photo other than the tile's fades into it.
export const playViewerClose = (
viewerRoot: HTMLElement,
- { origin, src, photo, fadeFromVideo }: ViewerFlightTarget,
+ { origin, src, photo }: ViewerFlightTarget,
from: ViewerPhotoState,
throwVelocity: { x: number; y: number },
startBackground: number,
- onStart: () => void = () => undefined,
) => {
- if (prefersReducedMotion() || !isOnScreen(origin)) {
- onStart();
- return;
- }
+ if (prefersReducedMotion() || !isOnScreen(origin)) return undefined;
const root = document.createElement("div");
root.setAttribute("aria-hidden", "true");
@@ -423,8 +414,8 @@ export const playViewerClose = (
rider?.draw(state, t);
root.style.backgroundColor = `rgb(0 0 0 / ${startBackground * viewerBackgroundOpacity(t)})`;
};
- draw(from);
- rider?.draw(from, 1);
+ origin.style.opacity = "0";
+ drawAt(1);
const dx =
end.visible.x +
@@ -440,9 +431,7 @@ export const playViewerClose = (
const tileImage = origin.querySelector<HTMLImageElement>(
"img:not([aria-hidden])",
);
- const isTilePhoto =
- !origin.querySelector("video") &&
- (tileImage?.currentSrc == src || tileImage?.src == src);
+ const isTilePhoto = tileImage?.currentSrc == src || tileImage?.src == src;
let fade: Animation | undefined;
const cleanUp = () => {
@@ -453,44 +442,29 @@ export const playViewerClose = (
root.remove();
closingFlights.delete(origin);
};
- let stopSpring: () => void = () => undefined;
- const start = () => {
- root.style.zIndex = "1299";
- origin.style.opacity = "0";
- drawAt(1);
- onStart();
- stopSpring = runSpring({
- from: 1,
- to: 0,
- velocity: Math.min(1.2, Math.max(-8, -toward / distance)),
- duration: closeDuration,
- bounce: 0,
- restDelta: 0.5 / Math.max(1, photoStateTravel(from, end)),
- onUpdate: drawAt,
- onDone: () => {
- origin.style.opacity = "";
- rider?.reveal();
- if (isTilePhoto) {
- cleanUp();
- } else {
- fade = root.animate([{ opacity: 1 }, { opacity: 0 }], {
- duration: swapFadeMs,
- easing: "ease",
- fill: "forwards",
- });
- fade.addEventListener("finish", cleanUp, { once: true });
- }
- },
- });
- };
- if (fadeFromVideo) {
- root.style.zIndex = "1301";
- fade = flyer.animate([{ opacity: 0 }, { opacity: 1 }], {
- duration: coverFadeMs,
- easing: "ease",
- });
- fade.addEventListener("finish", start, { once: true });
- } else start();
+ const stopSpring = runSpring({
+ from: 1,
+ to: 0,
+ velocity: Math.min(1.2, Math.max(-8, -toward / distance)),
+ duration: closeDuration,
+ bounce: 0,
+ restDelta: 0.5 / Math.max(1, photoStateTravel(from, end)),
+ onUpdate: drawAt,
+ onDone: () => {
+ origin.style.opacity = "";
+ rider?.reveal();
+ if (isTilePhoto) {
+ cleanUp();
+ } else {
+ fade = root.animate([{ opacity: 1 }, { opacity: 0 }], {
+ duration: swapFadeMs,
+ easing: "ease",
+ fill: "forwards",
+ });
+ fade.addEventListener("finish", cleanUp, { once: true });
+ }
+ },
+ });
closingFlights.set(origin, () => {
stopSpring();
cleanUp();
diff --git a/web/apps/space/tests/post-video-content.test.ts b/web/apps/space/tests/post-video-content.test.ts
index 408afb04b4..c72ba77765 100644
--- a/web/apps/space/tests/post-video-content.test.ts
+++ b/web/apps/space/tests/post-video-content.test.ts
@@ -52,11 +52,6 @@ class Video extends Element {
load = vi.fn();
}
-class Cover extends Element {
- decode = () => Promise.resolve();
- remove = vi.fn();
-}
-
let contents: ReturnType<typeof createSpaceVideoContent>[];
let load: ReturnType<typeof vi.fn<SpacePostAssetURLLoader>>;
let revokeURL: ReturnType<typeof vi.spyOn>;
@@ -73,11 +68,7 @@ beforeEach(() => {
Object.assign(new EventTarget(), {
hidden: false,
createElement: (tag: string) =>
- tag == "video"
- ? new Video()
- : tag == "img"
- ? new Cover()
- : new Element(),
+ tag == "video" ? new Video() : new Element(),
}),
);
});
@@ -108,7 +99,7 @@ const create = (
contents.push(content);
const [video, button, loading, error] = (
content.element as unknown as Element
- ).children.filter((child) => !(child instanceof Cover));
+ ).children;
return {
...content,
video: video as Video,
diff --git a/web/apps/space/tests/viewer-transition.test.ts b/web/apps/space/tests/viewer-transition.test.ts
index 3930e85bd5..61e20df8cf 100644
--- a/web/apps/space/tests/viewer-transition.test.ts
+++ b/web/apps/space/tests/viewer-transition.test.ts
@@ -3,6 +3,7 @@ import {
playViewerClose,
playViewerOpen,
viewerOriginKey,
+ viewerVideoFrame,
} from "../src/components/viewer-transition";
import { fullPhotoState } from "../src/utils/viewer-geometry";
@@ -15,7 +16,7 @@ test("tiles are matched by space and post", () => {
expect(viewerOriginKey("space", undefined)).toBeUndefined();
});
-const pendingFlight = () => {
+const pendingFlight = (video?: HTMLVideoElement) => {
let resolveDecode!: () => void;
let rejectDecode!: (error: Error) => void;
const decoded = new Promise<void>((resolve, reject) => {
@@ -29,6 +30,13 @@ const pendingFlight = () => {
remove: vi.fn(),
};
const image = { style: {}, src: "", decode: () => decoded };
+ const drawFrame = vi.fn();
+ const frame = {
+ style: {},
+ width: 0,
+ height: 0,
+ getContext: () => ({ drawImage: drawFrame }),
+ };
const origin = {
style: { opacity: "" },
closest: () => null,
@@ -39,12 +47,15 @@ const pendingFlight = () => {
height: 100,
}),
querySelector: (selector: string) =>
- selector == "img[aria-hidden]"
- ? { currentSrc: "data:thumbnail" }
- : null,
+ selector == "video"
+ ? (video ?? null)
+ : selector == "img[aria-hidden]"
+ ? { currentSrc: "data:thumbnail" }
+ : null,
};
vi.stubGlobal("document", {
- createElement: (tag: string) => (tag == "img" ? image : flyer),
+ createElement: (tag: string) =>
+ tag == "canvas" ? frame : tag == "img" ? image : flyer,
});
vi.stubGlobal("window", {
innerWidth: 390,
@@ -77,6 +88,8 @@ const pendingFlight = () => {
return {
flight,
flyer,
+ frame,
+ drawFrame,
image,
origin,
requestFrame,
@@ -127,7 +140,43 @@ test("a failed flyer leaves the tile visible and allows viewer handover", async
flight.stop();
});
-const closingFlight = (src = "full-photo") => {
+test("a playing video opens with its current frame without decoding the poster", async () => {
+ const video = {
+ readyState: 2,
+ HAVE_CURRENT_DATA: 2,
+ videoWidth: 1920,
+ videoHeight: 1080,
+ } as HTMLVideoElement;
+ const { flight, flyer, frame, drawFrame, origin, requestFrame } =
+ pendingFlight(video);
+
+ expect(frame.width).toBe(1920);
+ expect(frame.height).toBe(1080);
+ expect(drawFrame).toHaveBeenCalledExactlyOnceWith(video, 0, 0);
+ expect(flight.src).toBe(frame);
+ expect(flyer.append).toHaveBeenCalledWith(frame);
+ await Promise.resolve();
+ expect(origin.style.opacity).toBe("0");
+ expect(requestFrame).toHaveBeenCalledOnce();
+ flight.stop();
+});
+
+test("a video without a decoded frame opens using its poster", async () => {
+ const { flight, drawFrame, image, origin, resolveDecode } = pendingFlight({
+ readyState: 1,
+ HAVE_CURRENT_DATA: 2,
+ } as HTMLVideoElement);
+
+ expect(drawFrame).not.toHaveBeenCalled();
+ expect(image.src).toBe("data:thumbnail");
+ expect(origin.style.opacity).toBe("");
+ resolveDecode();
+ await Promise.resolve();
+ expect(origin.style.opacity).toBe("0");
+ flight.stop();
+});
+
+const closingFlight = (src: string | HTMLCanvasElement = "full-photo") => {
const { flight, origin, requestFrame } = pendingFlight();
flight.stop();
vi.mocked(cancelAnimationFrame).mockClear();
@@ -226,3 +275,26 @@ test("reopening during the thumbnail fade cancels the old overlay", () => {
expect(root.remove).toHaveBeenCalledOnce();
flight.stop();
});
+
+test("closing a video keeps its captured frame until it fades into the tile", () => {
+ const { flight, frame } = pendingFlight();
+ flight.stop();
+ const snapshot = viewerVideoFrame({
+ readyState: 2,
+ HAVE_CURRENT_DATA: 2,
+ videoWidth: 1920,
+ videoHeight: 1080,
+ } as HTMLVideoElement)!;
+ expect(snapshot).toBe(frame);
+
+ const { root, origin, fade, finishSpring } = closingFlight(snapshot);
+ expect(origin.style.opacity).toBe("0");
+ expect(snapshot.style.transform).toBeDefined();
+ finishSpring();
+ expect(origin.style.opacity).toBe("");
+ expect(root.animate).toHaveBeenCalledOnce();
+ expect(root.remove).not.toHaveBeenCalled();
+ const finish = fade.addEventListener.mock.calls[0]![1] as () => void;
+ finish();
+ expect(root.remove).toHaveBeenCalledOnce();
+});