Skip to content

Commit

Permalink
More consistent platform for out-of-tree platform extensions (#38703)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
rozele authored and facebook-github-bot committed Jul 31, 2023
1 parent 4884322 commit b0a8d45
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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);
Expand All @@ -75,7 +76,7 @@ void ViewShadowNode::initialize() noexcept {
traits_.unset(ShadowNodeTraits::Trait::FormsStackingContext);
}

traits_.set(ViewTraitsInitializer::extraTraits());
traits_.set(HostPlatformViewTraitsInitializer::extraTraits());
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

#include "ViewProps.h"
#include "HostPlatformViewProps.h"

#include <algorithm>

Expand All @@ -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),
Expand Down Expand Up @@ -89,7 +89,7 @@ ViewProps::ViewProps(
return; \
}

void ViewProps::setProp(
void HostPlatformViewProps::setProp(
const PropsParserContext &context,
RawPropsPropNameHash hash,
const char *propName,
Expand All @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
#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 ||
viewProps.needsOffscreenAlphaCompositing ||
viewProps.renderToHardwareTextureAndroid;
}

static ShadowNodeTraits::Trait extraTraits() {
inline ShadowNodeTraits::Trait extraTraits() {
return ShadowNodeTraits::Trait::AndroidMapBufferPropsSupported;
}

} // namespace facebook::react::ViewTraitsInitializer
} // namespace facebook::react::HostPlatformViewTraitsInitializer
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b0a8d45

Please sign in to comment.