ViewEventEmitter.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "ViewEventEmitter.h"
  8. namespace facebook {
  9. namespace react {
  10. #pragma mark - Accessibility
  11. void ViewEventEmitter::onAccessibilityAction(const std::string &name) const {
  12. dispatchEvent("accessibilityAction", [name](jsi::Runtime &runtime) {
  13. auto payload = jsi::Object(runtime);
  14. payload.setProperty(runtime, "action", name);
  15. return payload;
  16. });
  17. }
  18. void ViewEventEmitter::onAccessibilityTap() const {
  19. dispatchEvent("accessibilityTap");
  20. }
  21. void ViewEventEmitter::onAccessibilityMagicTap() const {
  22. dispatchEvent("magicTap");
  23. }
  24. void ViewEventEmitter::onAccessibilityEscape() const {
  25. dispatchEvent("accessibilityEscape");
  26. }
  27. #pragma mark - Layout
  28. void ViewEventEmitter::onLayout(const LayoutMetrics &layoutMetrics) const {
  29. // Due to State Reconciliation, `onLayout` can be called potentially many
  30. // times with identical layoutMetrics. Ensure that the JS event is only
  31. // dispatched when the value changes.
  32. {
  33. std::lock_guard<std::mutex> guard(layoutMetricsMutex_);
  34. if (lastLayoutMetrics_ == layoutMetrics) {
  35. return;
  36. }
  37. lastLayoutMetrics_ = layoutMetrics;
  38. }
  39. dispatchEvent("layout", [frame = layoutMetrics.frame](jsi::Runtime &runtime) {
  40. auto layout = jsi::Object(runtime);
  41. layout.setProperty(runtime, "x", frame.origin.x);
  42. layout.setProperty(runtime, "y", frame.origin.y);
  43. layout.setProperty(runtime, "width", frame.size.width);
  44. layout.setProperty(runtime, "height", frame.size.height);
  45. auto payload = jsi::Object(runtime);
  46. payload.setProperty(runtime, "layout", std::move(layout));
  47. return payload;
  48. });
  49. }
  50. } // namespace react
  51. } // namespace facebook