Tolerate slight touch drift when selecting book covers (#8135)

* fix(library): tolerate small touch drift

A deliberate hold on a book cover can be canceled by slight finger
movement. Allow more movement for shelf touch holds while preserving
the hold time, tap threshold, and drag behavior.

On the production web build, a 12 CSS px drift went from zero of three
selections to three of three. Swipes still scrolled and taps still
opened the book. The related regressions fail on main and pass with
this change.

Co-Authored-By: Codex (GPT-6.2 Sol) <noreply@openai.com>

* docs(library): say where the touch drift tolerance takes effect

The 16 px hold tolerance only matters where the browser waits more
than 10 px before it starts a pan, because the pan cancels the
pointer. Say so next to the constant, so nobody expects it to change
anything on Android phones.

Comment only, so there is no new test. Checked the behavior it
describes:
- dev-web, Chromium with CDP touch input (desktop slop): main selected
  a cover on neither a 12 px sideways nor a 14 px downward hold, and
  this PR selected on both. A 20 px drift, a tap and a 96 px swipe
  behaved the same on both.
- A probe page in desktop Chromium got pointercancel at 16 px.
  Chrome on an Android 15 emulator cancelled a 12 px drift at 8.1 px,
  before the 500 ms hold (one run).
- Related unit tests (21 files), the three bookshelf browser suites,
  lint and format pass. iOS not checked.

Co-Authored-By: Claude Code (Claude Opus 5.5) <noreply@anthropic.com>

* fix(library): drop the unrendered RecentShelf change

RecentShelf has not been rendered since the configurable bookshelves
(#6329), as #8134 found: the Recently read shelf draws its covers with
BookshelfItem, so the BookshelfItem change already covers it. Drop the
RecentShelf change and its two tests.

Move the drift-and-return test to BookshelfItem, next to a plain tap
that opens the book. With the tapCancelled check removed from
useLongPress it fails: expected "vi.fn()" to be called +0 times, but
got 1 times.

Co-Authored-By: Claude Code (Claude Opus 5.5) <noreply@anthropic.com>

---------

Co-authored-by: Codex (GPT-6.2 Sol) <noreply@openai.com>
Co-authored-by: Huang Xin <chrox.huang@gmail.com>
Co-authored-by: Claude Code (Claude Opus 5.5) <noreply@anthropic.com>
This commit is contained in:
Shigenobu Nishikawa 2026-12-01 11:48:41 +09:00 • committed by GitHub
parent bb2cc53961
commit 4d8a163a11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 78 additions and 3 deletions

View file

@ -125,6 +125,61 @@ describe('long-press selection on Android (issue #5596)', () => {
cleanup();
});
it('keeps a touch long press through a small drift', () => {
const onToggle = vi.fn();
render(<Harness onToggle={onToggle} />);
const item = getItem();
const start = { pointerId: 1, clientX: 50, clientY: 50, pointerType: 'touch' };
const drift = { ...start, clientX: 62 };
fireEvent.pointerDown(item, start);
fireEvent.pointerMove(item, drift);
act(() => void vi.advanceTimersByTime(499));
expect(onToggle).not.toHaveBeenCalled();
act(() => void vi.advanceTimersByTime(1));
expect(onToggle).toHaveBeenCalledExactlyOnceWith('hash-1');
fireEvent.pointerUp(item, drift);
expect(onToggle).toHaveBeenCalledTimes(1);
});
it.each([
{ name: 'opens a book on a touch tap', path: [] as number[], opens: true },
{
name: 'does not open a book when a touch drifts out of the tap range and returns',
path: [62, 50],
opens: false,
},
])('$name', ({ path, opens }) => {
render(<Harness onToggle={vi.fn()} />);
const item = getItem();
const start = { pointerId: 1, clientX: 50, clientY: 50, pointerType: 'touch' };
fireEvent.pointerDown(item, start);
for (const clientX of path) fireEvent.pointerMove(item, { ...start, clientX });
fireEvent.pointerUp(item, start);
expect(openBook).toHaveBeenCalledTimes(opens ? 1 : 0);
});
it.each([
{ pointerType: 'touch', clientX: 67 },
{ pointerType: 'mouse', clientX: 62 },
])('cancels a $pointerType long press outside its movement tolerance', (movement) => {
const onToggle = vi.fn();
render(<Harness onToggle={onToggle} />);
const item = getItem();
const start = { pointerId: 1, clientX: 50, clientY: 50, pointerType: movement.pointerType };
fireEvent.pointerDown(item, start);
fireEvent.pointerMove(item, { ...start, ...movement });
act(() => void vi.advanceTimersByTime(500));
fireEvent.pointerUp(item, start);
expect(onToggle).not.toHaveBeenCalled();
});
it('selects once when the WebView fires contextmenu after the long-press timer', () => {
const onToggle = vi.fn();
render(<Harness onToggle={onToggle} />);

View file

@ -20,6 +20,7 @@ import { FILE_REVEAL_LABELS, FILE_REVEAL_PLATFORMS } from '@/utils/os';
import { Book, BooksGroup, ReadingStatus } from '@/types/book';
import {
getBookContextMenuItemIds,
LIBRARY_TOUCH_LONG_PRESS_MOVE_THRESHOLD,
type BookContextMenuItemId,
} from '@/app/library/utils/libraryUtils';
import { isTauriAppPlatform } from '@/services/environment';
@ -428,6 +429,9 @@ const BookshelfItem: React.FC<BookshelfItemProps> = ({
const { pressing, handlers } = useLongPress(
{
touchLongPressMoveThreshold: dragEnabled
? undefined
: LIBRARY_TOUCH_LONG_PRESS_MOVE_THRESHOLD,
onLongPress: () => {
if (!dragEnabled) handleSelectItem();
},

View file

@ -40,6 +40,13 @@ export const bookshelfStreamKeys = (search: string) => {
return { scrollKey: params.toString(), navigationKey: navigation.toString() };
};
// How far a touch may drift during a hold on a cover and still select it.
// The browser cancels the pointer once it starts a pan, so this only takes
// effect where the platform's touch slop is larger than the 10 px tap limit:
// desktop Chromium starts a pan past 15 px, while Android starts one at about
// 8 px (its 8dp touch slop), so Android phones keep today's behavior.
export const LIBRARY_TOUCH_LONG_PRESS_MOVE_THRESHOLD = 16;
/** Valid sort types for the library */
const VALID_SORT_TYPES: LibrarySortByType[] = Object.values(LibrarySortByType);

View file

@ -8,6 +8,7 @@ interface UseLongPressOptions {
suppressTouchContextMenu?: boolean;
threshold?: number;
moveThreshold?: number;
touchLongPressMoveThreshold?: number;
}
interface UseLongPressResult {
@ -32,6 +33,7 @@ export const useLongPress = (
suppressTouchContextMenu = false,
threshold = 500,
moveThreshold = 10,
touchLongPressMoveThreshold = moveThreshold,
}: UseLongPressOptions,
deps: React.DependencyList,
): UseLongPressResult => {
@ -43,6 +45,7 @@ export const useLongPress = (
const hasPointerEventsRef = useRef(false);
const pointerEventTimeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
const isLongPressTriggered = useRef(false);
const tapCancelled = useRef(false);
const reset = useCallback(() => {
setPressing(false);
@ -72,6 +75,7 @@ export const useLongPress = (
pointerId.current = e.pointerId;
startPosRef.current = { x: e.clientX, y: e.clientY };
isLongPressTriggered.current = false;
tapCancelled.current = false;
pressDelayRef.current = setTimeout(() => {
setPressing(true);
@ -95,19 +99,24 @@ export const useLongPress = (
const deltaX = Math.abs(e.clientX - startPosRef.current.x);
const deltaY = Math.abs(e.clientY - startPosRef.current.y);
if (deltaX > moveThreshold || deltaY > moveThreshold) {
const distance = Math.max(deltaX, deltaY);
if (distance > moveThreshold) tapCancelled.current = true;
const longPressMoveThreshold =
e.pointerType === 'touch' ? touchLongPressMoveThreshold : moveThreshold;
if (distance > longPressMoveThreshold) {
onCancel?.();
reset();
}
},
[moveThreshold, onCancel, reset],
[moveThreshold, touchLongPressMoveThreshold, onCancel, reset],
);
const handlePointerUp = useCallback(
(e: React.PointerEvent) => {
if (e.pointerId !== pointerId.current) return;
if (!isLongPressTriggered.current && startPosRef.current) {
if (!isLongPressTriggered.current && !tapCancelled.current && startPosRef.current) {
const deltaX = Math.abs(e.clientX - startPosRef.current.x);
const deltaY = Math.abs(e.clientY - startPosRef.current.y);