Skip to content

Commit dbe5d95

Browse files
fix(mobile): keep inline chat images zoomed around the pinch point
1 parent 4c924ff commit dbe5d95

4 files changed

Lines changed: 446 additions & 72 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ import {
273273
ThreadMarkdownImageUnavailable,
274274
ThreadMarkdownImageView,
275275
} from "./ThreadMarkdownImage";
276+
import { ZoomableImagePressable } from "./ZoomableImagePressable";
276277
import { openSettings } from "../settings/settings-navigation";
277278
import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspaceDetail";
278279
import { formatMessageTimestamp } from "./messageTimestamp";
@@ -467,6 +468,8 @@ function MessageImage(props: {
467468
readonly className: string;
468469
readonly maxWidth?: number;
469470
readonly height?: number;
471+
readonly accessibilityLabel: string;
472+
readonly onPress: () => void;
470473
readonly onLoad?: () => void;
471474
readonly onError?: () => void;
472475
}) {
@@ -481,11 +484,13 @@ function MessageImage(props: {
481484
})
482485
: null;
483486
return (
484-
<ExpoImage
485-
source={props.source}
486-
cachePolicy="memory-disk"
487+
<ZoomableImagePressable
488+
accessibilityRole="imagebutton"
489+
accessibilityLabel={props.accessibilityLabel}
490+
onPress={props.onPress}
487491
style={[
488492
imageStyle,
493+
{ overflow: "hidden" },
489494
props.maxWidth !== undefined
490495
? (displaySize ?? {
491496
width: Math.min(props.maxWidth, MARKDOWN_IMAGE_MAX_WIDTH),
@@ -495,13 +500,19 @@ function MessageImage(props: {
495500
? { height: props.height }
496501
: undefined,
497502
]}
498-
contentFit={props.maxWidth === undefined ? "cover" : "contain"}
499-
onLoad={(event) => {
500-
if (props.maxWidth !== undefined) setSize(event.source);
501-
props.onLoad?.();
502-
}}
503-
onError={props.onError}
504-
/>
503+
>
504+
<ExpoImage
505+
source={props.source}
506+
cachePolicy="memory-disk"
507+
style={{ width: "100%", height: "100%" }}
508+
contentFit={props.maxWidth === undefined ? "cover" : "contain"}
509+
onLoad={(event) => {
510+
if (props.maxWidth !== undefined) setSize(event.source);
511+
props.onLoad?.();
512+
}}
513+
onError={props.onError}
514+
/>
515+
</ZoomableImagePressable>
505516
);
506517
}
507518

@@ -546,8 +557,12 @@ function MessageAttachmentImage(props: {
546557

547558
return (
548559
<PresentationSource identifier={sourceIdentifier}>
549-
<Pressable
550-
accessibilityRole="imagebutton"
560+
<MessageImage
561+
key={props.attachmentId}
562+
source={assetImageSource(props.environmentId, resource, uri)}
563+
className={props.className}
564+
maxWidth={props.maxWidth}
565+
height={props.height}
551566
accessibilityLabel={`Open ${props.name}`}
552567
onPress={() =>
553568
props.onPressPreview(
@@ -566,22 +581,15 @@ function MessageAttachmentImage(props: {
566581
}),
567582
)
568583
}
569-
>
570-
<MessageImage
571-
source={assetImageSource(props.environmentId, resource, uri)}
572-
className={props.className}
573-
maxWidth={props.maxWidth}
574-
height={props.height}
575-
onLoad={() => {
576-
retriedImage.current = false;
577-
}}
578-
onError={() => {
579-
if (retriedImage.current) return;
580-
retriedImage.current = true;
581-
void refreshAssetUrl();
582-
}}
583-
/>
584-
</Pressable>
584+
onLoad={() => {
585+
retriedImage.current = false;
586+
}}
587+
onError={() => {
588+
if (retriedImage.current) return;
589+
retriedImage.current = true;
590+
void refreshAssetUrl();
591+
}}
592+
/>
585593
</PresentationSource>
586594
);
587595
}
@@ -956,21 +964,18 @@ function PendingMessageMedia(props: {
956964
if (isImageAttachment(attachment)) {
957965
const uri = fileBacked ? preview.uri : (attachment.dataUrl ?? attachment.previewUri);
958966
return (
959-
<Pressable
960-
accessibilityRole="imagebutton"
967+
<MessageImage
968+
key={attachment.id}
969+
source={uri === null ? undefined : { uri }}
970+
className={props.className}
971+
maxWidth={props.maxWidth}
972+
height={props.height}
961973
accessibilityLabel={`Open ${attachment.name}`}
962974
onPress={() => {
963975
const source = composerImageGallery([attachment])[0];
964976
if (source) props.onPressPreview(withGallery(source));
965977
}}
966-
>
967-
<MessageImage
968-
source={uri === null ? undefined : { uri }}
969-
className={props.className}
970-
maxWidth={props.maxWidth}
971-
height={props.height}
972-
/>
973-
</Pressable>
978+
/>
974979
);
975980
}
976981
if (preview.uri === null) {

‎apps/mobile/src/features/threads/ThreadMarkdownImage.test.tsx‎

Lines changed: 201 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,71 @@ import { act } from "react";
33
import { createRoot } from "react-dom/client";
44
import { expect, it, vi } from "vite-plus/test";
55

6+
type HandlerProps = Record<string, ((event: unknown) => void) | undefined>;
7+
type GestureHandlers = Record<string, (event: unknown, manager?: unknown) => void>;
8+
type Transform = [{ translateX: number }, { translateY: number }, { scale: number }];
9+
610
const harness = vi.hoisted(() => ({
711
failImage: () => {},
812
loadImage: (_event: { source: { width: number; height: number } }) => {},
913
Passthrough: ({ children }: { children?: unknown }) => children,
14+
pressable: {} as HandlerProps,
15+
gestures: {} as Record<string, GestureHandlers>,
16+
contentStyle: () => ({}) as { transform?: Transform },
1017
}));
1118

12-
vi.mock("react-native", () => ({
13-
Pressable: harness.Passthrough,
14-
StyleSheet: { absoluteFill: {} },
15-
View: ({ children, style }: { children?: ReactNode; style?: unknown }) => (
19+
vi.mock("react-native", () => {
20+
const View = ({ children, style }: { children?: ReactNode; style?: unknown }) => (
1621
<div data-style={JSON.stringify(style)}>{children}</div>
17-
),
18-
}));
22+
);
23+
return {
24+
Platform: { OS: "ios" },
25+
Pressable: (props: HandlerProps & { children?: ReactNode; style?: unknown }) => {
26+
harness.pressable = props;
27+
return <View style={props.style}>{props.children}</View>;
28+
},
29+
StyleSheet: { absoluteFill: {} },
30+
View,
31+
};
32+
});
33+
vi.mock("react-native-gesture-handler", () => {
34+
// Records each gesture's callbacks so a test can drive the worklets with native-shaped events.
35+
const builder = (kind: string) => () => {
36+
const handlers: GestureHandlers = {};
37+
harness.gestures[kind] = handlers;
38+
const gesture: object = new Proxy(
39+
{},
40+
{
41+
get: (_target, name) => (callback: GestureHandlers[string]) => {
42+
if (typeof name === "string" && name.startsWith("on")) handlers[name] = callback;
43+
return gesture;
44+
},
45+
},
46+
);
47+
return gesture;
48+
};
49+
return {
50+
Gesture: { Pinch: builder("pinch"), Pan: builder("pan"), Simultaneous: () => ({}) },
51+
GestureDetector: ({ children }: { children?: ReactNode }) => children,
52+
};
53+
});
54+
vi.mock("react-native-reanimated", async () => {
55+
const { useState } = await import("react");
56+
return {
57+
default: { View: ({ children }: { children?: ReactNode }) => <div>{children}</div> },
58+
runOnJS: (callback: () => void) => callback,
59+
// Reads the UI-thread style on demand, as Reanimated would after a gesture frame.
60+
useAnimatedStyle: (updater: () => { transform?: Transform }) => {
61+
harness.contentStyle = updater;
62+
return {};
63+
},
64+
useSharedValue: <T,>(initial: T) =>
65+
useState(() => {
66+
let value = initial;
67+
return { get: () => value, set: (next: T) => void (value = next) };
68+
})[0],
69+
};
70+
});
1971
vi.mock("expo-image", () => ({
2072
Image: (props: {
2173
onError: () => void;
@@ -203,3 +255,146 @@ it("does not parse an obsolete SVG response after its last reader unmounts", asy
203255
registry.dispose();
204256
}
205257
});
258+
259+
const zoom = () => {
260+
const [{ translateX }, { translateY }, { scale }] = harness.contentStyle().transform!;
261+
// Clamping at fit can yield -0, which is the same position.
262+
return { scale, x: translateX + 0, y: translateY + 0 };
263+
};
264+
const pinch = (phase: "onStart" | "onUpdate", focalX: number, focalY: number, scale: number) =>
265+
act(async () => harness.gestures.pinch![phase]!({ focalX, focalY, scale, numberOfPointers: 2 }));
266+
const endPinch = () => act(async () => harness.gestures.pinch!.onFinalize!({}));
267+
const layout = (width: number, height: number) =>
268+
act(async () => harness.pressable.onLayout?.({ nativeEvent: { layout: { width, height } } }));
269+
const markdownImage = (sourceKey: string, onPressPreview: () => void = () => {}) => (
270+
<MarkdownImageAvailableWidthContext value={345}>
271+
<ThreadMarkdownImageView
272+
uri={`https://example.test/${sourceKey}.png`}
273+
sourceKey={sourceKey}
274+
unavailable={false}
275+
alt={null}
276+
knownSize={{ width: 1000, height: 585 }}
277+
onPressPreview={onPressPreview}
278+
/>
279+
</MarkdownImageAvailableWidthContext>
280+
);
281+
282+
it("does not open the preview when a touch on the inline image pinches or pans it", async () => {
283+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
284+
const root = createRoot(document.createElement("div"));
285+
const onPressPreview = vi.fn();
286+
const touch = (fingers: number) =>
287+
act(async () =>
288+
harness.pressable.onTouchStart?.({
289+
nativeEvent: { touches: Array.from({ length: fingers }, () => ({})) },
290+
}),
291+
);
292+
const release = () =>
293+
act(async () => harness.pressable.onPress?.({ nativeEvent: { identifier: 1 } }));
294+
try {
295+
await act(async () => root.render(markdownImage("hero", onPressPreview)));
296+
await touch(1);
297+
await touch(2);
298+
await release();
299+
expect(onPressPreview).not.toHaveBeenCalled();
300+
301+
await touch(1);
302+
await act(async () => harness.gestures.pan!.onStart!({ translationX: 5, translationY: 0 }));
303+
await release();
304+
expect(onPressPreview).not.toHaveBeenCalled();
305+
306+
await touch(1);
307+
await release();
308+
expect(onPressPreview).toHaveBeenCalledOnce();
309+
310+
// VoiceOver activation has no touch, so an earlier pinch must not swallow it.
311+
await touch(2);
312+
await act(async () => harness.pressable.onPress?.({ nativeEvent: {} }));
313+
expect(onPressPreview).toHaveBeenCalledTimes(2);
314+
} finally {
315+
await act(async () => root.unmount());
316+
}
317+
});
318+
319+
it("zooms the inline image around the pinch and keeps it until the source or frame changes", async () => {
320+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
321+
const root = createRoot(document.createElement("div"));
322+
const touchPan = () => {
323+
const manager = { fail: vi.fn() };
324+
harness.gestures.pan!.onTouchesDown!({}, manager);
325+
return manager;
326+
};
327+
try {
328+
await act(async () => root.render(markdownImage("a")));
329+
// The frame center is (172.5, 101); the pinch is off center.
330+
await layout(345, 202);
331+
await pinch("onStart", 300, 150, 1);
332+
await pinch("onUpdate", 300, 150, 2);
333+
expect(zoom()).toEqual({ scale: 2, x: -127.5, y: -49 });
334+
await pinch("onUpdate", 280, 140, 2);
335+
expect(zoom()).toEqual({ scale: 2, x: -147.5, y: -59 });
336+
await act(async () =>
337+
harness.gestures.pinch!.onUpdate!({ focalX: 90, focalY: 90, scale: 2, numberOfPointers: 1 }),
338+
);
339+
expect(zoom()).toEqual({ scale: 2, x: -147.5, y: -59 });
340+
await endPinch();
341+
342+
await act(async () => harness.loadImage({ source: { width: 1000, height: 585 } }));
343+
await act(async () => root.render(markdownImage("a")));
344+
await layout(345, 202);
345+
expect(zoom()).toEqual({ scale: 2, x: -147.5, y: -59 });
346+
347+
await pinch("onStart", 100, 50, 1);
348+
await pinch("onUpdate", 100, 50, 1.5);
349+
expect(zoom()).toEqual({ scale: 3, x: -185, y: -63 });
350+
await pinch("onUpdate", 100, 50, 10);
351+
expect(zoom().scale).toBe(6);
352+
await endPinch();
353+
354+
const zoomedPan = touchPan();
355+
await act(async () => harness.gestures.pan!.onStart!({ translationX: 5, translationY: 0 }));
356+
await act(async () =>
357+
harness.gestures.pan!.onUpdate!({ translationX: 5000, translationY: -5000 }),
358+
);
359+
expect(zoomedPan.fail).not.toHaveBeenCalled();
360+
expect(zoom()).toEqual({ scale: 6, x: 862.5, y: -505 });
361+
362+
await pinch("onStart", 172, 100, 1);
363+
await pinch("onUpdate", 172, 100, 0.01);
364+
await endPinch();
365+
expect(zoom()).toEqual({ scale: 1, x: 0, y: 0 });
366+
expect(touchPan().fail).toHaveBeenCalledOnce();
367+
368+
await pinch("onStart", 300, 150, 1);
369+
await pinch("onUpdate", 300, 150, 2);
370+
await endPinch();
371+
await layout(300, 175);
372+
expect(zoom()).toEqual({ scale: 1, x: 0, y: 0 });
373+
374+
await pinch("onStart", 300, 150, 1);
375+
await pinch("onUpdate", 300, 150, 2);
376+
await endPinch();
377+
await act(async () => root.render(markdownImage("b")));
378+
expect(zoom()).toEqual({ scale: 1, x: 0, y: 0 });
379+
380+
for (const panFirst of [true, false]) {
381+
await act(async () => root.render(markdownImage(`handoff-${panFirst}`)));
382+
await layout(300, 200);
383+
await pinch("onStart", 150, 100, 1);
384+
await pinch("onUpdate", 150, 100, 2);
385+
await endPinch();
386+
await act(async () => harness.gestures.pan!.onStart!({ translationX: 0, translationY: 0 }));
387+
await pinch("onStart", 150, 100, 1);
388+
const panUpdate = () =>
389+
act(async () => harness.gestures.pan!.onUpdate!({ translationX: 10, translationY: 0 }));
390+
if (panFirst) await panUpdate();
391+
await pinch("onUpdate", 150, 100, 1.5);
392+
if (!panFirst) await panUpdate();
393+
await endPinch();
394+
await act(async () => harness.gestures.pan!.onUpdate!({ translationX: 20, translationY: 0 }));
395+
expect(zoom()).toEqual({ scale: 3, x: 10, y: 0 });
396+
}
397+
} finally {
398+
await act(async () => root.unmount());
399+
}
400+
});

0 commit comments

Comments
 (0)