From 6e3e6b574f8c27cf666047e66c7362bdef76c099 Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Mon, 31 Jul 2023 11:38:36 -0700 Subject: [PATCH] Create option for out-of-tree platforms to customize ViewEventEmitter (#38582) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38582 Some out of tree platforms may have extra events (e.g., events for different input modalities) that don't exist on other platforms. For example, react-native-windows and react-native-macos have `onKeyUp` and `onKeyDown` that can be emitted from any native component. This change provides a hook for out of platforms to customize which events can be emitted from all native components. ## Changelog: [General] [Added] - Support additional View events in out of tree platform Fabric implementations Reviewed By: christophpurrer Differential Revision: D47721603 fbshipit-source-id: 5ae2ff0f6c1b1dfd72b0a310c1309a85e6b170b1 --- ...wEventEmitter.cpp => BaseViewEventEmitter.cpp} | 13 +++++++------ ...{ViewEventEmitter.h => BaseViewEventEmitter.h} | 6 +----- .../renderer/components/view/ViewEventEmitter.h | 15 +++++++++++++++ .../renderer/components/view/ViewEventEmitter.h | 15 +++++++++++++++ 4 files changed, 38 insertions(+), 11 deletions(-) rename packages/react-native/ReactCommon/react/renderer/components/view/{ViewEventEmitter.cpp => BaseViewEventEmitter.cpp} (90%) rename packages/react-native/ReactCommon/react/renderer/components/view/{ViewEventEmitter.h => BaseViewEventEmitter.h} (91%) create mode 100644 packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/ViewEventEmitter.h create mode 100644 packages/react-native/ReactCommon/react/renderer/components/view/platform/cxx/react/renderer/components/view/ViewEventEmitter.h diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/ViewEventEmitter.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/BaseViewEventEmitter.cpp similarity index 90% rename from packages/react-native/ReactCommon/react/renderer/components/view/ViewEventEmitter.cpp rename to packages/react-native/ReactCommon/react/renderer/components/view/BaseViewEventEmitter.cpp index d80e5a0d070cbf..4d716961cc7eaa 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/ViewEventEmitter.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/BaseViewEventEmitter.cpp @@ -5,13 +5,14 @@ * LICENSE file in the root directory of this source tree. */ -#include "ViewEventEmitter.h" +#include "BaseViewEventEmitter.h" namespace facebook::react { #pragma mark - Accessibility -void ViewEventEmitter::onAccessibilityAction(std::string const &name) const { +void BaseViewEventEmitter::onAccessibilityAction( + std::string const &name) const { dispatchEvent("accessibilityAction", [name](jsi::Runtime &runtime) { auto payload = jsi::Object(runtime); payload.setProperty(runtime, "actionName", name); @@ -19,21 +20,21 @@ void ViewEventEmitter::onAccessibilityAction(std::string const &name) const { }); } -void ViewEventEmitter::onAccessibilityTap() const { +void BaseViewEventEmitter::onAccessibilityTap() const { dispatchEvent("accessibilityTap"); } -void ViewEventEmitter::onAccessibilityMagicTap() const { +void BaseViewEventEmitter::onAccessibilityMagicTap() const { dispatchEvent("magicTap"); } -void ViewEventEmitter::onAccessibilityEscape() const { +void BaseViewEventEmitter::onAccessibilityEscape() const { dispatchEvent("accessibilityEscape"); } #pragma mark - Layout -void ViewEventEmitter::onLayout(const LayoutMetrics &layoutMetrics) const { +void BaseViewEventEmitter::onLayout(const LayoutMetrics &layoutMetrics) const { // A copy of a shared pointer (`layoutEventState_`) establishes shared // ownership that will be captured by lambda. auto layoutEventState = layoutEventState_; diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/ViewEventEmitter.h b/packages/react-native/ReactCommon/react/renderer/components/view/BaseViewEventEmitter.h similarity index 91% rename from packages/react-native/ReactCommon/react/renderer/components/view/ViewEventEmitter.h rename to packages/react-native/ReactCommon/react/renderer/components/view/BaseViewEventEmitter.h index 2267fac1ed08eb..eb4a99ff799a1a 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/ViewEventEmitter.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/BaseViewEventEmitter.h @@ -17,11 +17,7 @@ namespace facebook::react { -class ViewEventEmitter; - -using SharedViewEventEmitter = std::shared_ptr; - -class ViewEventEmitter : public TouchEventEmitter { +class BaseViewEventEmitter : public TouchEventEmitter { public: using TouchEventEmitter::TouchEventEmitter; diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/ViewEventEmitter.h b/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/ViewEventEmitter.h new file mode 100644 index 00000000000000..23140c98b561ba --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/ViewEventEmitter.h @@ -0,0 +1,15 @@ +/* + * 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 + +namespace facebook::react { +using ViewEventEmitter = BaseViewEventEmitter; +using SharedViewEventEmitter = std::shared_ptr; +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/platform/cxx/react/renderer/components/view/ViewEventEmitter.h b/packages/react-native/ReactCommon/react/renderer/components/view/platform/cxx/react/renderer/components/view/ViewEventEmitter.h new file mode 100644 index 00000000000000..23140c98b561ba --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/view/platform/cxx/react/renderer/components/view/ViewEventEmitter.h @@ -0,0 +1,15 @@ +/* + * 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 + +namespace facebook::react { +using ViewEventEmitter = BaseViewEventEmitter; +using SharedViewEventEmitter = std::shared_ptr; +} // namespace facebook::react