Skip to content

fix(mobile): claim the preview's pinch on the first finger and anchor it on the picture - #728

Merged
huichen merged 3 commits into
mainfrom
fix/mobile-pinch-zoom
Sep 20, 2026
Merged

huichen merged 3 commits into
mainfrom
fix/mobile-pinch-zoom

Conversation

@huichen

@huichen huichen commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

Why

The image preview's pinch-to-zoom did nothing usable on a device. The gesture was
only claimed once a second finger negotiated the responder, so ownership
depended on how the surrounding container handled the initial touch, and #663's own
note recorded that native delivery had never been validated.

Then, verifying it against the real frontend in the harness, the pinch turned out
to be actively broken even when it did fire: the zoom resolved every focal point
against the view's window origin, which is measured once per layout. The preview is
a sheet that slides in — it lays out while it is still below the viewport, so the
captured origin was 904 while the surface's top was 60, and nothing re-measures
afterwards. Every pinch then translated the picture by offset × (scale − 1),
most of a screen, which pushed it out of view. The harness capture of a pinch was a
uniform, empty frame, and on a phone it reads as "zoom does nothing".

What changed

  • Claim the surface on the first finger (capture phase): the picture has no
    other gesture to protect, and a press that does not travel is only read as a
    double-tap candidate.
  • Anchor on the touches' coordinates relative to the surface
    (locationX/locationY), recomputed from each event, so no origin can go stale.
    The measureInWindow plumbing (and the ref it needed) goes away with it; the
    pinch focal point, pan and double-tap anchor all use the same relative space.
  • Double-tap zoom (one finger in, one finger back to fit, anchored on the tap).
    Pinch is not available to every user or on every device, so the surface does not
    depend on it alone. The focal math is shared through one zoomTo helper.
  • The controller exposes state(), because a double-tap's result is delivered by a
    spring and the animated value is not observable in tests.
  • onLayout no longer measures through event.currentTarget, which is not a
    measurable instance on every renderer (on web it threw on every open).

Separately, MarkdownImage's load callback no longer assumes nativeEvent.source
exists: react-native-web reports no intrinsic size, so every inline image threw
inside that callback there.

Verification

  • Harness, with real touch events (mobile/image-zoom): dispatches a two-finger
    pinch and a double-tap and asserts what RN applied — the scale moves to ~3.3 and
    a centred pinch leaves the translate at 0 (that assertion fails on the
    previous behaviour), then a double-tap returns to fit. The earlier version of
    this scenario only checked the scale, which is exactly why a blank capture slipped
    through: the picture was scaled and thrown off screen.
  • Unit tests (11) cover the native contract: focal anchoring including the
    centred case, bounds, pan clamps, finger transitions, the double-tap window/slop,
    and that a two-finger press is never read as a tap. Six of them fail against the
    previous implementation.
  • Mobile: tsc, eslint, jest (1704 passed).

Note for whoever runs the harness

serve-mobile installs its web dependencies into the mobile workspace, which
leaves mobile/node_modules without @react-native/jest-preset; npm ci restores
the tree. The troubleshooting table in docs/guide/screenshots.{md,zh-CN.md} now
also records the stale-origin symptom, since it presents as a blank capture rather
than an error.

The preview's pinch was reported as doing nothing on a phone. The gesture was
only claimed once a SECOND finger negotiated the responder:

    onStartShouldSetPanResponder: event => event.nativeEvent.touches.length === 2

so ownership depended on how the container handled the initial touch, and #663's
own note said native delivery had never been validated on a device. Claim the
surface on the first finger instead (capture phase): the picture has no other
gesture to protect, and a press that does not travel is only read as a
double-tap candidate. That is also how the established RN zoom components do it.

While in here:

- `onLayout` measured through `event.currentTarget`, which is not a measurable
  instance on every renderer — on the web harness it threw
  "Cannot read properties of undefined (reading 'measureInWindow')" on every
  open, leaving the frame's screen origin at 0 and the zoom anchor wrong.
  Measure through a ref, as RN documents.
- Add double-tap zoom: one finger in, one finger back to fit, anchored on the
  tap. Pinch is not available to every user (assistive settings) and not every
  device manifests it, so the surface must not depend on it alone. The focal
  math is shared with the pinch through one `zoomTo` helper.
- The controller now exposes `state()`, because a double-tap's result is
  delivered by a spring and the animated value is not observable in tests.

Verification: unit tests cover the native contract (anchor, bounds, pan clamps,
finger transitions, double-tap window/slop, and that a two-finger press is never
a tap). The new `mobile/image-zoom` scenario drives the real preview with
dispatched TouchEvents and asserts the scale RN applied, so the capture run
fails if zoom stops working; docs note that a harness capture can prove "the
scale changed" but not the focal offset, because the browser's measureInWindow
is in page space while touches are dispatched in viewport space.
…t a size

`LoadedImage`'s `onLoad` destructured `nativeEvent.source` directly. That size
only exists on renderers that decode the image natively: react-native-web reports
no `source`, so every inline image threw inside the load callback there
("Cannot read properties of undefined (reading 'width')"). It looked harmless
because `onImageLoad` failures do not unmount the picture, but it is a real
uncaught error on that renderer and it made the screenshot harness console
unreadable for genuine problems.

Read the size defensively and keep the existing 1.5 default when it is absent. The
new test fails against the old code and covers the three shapes the callback can
arrive in: a real size, an empty `nativeEvent`, and no event at all.
…red origin

The zoom resolved every focal point against the view's window origin, which is
measured once per layout. A sheet that slides in lays out while it is still below
the viewport: the captured origin was 904 while the surface's real top was 60, and
nothing re-measures afterwards. Every pinch then translated the picture by
`offset × (scale − 1)` — most of a screen — so the zoom dragged the image out of
view instead of growing it under the fingers. The harness showed it plainly: the
pinch state was a uniform, empty frame.

Anchor on the touches' coordinates relative to the surface (`locationX`/
`locationY`), which each event recomputes, so no origin can go stale — and the
`measureInWindow` machinery (and the ref it needed) goes away with it. The pan and
the double-tap anchor use the same relative space.

The scenario now asserts what the user sees: a pinch centred on the picture must
leave the translate near zero (it fails on the previous behaviour), alongside the
scale assertion. Unit tests move to the relative contract and add that centred
case; six of them fail against the previous implementation.

Note for the harness: the mobile workspace's jest tree is unusable right after
`serve-mobile` installs its web dependencies (missing @react-native/jest-preset);
`npm ci` restores it.
@huichen
huichen enabled auto-merge (squash) September 20, 2026 00:47
@huichen
huichen merged commit 35ee137 into main Sep 20, 2026
12 checks passed
@huichen
huichen deleted the fix/mobile-pinch-zoom branch September 20, 2026 00:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant