ScrollEvent 1/n - Create ScrollEvent payload (#45851)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45851

Create ScrollEvent payload type, so we can unwrap the scroll event metrics if intercept it in C++

Changelog: [internal]

Reviewed By: christophpurrer

Differential Revision: D60526048

fbshipit-source-id: 219a690ccf67d0b1c90e3496b8e5970ab7e2a79b
This commit is contained in:
Zeya Peng
2024-08-05 22:12:26 -07:00
committed by Facebook GitHub Bot
parent ae73fe462e
commit e7caad2ee4
3 changed files with 144 additions and 1 deletions
@@ -0,0 +1,99 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ScrollEvent.h"
namespace facebook::react {
jsi::Value ScrollEvent::asJSIValue(jsi::Runtime& runtime) const {
auto payload = jsi::Object(runtime);
{
auto contentOffsetObj = jsi::Object(runtime);
contentOffsetObj.setProperty(runtime, "x", contentOffset.x);
contentOffsetObj.setProperty(runtime, "y", contentOffset.y);
payload.setProperty(runtime, "contentOffset", contentOffsetObj);
}
{
auto contentInsetObj = jsi::Object(runtime);
contentInsetObj.setProperty(runtime, "top", contentInset.top);
contentInsetObj.setProperty(runtime, "left", contentInset.left);
contentInsetObj.setProperty(runtime, "bottom", contentInset.bottom);
contentInsetObj.setProperty(runtime, "right", contentInset.right);
payload.setProperty(runtime, "contentInset", contentInsetObj);
}
{
auto contentSizeObj = jsi::Object(runtime);
contentSizeObj.setProperty(runtime, "width", contentSize.width);
contentSizeObj.setProperty(runtime, "height", contentSize.height);
payload.setProperty(runtime, "contentSize", contentSizeObj);
}
{
auto containerSizeObj = jsi::Object(runtime);
containerSizeObj.setProperty(runtime, "width", containerSize.width);
containerSizeObj.setProperty(runtime, "height", containerSize.height);
payload.setProperty(runtime, "layoutMeasurement", containerSizeObj);
}
payload.setProperty(runtime, "zoomScale", zoomScale);
return payload;
}
folly::dynamic ScrollEvent::asDynamic() const {
auto contentOffsetObj =
folly::dynamic::object("x", contentOffset.x)("y", contentOffset.y);
auto contentInsetObj = folly::dynamic::object("top", contentInset.top)(
"left", contentInset.left)("bottom", contentInset.bottom)(
"right", contentInset.right);
auto contentSizeObj = folly::dynamic::object("width", contentSize.width)(
"height", contentSize.height);
auto containerSizeObj = folly::dynamic::object("width", containerSize.width)(
"height", containerSize.height);
auto metrics = folly::dynamic::object(
"contentOffset", std::move(contentOffsetObj))(
"contentInset", std::move(contentInsetObj))(
"contentSize", std::move(contentSizeObj))(
"layoutMeasurement", std::move(containerSizeObj))("zoomScale", zoomScale);
return metrics;
};
EventPayloadType ScrollEvent::getType() const {
return EventPayloadType::ScrollEvent;
}
#if RN_DEBUG_STRING_CONVERTIBLE
std::string getDebugName(const ScrollEvent& /*scrollEvent*/) {
return "ScrollEvent";
}
std::vector<DebugStringConvertibleObject> getDebugProps(
const ScrollEvent& scrollEvent,
DebugStringConvertibleOptions options) {
return {
{"contentOffset",
getDebugDescription(scrollEvent.contentOffset, options)},
{"contentInset", getDebugDescription(scrollEvent.contentInset, options)},
{"contentSize", getDebugDescription(scrollEvent.contentSize, options)},
{"layoutMeasurement",
getDebugDescription(scrollEvent.layoutMeasurement, options)},
{"zoomScale", getDebugDescription(scrollEvent.zoomScale, options)},
};
}
#endif
} // namespace facebook::react
@@ -0,0 +1,44 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <folly/dynamic.h>
#include <react/renderer/core/EventPayload.h>
#include <react/renderer/graphics/RectangleEdges.h>
#include <react/renderer/graphics/Size.h>
namespace facebook::react {
struct ScrollEvent : public EventPayload {
Size contentSize;
Point contentOffset;
EdgeInsets contentInset;
Size containerSize;
Float zoomScale{};
ScrollEvent() = default;
folly::dynamic asDynamic() const;
/*
* EventPayload implementations
*/
jsi::Value asJSIValue(jsi::Runtime& runtime) const override;
EventPayloadType getType() const override;
};
#if RN_DEBUG_STRING_CONVERTIBLE
std::string getDebugName(const ScrollEvent& scrollEvent);
std::vector<DebugStringConvertibleObject> getDebugProps(
const ScrollEvent& scrollEvent,
DebugStringConvertibleOptions options);
#endif
} // namespace facebook::react
@@ -9,6 +9,6 @@
namespace facebook::react {
enum class EventPayloadType { ValueFactory, PointerEvent };
enum class EventPayloadType { ValueFactory, PointerEvent, ScrollEvent };
}