From 3e0ac0a0668b5906d7b81dbc075cfd04ba89911c Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Fri, 17 Jan 2025 20:09:44 -0600 Subject: Bring video cropping in line with images (#7462) * Mimic image cropping for videos on web * Same on native --- src/view/com/util/post-embeds/VideoEmbed.tsx | 67 ++++++++++++------ src/view/com/util/post-embeds/VideoEmbed.web.tsx | 90 +++++++++++++++--------- src/view/com/util/post-embeds/index.tsx | 11 ++- 3 files changed, 113 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/view/com/util/post-embeds/VideoEmbed.tsx b/src/view/com/util/post-embeds/VideoEmbed.tsx index f268bf8db..b45027089 100644 --- a/src/view/com/util/post-embeds/VideoEmbed.tsx +++ b/src/view/com/util/post-embeds/VideoEmbed.tsx @@ -5,9 +5,9 @@ import {AppBskyEmbedVideo} from '@atproto/api' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' -import {clamp} from '#/lib/numbers' +import {ConstrainedImage} from '#/view/com/util/images/AutoSizedImage' import {VideoEmbedInnerNative} from '#/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerNative' -import {atoms as a} from '#/alf' +import {atoms as a, useTheme} from '#/alf' import {Button} from '#/components/Button' import {useThrottledValue} from '#/components/hooks/useThrottledValue' import {PlayButtonIcon} from '#/components/video/PlayButtonIcon' @@ -16,9 +16,11 @@ import * as VideoFallback from './VideoEmbedInner/VideoFallback' interface Props { embed: AppBskyEmbedVideo.View + crop?: 'none' | 'square' | 'constrained' } -export function VideoEmbed({embed}: Props) { +export function VideoEmbed({embed, crop}: Props) { + const t = useTheme() const [key, setKey] = useState(0) const renderError = useCallback( @@ -28,26 +30,51 @@ export function VideoEmbed({embed}: Props) { [key], ) - let aspectRatio = 16 / 9 - if (embed.aspectRatio) { - const {width, height} = embed.aspectRatio - aspectRatio = width / height - aspectRatio = clamp(aspectRatio, 1 / 1, 3 / 1) + let aspectRatio: number | undefined + const dims = embed.aspectRatio + if (dims) { + aspectRatio = dims.width / dims.height + if (Number.isNaN(aspectRatio)) { + aspectRatio = undefined + } + } + + let constrained: number | undefined + let max: number | undefined + if (aspectRatio !== undefined) { + const ratio = 1 / 2 // max of 1:2 ratio in feeds + constrained = Math.max(aspectRatio, ratio) + max = Math.max(aspectRatio, 0.25) // max of 1:4 in thread } + const cropDisabled = crop === 'none' + + const contents = ( + + + + ) return ( - - - - + + {cropDisabled ? ( + + {contents} + + ) : ( + + {contents} + + )} ) } diff --git a/src/view/com/util/post-embeds/VideoEmbed.web.tsx b/src/view/com/util/post-embeds/VideoEmbed.web.tsx index a1f4652ac..b0ded6754 100644 --- a/src/view/com/util/post-embeds/VideoEmbed.web.tsx +++ b/src/view/com/util/post-embeds/VideoEmbed.web.tsx @@ -5,7 +5,7 @@ import {msg} from '@lingui/macro' import {useLingui} from '@lingui/react' import {isFirefox} from '#/lib/browser' -import {clamp} from '#/lib/numbers' +import {ConstrainedImage} from '#/view/com/util/images/AutoSizedImage' import { HLSUnsupportedError, VideoEmbedInnerWeb, @@ -18,7 +18,13 @@ import {ErrorBoundary} from '../ErrorBoundary' import {useActiveVideoWeb} from './ActiveVideoWebContext' import * as VideoFallback from './VideoEmbedInner/VideoFallback' -export function VideoEmbed({embed}: {embed: AppBskyEmbedVideo.View}) { +export function VideoEmbed({ + embed, + crop, +}: { + embed: AppBskyEmbedVideo.View + crop?: 'none' | 'square' | 'constrained' +}) { const ref = useRef(null) const {active, setActive, sendPosition, currentActiveView} = useActiveVideoWeb() @@ -52,42 +58,58 @@ export function VideoEmbed({embed}: {embed: AppBskyEmbedVideo.View}) { [key], ) - let aspectRatio = 16 / 9 + let aspectRatio: number | undefined + const dims = embed.aspectRatio + if (dims) { + aspectRatio = dims.width / dims.height + if (Number.isNaN(aspectRatio)) { + aspectRatio = undefined + } + } - if (embed.aspectRatio) { - const {width, height} = embed.aspectRatio - // min: 3/1, max: square - aspectRatio = clamp(width / height, 1 / 1, 3 / 1) + let constrained: number | undefined + let max: number | undefined + if (aspectRatio !== undefined) { + const ratio = 1 / 2 // max of 1:2 ratio in feeds + constrained = Math.max(aspectRatio, ratio) + max = Math.max(aspectRatio, 0.25) // max of 1:4 in thread } + const cropDisabled = crop === 'none' + + const contents = ( +
evt.stopPropagation()}> + + + + + +
+ ) return ( - -
evt.stopPropagation()}> - - - - - -
+ + {cropDisabled ? ( + + {contents} + + ) : ( + + {contents} + + )} ) } diff --git a/src/view/com/util/post-embeds/index.tsx b/src/view/com/util/post-embeds/index.tsx index 9dc43da8e..6f5f9d3ab 100644 --- a/src/view/com/util/post-embeds/index.tsx +++ b/src/view/com/util/post-embeds/index.tsx @@ -237,7 +237,16 @@ export function PostEmbeds({ if (AppBskyEmbedVideo.isView(embed)) { return ( - + ) } -- cgit 1.4.1