Repository navigation
fix(mobile): claim the preview's pinch on the first finger and anchor it on the picture - #728
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
other gesture to protect, and a press that does not travel is only read as a
double-tap candidate.
(
locationX/locationY), recomputed from each event, so no origin can go stale.The
measureInWindowplumbing (and the ref it needed) goes away with it; thepinch focal point, pan and double-tap anchor all use the same relative space.
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
zoomTohelper.state(), because a double-tap's result is delivered by aspring and the animated value is not observable in tests.
onLayoutno longer measures throughevent.currentTarget, which is not ameasurable instance on every renderer (on web it threw on every open).
Separately,
MarkdownImage's load callback no longer assumesnativeEvent.sourceexists: react-native-web reports no intrinsic size, so every inline image threw
inside that callback there.
Verification
mobile/image-zoom): dispatches a two-fingerpinch 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.
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.
tsc,eslint,jest(1704 passed).Note for whoever runs the harness
serve-mobileinstalls its web dependencies into the mobile workspace, whichleaves
mobile/node_moduleswithout@react-native/jest-preset;npm cirestoresthe tree. The troubleshooting table in
docs/guide/screenshots.{md,zh-CN.md}nowalso records the stale-origin symptom, since it presents as a blank capture rather
than an error.