More consistent platform for out-of-tree platform extensions (#38703)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38703

This change consolidates the pattern for setting up out-of-tree platform options for core classes like ViewProps and ViewEventEmitter. A similar pattern was used for Touch.h. As we move towards documenting how to build an out-of-tree platform, it would be nice to specify a set of HostPlatformX classes that need to be implemented and made resolvable from specific header paths.

At this point, there is:
- HostPlatformViewProps
- HostPlatformViewEventEmitter
- HostPlatformViewTraitsInitializer
- HostPlatformTouch
- HostPlatformColor

The other benefit of this pattern is to DRY helper aliases like SharedViewEventEmitter and SharedViewProps.

## Changelog:
[General] [Added] - Use more consistent pattern for out-of-tree platform Fabric C++ class extensions

Reviewed By: christophpurrer

Differential Revision: D47917598

fbshipit-source-id: 58ee9677eefd34eb0bc2d321103314642c457cd8
This commit is contained in:
Eric Rozell
2023-07-31 11:38:36 -07:00
committed by Facebook GitHub Bot
parent 4884322781
commit b0a8d45e28
12 changed files with 62 additions and 37 deletions
@@ -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 <react/renderer/components/view/HostPlatformViewEventEmitter.h>
namespace facebook::react {
using ViewEventEmitter = HostPlatformViewEventEmitter;
using SharedViewEventEmitter = std::shared_ptr<const ViewEventEmitter>;
} // namespace facebook::react
@@ -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 <react/renderer/components/view/HostPlatformViewProps.h>
namespace facebook::react {
using ViewProps = HostPlatformViewProps;
using SharedViewProps = std::shared_ptr<ViewProps const>;
} // namespace facebook::react
@@ -7,7 +7,7 @@
#include "ViewShadowNode.h"
#include <react/config/ReactNativeConfig.h>
#include <react/renderer/components/view/ViewTraitsInitializer.h>
#include <react/renderer/components/view/HostPlatformViewTraitsInitializer.h>
#include <react/renderer/components/view/primitives.h>
#include <react/utils/CoreFeatures.h>
@@ -56,12 +56,13 @@ void ViewShadowNode::initialize() noexcept {
viewProps.accessibilityViewIsModal ||
viewProps.importantForAccessibility != ImportantForAccessibility::Auto ||
viewProps.removeClippedSubviews ||
ViewTraitsInitializer::formsStackingContext(viewProps);
HostPlatformViewTraitsInitializer::formsStackingContext(viewProps);
bool formsView = formsStackingContext ||
isColorMeaningful(viewProps.backgroundColor) ||
!(viewProps.yogaStyle.border() == YGStyle::Edges{}) ||
!viewProps.testId.empty() || ViewTraitsInitializer::formsView(viewProps);
!viewProps.testId.empty() ||
HostPlatformViewTraitsInitializer::formsView(viewProps);
if (formsView) {
traits_.set(ShadowNodeTraits::Trait::FormsView);
@@ -75,7 +76,7 @@ void ViewShadowNode::initialize() noexcept {
traits_.unset(ShadowNodeTraits::Trait::FormsStackingContext);
}
traits_.set(ViewTraitsInitializer::extraTraits());
traits_.set(HostPlatformViewTraitsInitializer::extraTraits());
}
} // namespace facebook::react
@@ -10,6 +10,5 @@
#include <react/renderer/components/view/BaseViewEventEmitter.h>
namespace facebook::react {
using ViewEventEmitter = BaseViewEventEmitter;
using SharedViewEventEmitter = std::shared_ptr<const ViewEventEmitter>;
using HostPlatformViewEventEmitter = BaseViewEventEmitter;
} // namespace facebook::react
@@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
#include "ViewProps.h"
#include "HostPlatformViewProps.h"
#include <algorithm>
@@ -17,9 +17,9 @@
namespace facebook::react {
ViewProps::ViewProps(
HostPlatformViewProps::HostPlatformViewProps(
const PropsParserContext &context,
ViewProps const &sourceProps,
HostPlatformViewProps const &sourceProps,
RawProps const &rawProps,
bool shouldSetRawProps)
: BaseViewProps(context, sourceProps, rawProps, shouldSetRawProps),
@@ -89,7 +89,7 @@ ViewProps::ViewProps(
return; \
}
void ViewProps::setProp(
void HostPlatformViewProps::setProp(
const PropsParserContext &context,
RawPropsPropNameHash hash,
const char *propName,
@@ -99,7 +99,7 @@ void ViewProps::setProp(
// reuse the same values.
BaseViewProps::setProp(context, hash, propName, value);
static auto defaults = ViewProps{};
static auto defaults = HostPlatformViewProps{};
switch (hash) {
RAW_SET_PROP_SWITCH_CASE_BASIC(elevation);
@@ -112,12 +112,13 @@ void ViewProps::setProp(
}
}
bool ViewProps::getProbablyMoreHorizontalThanVertical_DEPRECATED() const {
bool HostPlatformViewProps::getProbablyMoreHorizontalThanVertical_DEPRECATED()
const {
return yogaStyle.flexDirection() == YGFlexDirectionRow;
}
#if RN_DEBUG_STRING_CONVERTIBLE
SharedDebugStringConvertibleList ViewProps::getDebugProps() const {
SharedDebugStringConvertibleList HostPlatformViewProps::getDebugProps() const {
return BaseViewProps::getDebugProps();
}
#endif
@@ -20,16 +20,12 @@
namespace facebook::react {
class ViewProps;
using SharedViewProps = std::shared_ptr<ViewProps const>;
class ViewProps : public BaseViewProps {
class HostPlatformViewProps : public BaseViewProps {
public:
ViewProps() = default;
ViewProps(
HostPlatformViewProps() = default;
HostPlatformViewProps(
const PropsParserContext &context,
ViewProps const &sourceProps,
HostPlatformViewProps const &sourceProps,
RawProps const &rawProps,
bool shouldSetRawProps = true);
@@ -10,13 +10,13 @@
#include <react/renderer/components/view/ViewProps.h>
#include <react/renderer/core/ShadowNodeTraits.h>
namespace facebook::react::ViewTraitsInitializer {
namespace facebook::react::HostPlatformViewTraitsInitializer {
static bool formsStackingContext(ViewProps const &viewProps) {
inline bool formsStackingContext(ViewProps const &viewProps) {
return viewProps.elevation != 0;
}
static bool formsView(ViewProps const &viewProps) {
inline bool formsView(ViewProps const &viewProps) {
return viewProps.nativeBackground.has_value() ||
viewProps.nativeForeground.has_value() || viewProps.focusable ||
viewProps.hasTVPreferredFocus ||
@@ -24,8 +24,8 @@ static bool formsView(ViewProps const &viewProps) {
viewProps.renderToHardwareTextureAndroid;
}
static ShadowNodeTraits::Trait extraTraits() {
inline ShadowNodeTraits::Trait extraTraits() {
return ShadowNodeTraits::Trait::AndroidMapBufferPropsSupported;
}
} // namespace facebook::react::ViewTraitsInitializer
} // namespace facebook::react::HostPlatformViewTraitsInitializer
@@ -6,8 +6,8 @@
*/
#include "ViewPropsMapBuffer.h"
#include "ViewProps.h"
#include <react/renderer/components/view/ViewProps.h>
#include <react/renderer/components/view/viewPropConversions.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
@@ -5,9 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
#include "ViewProps.h"
#include "ViewPropsMapBuffer.h"
#include <react/renderer/components/view/ViewProps.h>
#include <react/renderer/components/view/viewPropConversions.h>
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
@@ -10,6 +10,5 @@
#include <react/renderer/components/view/BaseViewEventEmitter.h>
namespace facebook::react {
using ViewEventEmitter = BaseViewEventEmitter;
using SharedViewEventEmitter = std::shared_ptr<const ViewEventEmitter>;
using HostPlatformViewEventEmitter = BaseViewEventEmitter;
} // namespace facebook::react
@@ -10,6 +10,5 @@
#include <react/renderer/components/view/BaseViewProps.h>
namespace facebook::react {
using ViewProps = BaseViewProps;
using SharedViewProps = std::shared_ptr<ViewProps const>;
using HostPlatformViewProps = BaseViewProps;
} // namespace facebook::react
@@ -10,18 +10,18 @@
#include <react/renderer/components/view/ViewProps.h>
#include <react/renderer/core/ShadowNodeTraits.h>
namespace facebook::react::ViewTraitsInitializer {
namespace facebook::react::HostPlatformViewTraitsInitializer {
static bool formsStackingContext(ViewProps const &props) {
inline bool formsStackingContext(ViewProps const &props) {
return false;
}
static bool formsView(ViewProps const &props) {
inline bool formsView(ViewProps const &props) {
return false;
}
static ShadowNodeTraits::Trait extraTraits() {
inline ShadowNodeTraits::Trait extraTraits() {
return ShadowNodeTraits::Trait::None;
}
} // namespace facebook::react::ViewTraitsInitializer
} // namespace facebook::react::HostPlatformViewTraitsInitializer