@@ -3,7 +3,7 @@ import { useVideoPlayer, VideoView } from "expo-video"; |
3 | 3 | import { useIsFocused } from "@react-navigation/native"; |
4 | 4 | import { T, useGT } from "gt-react-native"; |
5 | 5 | import { useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from "react"; |
6 | | -import { Pressable, View } from "react-native"; |
| 6 | +import { Pressable, View, type LayoutChangeEvent } from "react-native"; |
7 | 7 |
|
8 | 8 | import { AppActivityIndicator } from "./ui/AppActivityIndicator"; |
9 | 9 | import { AppText } from "./ui/AppText"; |
@@ -155,6 +155,8 @@ interface MediaVideoPlayerProps { |
155 | 155 | readonly borderRadius?: number; |
156 | 156 | } |
157 | 157 |
|
| 158 | +const INLINE_MAX_SIZE = 480; |
| 159 | + |
158 | 160 | function MediaVideoPlayerContent(props: MediaVideoPlayerProps) { |
159 | 161 | const gt = useGT(); |
160 | 162 | const mediaActions = useMediaActions(props.actionsSource); |
@@ -167,7 +169,9 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) { |
167 | 169 | : gt("Video unavailable"); |
168 | 170 | const [videoSize, setVideoSize] = useState<{ width: number; height: number } | null>(null); |
169 | 171 | const [bounds, setBounds] = useState<{ width: number; height: number } | null>(null); |
170 | | - const sizeSource = props.fitToVideo && isFocused && videoSize === null ? props.uri : null; |
| 172 | + const inline = !props.expanded && !props.fitToVideo; |
| 173 | + const measuresVideo = props.fitToVideo || (inline && props.thumbnailVisible !== false); |
| 174 | + const sizeSource = measuresVideo && isFocused && videoSize === null ? props.uri : null; |
171 | 175 | // Queue before the poster so early Play cannot cancel the extraction that supplies frame dimensions. |
172 | 176 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- loads native thumbnail dimensions independently of the poster's lifetime |
173 | 177 | useLayoutEffect(() => { |
@@ -186,7 +190,17 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) { |
186 | 190 | return () => controller.abort(); |
187 | 191 | }, [props.thumbnailKey, sizeSource]); |
188 | 192 | const fittedFrame = |
189 | | - props.fitToVideo && videoSize && bounds ? fitVideoFrame(videoSize, bounds, 480) : null; |
| 193 | + videoSize && bounds |
| 194 | + ? fitVideoFrame( |
| 195 | + videoSize, |
| 196 | + inline ? { width: bounds.width, height: INLINE_MAX_SIZE } : bounds, |
| 197 | + INLINE_MAX_SIZE, |
| 198 | + ) |
| 199 | + : null; |
| 200 | + const measureBounds = (event: LayoutChangeEvent) => { |
| 201 | + const { width, height } = event.nativeEvent.layout; |
| 202 | + setBounds({ width, height }); |
| 203 | + }; |
190 | 204 |
|
191 | 205 | const frame = ( |
192 | 206 | <View |
@@ -195,7 +209,7 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) { |
195 | 209 | style={[ |
196 | 210 | props.expanded |
197 | 211 | ? { flex: 1 } |
198 | | - : (fittedFrame ?? { width: "100%", maxWidth: 480, aspectRatio: 16 / 9 }), |
| 212 | + : (fittedFrame ?? { width: "100%", maxWidth: INLINE_MAX_SIZE, aspectRatio: 16 / 9 }), |
199 | 213 | props.borderRadius === undefined ? null : { borderRadius: props.borderRadius }, |
200 | 214 | ]} |
201 | 215 | > |
@@ -259,15 +273,16 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) { |
259 | 273 | )} |
260 | 274 | </View> |
261 | 275 | ); |
262 | | - if (!props.fitToVideo) return frame; |
| 276 | + if (props.fitToVideo) { |
| 277 | + return ( |
| 278 | + <View className="flex-1 items-center justify-center self-stretch" onLayout={measureBounds}> |
| 279 | + {frame} |
| 280 | + </View> |
| 281 | + ); |
| 282 | + } |
| 283 | + if (props.expanded) return frame; |
263 | 284 | return ( |
264 | | - <View |
265 | | - className="flex-1 items-center justify-center self-stretch" |
266 | | - onLayout={(event) => { |
267 | | - const { width, height } = event.nativeEvent.layout; |
268 | | - setBounds({ width, height }); |
269 | | - }} |
270 | | - > |
| 285 | + <View style={{ width: "100%", maxWidth: INLINE_MAX_SIZE }} onLayout={measureBounds}> |
271 | 286 | {frame} |
272 | 287 | </View> |
273 | 288 | ); |
|