react-native/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.h
Valentin Shergin e906d4cdc9 Simplifying child nodes management in YogaLayoutableShadowNode
Summary:
@public

This diff consists of many interdependent changes which support one simple idea: YogaLayoutableShadowNode is now using YGNode children to iterate on them (it previously relied on `ShadowNode::getChildren()`). All other changes are just an unavoidable consequence of that. Hence we don't need to filter child nodes every single time when we do layout anymore! The logic around `clone callback` is also drastically simpler now.
The new approach also implies that `LayoutableShadowNode` and `YogaLayoutableShadowNode` don't use `shared_ptr`s to refer to ShadowNode objects because new relationship does not imply ownership. No more `SharedShadowNode` objects in those two classes.

Reviewed By: mdvacca

Differential Revision: D8796159

fbshipit-source-id: 6f52f92d1826f3eb13b2f8a132c3ea77de155d82
2018-07-17 22:53:56 -07:00

101 lines
2.7 KiB
C++

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* 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 <memory>
#include <vector>
#include <yoga/YGNode.h>
#include <fabric/components/view/YogaStylableProps.h>
#include <fabric/core/LayoutableShadowNode.h>
#include <fabric/core/Sealable.h>
#include <fabric/debug/DebugStringConvertible.h>
namespace facebook {
namespace react {
class YogaLayoutableShadowNode:
public LayoutableShadowNode,
public virtual DebugStringConvertible,
public virtual Sealable {
public:
#pragma mark - Constructors
YogaLayoutableShadowNode();
YogaLayoutableShadowNode(const YogaLayoutableShadowNode &layoutableShadowNode);
#pragma mark - Mutating Methods
/*
* Connects `measureFunc` function of Yoga node with
* `LayoutableShadowNode::measure()` method.
*/
void enableMeasurement();
/*
* Appends `child`'s Yoga node to the own Yoga node.
* Complements `ShadowNode::appendChild(...)` functionality from Yoga perspective.
*/
void appendChild(YogaLayoutableShadowNode *child);
/*
* Sets Yoga children based on collection of `YogaLayoutableShadowNode` instances.
* Complements `ShadowNode::setChildren(...)` functionality from Yoga perspective.
*/
void setChildren(std::vector<YogaLayoutableShadowNode *> children);
/*
* Sets Yoga styles based on given `YogaStylableProps`.
*/
void setProps(const YogaStylableProps &props);
#pragma mark - LayoutableShadowNode
void cleanLayout() override;
void dirtyLayout() override;
bool getIsLayoutClean() const override;
void setHasNewLayout(bool hasNewLayout) override;
bool getHasNewLayout() const override;
/*
* Computes layout using Yoga layout engine.
* See `LayoutableShadowNode` for more details.
*/
void layout(LayoutContext layoutContext) override;
void layoutChildren(LayoutContext layoutContext) override;
std::vector<LayoutableShadowNode *> getLayoutableChildNodes() const override;
protected:
/*
* All Yoga functions only accept non-const arguments, so we have to mark
* Yoga node as `mutable` here to avoid `static_cast`ing the pointer to this
* all the time.
*/
mutable YGNode yogaNode_;
/*
* Yoga config associated (only) with this particular node.
*/
YGConfig yogaConfig_;
private:
static void initializeYogaConfig(YGConfig &config);
static YGNode *yogaNodeCloneCallbackConnector(YGNode *oldYogaNode, YGNode *parentYogaNode, int childIndex);
static YGSize yogaNodeMeasureCallbackConnector(YGNode *yogaNode, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode);
};
} // namespace react
} // namespace facebook