@devries-cameron20 / t3code Mirror · read-only / commits / bf4659a2d6
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 12:00 UTC

fix(mobile): file preview videos keep their own aspect ratio (#21894)

Co-authored-by: maria-rcks <maria@kuuro.net>

Nikita Maximilian Grimm committed Oct 30, 2026 at 21:29 UTC bf4659a2d65b862bc3c94a055d850c23426d3fbd
5 files changed +124 -15
apps/mobile/src/components/MediaVideoPlayer.tsx
+45 -3
@@ -1,6 +1,7 @@
1 import { useEvent } from "expo";
2 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";
5 import { Pressable, View } from "react-native";
6
7 import { AppActivityIndicator } from "./ui/AppActivityIndicator";
@@ -8,6 +9,8 @@ import { AppText } from "./ui/AppText";
9 import { SymbolView } from "./ui/AppSymbol";
10 import { VideoThumbnailImage } from "./VideoThumbnailImage";
11 import { useMediaActions } from "../state/mediaActions";
12 +import { fitVideoFrame } from "../lib/videoFrameFit";
13 +import { loadVideoThumbnail } from "../lib/videoThumbnails";
14 import { type MediaActionsSource } from "../lib/mediaActionsSource";
15 import { useMediaVideoPlayback } from "../lib/useMediaVideoPlayback";
16 import { MediaActionsMenu } from "./MediaActionsMenu";
@@ -136,6 +139,8 @@ interface MediaVideoPlayerProps {
139 readonly visible?: boolean;
140 readonly unavailable?: boolean;
141 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;
144 readonly autoPlay?: boolean;
145 readonly paused?: boolean;
146 readonly onPlaybackStart?: () => void;
@@ -145,16 +150,41 @@ interface MediaVideoPlayerProps {
150
151 function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
152 const mediaActions = useMediaActions(props.actionsSource);
153 + const isFocused = useIsFocused();
154 const [playbackUri, setPlaybackUri] = useState<string | null>(props.expanded ? props.uri : null);
155 // Keep an opened player mounted while signing or reconnecting temporarily has no usable URL.
156 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;
179
152 - return (
180 + const frame = (
181 <View
182 collapsable={false}
183 className="overflow-hidden rounded-[10px] bg-black"
184 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 }),
188 props.borderRadius === undefined ? null : { borderRadius: props.borderRadius },
189 ]}
190 >
@@ -214,6 +244,18 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
244 )}
245 </View>
246 );
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 + );
259 }
260
261 export function MediaVideoPlayer(props: MediaVideoPlayerProps) {
apps/mobile/src/features/files/WorkspaceFileVideoPreview.tsx
+1
@@ -19,6 +19,7 @@ export function WorkspaceFileVideoPreview(props: {
19 resolvePlaybackUri={props.resolvePlaybackUri}
20 name={props.name}
21 thumbnailKey={props.thumbnailKey}
22 + fitToVideo
23 actionsSource={props.source?.actionsSource}
24 />
25 </View>
apps/mobile/src/lib/useMediaVideoPlayback.test.tsx
+31 -12
@@ -119,6 +119,7 @@ import {
119 PullRequestMediaContext,
120 PullRequestMediaVisibleContext,
121 } from "../features/pull-requests/PullRequestMedia";
122 +import { MediaVideoPlayer } from "../components/MediaVideoPlayer";
123
124 function createPlayer() {
125 let playing = false;
@@ -149,7 +150,7 @@ function createPlayer() {
150 };
151 }
152
152 -function setup(kind: "thread" | "PR", compact = false) {
153 +function setup(kind: "thread" | "PR" | "files", compact = false) {
154 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
155 mocks.focused = true;
156 mocks.appState = "active";
@@ -166,10 +167,20 @@ function setup(kind: "thread" | "PR", compact = false) {
167 const environmentId = EnvironmentId.make("test");
168 return {
169 render(visible = true) {
170 + if (kind === "files") mocks.focused = visible;
171 act(() =>
172 root.render(
173 <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" ? (
184 <ThreadMediaVisibleContext value={visible}>
185 <ThreadMarkdownVideo
186 source={{
@@ -267,23 +278,31 @@ it.each(["thread", "PR"] as const)(
278 },
279 );
280
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);
289 mocks.thumbnail.mockReturnValue(new Promise(() => {}));
290 screen.render(false);
291 expect(mocks.thumbnail).not.toHaveBeenCalled();
292 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 + );
301 screen.render(false);
302 await act(() => aborted);
284 - expect(mocks.thumbnail).toHaveBeenCalledTimes(1);
303 + expect(mocks.thumbnail).toHaveBeenCalledTimes(requests);
304 await act(async () => screen.render(true));
286 - expect(mocks.thumbnail).toHaveBeenCalledTimes(2);
305 + expect(mocks.thumbnail).toHaveBeenCalledTimes(requests * 2);
306 },
307 );
308
apps/mobile/src/lib/videoFrameFit.test.ts new
+33
@@ -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 +});
apps/mobile/src/lib/videoFrameFit.ts new
+14
@@ -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 +}