-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement native inverted behaviors for ScrollView #8440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1433041
Implement native inverted behaviors for ScrollView
rozele 2e92792
Change files
rozele c7f0aa0
Ensure tail spacer is not used as a scroll anchor
rozele dee7245
Do not emit scroll events when ActualWidth and ActualHeight are invalid
rozele b181ffd
Simplify content anchoring updates and fix ScrollToEnd behavior
rozele fba0d4b
Fix debug overlay for native inversion
rozele a713bd4
yarn format
rozele bf8d65c
Adds unmodified JS files that will be forked
rozele a907580
Changes to VirtualizedList.js to support native inversion
rozele bdd4404
Allow ScrollViewViewChanger to be used in WinUI Fabric
rozele 282156c
yarn lint:fix
rozele File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@react-native-windows-virtualized-list-ae7563e8-178f-4f0d-a565-9e11f32ad2e6.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "Implement native inverted behaviors for ScrollView", | ||
| "packageName": "@react-native-windows/virtualized-list", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } |
7 changes: 7 additions & 0 deletions
7
change/react-native-windows-0e0722f1-9253-4400-b44f-9ac6159a3e61.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "Implement native inverted behaviors for ScrollView", | ||
| "packageName": "react-native-windows", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } |
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
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
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
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
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
117 changes: 117 additions & 0 deletions
117
vnext/Microsoft.ReactNative/Views/Impl/ScrollViewViewChanger.cpp
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "pch.h" | ||
|
|
||
| #include <UI.Xaml.Controls.h> | ||
| #include <winrt/Windows.UI.Xaml.Interop.h> | ||
| #include "ScrollViewUWPImplementation.h" | ||
| #include "ScrollViewViewChanger.h" | ||
| #include "SnapPointManagingContentControl.h" | ||
|
|
||
| namespace winrt { | ||
| using namespace winrt::Windows::UI::Xaml::Interop; | ||
| } | ||
|
|
||
| namespace Microsoft::ReactNative { | ||
|
|
||
| constexpr const double SCROLL_EPSILON = 1.0; | ||
|
|
||
| const winrt::TypeName viewViewManagerTypeName{winrt::hstring{L"ViewViewManager"}, winrt::TypeKind::Metadata}; | ||
|
|
||
| /*static*/ xaml::DependencyProperty ScrollViewViewChanger::CanBeScrollAnchorProperty() { | ||
| static xaml::DependencyProperty s_canBeScrollAnchorProperty = xaml::DependencyProperty::RegisterAttached( | ||
| L"CanBeScrollAnchor", | ||
| winrt::xaml_typename<bool>(), | ||
| viewViewManagerTypeName, | ||
| winrt::PropertyMetadata(winrt::box_value(true))); | ||
|
|
||
| return s_canBeScrollAnchorProperty; | ||
| } | ||
|
|
||
| void ScrollViewViewChanger::Horizontal(bool horizontal) { | ||
| m_horizontal = horizontal; | ||
| } | ||
|
|
||
| void ScrollViewViewChanger::Inverted(bool inverted) { | ||
| m_inverted = inverted; | ||
| } | ||
|
|
||
| void ScrollViewViewChanger::ScrollToEnd(const xaml::Controls::ScrollViewer &scrollViewer, bool animated) { | ||
| if (m_inverted) { | ||
| UpdateScrollAnchoringEnabled(scrollViewer, false); | ||
| } | ||
|
|
||
| if (m_horizontal) { | ||
| m_targetScrollToEndOffset = scrollViewer.ScrollableWidth(); | ||
| scrollViewer.ChangeView(m_targetScrollToEndOffset.value(), nullptr, nullptr, !animated); | ||
| } else { | ||
| m_targetScrollToEndOffset = scrollViewer.ScrollableHeight(); | ||
| scrollViewer.ChangeView(nullptr, m_targetScrollToEndOffset.value(), nullptr, !animated); | ||
| } | ||
| } | ||
|
|
||
| void ScrollViewViewChanger::OnViewChanging( | ||
| const xaml::Controls::ScrollViewer &scrollViewer, | ||
| const xaml::Controls::ScrollViewerViewChangingEventArgs &args) { | ||
| // For non-inverted views, a ScrollViewer.ViewChanging event always emits an `onScroll` event | ||
| if (m_inverted) { | ||
| // Do not update scroll anchoring during ScrollToEnd | ||
| if (!IsScrollToEndActive()) { | ||
| // For inverted views, we need to detect if we're scrolling to or away from the bottom edge to enable or disable | ||
| // view anchoring | ||
| const auto scrollingToEnd = | ||
| IsScrollingToEnd(scrollViewer, args.NextView().HorizontalOffset(), args.NextView().VerticalOffset()); | ||
| UpdateScrollAnchoringEnabled(scrollViewer, !scrollingToEnd); | ||
| } else if (!IsScrollingToEnd(scrollViewer, m_targetScrollToEndOffset.value(), m_targetScrollToEndOffset.value())) { | ||
| // If we were previously in an active ScrollToEnd command, we may need to | ||
| // restart the operation if the content size has changed | ||
| ScrollToEnd(scrollViewer, true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void ScrollViewViewChanger::OnViewChanged( | ||
| const xaml::Controls::ScrollViewer &scrollViewer, | ||
| const xaml::Controls::ScrollViewerViewChangedEventArgs &args) { | ||
| // Stop tracking scroll-to-end once the ScrollView comes to rest | ||
| if (!args.IsIntermediate()) { | ||
| m_targetScrollToEndOffset = std::nullopt; | ||
| if (m_inverted) { | ||
| const auto scrolledToEnd = | ||
| IsScrollingToEnd(scrollViewer, scrollViewer.HorizontalOffset(), scrollViewer.VerticalOffset()); | ||
| UpdateScrollAnchoringEnabled(scrollViewer, !scrolledToEnd); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void ScrollViewViewChanger::UpdateScrollAnchoringEnabled( | ||
| const xaml::Controls::ScrollViewer &scrollViewer, | ||
| bool enabled) { | ||
| if (m_wasScrollAnchoringEnabled != enabled) { | ||
| m_wasScrollAnchoringEnabled = enabled; | ||
| ScrollViewUWPImplementation(scrollViewer).ContentAnchoringEnabled(enabled); | ||
| const auto snapPointManager = scrollViewer.Content().as<SnapPointManagingContentControl>(); | ||
| auto panel = snapPointManager->Content().as<xaml::Controls::Panel>(); | ||
| for (auto child : panel.Children()) { | ||
| const auto childElement = child.as<xaml::UIElement>(); | ||
| if (winrt::unbox_value<bool>(childElement.GetValue(CanBeScrollAnchorProperty()))) { | ||
| if (enabled) { | ||
| childElement.CanBeScrollAnchor(true); | ||
| } else { | ||
| childElement.ClearValue(xaml::UIElement::CanBeScrollAnchorProperty()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool ScrollViewViewChanger::IsScrollingToEnd( | ||
| const xaml::Controls::ScrollViewer &scrollViewer, | ||
| double horizontalOffset, | ||
| double verticalOffset) { | ||
| return m_horizontal ? horizontalOffset > (scrollViewer.ScrollableWidth() - SCROLL_EPSILON) | ||
| : verticalOffset > (scrollViewer.ScrollableHeight() - SCROLL_EPSILON); | ||
| } | ||
|
|
||
| } // namespace Microsoft::ReactNative | ||
39 changes: 39 additions & 0 deletions
39
vnext/Microsoft.ReactNative/Views/Impl/ScrollViewViewChanger.h
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace Microsoft::ReactNative { | ||
|
|
||
| class ScrollViewViewChanger { | ||
| public: | ||
| static xaml::DependencyProperty CanBeScrollAnchorProperty(); | ||
|
|
||
| void Horizontal(bool horizontal); | ||
| void Inverted(bool inverted); | ||
|
|
||
| void ScrollToEnd(const xaml::Controls::ScrollViewer &scrollViewer, bool animated); | ||
|
|
||
| void OnViewChanging( | ||
| const xaml::Controls::ScrollViewer &scrollViewer, | ||
| const xaml::Controls::ScrollViewerViewChangingEventArgs &args); | ||
| void OnViewChanged( | ||
| const xaml::Controls::ScrollViewer &scrollViewer, | ||
| const xaml::Controls::ScrollViewerViewChangedEventArgs &args); | ||
|
|
||
| private: | ||
| bool m_inverted{false}; | ||
| bool m_horizontal{false}; | ||
| bool m_wasScrollAnchoringEnabled{false}; | ||
| std::optional<double> m_targetScrollToEndOffset{std::nullopt}; | ||
|
|
||
| inline bool IsScrollToEndActive() { | ||
| return m_targetScrollToEndOffset.has_value(); | ||
| } | ||
|
|
||
| void UpdateScrollAnchoringEnabled(const xaml::Controls::ScrollViewer &scrollViewer, bool enabled); | ||
| bool | ||
| IsScrollingToEnd(const xaml::Controls::ScrollViewer &scrollViewer, double horizontalOffset, double verticalOffset); | ||
| }; | ||
|
|
||
| } // namespace Microsoft::ReactNative |
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to better understand this logic, and why we do not anchor when scrolling to the end. Is it to special case allowing new content to show up?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we call the React ScrollView.scrollToEnd command, we presumably don't want anything to interrupt the scroll operation (except perhaps the user, but that's non-trivial to detect). This worked around a specific bug we saw where calling
scrollToEndon the ScrollView would land at the second item in the list, rather than the first item in the list when the first item mounted while scrolling. Typically with VirtualizedList, when a new item is mounted at the head of the list data, one item will be unmounted from the tail of the list data, and I think that causes a view port shift when anchoring is enabled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And actually, it wasn't just for the imperative
ScrollView.scrollToEndcommand, I believe bottom edge anchoring was broken unless we disabled content anchoring as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the same bug happen for offset based scroll to index?
Seems like we kind of conceptually want a batch of "scrolling to target" to "target reached and list settled" where we do not want to anchor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my original implementation, I inverted the coordinate space entirely on the native side. So
scrollTocommands would be inverted and if the scroll operation was interrupted by layout, it would "adjust" for the new offset. This worked really well, but was completely incompatible with virtualization because while the ScrollVieweronScrollevent andscrollTocommand could be easily inverted, list virtualization relies too heavily on theonLayoutevent, which we really cannot invert.We could add a
scrollToInvertedcommand that brought back this functionality so you could effectively scroll to any position in a layout dependent way and thisscollToEndfunctionality would be replaced byscrollToInverted(0).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should fix the
scrollToIndex/scrollToOffsetfunctionality in this PR though. As it is,scrollToIndexis known to be unreliable by default (without usinggetItemLayout)