diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index cc81ad3a45cd34..899d38bc2499b2 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -413,13 +413,12 @@ - (BOOL)_shouldDisableScrollInteraction - (ScrollViewEventEmitter::Metrics)_scrollViewMetrics { - auto metrics = ScrollViewEventEmitter::Metrics{ - .contentSize = RCTSizeFromCGSize(_scrollView.contentSize), - .contentOffset = RCTPointFromCGPoint(_scrollView.contentOffset), - .contentInset = RCTEdgeInsetsFromUIEdgeInsets(_scrollView.contentInset), - .containerSize = RCTSizeFromCGSize(_scrollView.bounds.size), - .zoomScale = _scrollView.zoomScale, - }; + auto metrics = ScrollViewEventEmitter::Metrics{}; + metrics.contentSize = RCTSizeFromCGSize(_scrollView.contentSize); + metrics.contentOffset = RCTPointFromCGPoint(_scrollView.contentOffset); + metrics.contentInset = RCTEdgeInsetsFromUIEdgeInsets(_scrollView.contentInset); + metrics.containerSize = RCTSizeFromCGSize(_scrollView.bounds.size); + metrics.zoomScale = _scrollView.zoomScale; if (_layoutMetrics.layoutDirection == LayoutDirection::RightToLeft) { metrics.contentOffset.x = metrics.contentSize.width - metrics.containerSize.width - metrics.contentOffset.x; diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.cpp b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.cpp index ed04e6b27b45b2..917741a1de08ed 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.cpp @@ -9,104 +9,48 @@ namespace facebook::react { -static jsi::Value scrollViewMetricsPayload( - jsi::Runtime& runtime, - const ScrollViewEventEmitter::Metrics& scrollViewMetrics) { - auto payload = jsi::Object(runtime); - - { - auto contentOffset = jsi::Object(runtime); - contentOffset.setProperty(runtime, "x", scrollViewMetrics.contentOffset.x); - contentOffset.setProperty(runtime, "y", scrollViewMetrics.contentOffset.y); - payload.setProperty(runtime, "contentOffset", contentOffset); - } - - { - auto contentInset = jsi::Object(runtime); - contentInset.setProperty( - runtime, "top", scrollViewMetrics.contentInset.top); - contentInset.setProperty( - runtime, "left", scrollViewMetrics.contentInset.left); - contentInset.setProperty( - runtime, "bottom", scrollViewMetrics.contentInset.bottom); - contentInset.setProperty( - runtime, "right", scrollViewMetrics.contentInset.right); - payload.setProperty(runtime, "contentInset", contentInset); - } - - { - auto contentSize = jsi::Object(runtime); - contentSize.setProperty( - runtime, "width", scrollViewMetrics.contentSize.width); - contentSize.setProperty( - runtime, "height", scrollViewMetrics.contentSize.height); - payload.setProperty(runtime, "contentSize", contentSize); - } - - { - auto containerSize = jsi::Object(runtime); - containerSize.setProperty( - runtime, "width", scrollViewMetrics.containerSize.width); - containerSize.setProperty( - runtime, "height", scrollViewMetrics.containerSize.height); - payload.setProperty(runtime, "layoutMeasurement", containerSize); - } - - payload.setProperty(runtime, "zoomScale", scrollViewMetrics.zoomScale); - - return payload; -} - -void ScrollViewEventEmitter::onScroll(const Metrics& scrollViewMetrics) const { - dispatchUniqueEvent("scroll", [scrollViewMetrics](jsi::Runtime& runtime) { - return scrollViewMetricsPayload(runtime, scrollViewMetrics); - }); +void ScrollViewEventEmitter::onScroll(const ScrollEvent& scrollEvent) const { + dispatchUniqueEvent("scroll", std::make_shared(scrollEvent)); } void ScrollViewEventEmitter::experimental_onDiscreteScroll( - const Metrics& scrollViewMetrics) const { + const ScrollEvent& scrollEvent) const { dispatchEvent( "scroll", - [scrollViewMetrics](jsi::Runtime& runtime) { - return scrollViewMetricsPayload(runtime, scrollViewMetrics); - }, + std::make_shared(scrollEvent), RawEvent::Category::Discrete); } void ScrollViewEventEmitter::onScrollToTop( - const Metrics& scrollViewMetrics) const { + const ScrollEvent& scrollEvent) const { dispatchUniqueEvent( - "scrollToTop", [scrollViewMetrics](jsi::Runtime& runtime) { - return scrollViewMetricsPayload(runtime, scrollViewMetrics); - }); + "scrollToTop", std::make_shared(scrollEvent)); } void ScrollViewEventEmitter::onScrollBeginDrag( - const Metrics& scrollViewMetrics) const { - dispatchScrollViewEvent("scrollBeginDrag", scrollViewMetrics); + const ScrollEvent& scrollEvent) const { + dispatchScrollViewEvent("scrollBeginDrag", scrollEvent); } void ScrollViewEventEmitter::onScrollEndDrag( - const Metrics& scrollViewMetrics) const { - dispatchScrollViewEvent("scrollEndDrag", scrollViewMetrics); + const ScrollEvent& scrollEvent) const { + dispatchScrollViewEvent("scrollEndDrag", scrollEvent); } void ScrollViewEventEmitter::onMomentumScrollBegin( - const Metrics& scrollViewMetrics) const { - dispatchScrollViewEvent("momentumScrollBegin", scrollViewMetrics); + const ScrollEvent& scrollEvent) const { + dispatchScrollViewEvent("momentumScrollBegin", scrollEvent); } void ScrollViewEventEmitter::onMomentumScrollEnd( - const Metrics& scrollViewMetrics) const { - dispatchScrollViewEvent("momentumScrollEnd", scrollViewMetrics); + const ScrollEvent& scrollEvent) const { + dispatchScrollViewEvent("momentumScrollEnd", scrollEvent); } void ScrollViewEventEmitter::dispatchScrollViewEvent( std::string name, - const Metrics& scrollViewMetrics) const { - dispatchEvent(std::move(name), [scrollViewMetrics](jsi::Runtime& runtime) { - return scrollViewMetricsPayload(runtime, scrollViewMetrics); - }); + const ScrollEvent& scrollEvent) const { + dispatchEvent(std::move(name), std::make_shared(scrollEvent)); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.h b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.h index 5a7e16d3240de6..5d0bbb7caf7264 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.h +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.h @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -19,26 +20,19 @@ class ScrollViewEventEmitter : public ViewEventEmitter { public: using ViewEventEmitter::ViewEventEmitter; - struct Metrics { - Size contentSize; - Point contentOffset; - EdgeInsets contentInset; - Size containerSize; - Float zoomScale{}; - }; - - void onScroll(const Metrics& scrollViewMetrics) const; - void experimental_onDiscreteScroll(const Metrics& scrollViewMetrics) const; - void onScrollBeginDrag(const Metrics& scrollViewMetrics) const; - void onScrollEndDrag(const Metrics& scrollViewMetrics) const; - void onMomentumScrollBegin(const Metrics& scrollViewMetrics) const; - void onMomentumScrollEnd(const Metrics& scrollViewMetrics) const; - void onScrollToTop(const Metrics& scrollViewMetrics) const; + using Metrics = ScrollEvent; + + void onScroll(const ScrollEvent& scrollEvent) const; + void experimental_onDiscreteScroll(const ScrollEvent& scrollEvent) const; + void onScrollBeginDrag(const ScrollEvent& scrollEvent) const; + void onScrollEndDrag(const ScrollEvent& scrollEvent) const; + void onMomentumScrollBegin(const ScrollEvent& scrollEvent) const; + void onMomentumScrollEnd(const ScrollEvent& scrollEvent) const; + void onScrollToTop(const ScrollEvent& scrollEvent) const; private: - void dispatchScrollViewEvent( - std::string name, - const Metrics& scrollViewMetrics) const; + void dispatchScrollViewEvent(std::string name, const ScrollEvent& scrollEvent) + const; }; } // namespace facebook::react