Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions docs/backlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ until the list stops, so during a fling the thumb may point at the first section
the whole time. Not confirmed on a device yet. The fix is the one in "The dots
bar remounts every time it shows" below.

### The section list can hide while the finger is still on the dots

[FastScrollSectionFullList.tsx:73-88](../src/FastScrollSectionDots/FastScrollSectionFullList.tsx#L73-L88):
the list hides 3 s after the last `show()`, and `show()` only runs while the
finger moves. Holding the finger still on the dots for 3 s hides the list and
unlocks the bar mid-touch. Lock the list's timer from the pan's `onBegin` and
start it from `onFinalize` instead of refreshing it per frame. Best done
together with "Dragging the dots does work on every frame" below. Not confirmed
on a device yet.

## Performance

None of these grows memory over time: they cost CPU and short-lived garbage.
Expand Down Expand Up @@ -117,8 +107,8 @@ animation per frame, even while the finger stays on one section.

Possible fix: find the section in the worklet (`findSectionOnPosition` already
is one), keep the last `sectionIndex` in a shared value, and only cross to JS
when it changes. Lock the list's timer during the drag instead of refreshing it
(see "The section list can hide while the finger is still on the dots" above).
when it changes. The finger already holds the list open from `onBegin` to
`onFinalize`, so `show()` would not need to touch its hide timer at all.

### Both components restart the hide timer on every scroll frame

Expand Down
12 changes: 12 additions & 0 deletions src/FastScrollSectionDots/FastScrollSectionDots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ const FastScrollSectionDots = React.forwardRef(
side === 'left' ? SlideOutLeft : SlideOutRight
).duration(500);

// The section list stays open while a finger rests on the dots.
const holdSectionList = useCallback(
() => fastScrollSectionFullListRef.current?.hold(true),
[]
);
const releaseSectionList = useCallback(
() => fastScrollSectionFullListRef.current?.hold(false),
[]
);

const onHide = useCallback(() => {
lockScrollBarVisibility(false);
}, [lockScrollBarVisibility]);
Expand Down Expand Up @@ -243,6 +253,8 @@ const FastScrollSectionDots = React.forwardRef(
dotColor={dotColor}
scrollBarColor={scrollBarColor}
onSectionChange={onSectionChange}
onFingerDown={holdSectionList}
onFingerUp={releaseSectionList}
/>
</Animated.View>
)}
Expand Down
23 changes: 21 additions & 2 deletions src/FastScrollSectionDots/FastScrollSectionDotsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export type Props = {
*/
onSectionChange: (section: SectionFullData) => void;

/** A finger touches the dots: called before the first `onSectionChange`. */
onFingerDown: () => void;

/** The finger lifts off the dots: called after the last `onSectionChange`. */
onFingerUp: () => void;

sections: SectionFullData[];
};

Expand All @@ -87,6 +93,8 @@ const FastScrollSectionDotsBar = React.forwardRef(
scrollBarColor = 'rgba(65, 64, 66, 0.9)',
dotMargin = 12,
onSectionChange,
onFingerDown,
onFingerUp,
...props
}: Props,
forwardedRef: React.ForwardedRef<FastScrollSectionDotsBarHandle>
Expand Down Expand Up @@ -174,10 +182,10 @@ const FastScrollSectionDotsBar = React.forwardRef(
};
});

