Open History to see the last commit
1diff --git a/dist/ImageViewing.js b/dist/ImageViewing.js
2index 3d7641a9a94864415d207c678b71f8c592c90aa1..160afbcbb74776e2a706817b5448a36e078ea0bc 100644
3--- a/dist/ImageViewing.js
4+++ b/dist/ImageViewing.js
5@@ -5,8 +5,8 @@
6 * LICENSE file in the root directory of this source tree.
7 *
8 */
9-import React, { useCallback, useRef, useEffect } from "react";
10-import { Animated, Dimensions, StyleSheet, View, VirtualizedList, Modal, } from "react-native";
11+import React, { useCallback, useRef, useEffect, useLayoutEffect } from "react";
12+import { Animated, Dimensions, StyleSheet, View, VirtualizedList, Modal, Platform, useWindowDimensions, } from "react-native";
13 import ImageItem from "./components/ImageItem/ImageItem";
14 import ImageDefaultHeader from "./components/ImageDefaultHeader";
15 import StatusBarManager from "./components/StatusBarManager";
16@@ -17,23 +17,34 @@ const DEFAULT_ANIMATION_TYPE = "fade";
17 const DEFAULT_BG_COLOR = "#000";
18 const DEFAULT_DELAY_LONG_PRESS = 800;
19 const SCREEN = Dimensions.get("screen");
20-const SCREEN_WIDTH = SCREEN.width;
21+const useViewingDimensions = Platform.OS === "android" ? useWindowDimensions : () => SCREEN;
22 function ImageViewing({ images, keyExtractor, imageIndex, visible, onRequestClose, onLongPress = () => { }, onImageIndexChange, animationType = DEFAULT_ANIMATION_TYPE, backgroundColor = DEFAULT_BG_COLOR, presentationStyle, swipeToCloseEnabled, doubleTapToZoomEnabled, delayLongPress = DEFAULT_DELAY_LONG_PRESS, HeaderComponent, FooterComponent, }) {
23+ const viewport = useViewingDimensions();
24+ const SCREEN_WIDTH = viewport.width;
25+ const viewportKey = Platform.OS === "android" ? `${viewport.width}:${viewport.height}` : undefined;
26 const imageList = useRef(null);
27 const [opacity, onRequestCloseEnhanced] = useRequestClose(onRequestClose);
28- const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, SCREEN);
29- const [headerTransform, footerTransform, toggleBarsVisible] = useAnimatedComponents();
30+ const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, viewport);
31+ const [headerTransform, footerTransform, toggleBarsVisible, toggleBars] = useAnimatedComponents(viewportKey);
32 useEffect(() => {
33 if (onImageIndexChange) {
34 onImageIndexChange(currentImageIndex);
35 }
36 }, [currentImageIndex]);
37- const onZoom = useCallback((isScaled) => {
38+ const activeZoom = useRef(null);
39+ const onZoom = useCallback(function handleZoom(isScaled) {
40+ if (Platform.OS === "android" && activeZoom.current !== handleZoom) {
41+ return;
42+ }
43 var _a, _b;
44 // @ts-ignore
45 (_b = (_a = imageList) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.setNativeProps({ scrollEnabled: !isScaled });
46 toggleBarsVisible(!isScaled);
47- }, [imageList]);
48+ }, [imageList, toggleBarsVisible]);
49+ useLayoutEffect(() => {
50+ activeZoom.current = onZoom;
51+ return () => { activeZoom.current = null; };
52+ }, [onZoom]);
53 if (!visible) {
54 return null;
55 }
56@@ -45,11 +56,12 @@ function ImageViewing({ images, keyExtractor, imageIndex, visible, onRequestClos
57 imageIndex: currentImageIndex,
58 })) : (<ImageDefaultHeader onRequestClose={onRequestCloseEnhanced}/>)}
59 </Animated.View>
60- <VirtualizedList ref={imageList} data={images} horizontal pagingEnabled windowSize={2} initialNumToRender={1} maxToRenderPerBatch={1} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} initialScrollIndex={imageIndex} getItem={(_, index) => images[index]} getItemCount={() => images.length} getItemLayout={(_, index) => ({
61+ {/* Reset Android image gestures on resize while preserving custom header state. */}
62+ <VirtualizedList key={viewportKey} ref={imageList} onScroll={Platform.OS === "android" ? onScroll : undefined} scrollEventThrottle={Platform.OS === "android" ? 16 : undefined} data={images} horizontal pagingEnabled windowSize={2} initialNumToRender={1} maxToRenderPerBatch={1} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} initialScrollIndex={currentImageIndex} getItem={(_, index) => images[index]} getItemCount={() => images.length} getItemLayout={(_, index) => ({
63 length: SCREEN_WIDTH,
64 offset: SCREEN_WIDTH * index,
65 index,
66- })} renderItem={({ item: imageSrc }) => (<ImageItem onZoom={onZoom} imageSrc={imageSrc} onRequestClose={onRequestCloseEnhanced} onLongPress={onLongPress} delayLongPress={delayLongPress} swipeToCloseEnabled={swipeToCloseEnabled} doubleTapToZoomEnabled={doubleTapToZoomEnabled}/>)} onMomentumScrollEnd={onScroll}
67+ })} renderItem={({ item: imageSrc }) => (<ImageItem onZoom={onZoom} imageSrc={imageSrc} onRequestClose={onRequestCloseEnhanced} onLongPress={onLongPress} delayLongPress={delayLongPress} swipeToCloseEnabled={swipeToCloseEnabled} doubleTapToZoomEnabled={doubleTapToZoomEnabled} onTap={toggleBars}/>)} onMomentumScrollEnd={onScroll}
68 //@ts-ignore
69 keyExtractor={(imageSrc, index) => keyExtractor
70 ? keyExtractor(imageSrc, index)
71diff --git a/dist/components/ImageItem/ImageItem.android.d.ts b/dist/components/ImageItem/ImageItem.android.d.ts
72index 0b1aa96dce0f1f9336934be1f44c4e30687454e5..cba7d2d28e1435f37d4287e20c4d02f30ea4940a 100644
73--- a/dist/components/ImageItem/ImageItem.android.d.ts
74+++ b/dist/components/ImageItem/ImageItem.android.d.ts
75@@ -15,6 +15,7 @@ declare type Props = {
76 delayLongPress: number;
77 swipeToCloseEnabled?: boolean;
78 doubleTapToZoomEnabled?: boolean;
79+ onTap?: () => void;
80 };
81-declare const _default: React.MemoExoticComponent<({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPress, swipeToCloseEnabled, doubleTapToZoomEnabled, }: Props) => JSX.Element>;
82+declare const _default: React.MemoExoticComponent<({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPress, swipeToCloseEnabled, doubleTapToZoomEnabled, onTap, }: Props) => JSX.Element>;
83 export default _default;
84diff --git a/dist/components/ImageItem/ImageItem.android.js b/dist/components/ImageItem/ImageItem.android.js
85index 5fcaa9f9a7d893817fee89b85186ee3936118e69..9e449400e042b4ece61f9a0262700e8c9b50df5e 100644
86--- a/dist/components/ImageItem/ImageItem.android.js
87+++ b/dist/components/ImageItem/ImageItem.android.js
88@@ -5,20 +5,39 @@
89 * LICENSE file in the root directory of this source tree.
90 *
91 */
92-import React, { useCallback, useRef, useState } from "react";
93-import { Animated, ScrollView, Dimensions, StyleSheet, } from "react-native";
94+import React, { useCallback, useEffect, useRef, useState } from "react";
95+import { Animated, ScrollView, Dimensions, View, } from "react-native";
96+import { Image as ExpoImage } from "expo-image";
97 import useImageDimensions from "../../hooks/useImageDimensions";
98 import usePanResponder from "../../hooks/usePanResponder";
99 import { getImageStyles, getImageTransform } from "../../utils";
100 import { ImageLoading } from "./ImageLoading";
101 const SWIPE_CLOSE_OFFSET = 75;
102 const SWIPE_CLOSE_VELOCITY = 1.75;
103-const SCREEN = Dimensions.get("window");
104-const SCREEN_WIDTH = SCREEN.width;
105-const SCREEN_HEIGHT = SCREEN.height;
106-const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPress, swipeToCloseEnabled = true, doubleTapToZoomEnabled = true, }) => {
107+const AnimatedExpoImage = Animated.createAnimatedComponent(ExpoImage);
108+const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPress, swipeToCloseEnabled = true, doubleTapToZoomEnabled = true, onTap, }) => {
109+ const SCREEN = Dimensions.get("window");
110+ const SCREEN_WIDTH = SCREEN.width;
111+ const SCREEN_HEIGHT = SCREEN.height;
112 const imageContainer = useRef(null);
113- const imageDimensions = useImageDimensions(imageSrc);
114+ const sizedDimensions = useImageDimensions(imageSrc);
115+ // React Native's decoder cannot read some images, such as SVG; expo-image draws those or reports a failure.
116+ const fallbackUri = imageSrc?.uri && sizedDimensions?.width === 0 ? imageSrc.uri : null;
117+ const [fallback, setFallback] = useState(null);
118+ useEffect(() => {
119+ if (!fallbackUri) return;
120+ let live = true;
121+ ExpoImage.loadAsync(imageSrc).then((image) => live ? setFallback({ image }) : image.release(), () => live && setFallback({ failed: true }));
122+ return () => { live = false; };
123+ }, [fallbackUri]);
124+ useEffect(() => {
125+ const image = fallback?.image;
126+ // Released after the commit that unmounts it, as expo-image's useImage does.
127+ return image ? () => { Promise.resolve().then(() => image.release()); } : undefined;
128+ }, [fallback]);
129+ // Lay small images out at the fitted size, so double-tap's fixed 2x zooms in rather than out.
130+ const fit = fallback?.image && Math.max(1, Math.min(SCREEN_WIDTH / fallback.image.width, SCREEN_HEIGHT / fallback.image.height));
131+ const imageDimensions = !fallbackUri ? sizedDimensions : fit ? { width: fallback.image.width * fit, height: fallback.image.height * fit } : null;
132 const [translate, scale] = getImageTransform(imageDimensions, SCREEN);
133 const scrollValueY = new Animated.Value(0);
134 const [isLoaded, setLoadEnd] = useState(false);
135@@ -28,10 +47,10 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
136 onZoom(isZoomed);
137 if ((_a = imageContainer) === null || _a === void 0 ? void 0 : _a.current) {
138 imageContainer.current.setNativeProps({
139- scrollEnabled: !isZoomed,
140+ scrollEnabled: swipeToCloseEnabled && !isZoomed,
141 });
142 }
143- }, [imageContainer]);
144+ }, [imageContainer, onZoom, swipeToCloseEnabled]);
145 const onLongPressHandler = useCallback(() => {
146 onLongPress(imageSrc);
147 }, [imageSrc, onLongPress]);
148@@ -42,6 +61,7 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
149 doubleTapToZoomEnabled,
150 onLongPress: onLongPressHandler,
151 delayLongPress,
152+ onTap,
153 });
154 const imagesStyles = getImageStyles(imageDimensions, translateValue, scaleValue);
155 const imageOpacity = scrollValueY.interpolate({
156@@ -64,21 +84,15 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
157 const offsetY = (_c = (_b = (_a = nativeEvent) === null || _a === void 0 ? void 0 : _a.contentOffset) === null || _b === void 0 ? void 0 : _b.y, (_c !== null && _c !== void 0 ? _c : 0));
158 scrollValueY.setValue(offsetY);
159 };
160- return (<ScrollView ref={imageContainer} style={styles.listItem} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={styles.imageScrollContainer} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {
161+ return (<ScrollView ref={imageContainer} style={{ width: SCREEN_WIDTH, height: SCREEN_HEIGHT }} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={{ height: SCREEN_HEIGHT * 2 }} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {
162 onScroll,
163 onScrollEndDrag,
164 })}>
165- <Animated.Image {...panHandlers} source={imageSrc} style={imageStylesWithOpacity} onLoad={onLoaded}/>
166- {(!isLoaded || !imageDimensions) && <ImageLoading />}
167+ {/* Pinch from anywhere on screen, not only on the fitted image. */}
168+ <View {...panHandlers} pointerEvents={isLoaded && imageDimensions ? "auto" : "none"} style={{ width: SCREEN_WIDTH, height: SCREEN_HEIGHT }}>
169+ {fallbackUri ? (fallback?.image && <AnimatedExpoImage source={fallback.image} style={imageStylesWithOpacity} onLoad={onLoaded}/>) : (<Animated.Image source={imageSrc} style={imageStylesWithOpacity} onLoad={onLoaded}/>)}
170+ {(!isLoaded || !imageDimensions) && !fallback?.failed && <ImageLoading />}
171+ </View>
172 </ScrollView>);
173 };
174-const styles = StyleSheet.create({
175- listItem: {
176- width: SCREEN_WIDTH,
177- height: SCREEN_HEIGHT,
178- },
179- imageScrollContainer: {
180- height: SCREEN_HEIGHT * 2,
181- },
182-});
183 export default React.memo(ImageItem);
184diff --git a/dist/components/ImageItem/ImageLoading.js b/dist/components/ImageItem/ImageLoading.js
185index 9770f7db158a2fb2b1c69a49e5fcbc89d8624603..995fab7524bb8673258d44e5eda361c62dae0324 100644
186--- a/dist/components/ImageItem/ImageLoading.js
187+++ b/dist/components/ImageItem/ImageLoading.js
188@@ -6,25 +6,17 @@
189 *
190 */
191 import React from "react";
192-import { ActivityIndicator, Dimensions, StyleSheet, View } from "react-native";
193+import { ActivityIndicator, Dimensions, Platform, StyleSheet, View } from "react-native";
194 const SCREEN = Dimensions.get("screen");
195-const SCREEN_WIDTH = SCREEN.width;
196-const SCREEN_HEIGHT = SCREEN.height;
197-export const ImageLoading = () => (<View style={styles.loading}>
198+export const ImageLoading = () => {
199+ const { width, height } = Platform.OS === "android" ? Dimensions.get("window") : SCREEN;
200+ return (<View style={[styles.loading, { width, height }]}>
201 <ActivityIndicator size="small" color="#FFF"/>
202 </View>);
203+};
204 const styles = StyleSheet.create({
205- listItem: {
206- width: SCREEN_WIDTH,
207- height: SCREEN_HEIGHT,
208- },
209 loading: {
210- width: SCREEN_WIDTH,
211- height: SCREEN_HEIGHT,
212 alignItems: "center",
213 justifyContent: "center",
214 },
215- imageScrollContainer: {
216- height: SCREEN_HEIGHT,
217- },
218 });
219diff --git a/dist/hooks/useAnimatedComponents.d.ts b/dist/hooks/useAnimatedComponents.d.ts
220index de5957458fea62f33743103acaa170a2c90d3638..16519797ee3da81cde2712d14aff117668d9675d 100644
221--- a/dist/hooks/useAnimatedComponents.d.ts
222+++ b/dist/hooks/useAnimatedComponents.d.ts
223@@ -6,9 +6,9 @@
224 *
225 */
226 import { Animated } from "react-native";
227-declare const useAnimatedComponents: () => readonly [{
228+declare const useAnimatedComponents: (resetKey?: string) => readonly [{
229 [key: string]: Animated.Value;
230 }[], {
231 [key: string]: Animated.Value;
232-}[], (isVisible: boolean) => void];
233+}[], (isVisible: boolean) => void, () => void];
234 export default useAnimatedComponents;
235diff --git a/dist/hooks/useAnimatedComponents.js b/dist/hooks/useAnimatedComponents.js
236index 4730fe40735bd80afc97abdd14150a2fb4abf3e5..dacd69487ec88b152bede0fbcd1b6394a3a4ba56 100644
237--- a/dist/hooks/useAnimatedComponents.js
238+++ b/dist/hooks/useAnimatedComponents.js
239@@ -5,16 +5,24 @@
240 * LICENSE file in the root directory of this source tree.
241 *
242 */
243+import { useCallback, useMemo, useRef } from "react";
244 import { Animated } from "react-native";
245 const INITIAL_POSITION = { x: 0, y: 0 };
246 const ANIMATION_CONFIG = {
247 duration: 200,
248 useNativeDriver: true,
249 };
250-const useAnimatedComponents = () => {
251- const headerTranslate = new Animated.ValueXY(INITIAL_POSITION);
252- const footerTranslate = new Animated.ValueXY(INITIAL_POSITION);
253- const toggleVisible = (isVisible) => {
254+const useAnimatedComponents = (resetKey) => {
255+ const barsVisible = useRef(true);
256+ const [headerTranslate, footerTranslate] = useMemo(() => {
257+ barsVisible.current = true;
258+ return [
259+ new Animated.ValueXY(INITIAL_POSITION),
260+ new Animated.ValueXY(INITIAL_POSITION),
261+ ];
262+ }, [resetKey]);
263+ const toggleVisible = useCallback((isVisible) => {
264+ barsVisible.current = isVisible;
265 if (isVisible) {
266 Animated.parallel([
267 Animated.timing(headerTranslate.y, { ...ANIMATION_CONFIG, toValue: 0 }),
268@@ -33,9 +41,10 @@ const useAnimatedComponents = () => {
269 }),
270 ]).start();
271 }
272- };
273+ }, [headerTranslate, footerTranslate]);
274 const headerTransform = headerTranslate.getTranslateTransform();
275 const footerTransform = footerTranslate.getTranslateTransform();
276- return [headerTransform, footerTransform, toggleVisible];
277+ const toggleBars = useCallback(() => toggleVisible(!barsVisible.current), [toggleVisible]);
278+ return [headerTransform, footerTransform, toggleVisible, toggleBars];
279 };
280 export default useAnimatedComponents;
281diff --git a/dist/hooks/useImageIndexChange.js b/dist/hooks/useImageIndexChange.js
282index cb5f0f798e5cfeedf459b7d22e92871db3092020..97ca2c9c326b1c53e6a5d90c3bb95d53342088d1 100644
283--- a/dist/hooks/useImageIndexChange.js
284+++ b/dist/hooks/useImageIndexChange.js
285@@ -5,16 +5,34 @@
286 * LICENSE file in the root directory of this source tree.
287 *
288 */
289-import { useState } from "react";
290+import { useCallback, useLayoutEffect, useRef, useState } from "react";
291+import { Platform } from "react-native";
292 const useImageIndexChange = (imageIndex, screen) => {
293 const [currentImageIndex, setImageIndex] = useState(imageIndex);
294- const onScroll = (event) => {
295+ const indexRef = useRef(imageIndex);
296+ const activeScroll = useRef(null);
297+ const onScroll = useCallback(function handleScroll(event) {
298+ if (Platform.OS === "android") {
299+ // Events at this width stay valid until commit; replaced or relaid-out lists do not.
300+ if (activeScroll.current !== handleScroll ||
301+ Math.abs(event.nativeEvent.layoutMeasurement.width - screen.width) > 1) {
302+ return;
303+ }
304+ }
305 const { nativeEvent: { contentOffset: { x: scrollX }, }, } = event;
306 if (screen.width) {
307 const nextIndex = Math.round(scrollX / screen.width);
308- setImageIndex(nextIndex < 0 ? 0 : nextIndex);
309+ const boundedIndex = nextIndex < 0 ? 0 : nextIndex;
310+ if (boundedIndex !== indexRef.current) {
311+ indexRef.current = boundedIndex;
312+ setImageIndex(boundedIndex);
313+ }
314 }
315- };
316+ }, [screen]);
317+ useLayoutEffect(() => {
318+ activeScroll.current = onScroll;
319+ return () => { activeScroll.current = null; };
320+ }, [onScroll]);
321 return [currentImageIndex, onScroll];
322 };
323 export default useImageIndexChange;
324diff --git a/dist/hooks/usePanResponder.d.ts b/dist/hooks/usePanResponder.d.ts
325index a4e41f0aaab61e029088f4efbdf940b1d23357c1..20f4929b66dce1d7647eb0690b2c097b9ed1c490 100644
326--- a/dist/hooks/usePanResponder.d.ts
327+++ b/dist/hooks/usePanResponder.d.ts
328@@ -14,6 +14,7 @@ declare type Props = {
329 doubleTapToZoomEnabled: boolean;
330 onLongPress: () => void;
331 delayLongPress: number;
332+ onTap?: () => void;
333 };
334-declare const usePanResponder: ({ initialScale, initialTranslate, onZoom, doubleTapToZoomEnabled, onLongPress, delayLongPress, }: Props) => readonly [GestureResponderHandlers, Animated.Value, Animated.ValueXY];
335+declare const usePanResponder: ({ initialScale, initialTranslate, onZoom, doubleTapToZoomEnabled, onLongPress, delayLongPress, onTap, }: Props) => readonly [GestureResponderHandlers, Animated.Value, Animated.ValueXY];
336 export default usePanResponder;
337diff --git a/dist/hooks/usePanResponder.js b/dist/hooks/usePanResponder.js
338index 99dcbc7efea4eba86a8bc2bad71dde73c8ebf1b4..15bcfe657eb7f05ba0c8b298dbeba1f6e5b2680a 100644
339--- a/dist/hooks/usePanResponder.js
340+++ b/dist/hooks/usePanResponder.js
341@@ -5,17 +5,17 @@
342 * LICENSE file in the root directory of this source tree.
343 *
344 */
345-import { useMemo, useEffect } from "react";
346+import { useMemo, useEffect, useRef } from "react";
347 import { Animated, Dimensions, } from "react-native";
348 import { createPanResponder, getDistanceBetweenTouches, getImageTranslate, getImageDimensionsByTranslate, } from "../utils";
349-const SCREEN = Dimensions.get("window");
350-const SCREEN_WIDTH = SCREEN.width;
351-const SCREEN_HEIGHT = SCREEN.height;
352-const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);
353 const SCALE_MAX = 2;
354 const DOUBLE_TAP_DELAY = 300;
355 const OUT_BOUND_MULTIPLIER = 0.75;
356-const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZoomEnabled, onLongPress, delayLongPress, }) => {
357+const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZoomEnabled, onLongPress, delayLongPress, onTap, }) => {
358+ const SCREEN = Dimensions.get("window");
359+ const SCREEN_WIDTH = SCREEN.width;
360+ const SCREEN_HEIGHT = SCREEN.height;
361+ const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);
362 let numberInitialTouches = 1;
363 let initialTouches = [];
364 let currentScale = initialScale;
365@@ -25,6 +25,9 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
366 let isDoubleTapPerformed = false;
367 let lastTapTS = null;
368 let longPressHandlerRef = null;
369+ let tapCandidate = false;
370+ const tapTimer = useRef(null);
371+ const pendingTaps = useRef(new Set());
372 const meaningfulShift = MIN_DIMENSION * 0.01;
373 const scaleValue = new Animated.Value(initialScale);
374 const translateValue = new Animated.ValueXY(initialTranslate);
375@@ -68,21 +71,37 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
376 });
377 return () => scaleValue.removeAllListeners();
378 });
379+ useEffect(() => () => pendingTaps.current.forEach(clearTimeout), []);
380 const cancelLongPressHandle = () => {
381 longPressHandlerRef && clearTimeout(longPressHandlerRef);
382 };
383+ const cancelTap = (timer) => {
384+ clearTimeout(timer);
385+ pendingTaps.current.delete(timer);
386+ };
387+ // A pinch is never a tap: every toggle still waiting on its double-tap window is dropped.
388+ const cancelPendingTaps = () => {
389+ pendingTaps.current.forEach(clearTimeout);
390+ pendingTaps.current.clear();
391+ };
392 const handlers = {
393 onGrant: (_, gestureState) => {
394 numberInitialTouches = gestureState.numberActiveTouches;
395 if (gestureState.numberActiveTouches > 1)
396 return;
397- longPressHandlerRef = setTimeout(onLongPress, delayLongPress);
398+ longPressHandlerRef = setTimeout(() => {
399+ tapCandidate = false;
400+ onLongPress();
401+ }, delayLongPress);
402 },
403 onStart: (event, gestureState) => {
404 initialTouches = event.nativeEvent.touches;
405 numberInitialTouches = gestureState.numberActiveTouches;
406- if (gestureState.numberActiveTouches > 1)
407+ tapCandidate = gestureState.numberActiveTouches === 1;
408+ if (gestureState.numberActiveTouches > 1) {
409+ cancelPendingTaps();
410 return;
411+ }
412 const tapTS = Date.now();
413 // Handle double tap event by calculating diff between first and second taps timestamps
414 isDoubleTapPerformed = Boolean(lastTapTS && tapTS - lastTapTS < DOUBLE_TAP_DELAY);
415@@ -99,6 +118,8 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
416 y: initialTranslate.y +
417 (SCREEN_HEIGHT / 2 - touchY) * (targetScale / currentScale),
418 }, targetScale);
419+ tapCandidate = false;
420+ cancelTap(tapTimer.current);
421 onZoom(!isScaled);
422 Animated.parallel([
423 Animated.timing(translateValue.x, {
424@@ -130,6 +151,7 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
425 const { dx, dy } = gestureState;
426 if (Math.abs(dx) >= meaningfulShift || Math.abs(dy) >= meaningfulShift) {
427 cancelLongPressHandle();
428+ tapCandidate = false;
429 }
430 // Don't need to handle move because double tap in progress (was handled in onStart)
431 if (doubleTapToZoomEnabled && isDoubleTapPerformed) {
432@@ -140,6 +162,8 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
433 gestureState.numberActiveTouches === 2) {
434 numberInitialTouches = 2;
435 initialTouches = event.nativeEvent.touches;
436+ tapCandidate = false;
437+ cancelPendingTaps();
438 }
439 const isTapGesture = numberInitialTouches == 1 && gestureState.numberActiveTouches === 1;
440 const isPinchGesture = numberInitialTouches === 2 && gestureState.numberActiveTouches === 2;
441@@ -214,6 +238,16 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
442 },
443 onRelease: () => {
444 cancelLongPressHandle();
445+ if (tapCandidate && onTap) {
446+ // Wait out the double tap window so a double tap zooms without also toggling.
447+ const timer = setTimeout(() => {
448+ pendingTaps.current.delete(timer);
449+ onTap();
450+ }, DOUBLE_TAP_DELAY);
451+ pendingTaps.current.add(timer);
452+ tapTimer.current = timer;
453+ }
454+ tapCandidate = false;
455 if (isDoubleTapPerformed) {
456 isDoubleTapPerformed = false;
457 }
458@@ -266,6 +300,12 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
459 tmpTranslate = null;
460 }
461 },
462+ // A gesture taken over mid-pinch must commit like a release, or pans stay
463+ // gated on the pre-pinch scale while the image still shows zoomed.
464+ onTerminate: () => {
465+ tapCandidate = false;
466+ handlers.onRelease();
467+ },
468 };
469 const panResponder = useMemo(() => createPanResponder(handlers), [handlers]);
470 return [panResponder.panHandlers, scaleValue, translateValue];