Repository navigation
refactor(scroll-view): read the offset from onScrollGeometryChange - #32
Open
romchornyi wants to merge 1 commit into
Open
romchornyi wants to merge 1 commit into
romchornyi wants to merge 1 commit into
Conversation
`ScrollViewWithOnScrollChanged` measured its offset with a zero-size `LocationReader` placed as the first child of the content, inside a per-instance named coordinate space. `onScrollGeometryChange` is available at the iOS 18 / macOS 15 floor and reports the offset itself. - y is the same as before at rest, while scrolled and when pulled past the top, under safe-area insets, `safeAreaPadding`, `ignoresSafeArea`, a collapsing large title and a refresh in progress. - x now is the real offset; it used to be the probe's position, the centre of the content. Right-to-left layouts are handled. - With `contentMargins`, y reads 0 at rest: the origin is measured inside every content inset. - A zero-size first child stays, so content keeps the stack spacing hosts lay out against. - The last value is sent again when the content reappears, as the probe's `onAppear` did. `LocationReader` and `readingLocation` were internal and had no other caller, so they go too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configuration
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Issue being fixed or feature implemented
Closes #25.
ScrollViewWithOnScrollChangedmeasured its scroll offset with a zero-sizeLocationReaderplaced as the first child of the content, inside a per-instance named coordinate space. The reason was thatonScrollGeometryChangewasn't available. It is available at the iOS 18 / macOS 15 floor set by #15. The type is public API, so it stays with the same signature and is reimplemented on the system API.What was done?
onScrollGeometryChange, computed by an internalscrollContentOrigin(of:layoutDirection:). The named coordinate space and the probe are gone.yvalue is unchanged. It is.zeroat rest, negative while scrolled, and positive while pulled past the start.xis now the real offset. Before, it was the probe's position, which was the centre of the content (for example1230= half the content width at rest in a horizontal view). In right-to-left layoutsScrollGeometryreports the horizontal axis differently: at restcontentOffset.xequals the trailing inset, and the leading inset does not move it. The function handles this, and tests cover it.Color.clearstays as the first child. The probe added one default stack spacing above the content, and hosts lay out against it. Removing it would move their content up.onAppeardid the same. On first appearance exactly one value is sent.LocationReader/readingLocationwere internal, and the probe was their only caller, so the file is deleted. Docs and README no longer listreadingLocation.How Has This Been Tested?
swift build,swift test(23 tests, 7 of them new inScrollContentOriginTests) andxcodebuild build-for-testingfor the iOS Simulator are clean.UIWindow, setcontentOffseton the realUIScrollView, and recorded every reported value.yis identical in every case below:safeAreaPadding(.top, 20)ignoresSafeArea().refreshablewhile refreshingxon the branch reads 0 at rest, goes negative towards the end, and positive past the start. That holds in LTR (with a leading or trailing inset) and in RTL (with a start inset, an end inset, orcontentMargins(.horizontal, 20)), and for a vertical RTL view with horizontal margins. The unit tests use the rawScrollGeometryvalues captured in those runs.LocalCurrencyView, readsmin(offset.y, 0)and uses nocontentMargins, so it sees no change.Breaking Changes
No API change: the signature is the same, and the API check covers it. The reported values change in two cases:
x: it is now the scroll offset instead of the content centre. Nothing readsxtoday.contentMargins:yat rest is now0. Before, it was the margin, because the probe sat inside the margin. With safe-area insets andsafeAreaPadding,yat rest was already0, and it still is.Not handled and not measured, both are edge cases:
xupdates only on the next geometry change.Checklist:
🤖 Generated with Claude Code