Fabric: Introducing `LayoutableShadowNode::layoutTree`

Summary:
All logic that is performed on the root node only was moved from `layout` to a separate methods `layoutTree`. That makes the code simpler and allows reusing `layoutTree` in InlineViews feature.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: mdvacca

Differential Revision: D20052025

fbshipit-source-id: 3070a1cca4e322c6d077ede751ea80359c96a600
This commit is contained in:
Valentin Shergin 2020-02-26 22:03:14 -08:00 коммит произвёл Facebook Github Bot
Родитель 1801d63de2
Коммит 011b470961
5 изменённых файлов: 47 добавлений и 28 удалений

Просмотреть файл

@ -28,14 +28,7 @@ bool RootShadowNode::layoutIfNeeded(
auto layoutContext = getConcreteProps().layoutContext;
layoutContext.affectedNodes = affectedNodes;
layout(layoutContext);
// This is the rare place where shadow node must layout (set `layoutMetrics`)
// itself because there is no a parent node which usually should do it.
if (getHasNewLayout()) {
setLayoutMetrics(layoutMetricsFromYogaNode(yogaNode_));
setHasNewLayout(false);
}
layoutTree(layoutContext, getConcreteProps().layoutConstraints);
return true;
}

Просмотреть файл

@ -221,30 +221,39 @@ void YogaLayoutableShadowNode::setPositionType(
yogaNode_.setDirty(true);
}
void YogaLayoutableShadowNode::layout(LayoutContext layoutContext) {
if (!getIsLayoutClean()) {
ensureUnsealed();
void YogaLayoutableShadowNode::layoutTree(
LayoutContext layoutContext,
LayoutConstraints layoutConstraints) {
ensureUnsealed();
/*
* In Yoga, every single Yoga Node has to have a (non-null) pointer to
* Yoga Config (this config can be shared between many nodes),
* so every node can be individually configured. This does *not* mean
* however that Yoga consults with every single Yoga Node Config for every
* config parameter. Especially in case of `pointScaleFactor`,
* the only value in the config of the root node is taken into account
* (and this is by design).
*/
yogaConfig_.pointScaleFactor = layoutContext.pointScaleFactor;
/*
* In Yoga, every single Yoga Node has to have a (non-null) pointer to
* Yoga Config (this config can be shared between many nodes),
* so every node can be individually configured. This does *not* mean
* however that Yoga consults with every single Yoga Node Config for every
* config parameter. Especially in case of `pointScaleFactor`,
* the only value in the config of the root node is taken into account
* (and this is by design).
*/
yogaConfig_.pointScaleFactor = layoutContext.pointScaleFactor;
{
SystraceSection s("YogaLayoutableShadowNode::YGNodeCalculateLayout");
auto maximumSize = layoutConstraints.maximumSize;
auto availableWidth = yogaFloatFromFloat(maximumSize.width);
auto availableHeight = yogaFloatFromFloat(maximumSize.height);
YGNodeCalculateLayout(
&yogaNode_, YGUndefined, YGUndefined, YGDirectionInherit);
}
{
SystraceSection s("YogaLayoutableShadowNode::YGNodeCalculateLayout");
YGNodeCalculateLayout(
&yogaNode_, availableWidth, availableHeight, YGDirectionInherit);
}
LayoutableShadowNode::layout(layoutContext);
layout(layoutContext);
if (getHasNewLayout()) {
setLayoutMetrics(layoutMetricsFromYogaNode(yogaNode_));
setHasNewLayout(false);
}
}
void YogaLayoutableShadowNode::layoutChildren(LayoutContext layoutContext) {

Просмотреть файл

@ -80,7 +80,9 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode {
* Computes layout using Yoga layout engine.
* See `LayoutableShadowNode` for more details.
*/
void layout(LayoutContext layoutContext) override;
void layoutTree(
LayoutContext layoutContext,
LayoutConstraints layoutConstraints) override;
void layoutChildren(LayoutContext layoutContext) override;

Просмотреть файл

@ -168,6 +168,12 @@ Float LayoutableShadowNode::lastBaseline(Size size) const {
return 0;
}
void LayoutableShadowNode::layoutTree(
LayoutContext layoutContext,
LayoutConstraints layoutConstraints) {
// Default implementation does nothing.
}
void LayoutableShadowNode::layout(LayoutContext layoutContext) {
layoutChildren(layoutContext);

Просмотреть файл

@ -52,6 +52,15 @@ class LayoutableShadowNode : public ShadowNode {
using UnsharedList = better::
small_vector<LayoutableShadowNode *, kShadowNodeChildrenSmallVectorSize>;
/*
* Performs layout of the tree starting from this node. Usually is being
* called on the root node.
* Default implementation does nothing.
*/
virtual void layoutTree(
LayoutContext layoutContext,
LayoutConstraints layoutConstraints);
/*
* Measures the node (and node content, probably recursively) with
* given constrains and relying on possible layout.