Files
react-native/ReactCommon/fabric/components/view/ViewShadowNode.cpp
T
Valentin Shergin b5117bf6ea Fabric: New two ShadowNode Traits: FormsStackingContext & FormsView
Summary:
This diff introduces two new `ShadowNodeTrait`s that we will use in the future in the new (slightly tweaked) view-flattening algorithm. (Note: this diff does not enable the new flattening, it's just preparation.)
The idea is that we split the notion of `isLayoutOnlyView` into two traits:
* `FormsView`: `ShadowNode`s with this trait must be represented as `ShadowView`s. Normal "visible" ShadowNodes will have this trait, but "layout only views" in old nomenclature will not.
* `FormsStackingContext`: `ShadowNode`s with this thread not only must be represented as `ShadowView`s but also have to form a "stacking context" which means that their children must be mounted as `ShadowView`'s children.

Our implementation does not exactly follow W3C spec in terms of sets of props that create the stacking context (because of historical reasons and mobile specifics) but ideologically it's the same. We start from the very conservative implementation where only views with background-color and borders do not form stacking context and then we probably extend that to more props.

Most importantly for us now, we will enforce the absence of ``FormsStackingContext` for `ParagraphShadowNode` and the presence of `FormsView` for `TextShadowNode` on Android where it's essential for mounting layer.

Read more about stacking context here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D20212253

fbshipit-source-id: 0fbaee214ce2c5886cb0232843a2a3c7bb20655d
2020-03-04 12:33:03 -08:00

91 lines
2.8 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ViewShadowNode.h"
#include <react/components/view/primitives.h>
namespace facebook {
namespace react {
const char ViewComponentName[] = "View";
bool ViewShadowNode::isLayoutOnly() const {
const auto &viewProps = *std::static_pointer_cast<const ViewProps>(props_);
return viewProps.collapsable &&
// Generic Props
viewProps.nativeId.empty() &&
// Accessibility Props
!viewProps.accessible &&
// Pointer events Props
(viewProps.pointerEvents == PointerEventsMode::Auto ||
viewProps.pointerEvents == PointerEventsMode::BoxNone) &&
// Style Props
viewProps.opacity == 1.0 && !viewProps.backgroundColor &&
!viewProps.foregroundColor && !viewProps.shadowColor &&
viewProps.transform == Transform{} && viewProps.zIndex == 0 &&
!viewProps.getClipsContentToBounds() &&
// Layout Metrics
getLayoutMetrics().borderWidth == EdgeInsets{};
}
ViewShadowNode::ViewShadowNode(
ShadowNodeFragment const &fragment,
ShadowNodeFamily::Shared const &family,
ShadowNodeTraits traits)
: ConcreteViewShadowNode(fragment, family, traits) {
updateTraits();
}
ViewShadowNode::ViewShadowNode(
ShadowNode const &sourceShadowNode,
ShadowNodeFragment const &fragment)
: ConcreteViewShadowNode(sourceShadowNode, fragment) {
updateTraits();
}
static bool isColorMeaningful(SharedColor const &color) {
if (!color) {
return false;
}
return colorComponentsFromColor(color).alpha > 0;
}
void ViewShadowNode::updateTraits() {
auto &viewProps = static_cast<ViewProps const &>(*props_);
bool formsStackingContext = !viewProps.collapsable ||
viewProps.pointerEvents == PointerEventsMode::None ||
!viewProps.nativeId.empty() || viewProps.accessible ||
viewProps.opacity != 1.0 || viewProps.transform != Transform{} ||
viewProps.zIndex != 0 || viewProps.getClipsContentToBounds() ||
viewProps.yogaStyle.positionType() == YGPositionTypeAbsolute;
bool formsView = isColorMeaningful(viewProps.backgroundColor) ||
isColorMeaningful(viewProps.foregroundColor) ||
isColorMeaningful(viewProps.shadowColor) ||
!(viewProps.yogaStyle.border() == YGStyle::Edges{});
formsView = formsView || formsStackingContext;
if (formsView) {
traits_.set(ShadowNodeTraits::Trait::FormsView);
} else {
traits_.unset(ShadowNodeTraits::Trait::FormsView);
}
if (formsStackingContext) {
traits_.set(ShadowNodeTraits::Trait::FormsStackingContext);
} else {
traits_.unset(ShadowNodeTraits::Trait::FormsStackingContext);
}
}
} // namespace react
} // namespace facebook