MCPcopy
hub / github.com/bbycroft/llm-viz / useTouchEvents

Function useTouchEvents

src/utils/pointer.ts:149–244  ·  view source on GitHub ↗
(
    el: GlobalEventHandlers | null,
    data: T,
    options: ITouchEventOptions,
    handle1PointDrag?: (ev: TouchEvent, start: TouchEventStart1PointDrag<T>, end: boolean) => void,
    handle2PointDrag?: (ev: TouchEvent, start: TouchEventStart<T>) => void,
    handle1PointClick?: (ev: TouchEvent, start: TouchEventStart<T>) => void,
)

Source from the content-addressed store, hash-verified

147}
148
149export function useTouchEvents<T>(
150 el: GlobalEventHandlers | null,
151 data: T,
152 options: ITouchEventOptions,
153 handle1PointDrag?: (ev: TouchEvent, start: TouchEventStart1PointDrag<T>, end: boolean) => void,
154 handle2PointDrag?: (ev: TouchEvent, start: TouchEventStart<T>) => void,
155 handle1PointClick?: (ev: TouchEvent, start: TouchEventStart<T>) => void,
156) {
157 let alwaysSendDragEvent = options.alwaysSendDragEvent ?? false;
158 let sendDragEnd = options.sendDragEnd ?? false;
159 let initialData = useRef<T>(data);
160 let initialTouches = useRef<TouchSimple[]>();
161 let lastTouch = useRef<{ time: number, velocity: number, touch: TouchSimple } | null>(null);
162 let isDrag = useRef<boolean>(false);
163 let latestData = useRef<T>(data);
164 let lastPressTime = useRef<number>(0);
165 latestData.current = data;
166 let handle1PointDragRef = useFunctionRef(handle1PointDrag);
167 let handle2PointDragRef = useFunctionRef(handle2PointDrag);
168 let handle1PointClickRef = useFunctionRef(handle1PointClick);
169
170 useEffect(() => {
171 if (!el) {
172 return;
173 }
174
175 function sendEvent(ev: TouchEvent) {
176 let initial = {data: initialData.current, touches: initialTouches.current!};
177 if (!ev.touches || !initial.touches || ev.touches.length !== initial.touches.length) {
178 return;
179 }
180
181 if (!isDrag.current) {
182 if (ev.touches.length > 1 || (ev.touches.length === 1 && touchPixelDist(ev.touches[0], initial.touches[0]) >= 10)) {
183 isDrag.current = true;
184 }
185 }
186
187 if (ev.touches.length === 1 && handle1PointDragRef.current && (alwaysSendDragEvent || isDrag.current)) {
188 handle1PointDragRef.current(ev, { ...initial, isDragging: isDrag.current }, false);
189 }
190 if (ev.touches.length === 2 && handle2PointDragRef.current) {
191 handle2PointDragRef.current(ev, initial);
192 }
193
194 if (ev.touches.length === 1) {
195 lastTouch.current = {
196 time: 0,
197 velocity: 0,
198 touch: copyTouchList(ev.touches)[0],
199 };
200
201 } else {
202 lastTouch.current = null;
203 }
204 }
205
206 function captureInitialAndSend(ev: TouchEvent) {

Callers 4

CanvasEventSurfaceFunction · 0.90
MovementControlsFunction · 0.90

Calls 1

useFunctionRefFunction · 0.90

Tested by

no test coverage detected