diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollEvent.cpp b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollEvent.cpp new file mode 100644 index 00000000000..95313a2e91f --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollEvent.cpp @@ -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 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 diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollEvent.h b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollEvent.h new file mode 100644 index 00000000000..5a8ff342f3e --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollEvent.h @@ -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 +#include +#include +#include + +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 getDebugProps( + const ScrollEvent& scrollEvent, + DebugStringConvertibleOptions options); + +#endif + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/core/EventPayloadType.h b/packages/react-native/ReactCommon/react/renderer/core/EventPayloadType.h index 35d1b6e56fe..4d9624a69a8 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/EventPayloadType.h +++ b/packages/react-native/ReactCommon/react/renderer/core/EventPayloadType.h @@ -9,6 +9,6 @@ namespace facebook::react { -enum class EventPayloadType { ValueFactory, PointerEvent }; +enum class EventPayloadType { ValueFactory, PointerEvent, ScrollEvent }; }