ViewShadowNode.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "ViewShadowNode.h"
  8. #include <react/components/view/primitives.h>
  9. namespace facebook {
  10. namespace react {
  11. char const ViewComponentName[] = "View";
  12. ViewShadowNode::ViewShadowNode(
  13. ShadowNodeFragment const &fragment,
  14. ShadowNodeFamily::Shared const &family,
  15. ShadowNodeTraits traits)
  16. : ConcreteViewShadowNode(fragment, family, traits) {
  17. initialize();
  18. }
  19. ViewShadowNode::ViewShadowNode(
  20. ShadowNode const &sourceShadowNode,
  21. ShadowNodeFragment const &fragment)
  22. : ConcreteViewShadowNode(sourceShadowNode, fragment) {
  23. initialize();
  24. }
  25. static bool isColorMeaningful(SharedColor const &color) noexcept {
  26. if (!color) {
  27. return false;
  28. }
  29. return colorComponentsFromColor(color).alpha > 0;
  30. }
  31. void ViewShadowNode::initialize() noexcept {
  32. auto &viewProps = static_cast<ViewProps const &>(*props_);
  33. bool formsStackingContext = !viewProps.collapsable ||
  34. viewProps.pointerEvents == PointerEventsMode::None ||
  35. !viewProps.nativeId.empty() || viewProps.accessible ||
  36. viewProps.opacity != 1.0 || viewProps.transform != Transform{} ||
  37. viewProps.zIndex != 0 || viewProps.getClipsContentToBounds() ||
  38. viewProps.yogaStyle.positionType() == YGPositionTypeAbsolute ||
  39. isColorMeaningful(viewProps.shadowColor);
  40. bool formsView = isColorMeaningful(viewProps.backgroundColor) ||
  41. isColorMeaningful(viewProps.foregroundColor) ||
  42. !(viewProps.yogaStyle.border() == YGStyle::Edges{});
  43. formsView = formsView || formsStackingContext;
  44. #ifdef ANDROID
  45. // Force `formsStackingContext` trait for nodes which have `formsView`.
  46. // TODO: T63560216 Investigate why/how `formsView` entangled with
  47. // `formsStackingContext`.
  48. formsStackingContext = formsStackingContext || formsView;
  49. #endif
  50. if (formsView) {
  51. traits_.set(ShadowNodeTraits::Trait::FormsView);
  52. } else {
  53. traits_.unset(ShadowNodeTraits::Trait::FormsView);
  54. }
  55. if (formsStackingContext) {
  56. traits_.set(ShadowNodeTraits::Trait::FormsStackingContext);
  57. } else {
  58. traits_.unset(ShadowNodeTraits::Trait::FormsStackingContext);
  59. }
  60. }
  61. } // namespace react
  62. } // namespace facebook