Skip to content

Commit

Permalink
[Fabric] Add implementation of ScrollView.scrollto (#11344)
Browse files Browse the repository at this point in the history
* [Fabric] Add implementation of ScrollView.scrollto

* Change files
  • Loading branch information
acoates-ms authored Mar 9, 2023
1 parent 936e715 commit b08df30
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "[Fabric] Add implementation of ScrollView.scrollto",
"packageName": "react-native-windows",
"email": "30809111+acoates-ms@users.noreply.github.com",
"dependentChangeType": "patch"
}
1 change: 1 addition & 0 deletions vnext/Microsoft.ReactNative/CompositionSwitcher.idl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace Microsoft.ReactNative.Composition
void ContentSize(Windows.Foundation.Numerics.Vector2 size);
Windows.Foundation.Numerics.Vector3 ScrollPosition { get; };
void ScrollBy(Windows.Foundation.Numerics.Vector3 offset);
void TryUpdatePosition(Windows.Foundation.Numerics.Vector3 position, Boolean animate);
}

[webhosthidden]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,19 @@ struct CompScrollerVisual : winrt::Microsoft::ReactNative::Composition::implemen
m_interactionTracker.TryUpdatePositionBy(offset);
};

void TryUpdatePosition(winrt::Windows::Foundation::Numerics::float3 const &position, bool animate) noexcept {
if (animate) {
auto compositor = m_visual.Compositor();
auto cubicBezier = compositor.CreateCubicBezierEasingFunction({0.17f, 0.67f}, {1.0f, 1.0f});
auto kfa = compositor.CreateVector3KeyFrameAnimation();
kfa.Duration(std::chrono::seconds{1});
kfa.InsertKeyFrame(1.0f, position, cubicBezier);
m_interactionTracker.TryUpdatePositionWithAnimation(kfa);
} else {
m_interactionTracker.TryUpdatePosition(position);
}
}

private:
void FireScrollPositionChanged(winrt::Windows::Foundation::Numerics::float2 position) {
m_scrollPositionChangedEvent(*this, winrt::make<CompScrollPositionChangedArgs>(position));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,23 @@ bool ScrollViewComponentView::ScrollWheel(facebook::react::Point pt, int32_t del
return false;
}

void ScrollViewComponentView::handleCommand(std::string const &commandName, folly::dynamic const &arg) noexcept {
if (commandName == "scrollTo") {
auto x = arg[0].asDouble();
auto y = arg[1].asDouble();
auto animate = arg[2].asBool();
m_visual.TryUpdatePosition({static_cast<float>(x), static_cast<float>(y), 0.0f}, animate);
} else if (commandName == "flashScrollIndicators") {
// No-op for now
} else if (commandName == "scrollToEnd") {
// No-op for now
} else if (commandName == "zoomToRect") {
// No-op for now
} else {
Super::handleCommand(commandName, arg);
}
}

void ScrollViewComponentView::ensureVisual() noexcept {
if (!m_visual) {
m_visual = m_compContext.CreateSpriteVisual();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct ScrollInteractionTrackerOwner : public winrt::implements<
void prepareForRecycle() noexcept override;
facebook::react::Props::Shared props() noexcept override;

void handleCommand(std::string const &commandName, folly::dynamic const &arg) noexcept override;
facebook::react::Tag hitTest(facebook::react::Point pt, facebook::react::Point &localPt) const noexcept override;
winrt::Microsoft::ReactNative::Composition::IVisual Visual() const noexcept override;

Expand Down

0 comments on commit b08df30

Please # to comment.