Skip to content

Commit bf4659a

Browse files
fix(mobile): file preview videos keep their own aspect ratio (#21894)
Co-authored-by: maria-rcks <[email protected]>
1 parent 50cdc24 commit bf4659a

5 files changed

Lines changed: 124 additions & 15 deletions

File tree

‎apps/mobile/src/components/MediaVideoPlayer.tsx‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { useEvent } from "expo";
22
import { useVideoPlayer, VideoView } from "expo-video";
3-
import { useEffect, useEffectEvent, useRef, useState } from "react";
3+
import { useIsFocused } from "@react-navigation/native";
4+
import { useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from "react";
45
import { Pressable, View } from "react-native";
56

67
import { AppActivityIndicator } from "./ui/AppActivityIndicator";
78
import { AppText } from "./ui/AppText";
89
import { SymbolView } from "./ui/AppSymbol";
910
import { VideoThumbnailImage } from "./VideoThumbnailImage";
1011
import { useMediaActions } from "../state/mediaActions";
12+
import { fitVideoFrame } from "../lib/videoFrameFit";
13+
import { loadVideoThumbnail } from "../lib/videoThumbnails";
1114
import { type MediaActionsSource } from "../lib/mediaActionsSource";
1215
import { useMediaVideoPlayback } from "../lib/useMediaVideoPlayback";
1316
import { MediaActionsMenu } from "./MediaActionsMenu";
@@ -136,6 +139,8 @@ interface MediaVideoPlayerProps {
136139
readonly visible?: boolean;
137140
readonly unavailable?: boolean;
138141
readonly expanded?: boolean;
142+
/** Fill the parent and size the frame to the video's shape instead of a fixed 16:9 slot. */
143+
readonly fitToVideo?: boolean;
139144
readonly autoPlay?: boolean;
140145
readonly paused?: boolean;
141146
readonly onPlaybackStart?: () => void;
@@ -145,16 +150,41 @@ interface MediaVideoPlayerProps {
145150

146151
function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
147152
const mediaActions = useMediaActions(props.actionsSource);
153+
const isFocused = useIsFocused();
148154
const [playbackUri, setPlaybackUri] = useState<string | null>(props.expanded ? props.uri : null);
149155
// Keep an opened player mounted while signing or reconnecting temporarily has no usable URL.
150156
if (playbackUri === null && props.expanded && props.uri !== null) setPlaybackUri(props.uri);
157+
const [videoSize, setVideoSize] = useState<{ width: number; height: number } | null>(null);
158+
const [bounds, setBounds] = useState<{ width: number; height: number } | null>(null);
159+
const sizeSource = props.fitToVideo && isFocused && videoSize === null ? props.uri : null;
160+
// Queue before the poster so early Play cannot cancel the extraction that supplies frame dimensions.
161+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- loads native thumbnail dimensions independently of the poster's lifetime
162+
useLayoutEffect(() => {
163+
if (sizeSource === null) return;
164+
// Thumbnails are oriented like the displayed frame, unlike VideoTrack.size.
165+
const controller = new AbortController();
166+
void loadVideoThumbnail(
167+
props.thumbnailKey,
168+
async () => ({ uri: sizeSource, dispose: () => undefined }),
169+
controller.signal,
170+
).then((thumbnail) => {
171+
if (thumbnail && !controller.signal.aborted) {
172+
setVideoSize({ width: thumbnail.width, height: thumbnail.height });
173+
}
174+
});
175+
return () => controller.abort();
176+
}, [props.thumbnailKey, sizeSource]);
177+
const fittedFrame =
178+
props.fitToVideo && videoSize && bounds ? fitVideoFrame(videoSize, bounds, 480) : null;
151179

152-
return (
180+
const frame = (
153181
<View
154182
collapsable={false}
155183
className="overflow-hidden rounded-[10px] bg-black"
156184
style={[
157-
props.expanded ? { flex: 1 } : { width: "100%", maxWidth: 480, aspectRatio: 16 / 9 },
185+
props.expanded
186+
? { flex: 1 }
187+
: (fittedFrame ?? { width: "100%", maxWidth: 480, aspectRatio: 16 / 9 }),
158188
props.borderRadius === undefined ? null : { borderRadius: props.borderRadius },
159189
]}
160190
>
@@ -214,6 +244,18 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
214244
)}
215245
</View>
216246
);
247+
if (!props.fitToVideo) return frame;
248+
return (
249+
<View
250+
className="flex-1 items-center justify-center self-stretch"
251+
onLayout={(event) => {
252+
const { width, height } = event.nativeEvent.layout;
253+
setBounds({ width, height });
254+
}}
255+
>
256+
{frame}
257+
</View>
258+
);
217259
}
218260

219261
export function MediaVideoPlayer(props: MediaVideoPlayerProps) {

‎apps/mobile/src/features/files/WorkspaceFileVideoPreview.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function WorkspaceFileVideoPreview(props: {
1919
resolvePlaybackUri={props.resolvePlaybackUri}
2020
name={props.name}
2121
thumbnailKey={props.thumbnailKey}
22+
fitToVideo
2223
actionsSource={props.source?.actionsSource}
2324
/>
2425
</View>

‎apps/mobile/src/lib/useMediaVideoPlayback.test.tsx‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ import {
119119
PullRequestMediaContext,
120120
PullRequestMediaVisibleContext,
121121
} from "../features/pull-requests/PullRequestMedia";
122+
import { MediaVideoPlayer } from "../components/MediaVideoPlayer";
122123

123124
function createPlayer() {
124125
let playing = false;
@@ -149,7 +150,7 @@ function createPlayer() {
149150
};
150151
}
151152

152-
function setup(kind: "thread" | "PR", compact = false) {
153+
function setup(kind: "thread" | "PR" | "files", compact = false) {
153154
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
154155
mocks.focused = true;
155156
mocks.appState = "active";
@@ -166,10 +167,20 @@ function setup(kind: "thread" | "PR", compact = false) {
166167
const environmentId = EnvironmentId.make("test");
167168
return {
168169
render(visible = true) {
170+
if (kind === "files") mocks.focused = visible;
169171
act(() =>
170172
root.render(
171173
<RegistryContext.Provider value={registry}>
172-
{kind === "thread" ? (
174+
{kind === "files" ? (
175+
<MediaVideoPlayer
176+
name="clip.mp4"
177+
thumbnailKey="workspace-video:test"
178+
uri="https://media.example/clip.mp4"
179+
fitToVideo
180+
visible={visible}
181+
resolvePlaybackUri={mocks.refresh}
182+
/>
183+
) : kind === "thread" ? (
173184
<ThreadMediaVisibleContext value={visible}>
174185
<ThreadMarkdownVideo
175186
source={{
@@ -267,23 +278,31 @@ it.each(["thread", "PR"] as const)(
267278
},
268279
);
269280

270-
it.each([false, true])(
271-
"stops inactive PR thumbnail requests (compact: %s) and restarts on return",
272-
async (compact) => {
273-
using screen = setup("PR", compact);
281+
it.each([
282+
["PR", false],
283+
["PR", true],
284+
["files", false],
285+
] as const)(
286+
"stops inactive %s thumbnail requests (compact: %s) and restarts on return",
287+
async (kind, compact) => {
288+
using screen = setup(kind, compact);
274289
mocks.thumbnail.mockReturnValue(new Promise(() => {}));
275290
screen.render(false);
276291
expect(mocks.thumbnail).not.toHaveBeenCalled();
277292
await act(async () => screen.render(true));
278-
expect(mocks.thumbnail).toHaveBeenCalledTimes(1);
279-
const signal = mocks.thumbnail.mock.calls[0]![2];
280-
expect(signal.aborted).toBe(false);
281-
const aborted = new Promise((resolve) => signal.addEventListener("abort", resolve));
293+
const requests = kind === "files" ? 2 : 1;
294+
expect(mocks.thumbnail).toHaveBeenCalledTimes(requests);
295+
const aborted = Promise.all(
296+
mocks.thumbnail.mock.calls.map(([, , signal]) => {
297+
expect(signal.aborted).toBe(false);
298+
return new Promise((resolve) => signal.addEventListener("abort", resolve));
299+
}),
300+
);
282301
screen.render(false);
283302
await act(() => aborted);
284-
expect(mocks.thumbnail).toHaveBeenCalledTimes(1);
303+
expect(mocks.thumbnail).toHaveBeenCalledTimes(requests);
285304
await act(async () => screen.render(true));
286-
expect(mocks.thumbnail).toHaveBeenCalledTimes(2);
305+
expect(mocks.thumbnail).toHaveBeenCalledTimes(requests * 2);
287306
},
288307
);
289308

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { fitVideoFrame } from "./videoFrameFit";
4+
5+
describe("fitVideoFrame", () => {
6+
const bounds = { width: 360, height: 600 };
7+
8+
it("keeps landscape videos at the full capped width", () => {
9+
expect(fitVideoFrame({ width: 1920, height: 1080 }, bounds, 480)).toEqual({
10+
width: 360,
11+
height: 202.5,
12+
});
13+
});
14+
15+
it("limits portrait videos by the available height", () => {
16+
expect(fitVideoFrame({ width: 1080, height: 1920 }, bounds, 480)).toEqual({
17+
width: 337.5,
18+
height: 600,
19+
});
20+
});
21+
22+
it("caps wide bounds at the inline width", () => {
23+
expect(fitVideoFrame({ width: 1080, height: 1080 }, { width: 800, height: 900 }, 480)).toEqual({
24+
width: 480,
25+
height: 480,
26+
});
27+
});
28+
29+
it("returns null before the video or bounds have a size", () => {
30+
expect(fitVideoFrame({ width: 0, height: 0 }, bounds, 480)).toBeNull();
31+
expect(fitVideoFrame({ width: 1080, height: 1920 }, { width: 0, height: 0 }, 480)).toBeNull();
32+
});
33+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
interface Size {
2+
readonly width: number;
3+
readonly height: number;
4+
}
5+
6+
/** Largest frame with the video's aspect ratio that fits the bounds and the inline width cap. */
7+
export function fitVideoFrame(video: Size, bounds: Size, maxWidth: number): Size | null {
8+
if (video.width <= 0 || video.height <= 0 || bounds.width <= 0 || bounds.height <= 0) {
9+
return null;
10+
}
11+
const ratio = video.width / video.height;
12+
const width = Math.min(bounds.width, maxWidth, bounds.height * ratio);
13+
return { width, height: width / ratio };
14+
}

0 commit comments

Comments
 (0)