|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file RefreshPullRecognizer.ts |
| 5 | + * @input Touches from the gesture arena (Core's internal touch entry) |
| 6 | + * @output Exports RefreshPullRecognizer, the vertical pan that reads a pull |
| 7 | + * to refresh without taking the touch, and RefreshScrollPullRecognizer |
| 8 | + * @position The gesture of RefreshControl, on the foundation's |
| 9 | + * PanGestureRecognizer |
| 10 | + * |
| 11 | + * SYNC: When modified, update: |
| 12 | + * - /packages/lab/src/RefreshControl/useRefreshControl.ts |
| 13 | + */ |
| 14 | + |
| 15 | +// UIKit's refresh pull is the scroll view's own drag, not a second gesture. |
| 16 | +// One touch can scroll, pull past the top, and scroll the same content again. |
| 17 | +// So this pan never claims its touch, and only watches it. The browser keeps |
| 18 | +// the finger for native scrolling, so nothing changes hands midway. The pan |
| 19 | +// reads the finger to draw the overscroll, because the scroll owner's |
| 20 | +// `overscroll-behavior-y` is `none` near the top. |
| 21 | +// |
| 22 | +// The pan begins on vertical travel past its hysteresis. The touch layer |
| 23 | +// starts the pull where the content is at its top and the finger moves down. |
| 24 | +// When the browser takes the touch to scroll, the pan keeps reading it to the |
| 25 | +// lift. A second finger is the browser's, for pinch-zoom. |
| 26 | +// |
| 27 | +// A trackpad's scroll pulls through RefreshScrollPullRecognizer. A pen's or |
| 28 | +// a mouse's drag stays the browser's, for text selection: the pan takes only |
| 29 | +// a finger. `pointerType` and `scrollType` record the first contact's input. |
| 30 | +// `onTouchDown` reports that contact landing, so a settle in flight can stop |
| 31 | +// where it is, as UIScrollView brakes on touch-down. |
| 32 | + |
| 33 | +import { |
| 34 | + PanGestureRecognizer, |
| 35 | + type PanGestureRecognizerOptions, |
| 36 | +} from '@astryxdesign/core/internal/touch'; |
| 37 | +import type { |
| 38 | + GestureEventContext, |
| 39 | + GestureTouch, |
| 40 | + PointerKind, |
| 41 | +} from '@astryxdesign/core/internal/touch'; |
| 42 | + |
| 43 | +export class RefreshPullRecognizer extends PanGestureRecognizer { |
| 44 | + /** Called when the first contact of a gesture lands, with its timestamp. */ |
| 45 | + onTouchDown: ((time: number) => void) | null = null; |
| 46 | + /** The pointer of the gesture's first contact. */ |
| 47 | + pointerType: PointerKind = 'touch'; |
| 48 | + /** Set when the gesture is a trackpad's scroll rather than a contact. */ |
| 49 | + scrollType: GestureTouch['scrollType'] | null = null; |
| 50 | + |
| 51 | + constructor(options: PanGestureRecognizerOptions = {}) { |
| 52 | + super({ |
| 53 | + name: 'RefreshPull', |
| 54 | + direction: 'vertical', |
| 55 | + maximumNumberOfTouches: 1, |
| 56 | + allowedPointerTypes: ['touch'], |
| 57 | + ...options, |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + /** Native scrolling keeps the finger: the pull only reads it. */ |
| 62 | + override get claimsTouches(): boolean { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + protected override touchesBegan( |
| 67 | + touches: ReadonlyArray<GestureTouch>, |
| 68 | + event: GestureEventContext, |
| 69 | + ): void { |
| 70 | + if (this.numberOfTouches === touches.length) { |
| 71 | + const touch = touches[0]; |
| 72 | + this.pointerType = touch?.pointerType ?? 'touch'; |
| 73 | + this.scrollType = touch?.scrollType ?? null; |
| 74 | + this.onTouchDown?.(event.time); |
| 75 | + } |
| 76 | + super.touchesBegan(touches, event); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * The scroll pull: a trackpad's two-finger scroll at the top. It is a |
| 82 | + * scroll-edge pan that takes a trackpad's scroll and no pointer. So it takes a |
| 83 | + * scroll only where nothing under the pointer could scroll: the owner at its |
| 84 | + * top and the content moving down. The host cancels each wheel event of a |
| 85 | + * gesture that it takes, so its element gets a blocking `wheel` listener. Only |
| 86 | + * a recognizer that claims may register one. It takes no pointer, so it adds |
| 87 | + * no `touchmove` listener. |
| 88 | + */ |
| 89 | +export class RefreshScrollPullRecognizer extends RefreshPullRecognizer { |
| 90 | + constructor(options: PanGestureRecognizerOptions = {}) { |
| 91 | + super({ |
| 92 | + name: 'RefreshScrollPull', |
| 93 | + scrollEdge: true, |
| 94 | + allowedPointerTypes: [], |
| 95 | + allowedScrollTypes: ['continuous'], |
| 96 | + ...options, |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + /** The scroll it takes is its own: the host cancels its wheel events. */ |
| 101 | + override get claimsTouches(): boolean { |
| 102 | + return true; |
| 103 | + } |
| 104 | +} |
0 commit comments