|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "ScrollEventHelper.h" |
| 9 | + |
| 10 | +#include <stdexcept> |
| 11 | + |
| 12 | +#include "WritableNativeMap.h" |
| 13 | + |
| 14 | +using namespace facebook::jni; |
| 15 | + |
| 16 | +namespace facebook::react { |
| 17 | + |
| 18 | +namespace { |
| 19 | + |
| 20 | +// Keep in sync with the field indices in ScrollEventHelper.kt. |
| 21 | +constexpr jint kContentOffsetXIndex = 0; |
| 22 | +constexpr jint kContentOffsetYIndex = 1; |
| 23 | +constexpr jint kContentWidthIndex = 2; |
| 24 | +constexpr jint kContentHeightIndex = 3; |
| 25 | +constexpr jint kLayoutWidthIndex = 4; |
| 26 | +constexpr jint kLayoutHeightIndex = 5; |
| 27 | +constexpr jint kVelocityXIndex = 6; |
| 28 | +constexpr jint kVelocityYIndex = 7; |
| 29 | +constexpr jint kTimestampIndex = 8; |
| 30 | +constexpr jint kFields = 9; |
| 31 | + |
| 32 | +local_ref<WritableNativeMap::jhybridobject> createScrollPayload( |
| 33 | + alias_ref<jclass> /*unused*/, |
| 34 | + alias_ref<JArrayDouble> data, |
| 35 | + jint target, |
| 36 | + jboolean responderIgnoreScroll) { |
| 37 | + if (data->size() != kFields) { |
| 38 | + throw std::invalid_argument("Invalid scroll event data"); |
| 39 | + } |
| 40 | + auto values = data->getRegion(0, kFields); |
| 41 | + const double* fields = values.get(); |
| 42 | + |
| 43 | + auto jPayload = WritableNativeMap::newObjectJavaArgs(); |
| 44 | + auto* payload = jPayload->cthis(); |
| 45 | + // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 46 | + WritableNativeMap contentInset( |
| 47 | + folly::dynamic::object("top", 0.0)("bottom", 0.0)("left", 0.0)( |
| 48 | + "right", 0.0)); |
| 49 | + payload->putNativeMap("contentInset", &contentInset); |
| 50 | + WritableNativeMap contentOffset( |
| 51 | + folly::dynamic::object("x", fields[kContentOffsetXIndex])( |
| 52 | + "y", fields[kContentOffsetYIndex])); |
| 53 | + payload->putNativeMap("contentOffset", &contentOffset); |
| 54 | + WritableNativeMap contentSize( |
| 55 | + folly::dynamic::object("width", fields[kContentWidthIndex])( |
| 56 | + "height", fields[kContentHeightIndex])); |
| 57 | + payload->putNativeMap("contentSize", &contentSize); |
| 58 | + WritableNativeMap layoutMeasurement( |
| 59 | + folly::dynamic::object("width", fields[kLayoutWidthIndex])( |
| 60 | + "height", fields[kLayoutHeightIndex])); |
| 61 | + payload->putNativeMap("layoutMeasurement", &layoutMeasurement); |
| 62 | + WritableNativeMap velocity( |
| 63 | + folly::dynamic::object("x", fields[kVelocityXIndex])( |
| 64 | + "y", fields[kVelocityYIndex])); |
| 65 | + payload->putNativeMap("velocity", &velocity); |
| 66 | + payload->putInt("target", target); |
| 67 | + payload->putDouble("timestamp", fields[kTimestampIndex]); |
| 68 | + // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 69 | + payload->putBoolean("responderIgnoreScroll", responderIgnoreScroll != 0u); |
| 70 | + return jPayload; |
| 71 | +} |
| 72 | + |
| 73 | +} // namespace |
| 74 | + |
| 75 | +void ScrollEventHelper::registerNatives() { |
| 76 | + javaClassLocal()->registerNatives({ |
| 77 | + makeNativeMethod("nativeCreateScrollPayload", createScrollPayload), |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace facebook::react |
0 commit comments