The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 177 lines 6.29 KB
Raw
1 import { useEffectEvent, useRef, useState, type ComponentProps, type ReactNode } from "react";
2 import { Platform, Pressable, StyleSheet } from "react-native";
3 import { Gesture, GestureDetector } from "react-native-gesture-handler";
4 import Animated, { runOnJS, useAnimatedStyle, useSharedValue } from "react-native-reanimated";
5
6 const MAX_INLINE_ZOOM = 6;
7 // Below UIScrollView's drag hysteresis, so a zoomed pan wins over the feed scroll.
8 const ZOOMED_PAN_MIN_DISTANCE = 5;
9
10 type Zoom = { readonly scale: number; readonly x: number; readonly y: number };
11 type Size = { readonly width: number; readonly height: number };
12
13 const FIT: Zoom = { scale: 1, x: 0, y: 0 };
14
15 /** Keeps the scaled content covering the frame: translation is about the frame center. */
16 function clampZoom(zoom: Zoom, size: Size): Zoom {
17 "worklet";
18 const scale = Math.min(Math.max(zoom.scale, 1), MAX_INLINE_ZOOM);
19 const maxX = ((scale - 1) * size.width) / 2;
20 const maxY = ((scale - 1) * size.height) / 2;
21 return {
22 scale,
23 x: Math.min(Math.max(zoom.x, -maxX), maxX),
24 y: Math.min(Math.max(zoom.y, -maxY), maxY),
25 };
26 }
27
28 type ZoomableImagePressableProps = Omit<ComponentProps<typeof Pressable>, "children"> & {
29 readonly children: ReactNode;
30 };
31
32 /**
33 * Inline chat image that pinch-zooms in place on iOS. Key it by image source so a
34 * new source starts at fit; ordinary rerenders keep the zoom and offset.
35 */
36 export function ZoomableImagePressable(props: ZoomableImagePressableProps) {
37 if (Platform.OS !== "ios") return <Pressable {...props} />;
38 return <IosZoomableImagePressable {...props} />;
39 }
40
41 function IosZoomableImagePressable({
42 children,
43 onTouchStart,
44 onPress,
45 onLayout,
46 ...pressableProps
47 }: ZoomableImagePressableProps) {
48 const gestured = useRef(false);
49 const layoutSize = useRef<Size | null>(null);
50 const zoom = useSharedValue(FIT);
51 const size = useSharedValue<Size>({ width: 0, height: 0 });
52 const pinching = useSharedValue(false);
53 const pinchStart = useSharedValue({ zoom: FIT, focalX: 0, focalY: 0, scale: 1, pointers: 0 });
54 const panStart = useSharedValue({ zoom: FIT, translationX: 0, translationY: 0 });
55
56 const markGestured = useEffectEvent(() => {
57 gestured.current = true;
58 });
59 const [gesture] = useState(() => {
60 // Focal points arrive in the untransformed frame; the transform is about its center.
61 const pinch = Gesture.Pinch()
62 .onStart((event) => {
63 const frame = size.get();
64 pinching.set(true);
65 pinchStart.set({
66 zoom: zoom.get(),
67 focalX: event.focalX - frame.width / 2,
68 focalY: event.focalY - frame.height / 2,
69 scale: event.scale,
70 pointers: event.numberOfPointers,
71 });
72 runOnJS(markGestured)();
73 })
74 .onUpdate((event) => {
75 const frame = size.get();
76 const start = pinchStart.get();
77 const focalX = event.focalX - frame.width / 2;
78 const focalY = event.focalY - frame.height / 2;
79 if (event.numberOfPointers !== start.pointers) {
80 // A lifted or added finger jumps the focal point; continue from here instead.
81 pinchStart.set({
82 zoom: zoom.get(),
83 focalX,
84 focalY,
85 scale: event.scale,
86 pointers: event.numberOfPointers,
87 });
88 return;
89 }
90 const scale = Math.min(
91 Math.max((start.zoom.scale * event.scale) / start.scale, 1),
92 MAX_INLINE_ZOOM,
93 );
94 // Keep the content point that started under the fingers under them.
95 const contentX = (start.focalX - start.zoom.x) / start.zoom.scale;
96 const contentY = (start.focalY - start.zoom.y) / start.zoom.scale;
97 zoom.set(
98 clampZoom({ scale, x: focalX - scale * contentX, y: focalY - scale * contentY }, frame),
99 );
100 })
101 .onFinalize(() => {
102 panStart.set({ ...panStart.get(), zoom: zoom.get() });
103 pinching.set(false);
104 });
105 const pan = Gesture.Pan()
106 .minDistance(ZOOMED_PAN_MIN_DISTANCE)
107 .onTouchesDown((_event, manager) => {
108 // At fit, a one-finger drag belongs to the feed.
109 if (zoom.get().scale <= 1) manager.fail();
110 })
111 .onStart((event) => {
112 panStart.set({
113 zoom: zoom.get(),
114 translationX: event.translationX,
115 translationY: event.translationY,
116 });
117 runOnJS(markGestured)();
118 })
119 .onUpdate((event) => {
120 if (pinching.get()) {
121 // The pinch owns the transform; resume panning from wherever it leaves it.
122 panStart.set({
123 zoom: zoom.get(),
124 translationX: event.translationX,
125 translationY: event.translationY,
126 });
127 return;
128 }
129 const start = panStart.get();
130 zoom.set(
131 clampZoom(
132 {
133 scale: start.zoom.scale,
134 x: start.zoom.x + event.translationX - start.translationX,
135 y: start.zoom.y + event.translationY - start.translationY,
136 },
137 size.get(),
138 ),
139 );
140 });
141 return Gesture.Simultaneous(pinch, pan);
142 });
143 const contentStyle = useAnimatedStyle(() => {
144 const { scale, x, y } = zoom.get();
145 return { transform: [{ translateX: x }, { translateY: y }, { scale }] };
146 });
147
148 return (
149 <GestureDetector gesture={gesture}>
150 <Pressable
151 {...pressableProps}
152 onTouchStart={(event) => {
153 gestured.current = event.nativeEvent.touches.length > 1;
154 onTouchStart?.(event);
155 }}
156 onPress={(event) => {
157 // Accessibility activation carries no touch identifier and no gesture.
158 if (typeof event.nativeEvent.identifier === "number" && gestured.current) return;
159 onPress?.(event);
160 }}
161 onLayout={(event) => {
162 const { width, height } = event.nativeEvent.layout;
163 const previous = layoutSize.current;
164 if (previous?.width !== width || previous?.height !== height) {
165 // The old offsets no longer fit the new frame, so a resize starts over at fit.
166 layoutSize.current = { width, height };
167 size.set({ width, height });
168 zoom.set(FIT);
169 }
170 onLayout?.(event);
171 }}
172 >
173 <Animated.View style={[StyleSheet.absoluteFill, contentStyle]}>{children}</Animated.View>
174 </Pressable>
175 </GestureDetector>
176 );
177 }