ScrollViewEventEmitter.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) Facebook, Inc. and its 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. #include "ScrollViewEventEmitter.h"
  8. namespace facebook {
  9. namespace react {
  10. static jsi::Value scrollViewMetricsPayload(
  11. jsi::Runtime &runtime,
  12. const ScrollViewMetrics &scrollViewMetrics) {
  13. auto payload = jsi::Object(runtime);
  14. {
  15. auto contentOffset = jsi::Object(runtime);
  16. contentOffset.setProperty(runtime, "x", scrollViewMetrics.contentOffset.x);
  17. contentOffset.setProperty(runtime, "y", scrollViewMetrics.contentOffset.y);
  18. payload.setProperty(runtime, "contentOffset", contentOffset);
  19. }
  20. {
  21. auto contentInset = jsi::Object(runtime);
  22. contentInset.setProperty(
  23. runtime, "top", scrollViewMetrics.contentInset.top);
  24. contentInset.setProperty(
  25. runtime, "left", scrollViewMetrics.contentInset.left);
  26. contentInset.setProperty(
  27. runtime, "bottom", scrollViewMetrics.contentInset.bottom);
  28. contentInset.setProperty(
  29. runtime, "right", scrollViewMetrics.contentInset.right);
  30. payload.setProperty(runtime, "contentInset", contentInset);
  31. }
  32. {
  33. auto contentSize = jsi::Object(runtime);
  34. contentSize.setProperty(
  35. runtime, "width", scrollViewMetrics.contentSize.width);
  36. contentSize.setProperty(
  37. runtime, "height", scrollViewMetrics.contentSize.height);
  38. payload.setProperty(runtime, "contentSize", contentSize);
  39. }
  40. {
  41. auto containerSize = jsi::Object(runtime);
  42. containerSize.setProperty(
  43. runtime, "width", scrollViewMetrics.containerSize.width);
  44. containerSize.setProperty(
  45. runtime, "height", scrollViewMetrics.containerSize.height);
  46. payload.setProperty(runtime, "layoutMeasurement", containerSize);
  47. }
  48. payload.setProperty(runtime, "zoomScale", scrollViewMetrics.zoomScale);
  49. return payload;
  50. }
  51. void ScrollViewEventEmitter::onScroll(
  52. const ScrollViewMetrics &scrollViewMetrics) const {
  53. dispatchScrollViewEvent("scroll", scrollViewMetrics);
  54. }
  55. void ScrollViewEventEmitter::onScrollBeginDrag(
  56. const ScrollViewMetrics &scrollViewMetrics) const {
  57. dispatchScrollViewEvent("scrollBeginDrag", scrollViewMetrics);
  58. }
  59. void ScrollViewEventEmitter::onScrollEndDrag(
  60. const ScrollViewMetrics &scrollViewMetrics) const {
  61. dispatchScrollViewEvent("scrollEndDrag", scrollViewMetrics);
  62. }
  63. void ScrollViewEventEmitter::onMomentumScrollBegin(
  64. const ScrollViewMetrics &scrollViewMetrics) const {
  65. dispatchScrollViewEvent("momentumScrollBegin", scrollViewMetrics);
  66. }
  67. void ScrollViewEventEmitter::onMomentumScrollEnd(
  68. const ScrollViewMetrics &scrollViewMetrics) const {
  69. dispatchScrollViewEvent("momentumScrollEnd", scrollViewMetrics);
  70. }
  71. void ScrollViewEventEmitter::dispatchScrollViewEvent(
  72. const std::string &name,
  73. const ScrollViewMetrics &scrollViewMetrics,
  74. EventPriority priority) const {
  75. dispatchEvent(
  76. name,
  77. [scrollViewMetrics](jsi::Runtime &runtime) {
  78. return scrollViewMetricsPayload(runtime, scrollViewMetrics);
  79. },
  80. priority);
  81. }
  82. } // namespace react
  83. } // namespace facebook