From 364cd3c82791d5c86d9ec47b15fba7c41410592b Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Mon, 31 Jul 2023 08:27:16 -0700 Subject: [PATCH] Create option for out-of-tree platforms to customize View traits (#38580) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38580 We already have a differential between Android and iOS View props that force view flattening (or unflattening) behaviors. Other out-of-tree platforms may have a need to customize view flattening behaviors. This change adds a ViewTraitsInitializer header that out of tree platforms can inject to include platform-specific ViewProps fields when considering view flattening behaviors. ## Changelog: [General] [Added] - Support customization of View traits for flattening in out of tree platform Fabric implementations Differential Revision: https://internalfb.com/D47721377 fbshipit-source-id: 7ba77a294e80a50e701e81ec0f971141011975b1 --- .../components/view/ViewShadowNode.cpp | 22 +++---------- .../components/view/ViewTraitsInitializer.h | 31 +++++++++++++++++++ .../components/view/ViewTraitsInitializer.h | 27 ++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/ViewTraitsInitializer.h create mode 100644 packages/react-native/ReactCommon/react/renderer/components/view/platform/cxx/react/renderer/components/view/ViewTraitsInitializer.h diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp index 98b87012855e0d..4e4aa9479205cb 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp @@ -7,6 +7,7 @@ #include "ViewShadowNode.h" #include +#include #include #include @@ -54,24 +55,13 @@ void ViewShadowNode::initialize() noexcept { viewProps.accessibilityElementsHidden || viewProps.accessibilityViewIsModal || viewProps.importantForAccessibility != ImportantForAccessibility::Auto || - viewProps.removeClippedSubviews; - -#ifdef ANDROID - formsStackingContext = formsStackingContext || viewProps.elevation != 0; -#endif + viewProps.removeClippedSubviews || + ViewTraitsInitializer::formsStackingContext(viewProps); bool formsView = formsStackingContext || isColorMeaningful(viewProps.backgroundColor) || !(viewProps.yogaStyle.border() == YGStyle::Edges{}) || - !viewProps.testId.empty(); - -#ifdef ANDROID - formsView = formsView || viewProps.nativeBackground.has_value() || - viewProps.nativeForeground.has_value() || viewProps.focusable || - viewProps.hasTVPreferredFocus || - viewProps.needsOffscreenAlphaCompositing || - viewProps.renderToHardwareTextureAndroid; -#endif + !viewProps.testId.empty() || ViewTraitsInitializer::formsView(viewProps); if (formsView) { traits_.set(ShadowNodeTraits::Trait::FormsView); @@ -85,9 +75,7 @@ void ViewShadowNode::initialize() noexcept { traits_.unset(ShadowNodeTraits::Trait::FormsStackingContext); } -#ifdef ANDROID - traits_.set(ShadowNodeTraits::Trait::AndroidMapBufferPropsSupported); -#endif + traits_.set(ViewTraitsInitializer::extraTraits()); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/ViewTraitsInitializer.h b/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/ViewTraitsInitializer.h new file mode 100644 index 00000000000000..e8d6ac59db5673 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/ViewTraitsInitializer.h @@ -0,0 +1,31 @@ +/* + * 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 +#include + +namespace facebook::react::ViewTraitsInitializer { + +static bool formsStackingContext(ViewProps const &viewProps) { + return viewProps.elevation != 0; +} + +static 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() { + return ShadowNodeTraits::Trait::AndroidMapBufferPropsSupported; +} + +} // namespace facebook::react::ViewTraitsInitializer diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/platform/cxx/react/renderer/components/view/ViewTraitsInitializer.h b/packages/react-native/ReactCommon/react/renderer/components/view/platform/cxx/react/renderer/components/view/ViewTraitsInitializer.h new file mode 100644 index 00000000000000..72b7203dea5ba2 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/components/view/platform/cxx/react/renderer/components/view/ViewTraitsInitializer.h @@ -0,0 +1,27 @@ +/* + * 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 +#include + +namespace facebook::react::ViewTraitsInitializer { + +static bool formsStackingContext(ViewProps const &props) { + return false; +} + +static bool formsView(ViewProps const &props) { + return false; +} + +static ShadowNodeTraits::Trait extraTraits() { + return ShadowNodeTraits::Trait::None; +} + +} // namespace facebook::react::ViewTraitsInitializer