Skip to content

Commit

Permalink
refactor metrics for ScrollViewEventEmitter (#43532)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #43532

changelog: [internal]

- use designated initializers.
- make ScrollViewMetrics a struct.
- move ScrollViewMetrics inside of ScrollViewEventEmitter.

This is to be more consistent with other event emitters.

Reviewed By: rubennorte

Differential Revision: D54896331

fbshipit-source-id: 01aecd1835b23bdaccff1355d33eb7b4c5ba8d92
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Mar 21, 2024
1 parent 1c52d38 commit c574ca5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,15 @@ - (BOOL)_shouldDisableScrollInteraction
return NO;
}

- (ScrollViewMetrics)_scrollViewMetrics
{
ScrollViewMetrics 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;
- (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,
};

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 @@ -11,7 +11,7 @@ namespace facebook::react {

static jsi::Value scrollViewMetricsPayload(
jsi::Runtime& runtime,
const ScrollViewMetrics& scrollViewMetrics) {
const ScrollViewEventEmitter::Metrics& scrollViewMetrics) {
auto payload = jsi::Object(runtime);

{
Expand Down Expand Up @@ -57,44 +57,43 @@ static jsi::Value scrollViewMetricsPayload(
return payload;
}

void ScrollViewEventEmitter::onScroll(
const ScrollViewMetrics& scrollViewMetrics) const {
void ScrollViewEventEmitter::onScroll(const Metrics& scrollViewMetrics) const {
dispatchUniqueEvent("scroll", [scrollViewMetrics](jsi::Runtime& runtime) {
return scrollViewMetricsPayload(runtime, scrollViewMetrics);
});
}

void ScrollViewEventEmitter::onScrollToTop(
const ScrollViewMetrics& scrollViewMetrics) const {
const Metrics& scrollViewMetrics) const {
dispatchUniqueEvent(
"scrollToTop", [scrollViewMetrics](jsi::Runtime& runtime) {
return scrollViewMetricsPayload(runtime, scrollViewMetrics);
});
}

void ScrollViewEventEmitter::onScrollBeginDrag(
const ScrollViewMetrics& scrollViewMetrics) const {
const Metrics& scrollViewMetrics) const {
dispatchScrollViewEvent("scrollBeginDrag", scrollViewMetrics);
}

void ScrollViewEventEmitter::onScrollEndDrag(
const ScrollViewMetrics& scrollViewMetrics) const {
const Metrics& scrollViewMetrics) const {
dispatchScrollViewEvent("scrollEndDrag", scrollViewMetrics);
}

void ScrollViewEventEmitter::onMomentumScrollBegin(
const ScrollViewMetrics& scrollViewMetrics) const {
const Metrics& scrollViewMetrics) const {
dispatchScrollViewEvent("momentumScrollBegin", scrollViewMetrics);
}

void ScrollViewEventEmitter::onMomentumScrollEnd(
const ScrollViewMetrics& scrollViewMetrics) const {
const Metrics& scrollViewMetrics) const {
dispatchScrollViewEvent("momentumScrollEnd", scrollViewMetrics);
}

void ScrollViewEventEmitter::dispatchScrollViewEvent(
std::string name,
const ScrollViewMetrics& scrollViewMetrics) const {
const Metrics& scrollViewMetrics) const {
dispatchEvent(std::move(name), [scrollViewMetrics](jsi::Runtime& runtime) {
return scrollViewMetricsPayload(runtime, scrollViewMetrics);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@

namespace facebook::react {

class ScrollViewMetrics {
public:
Size contentSize;
Point contentOffset;
EdgeInsets contentInset;
Size containerSize;
Float zoomScale;
};

class ScrollViewEventEmitter : public ViewEventEmitter {
public:
using ViewEventEmitter::ViewEventEmitter;

void onScroll(const ScrollViewMetrics& scrollViewMetrics) const;
void onScrollBeginDrag(const ScrollViewMetrics& scrollViewMetrics) const;
void onScrollEndDrag(const ScrollViewMetrics& scrollViewMetrics) const;
void onMomentumScrollBegin(const ScrollViewMetrics& scrollViewMetrics) const;
void onMomentumScrollEnd(const ScrollViewMetrics& scrollViewMetrics) const;
void onScrollToTop(const ScrollViewMetrics& scrollViewMetrics) const;
struct Metrics {
Size contentSize;
Point contentOffset;
EdgeInsets contentInset;
Size containerSize;
Float zoomScale{};
};

void onScroll(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;

private:
void dispatchScrollViewEvent(
std::string name,
const ScrollViewMetrics& scrollViewMetrics) const;
const Metrics& scrollViewMetrics) const;
};

} // namespace facebook::react

0 comments on commit c574ca5

Please sign in to comment.