@@ -137,6 +137,11 @@ import { assetImageSource } from "../../lib/assetImageSource"; |
137 | 137 | import { composerImageGallery } from "../../lib/composerImageGallery"; |
138 | 138 | import { isFileBackedComposerAttachment } from "../../lib/composerImages"; |
139 | 139 | import { useLocalAttachmentPreview } from "../../lib/localAttachmentPreview"; |
| 140 | +import { |
| 141 | + rememberSentImage, |
| 142 | + sentImageHandoff, |
| 143 | + type SentImageHandoff, |
| 144 | +} from "../../lib/sentImageHandoff"; |
140 | 145 | import { messageImageGallery } from "./messageImageGallery"; |
141 | 146 | import { MARKDOWN_IMAGE_MAX_WIDTH, resolveMarkdownImageDisplaySize } from "./markdownImageSize"; |
142 | 147 | import { FilePreviewModal, type FilePreviewSource } from "../../components/ui/FilePreviewModal"; |
@@ -483,12 +488,18 @@ function MessageImage(props: { |
483 | 488 | readonly maxWidth?: number; |
484 | 489 | readonly height?: number; |
485 | 490 | readonly accessibilityLabel: string; |
| 491 | + readonly handoff?: SentImageHandoff; |
486 | 492 | readonly onPress: () => void; |
487 | | - readonly onLoad?: () => void; |
| 493 | + readonly onLoad?: (size: { readonly width: number; readonly height: number }) => void; |
488 | 494 | readonly onError?: () => void; |
489 | 495 | }) { |
490 | 496 | const imageStyle = useResolveClassNames(props.className); |
491 | | - const [size, setSize] = useState<{ width: number; height: number } | null>(null); |
| 497 | + const [loadedSize, setSize] = useState<{ width: number; height: number } | null>(null); |
| 498 | + // Until it loads, assume a handed-off image is a photo larger than the bubble's caps. |
| 499 | + const size = |
| 500 | + loadedSize ?? |
| 501 | + (props.handoff ? { width: props.handoff.aspectRatio * 4096, height: 4096 } : null); |
| 502 | + const contentFit = props.maxWidth === undefined ? "cover" : "contain"; |
492 | 503 | const displaySize = |
493 | 504 | props.maxWidth !== undefined && size !== null |
494 | 505 | ? resolveMarkdownImageDisplaySize({ |
@@ -519,10 +530,12 @@ function MessageImage(props: { |
519 | 530 | source={props.source} |
520 | 531 | cachePolicy="memory-disk" |
521 | 532 | style={{ width: "100%", height: "100%" }} |
522 | | - contentFit={props.maxWidth === undefined ? "cover" : "contain"} |
| 533 | + contentFit={contentFit} |
| 534 | + placeholder={props.handoff ? { uri: props.handoff.uri } : undefined} |
| 535 | + placeholderContentFit={contentFit} |
523 | 536 | onLoad={(event) => { |
524 | 537 | if (props.maxWidth !== undefined) setSize(event.source); |
525 | | - props.onLoad?.(); |
| 538 | + props.onLoad?.(event.source); |
526 | 539 | }} |
527 | 540 | onError={props.onError} |
528 | 541 | /> |
@@ -538,6 +551,8 @@ function MessageAttachmentImage(props: { |
538 | 551 | readonly className: string; |
539 | 552 | readonly maxWidth?: number; |
540 | 553 | readonly height?: number; |
| 554 | + readonly handoff?: SentImageHandoff; |
| 555 | + readonly onImageLoad?: (image: SentImageHandoff) => void; |
541 | 556 | readonly onPressPreview: (source: FilePreviewSource) => void; |
542 | 557 | }) { |
543 | 558 | const withGallery = useFilePreviewGallery(); |
@@ -551,8 +566,15 @@ function MessageAttachmentImage(props: { |
551 | 566 | const uri = useAssetUrl(props.environmentId, resource); |
552 | 567 | const refreshAssetUrl = useRefreshAssetUrl(props.environmentId, resource); |
553 | 568 | const retriedImage = useRef(false); |
| 569 | + const { handoff } = props; |
| 570 | + const source = |
| 571 | + uri !== null |
| 572 | + ? assetImageSource(props.environmentId, resource, uri) |
| 573 | + : handoff !== undefined |
| 574 | + ? { uri: handoff.uri } |
| 575 | + : null; |
554 | 576 |
|
555 | | - if (uri === null) { |
| 577 | + if (source === null) { |
556 | 578 | return ( |
557 | 579 | <View |
558 | 580 | className={`${props.className} items-center justify-center`} |
@@ -573,11 +595,12 @@ function MessageAttachmentImage(props: { |
573 | 595 | <PresentationSource identifier={sourceIdentifier}> |
574 | 596 | <MessageImage |
575 | 597 | key={props.attachmentId} |
576 | | - source={assetImageSource(props.environmentId, resource, uri)} |
| 598 | + source={source} |
577 | 599 | className={props.className} |
578 | 600 | maxWidth={props.maxWidth} |
579 | 601 | height={props.height} |
580 | 602 | accessibilityLabel={`Open ${props.name}`} |
| 603 | + {...handoffProps(handoff)} |
581 | 604 | onPress={() => |
582 | 605 | props.onPressPreview( |
583 | 606 | withGallery({ |
@@ -595,8 +618,10 @@ function MessageAttachmentImage(props: { |
595 | 618 | }), |
596 | 619 | ) |
597 | 620 | } |
598 | | - onLoad={() => { |
| 621 | + onLoad={(size) => { |
599 | 622 | retriedImage.current = false; |
| 623 | + if (handoff) |
| 624 | + props.onImageLoad?.({ uri: handoff.uri, aspectRatio: size.width / size.height }); |
600 | 625 | }} |
601 | 626 | onError={() => { |
602 | 627 | if (retriedImage.current) return; |
@@ -835,7 +860,10 @@ function MessageAttachmentFile(props: { |
835 | 860 | ); |
836 | 861 | } |
837 | 862 |
|
| 863 | +const handoffProps = (handoff: SentImageHandoff | undefined) => (handoff ? { handoff } : {}); |
| 864 | + |
838 | 865 | function UserMessageMedia(props: { |
| 866 | + readonly messageId: string; |
839 | 867 | readonly attachments: ReadonlyArray<ChatAttachment>; |
840 | 868 | readonly pendingAttachments: QueuedThreadMessage["attachments"]; |
841 | 869 | readonly environmentId: EnvironmentId; |
@@ -870,6 +898,8 @@ function UserMessageMedia(props: { |
870 | 898 | name={attachment.name} |
871 | 899 | mimeType={attachment.mimeType} |
872 | 900 | {...imageProps(index)} |
| 901 | + {...handoffProps(sentImageHandoff(props.messageId, index, attachment.id))} |
| 902 | + onImageLoad={(loaded) => rememberSentImage(props.messageId, index, loaded)} |
873 | 903 | onPressPreview={props.onPressPreview} |
874 | 904 | /> |
875 | 905 | ) : !image && attachment.uploadedAttachmentId ? ( |
@@ -886,6 +916,8 @@ function UserMessageMedia(props: { |
886 | 916 | <PendingMessageMedia |
887 | 917 | attachment={attachment} |
888 | 918 | {...imageProps(index)} |
| 919 | + {...handoffProps(sentImageHandoff(props.messageId, index, attachment.id))} |
| 920 | + onImageLoad={(image) => rememberSentImage(props.messageId, index, image)} |
889 | 921 | onPressPreview={props.onPressPreview} |
890 | 922 | onPlaybackStart={props.onPlaybackStart} |
891 | 923 | /> |
@@ -902,6 +934,7 @@ function UserMessageMedia(props: { |
902 | 934 | name={attachment.name} |
903 | 935 | mimeType={attachment.mimeType} |
904 | 936 | {...imageProps(props.pendingAttachments.length + index)} |
| 937 | + {...handoffProps(sentImageHandoff(props.messageId, index))} |
905 | 938 | onPressPreview={props.onPressPreview} |
906 | 939 | /> |
907 | 940 | ) : isFileAttachment(attachment) ? ( |
@@ -968,23 +1001,31 @@ function PendingMessageMedia(props: { |
968 | 1001 | readonly className: string; |
969 | 1002 | readonly maxWidth?: number; |
970 | 1003 | readonly height?: number; |
| 1004 | + readonly handoff?: SentImageHandoff; |
| 1005 | + readonly onImageLoad?: (image: SentImageHandoff) => void; |
971 | 1006 | readonly onPressPreview: (source: FilePreviewSource) => void; |
972 | 1007 | readonly onPlaybackStart: () => void; |
973 | 1008 | }) { |
974 | 1009 | const withGallery = useFilePreviewGallery(); |
975 | | - const { attachment } = props; |
| 1010 | + const { attachment, handoff } = props; |
976 | 1011 | const fileBacked = isFileBackedComposerAttachment(attachment); |
977 | 1012 | const preview = useLocalAttachmentPreview(fileBacked ? attachment : null); |
978 | 1013 | if (isImageAttachment(attachment)) { |
979 | | - const uri = fileBacked ? preview.uri : (attachment.dataUrl ?? attachment.previewUri); |
| 1014 | + // The composer already showed this image; reuse its pixels and shape instead of a blank box. |
| 1015 | + const uri = |
| 1016 | + handoff?.uri ?? (fileBacked ? preview.uri : (attachment.dataUrl ?? attachment.previewUri)); |
980 | 1017 | return ( |
981 | 1018 | <MessageImage |
982 | 1019 | key={attachment.id} |
983 | | - source={uri === null ? undefined : { uri }} |
| 1020 | + source={uri == null ? undefined : { uri }} |
984 | 1021 | className={props.className} |
985 | 1022 | maxWidth={props.maxWidth} |
986 | 1023 | height={props.height} |
987 | 1024 | accessibilityLabel={`Open ${attachment.name}`} |
| 1025 | + {...handoffProps(handoff)} |
| 1026 | + onLoad={(size) => { |
| 1027 | + if (uri) props.onImageLoad?.({ uri, aspectRatio: size.width / size.height }); |
| 1028 | + }} |
988 | 1029 | onPress={() => { |
989 | 1030 | const source = composerImageGallery([attachment])[0]; |
990 | 1031 | if (source) props.onPressPreview(withGallery(source)); |
@@ -2061,6 +2102,7 @@ function renderFeedEntry( |
2061 | 2102 | style={{ width: props.duoColumns ? "85%" : props.userBubbleMaxWidth }} |
2062 | 2103 | > |
2063 | 2104 | <UserMessageMedia |
| 2105 | + messageId={message.id} |
2064 | 2106 | attachments={mediaAttachments} |
2065 | 2107 | pendingAttachments={pendingMedia} |
2066 | 2108 | environmentId={props.environmentId} |
|