Skip to content

armgit / ente

Public Imported from GitHub
0

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

Smooth Space video viewer transitions

Anand Baburajanb196f93232b7
diff --git a/web/apps/space/src/components/FileViewer.tsx b/web/apps/space/src/components/FileViewer.tsx
index d2681e82ef..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";
@@ -286,6 +287,7 @@ const handOverOpenFlight = (
const image = content?.element;
const isShown =
content?.isError() ||
+ (content?.data.type == "video" && image?.isConnected) ||
(image instanceof HTMLImageElement &&
image.complete &&
image.naturalWidth > 0 &&
@@ -802,30 +804,36 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
const item =
viewerPhotosRef.current[pswp?.currIndex ?? activePhotoIndex];
const element = pswp?.currSlide?.content.element;
+ const video = element?.querySelector("video");
let from: ViewerPhotoState | undefined = motion.flight?.current();
let src = from ? motion.flight?.src : undefined;
const rect = element?.getBoundingClientRect();
if (
!from &&
!motion.flight &&
+ element &&
rect &&
rect.width > 0 &&
rect.height > 0 &&
- element instanceof HTMLImageElement &&
- element.complete &&
- element.naturalWidth > 0
+ ((element instanceof HTMLImageElement &&
+ element.complete &&
+ element.naturalWidth > 0) ||
+ video)
) {
- const scale = Number(pswp?.currSlide?.container.style.scale) || 1;
+ const scale = Number(pswp.currSlide?.container.style.scale) || 1;
from = shownPhotoState(
{ x: rect.left, y: rect.top, w: rect.width, h: rect.height },
{ x: 0, y: 0, w: window.innerWidth, h: window.innerHeight },
viewerCornerRadii(element, scale),
);
- src = element.currentSrc || element.src;
+ src =
+ element instanceof HTMLImageElement
+ ? element.currentSrc || element.src
+ : (viewerVideoFrame(video) ?? video?.poster);
}
window.scrollTo({ ...pageScrollRef.current, behavior: "instant" });
const origin =
- item && !isDraftPost && !item.video
+ item && !isDraftPost
? findViewerOrigin(viewerOriginKey(item.spaceId, item.postId))
: null;
const background = root?.style.getPropertyValue(
@@ -1442,8 +1450,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
}
};
const item = fallbackPhotoRef.current;
- if (!root || isDraftPost || item.video || !item.imageUrl)
- return stopMotion;
+ if (!root || isDraftPost || !item.imageUrl) return stopMotion;
const origin = findViewerOrigin(
viewerOriginKey(item.spaceId, item.postId),
);
diff --git a/web/apps/space/src/components/viewer-transition.ts b/web/apps/space/src/components/viewer-transition.ts
index 3ba120b452..a04fa44a80 100644
--- a/web/apps/space/src/components/viewer-transition.ts
+++ b/web/apps/space/src/components/viewer-transition.ts
@@ -28,10 +28,21 @@ export const findViewerOrigin = (key: string | undefined) =>
interface ViewerFlightTarget {
origin: HTMLElement;
- src: string;
+ 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 };
@@ -113,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, {
@@ -128,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`,
@@ -293,7 +309,9 @@ 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;
@@ -318,7 +336,7 @@ export const playViewerOpen = (
origin.style.opacity = "";
rider?.reveal();
};
- void image.decode().then(
+ void ("decode" in image ? image.decode() : Promise.resolve()).then(
() => {
if (disposed) return;
started = true;
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();
+});