From 5bf58e8ea13c41dd3ab995ddc242ca05ad604504 Mon Sep 17 00:00:00 2001 From: Andy Himberger Date: Thu, 30 May 2019 19:10:15 -0700 Subject: [PATCH] ScrollContentView and ScrollView cleanup (#2545) * add ScrollContentViewManager, which is just a but removes forking from react-native * cleanup in ScrollViewManager --- vnext/ReactUWP/Base/UwpReactInstance.cpp | 2 + vnext/ReactUWP/ReactUWP.vcxproj | 2 + vnext/ReactUWP/ReactUWP.vcxproj.filters | 12 ++-- .../Impl/ScrollViewUWPImplementation.cpp | 51 ++------------ .../Views/Impl/ScrollViewUWPImplementation.h | 13 +--- .../Impl/SnapPointManagingContentControl.cpp | 5 ++ .../Impl/SnapPointManagingContentControl.h | 6 +- .../Views/ScrollContentViewManager.cpp | 20 ++++++ .../ReactUWP/Views/ScrollContentViewManager.h | 19 ++++++ vnext/ReactUWP/Views/ScrollViewManager.cpp | 66 +++++++------------ 10 files changed, 90 insertions(+), 106 deletions(-) create mode 100644 vnext/ReactUWP/Views/ScrollContentViewManager.cpp create mode 100644 vnext/ReactUWP/Views/ScrollContentViewManager.h diff --git a/vnext/ReactUWP/Base/UwpReactInstance.cpp b/vnext/ReactUWP/Base/UwpReactInstance.cpp index 3dcf7a4e540..a148c067d45 100644 --- a/vnext/ReactUWP/Base/UwpReactInstance.cpp +++ b/vnext/ReactUWP/Base/UwpReactInstance.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -93,6 +94,7 @@ REACTWINDOWS_API_(std::shared_ptr) CreateUIManager( viewManagers.push_back(std::make_unique(instance)); viewManagers.push_back(std::make_unique(instance)); viewManagers.push_back(std::make_unique(instance)); + viewManagers.push_back(std::make_unique(instance)); viewManagers.push_back(std::make_unique(instance)); viewManagers.push_back(std::make_unique(instance)); viewManagers.push_back(std::make_unique(instance)); diff --git a/vnext/ReactUWP/ReactUWP.vcxproj b/vnext/ReactUWP/ReactUWP.vcxproj index dd0af6bea9c..05ff6ac374a 100644 --- a/vnext/ReactUWP/ReactUWP.vcxproj +++ b/vnext/ReactUWP/ReactUWP.vcxproj @@ -202,6 +202,7 @@ + @@ -276,6 +277,7 @@ + diff --git a/vnext/ReactUWP/ReactUWP.vcxproj.filters b/vnext/ReactUWP/ReactUWP.vcxproj.filters index 0e3c6d17014..9e4eaba7e32 100644 --- a/vnext/ReactUWP/ReactUWP.vcxproj.filters +++ b/vnext/ReactUWP/ReactUWP.vcxproj.filters @@ -209,12 +209,12 @@ Views - Views\Impl - - Views\Impl + + + Views @@ -425,12 +425,12 @@ Views - Views\Impl - - Views\Impl + + + Views diff --git a/vnext/ReactUWP/Views/Impl/ScrollViewUWPImplementation.cpp b/vnext/ReactUWP/Views/Impl/ScrollViewUWPImplementation.cpp index c5169c17059..ba39b53846b 100644 --- a/vnext/ReactUWP/Views/Impl/ScrollViewUWPImplementation.cpp +++ b/vnext/ReactUWP/Views/Impl/ScrollViewUWPImplementation.cpp @@ -12,41 +12,10 @@ ScrollViewUWPImplementation::ScrollViewUWPImplementation(const winrt::ScrollView assert(scrollViewContent); const auto snapPointManager = scrollViewContent.as(); assert(snapPointManager); - assert(snapPointManager->Content().as()); m_scrollViewer = winrt::make_weak(scrollViewer); } -void ScrollViewUWPImplementation::ConvertScrollViewer(const winrt::ScrollViewer& scrollViewer) -{ - if (scrollViewer) - { - const auto snapPointManager = new SnapPointManagingContentControl(); - snapPointManager->Content(winrt::StackPanel{}); - - scrollViewer.Content(*snapPointManager); - } -} - -void ScrollViewUWPImplementation::AddView(const XamlView& child, uint32_t index) -{ - const auto contentCollection = ScrollViewerContent().Children(); - assert(index <= contentCollection.Size()); - contentCollection.InsertAt(index, child.as()); -} - -void ScrollViewUWPImplementation::RemoveAllChildren() -{ - ScrollViewerContent().Children().Clear(); -} - -void ScrollViewUWPImplementation::RemoveChildAt(uint32_t index) -{ - const auto contentCollection = ScrollViewerContent().Children(); - assert(index < contentCollection.Size()); - contentCollection.RemoveAt(index); -} - void ScrollViewUWPImplementation::SetHorizontal(bool horizontal) { ScrollViewerSnapPointManager()->SetHorizontal(horizontal); @@ -62,7 +31,7 @@ void ScrollViewUWPImplementation::SnapToOffsets(const winrt::IVectorView& ScrollViewerSnapPointManager()->SnapToOffsets(offsets); } -void ScrollViewUWPImplementation::UpdateScrollableSize() +void ScrollViewUWPImplementation::UpdateScrollableSize() const { if (const auto scrollViewer = m_scrollViewer.get()) { @@ -94,16 +63,16 @@ void ScrollViewUWPImplementation::UpdateScrollableSize() } } -winrt::ScrollViewer ScrollViewUWPImplementation::ScrollViewer() +winrt::ScrollViewer ScrollViewUWPImplementation::ScrollViewer() const { return m_scrollViewer.get(); } // privates // -winrt::com_ptrScrollViewUWPImplementation::ScrollViewerSnapPointManager() +winrt::com_ptr ScrollViewUWPImplementation::ScrollViewerSnapPointManager() const { - if (const auto scrollViewer = m_scrollViewer.get()) + if (const auto scrollViewer = ScrollViewer()) { if (const auto content = scrollViewer.Content()) { @@ -113,16 +82,4 @@ winrt::com_ptrScrollViewUWPImplementation::Scro return nullptr; } -winrt::StackPanel ScrollViewUWPImplementation::ScrollViewerContent() -{ - if (const auto snapPointManagingContentControl = ScrollViewerSnapPointManager()) - { - if (const auto content = snapPointManagingContentControl->Content()) - { - return content.as(); - } - } - return nullptr; -} - } } diff --git a/vnext/ReactUWP/Views/Impl/ScrollViewUWPImplementation.h b/vnext/ReactUWP/Views/Impl/ScrollViewUWPImplementation.h index 2b090c8bec0..d841a28a3a7 100644 --- a/vnext/ReactUWP/Views/Impl/ScrollViewUWPImplementation.h +++ b/vnext/ReactUWP/Views/Impl/ScrollViewUWPImplementation.h @@ -28,24 +28,17 @@ class ScrollViewUWPImplementation { public: ScrollViewUWPImplementation(const winrt::ScrollViewer& scrollViewer); - static void ConvertScrollViewer(const winrt::ScrollViewer& scrollViewer); - - void AddView(const XamlView& child, uint32_t index); - void RemoveAllChildren(); - void RemoveChildAt(uint32_t index); void SetHorizontal(bool isHorizontal); void SnapToInterval(float interval); void SnapToOffsets(const winrt::IVectorView& offsets); - void UpdateScrollableSize(); + void UpdateScrollableSize() const; - winrt::ScrollViewer ScrollViewer(); - winrt::com_ptr ScrollViewerSnapPointManager(); + winrt::ScrollViewer ScrollViewer() const; + winrt::com_ptr ScrollViewerSnapPointManager() const; private: - winrt::StackPanel ScrollViewerContent(); - winrt::weak_ref m_scrollViewer{}; }; diff --git a/vnext/ReactUWP/Views/Impl/SnapPointManagingContentControl.cpp b/vnext/ReactUWP/Views/Impl/SnapPointManagingContentControl.cpp index 3515264177c..560864a68ea 100644 --- a/vnext/ReactUWP/Views/Impl/SnapPointManagingContentControl.cpp +++ b/vnext/ReactUWP/Views/Impl/SnapPointManagingContentControl.cpp @@ -10,6 +10,11 @@ SnapPointManagingContentControl::SnapPointManagingContentControl() } +/*static*/ winrt::com_ptr SnapPointManagingContentControl::Create() +{ + return winrt::make_self(); +} + void SnapPointManagingContentControl::SnapToInterval(float interval) { if (interval != m_interval) diff --git a/vnext/ReactUWP/Views/Impl/SnapPointManagingContentControl.h b/vnext/ReactUWP/Views/Impl/SnapPointManagingContentControl.h index bd4308d4a3e..4f3aa7f4867 100644 --- a/vnext/ReactUWP/Views/Impl/SnapPointManagingContentControl.h +++ b/vnext/ReactUWP/Views/Impl/SnapPointManagingContentControl.h @@ -15,9 +15,13 @@ namespace uwp { class SnapPointManagingContentControl : public winrt::ContentControlT { - public: + private: SnapPointManagingContentControl(); + public: + static winrt::com_ptr Create(); + template friend auto winrt::make_self(Args&& ... args); + // ScrollView Implementation void SnapToInterval(float interval); void SnapToOffsets(const winrt::IVectorView& offsets); diff --git a/vnext/ReactUWP/Views/ScrollContentViewManager.cpp b/vnext/ReactUWP/Views/ScrollContentViewManager.cpp new file mode 100644 index 00000000000..26e5c94a5b7 --- /dev/null +++ b/vnext/ReactUWP/Views/ScrollContentViewManager.cpp @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#include "pch.h" + +#include "ScrollContentViewManager.h" + +namespace react { namespace uwp { + +ScrollContentViewManager::ScrollContentViewManager(const std::shared_ptr& reactInstance) + : Super(reactInstance) +{ +} + +const char* ScrollContentViewManager::GetName() const +{ + return "RCTScrollContentView"; +} + +}} diff --git a/vnext/ReactUWP/Views/ScrollContentViewManager.h b/vnext/ReactUWP/Views/ScrollContentViewManager.h new file mode 100644 index 00000000000..b3ebbaae339 --- /dev/null +++ b/vnext/ReactUWP/Views/ScrollContentViewManager.h @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once + +#include + +namespace react { namespace uwp { + +class ScrollContentViewManager : public ViewViewManager +{ + using Super = ViewViewManager; +public: + ScrollContentViewManager(const std::shared_ptr& reactInstance); + + const char* GetName() const override; +}; + +} } diff --git a/vnext/ReactUWP/Views/ScrollViewManager.cpp b/vnext/ReactUWP/Views/ScrollViewManager.cpp index 17a523e8bc1..818007f4f33 100644 --- a/vnext/ReactUWP/Views/ScrollViewManager.cpp +++ b/vnext/ReactUWP/Views/ScrollViewManager.cpp @@ -83,14 +83,15 @@ void ScrollViewShadowNode::createView() Super::createView(); const auto scrollViewer = GetView().as(); - const auto scrollViewUWPImplementation = new ScrollViewUWPImplementation(scrollViewer); + const auto scrollViewUWPImplementation = ScrollViewUWPImplementation(scrollViewer); + scrollViewUWPImplementation.ScrollViewerSnapPointManager(); AddHandlers(scrollViewer); m_scrollViewerSizeChangedRevoker = scrollViewer.SizeChanged(winrt::auto_revoke, [scrollViewUWPImplementation](const auto&, const auto&) { - scrollViewUWPImplementation->UpdateScrollableSize(); + scrollViewUWPImplementation.UpdateScrollableSize(); }); m_scrollViewerViewChangedRevoker = scrollViewer.ViewChanged(winrt::auto_revoke, @@ -101,14 +102,14 @@ void ScrollViewShadowNode::createView() if (m_zoomFactor != zoomFactor) { m_zoomFactor = zoomFactor; - scrollViewUWPImplementation->UpdateScrollableSize(); + scrollViewUWPImplementation.UpdateScrollableSize(); } }); - m_contentSizeChangedRevoker = scrollViewUWPImplementation->ScrollViewerSnapPointManager()->SizeChanged(winrt::auto_revoke, + m_contentSizeChangedRevoker = scrollViewUWPImplementation.ScrollViewerSnapPointManager()->SizeChanged(winrt::auto_revoke, [this, scrollViewUWPImplementation](const auto&, const auto&) { - scrollViewUWPImplementation->UpdateScrollableSize(); + scrollViewUWPImplementation.UpdateScrollableSize(); }); } @@ -472,59 +473,40 @@ folly::dynamic ScrollViewManager::GetExportedCustomDirectEventTypeConstants() co XamlView ScrollViewManager::CreateViewCore(int64_t tag) { - const auto scrollViewer = [this]() - { - const auto scrollViewer = winrt::ScrollViewer{}; + const auto scrollViewer = winrt::ScrollViewer{}; - scrollViewer.HorizontalScrollBarVisibility(winrt::ScrollBarVisibility::Visible); - scrollViewer.VerticalScrollBarVisibility(winrt::ScrollBarVisibility::Visible); - scrollViewer.VerticalSnapPointsAlignment(winrt::SnapPointsAlignment::Near); - scrollViewer.VerticalSnapPointsType(winrt::SnapPointsType::Mandatory); - scrollViewer.HorizontalSnapPointsType(winrt::SnapPointsType::Mandatory); + scrollViewer.HorizontalScrollBarVisibility(winrt::ScrollBarVisibility::Visible); + scrollViewer.VerticalScrollBarVisibility(winrt::ScrollBarVisibility::Visible); + scrollViewer.VerticalSnapPointsAlignment(winrt::SnapPointsAlignment::Near); + scrollViewer.VerticalSnapPointsType(winrt::SnapPointsType::Mandatory); + scrollViewer.HorizontalSnapPointsType(winrt::SnapPointsType::Mandatory); - return scrollViewer; - }(); + const auto snapPointManager = SnapPointManagingContentControl::Create(); + scrollViewer.Content(*snapPointManager); - const auto scrollViewUWPImplementation = [scrollViewer]() - { - ScrollViewUWPImplementation::ConvertScrollViewer(scrollViewer); - return new ScrollViewUWPImplementation(scrollViewer); - }(); - return scrollViewer; } void ScrollViewManager::AddView(XamlView parent, XamlView child, int64_t index) { - if (parent && child && index >= 0) - { - if (const auto scrollViewer = parent.as()) - { - ScrollViewUWPImplementation(scrollViewer).AddView(child, static_cast(index)); - } - } + assert(index == 0); + + auto scrollViewer = parent.as(); + auto snapPointManager = scrollViewer.Content().as(); + snapPointManager->Content(child); } void ScrollViewManager::RemoveAllChildren(XamlView parent) { - if (parent) - { - if (const auto scrollViewer = parent.as()) - { - ScrollViewUWPImplementation(scrollViewer).RemoveAllChildren(); - } - } + auto scrollViewer = parent.as(); + auto snapPointManager = scrollViewer.Content().as(); + snapPointManager->Content(nullptr); } void ScrollViewManager::RemoveChildAt(XamlView parent, int64_t index) { - if (parent && index >= 0) - { - if (const auto scrollViewer = parent.as()) - { - ScrollViewUWPImplementation(scrollViewer).RemoveChildAt(static_cast(index)); - } - } + assert(index == 0); + RemoveAllChildren(parent); } void ScrollViewManager::SnapToInterval(XamlView parent, float interval)