Skip to content

Commit

Permalink
ScrollEvent 2/n - use ScrollEvent over ScrollViewEventEmitter::Metrics (
Browse files Browse the repository at this point in the history
facebook#45899)

Summary:
Pull Request resolved: facebook#45899

Changelog: [internal]

replace ScrollViewEventEmitter::Metrics for ScrollEvent payload type created earlier
make ScrollViewEventEmitter::Metrics an alias of ScrollEvent as well

Reviewed By: christophpurrer

Differential Revision: D60767390

fbshipit-source-id: 8db88c0e1fa837b5dbad92d7bcce825882feaaf6
  • Loading branch information
zeyap authored and facebook-github-bot committed Aug 6, 2024
1 parent e7caad2 commit b0b898b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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>(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>(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>(scrollEvent));
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <memory>

#include <folly/dynamic.h>
#include <react/renderer/components/scrollview/ScrollEvent.h>
#include <react/renderer/components/view/ViewEventEmitter.h>
#include <react/renderer/core/EventEmitter.h>

Expand All @@ -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

0 comments on commit b0b898b

Please sign in to comment.