// The pan gesture below calls onThumbPositionChange through runOnJS, and on
// The pan gesture below calls JS functions through runOnJS, and on
// a device Reanimated holds every function a worklet calls, and a copy of
// every value it reads, from native code. So the pan is built once and
// captures no app data: the function reads the sections and the callback
// captures no app data: the functions read the sections and the callbacks
// through refs, and the worklets read only the dots' positions, from a
// shared value. Rebuilt on each render, it kept each render of the app's
// screen alive through the sections and callbacks it closed over.
Expand All @@ -186,12 +194,19 @@ const FastScrollSectionDotsBar = React.forwardRef(
const onSectionChangeRef = useRef(onSectionChange);
onSectionChangeRef.current = onSectionChange;

const onFingerDownRef = useRef(onFingerDown);
onFingerDownRef.current = onFingerDown;
const onFingerUpRef = useRef(onFingerUp);
onFingerUpRef.current = onFingerUp;

const onThumbPositionChange = useCallback((positionY: number) => {
const section = findSectionOnPosition(positionY, sectionsRef.current);
if (section) {
onSectionChangeRef.current(section);
}
}, []);
const fingerDown = useCallback(() => onFingerDownRef.current(), []);
const fingerUp = useCallback(() => onFingerUpRef.current(), []);

const dotPositions = useMemo(
() => sections.map(({ sectionIndex, y }) => ({ sectionIndex, y })),
Expand Down Expand Up @@ -227,6 +242,7 @@ const FastScrollSectionDotsBar = React.forwardRef(
// when the user start a gesture we avoid noise coming from the list
enableExternalScrollEvents.value = false;
scale.value = thumbScaleOnPress;
runOnJS(fingerDown)();
followFinger(event.y);
})
// The finger has moved far enough to activate the pan, then moves on
Expand All @@ -250,6 +266,7 @@ const FastScrollSectionDotsBar = React.forwardRef(
scale.value = 1;

runOnJS(onThumbPositionChange)(pointStylePositionY.value);
runOnJS(fingerUp)();
})
);
}, [
Expand All @@ -259,6 +276,8 @@ const FastScrollSectionDotsBar = React.forwardRef(
dotPositionsValue,
dotSize,
enableExternalScrollEvents,
fingerDown,
fingerUp,
onThumbPositionChange,
pointStylePositionY,
scale,
Expand Down
27 changes: 26 additions & 1 deletion src/FastScrollSectionDots/FastScrollSectionFullList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export type FastScrollSectionFullListHandle = {
* @param index The first index
*/
show: (activeSection: SectionFullData) => void;

/**
* Keeps the list open while `held` (a finger rests on the dots), then
* closes it after the usual delay once released.
*/
hold: (held: boolean) => void;
};

const FastScrollSectionFullList = React.forwardRef(
Expand Down Expand Up @@ -89,10 +95,21 @@ const FastScrollSectionFullList = React.forwardRef(
};
}, [onHide]);

const [startShowActiveIndexVisibleTimer] = useTimer(
const [startHideTimer, clearHideTimer, scheduleHideTimer] = useTimer(
activeIndexVisibleCallbacks
);

// The list closes a while after the last show() or scroll. show() only
// runs while the finger moves on the dots, so a finger resting on them
// holds the list open instead.
const held = useRef(false);
const startShowActiveIndexVisibleTimer = useCallback(() => {
startHideTimer();
if (held.current) {
clearHideTimer();
}
}, [clearHideTimer, startHideTimer]);

const closeAndScrollToIndex = useCallback(
(index: number) => {
setActiveIndexVisible(false);
Expand Down Expand Up @@ -139,6 +156,14 @@ const FastScrollSectionFullList = React.forwardRef(
}, 1000);
}
},
hold(isHeld: boolean) {
held.current = isHeld;
if (isHeld) {
clearHideTimer();
} else {
scheduleHideTimer();
}
},
})
);

Expand Down
20 changes: 20 additions & 0 deletions src/FastScrollSectionDots/__tests__/FastScrollSectionDots.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,26 @@ describe('FastScrollSectionDots', () => {
expect(screen.queryByText('Dairy')).not.toBeOnTheScreen();
});

it('keeps the section list and the dots open while the finger rests on the dots', () => {
const { scrollList } = renderDots({ hideFastScrollIndicatorTimeout: 1000 });
scrollList();

// The finger lands on a dot, moves to another, then stays still.
const finger = touchDown(FINGER_ON.fruits);
finger.moveTo(FINGER_ON.vegetables);
act(() => jest.advanceTimersByTime(10_000));
expect(screen.getByText('Vegetables')).toBeOnTheScreen();

// Once it lifts, the section list closes 3 s later, then the dots.
finger.lift();
act(() => jest.advanceTimersByTime(2999));
expect(screen.getByText('Vegetables')).toBeOnTheScreen();
act(() => jest.advanceTimersByTime(1));
expect(screen.queryByText('Vegetables')).not.toBeOnTheScreen();
act(() => jest.advanceTimersByTime(1000));
expect(isAnythingShown()).toBe(false);
});

it('scrolls the open section list to center the section under the finger, and again a second later', () => {
renderDots({ hideFastScrollIndicatorTimeout: 0 });
drag([FINGER_ON.fruits]);
Expand Down
Loading