11import { useEvent } from "expo" ;
22import { 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" ;
45import { Pressable , View } from "react-native" ;
56
67import { AppActivityIndicator } from "./ui/AppActivityIndicator" ;
78import { AppText } from "./ui/AppText" ;
89import { SymbolView } from "./ui/AppSymbol" ;
910import { VideoThumbnailImage } from "./VideoThumbnailImage" ;
1011import { useMediaActions } from "../state/mediaActions" ;
12+ import { fitVideoFrame } from "../lib/videoFrameFit" ;
13+ import { loadVideoThumbnail } from "../lib/videoThumbnails" ;
1114import { type MediaActionsSource } from "../lib/mediaActionsSource" ;
1215import { useMediaVideoPlayback } from "../lib/useMediaVideoPlayback" ;
1316import { 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
146151function 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
219261export function MediaVideoPlayer ( props : MediaVideoPlayerProps ) {
0 commit comments