slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
Node.h
浏览该文件的文档.
1/*
2 * Copyright (c) Meta Platforms, Inc. and 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
8#pragma once
9
10#include <cstdint>
11#include <cstdio>
12#include <vector>
13
14#include <yoga/Yoga.h>
16
17#include <yoga/config/Config.h>
20#include <yoga/enums/Edge.h>
21#include <yoga/enums/Errata.h>
23#include <yoga/enums/NodeType.h>
26#include <yoga/style/Style.h>
27
28// Tag struct used to form the opaque YGNodeRef for the public C API
29struct YGNode {};
30
31namespace facebook::yoga {
32
33class YG_EXPORT Node : public ::YGNode {
34 public:
36 Node();
37 explicit Node(const Config* config);
38
39 Node(Node&& node) noexcept;
40
41 // Does not expose true value semantics, as children are not cloned eagerly.
42 // Should we remove this?
43 Node(const Node& node) = default;
44
45 // assignment means potential leaks of existing children, or alternatively
46 // freeing unowned memory, double free, or freeing stack memory.
47 Node& operator=(const Node&) = delete;
48
49 // Getters
50 void* getContext() const {
51 return context_;
52 }
53
55 return alwaysFormsContainingBlock_;
56 }
57
58 bool getHasNewLayout() const {
59 return hasNewLayout_;
60 }
61
63 return nodeType_;
64 }
65
66 bool hasMeasureFunc() const noexcept {
67 return measureFunc_ != nullptr;
68 }
69
70 YGSize measure(
71 float availableWidth,
72 MeasureMode widthMode,
73 float availableHeight,
75
76 bool hasBaselineFunc() const noexcept {
77 return baselineFunc_ != nullptr;
78 }
79
80 float baseline(float width, float height) const;
81
82 float dimensionWithMargin(FlexDirection axis, float widthSize);
83
84 bool isLayoutDimensionDefined(FlexDirection axis);
85
90 inline bool hasDefiniteLength(Dimension dimension, float ownerSize) {
91 auto usedValue = getProcessedDimension(dimension).resolve(ownerSize);
92 return usedValue.isDefined() && usedValue.unwrap() >= 0.0f;
93 }
94
95 bool hasErrata(Errata errata) const {
96 return config_->hasErrata(errata);
97 }
98
99 bool hasContentsChildren() const {
100 return contentsChildrenCount_ != 0;
101 }
102
104 return dirtiedFunc_;
105 }
106
107 // For Performance reasons passing as reference.
109 return style_;
110 }
111
112 const Style& style() const {
113 return style_;
114 }
115
116 // For Performance reasons passing as reference.
118 return layout_;
119 }
120
121 const LayoutResults& getLayout() const {
122 return layout_;
123 }
124
125 size_t getLineIndex() const {
126 return lineIndex_;
127 }
128
129 bool isReferenceBaseline() const {
130 return isReferenceBaseline_;
131 }
132
133 // returns the Node that owns this Node. An owner is used to identify
134 // the YogaTree that a Node belongs to. This method will return the parent
135 // of the Node when a Node only belongs to one YogaTree or nullptr when
136 // the Node is shared between two or more YogaTrees.
137 Node* getOwner() const {
138 return owner_;
139 }
140
141 const std::vector<Node*>& getChildren() const {
142 return children_;
143 }
144
145 Node* getChild(size_t index) const {
146 return children_.at(index);
147 }
148
149 size_t getChildCount() const {
150 return children_.size();
151 }
152
154 return LayoutableChildren(this);
155 }
156
157 size_t getLayoutChildCount() const {
158 if (contentsChildrenCount_ == 0) {
159 return children_.size();
160 } else {
161 size_t count = 0;
162 for (auto iter = getLayoutChildren().begin();
163 iter != getLayoutChildren().end();
164 iter++) {
165 count++;
166 }
167 return count;
168 }
169 }
170
171 const Config* getConfig() const {
172 return config_;
173 }
174
175 bool isDirty() const {
176 return isDirty_;
177 }
178
180 return processedDimensions_[static_cast<size_t>(dimension)];
181 }
182
184 Direction direction,
185 Dimension dimension,
186 float referenceLength,
187 float ownerWidth) const {
188 FloatOptional value =
189 getProcessedDimension(dimension).resolve(referenceLength);
190 if (style_.boxSizing() == BoxSizing::BorderBox) {
191 return value;
192 }
193
194 FloatOptional dimensionPaddingAndBorder =
195 FloatOptional{style_.computePaddingAndBorderForDimension(
196 direction, dimension, ownerWidth)};
197
198 return value +
199 (dimensionPaddingAndBorder.isDefined() ? dimensionPaddingAndBorder
200 : FloatOptional{0.0});
201 }
202
203 // Setters
204
205 void setContext(void* context) {
206 context_ = context;
207 }
208
209 void setAlwaysFormsContainingBlock(bool alwaysFormsContainingBlock) {
210 alwaysFormsContainingBlock_ = alwaysFormsContainingBlock;
211 }
212
213 void setHasNewLayout(bool hasNewLayout) {
214 hasNewLayout_ = hasNewLayout;
215 }
216
217 void setNodeType(NodeType nodeType) {
218 nodeType_ = nodeType;
219 }
220
221 void setMeasureFunc(YGMeasureFunc measureFunc);
222
223 void setBaselineFunc(YGBaselineFunc baseLineFunc) {
224 baselineFunc_ = baseLineFunc;
225 }
226
227 void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) {
228 dirtiedFunc_ = dirtiedFunc;
229 }
230
231 void setStyle(const Style& style) {
232 style_ = style;
233 }
234
235 void setLayout(const LayoutResults& layout) {
236 layout_ = layout;
237 }
238
239 void setLineIndex(size_t lineIndex) {
240 lineIndex_ = lineIndex;
241 }
242
243 void setIsReferenceBaseline(bool isReferenceBaseline) {
244 isReferenceBaseline_ = isReferenceBaseline;
245 }
246
247 void setOwner(Node* owner) {
248 owner_ = owner;
249 }
250
251 // TODO: rvalue override for setChildren
252
253 void setConfig(Config* config);
254
255 void setDirty(bool isDirty);
256 void setChildren(const std::vector<Node*>& children);
257 void setLayoutLastOwnerDirection(Direction direction);
258 void setLayoutComputedFlexBasis(FloatOptional computedFlexBasis);
259 void setLayoutComputedFlexBasisGeneration(
260 uint32_t computedFlexBasisGeneration);
261 void setLayoutMeasuredDimension(float measuredDimension, Dimension dimension);
262 void setLayoutHadOverflow(bool hadOverflow);
263 void setLayoutDimension(float lengthValue, Dimension dimension);
264 void setLayoutDirection(Direction direction);
265 void setLayoutMargin(float margin, PhysicalEdge edge);
266 void setLayoutBorder(float border, PhysicalEdge edge);
267 void setLayoutPadding(float padding, PhysicalEdge edge);
268 void setLayoutPosition(float position, PhysicalEdge edge);
269 void setPosition(Direction direction, float ownerWidth, float ownerHeight);
270
271 // Other methods
272 Style::SizeLength processFlexBasis() const;
273 FloatOptional resolveFlexBasis(
274 Direction direction,
275 FlexDirection flexDirection,
276 float referenceLength,
277 float ownerWidth) const;
278 void processDimensions();
279 Direction resolveDirection(Direction ownerDirection);
280 void clearChildren();
282 void replaceChild(Node* oldChild, Node* newChild);
283 void replaceChild(Node* child, size_t index);
284 void insertChild(Node* child, size_t index);
286 bool removeChild(Node* child);
287 void removeChild(size_t index);
288
289 void cloneChildrenIfNeeded();
290 void cloneContentsChildrenIfNeeded();
291 void markDirtyAndPropagate();
292 float resolveFlexGrow() const;
293 float resolveFlexShrink() const;
294 bool isNodeFlexible();
295 void reset();
296
297 float relativePosition(
298 FlexDirection axis,
299 Direction direction,
300 float axisSize) const;
301
302 private:
303 // Used to allow resetting the node
304 Node& operator=(Node&&) noexcept = default;
305
306 void useWebDefaults() {
307 style_.setFlexDirection(FlexDirection::Row);
308 style_.setAlignContent(Align::Stretch);
309 }
310
311 bool hasNewLayout_ : 1 = true;
312 bool isReferenceBaseline_ : 1 = false;
313 bool isDirty_ : 1 = true;
315 NodeType nodeType_ : bitCount<NodeType>() = NodeType::Default;
316 void* context_ = nullptr;
317 YGMeasureFunc measureFunc_ = nullptr;
318 YGBaselineFunc baselineFunc_ = nullptr;
319 YGDirtiedFunc dirtiedFunc_ = nullptr;
322 size_t lineIndex_ = 0;
323 size_t contentsChildrenCount_ = 0;
324 Node* owner_ = nullptr;
325 std::vector<Node*> children_;
327 std::array<Style::SizeLength, 2> processedDimensions_{
328 {StyleSizeLength::undefined(), StyleSizeLength::undefined()}};
329};
330
331inline Node* resolveRef(const YGNodeRef ref) {
332 return static_cast<Node*>(ref);
333}
334
335inline const Node* resolveRef(const YGNodeConstRef ref) {
336 return static_cast<const Node*>(ref);
337}
338
339} // namespace facebook::yoga
typedefYG_EXTERN_C_BEGIN struct YGNode * YGNodeRef
Definition YGConfig.h:19
#define YG_EXPORT
Definition YGMacros.h:35
float(* YGBaselineFunc)(YGNodeConstRef node, float width, float height)
Definition YGNode.h:226
float YGMeasureMode float availableHeight
Definition YGNode.h:293
void(* YGDirtiedFunc)(YGNodeConstRef node)
Definition YGNode.h:105
float availableWidth
Definition YGNode.h:291
float YGMeasureMode float YGMeasureMode float YGMeasureMode float float float float float YGConfigRef config
Definition YGNode.h:302
YGSize(* YGMeasureFunc)(YGNodeConstRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
Definition YGNode.h:204
float YGMeasureMode heightMode
Definition YGNode.h:292
Definition Config.h:33
Definition LayoutableChildren.h:21
Node & operator=(const Node &)=delete
bool alwaysFormsContainingBlock() const
Definition Node.h:54
bool isDirty_
Definition Node.h:313
LayoutResults & getLayout()
Definition Node.h:117
size_t getLayoutChildCount() const
Definition Node.h:157
const Config * config_
Definition Node.h:326
void setHasNewLayout(bool hasNewLayout)
Definition Node.h:213
bool isDirty() const
Definition Node.h:175
void setNodeType(NodeType nodeType)
Definition Node.h:217
void setBaselineFunc(YGBaselineFunc baseLineFunc)
Definition Node.h:223
Style style_
Definition Node.h:320
Style & style()
Definition Node.h:108
void setDirtiedFunc(YGDirtiedFunc dirtiedFunc)
Definition Node.h:227
NodeType nodeType_
Definition Node.h:315
void setAlwaysFormsContainingBlock(bool alwaysFormsContainingBlock)
Definition Node.h:209
Style::SizeLength getProcessedDimension(Dimension dimension) const
Definition Node.h:179
std::vector< Node * > children_
Definition Node.h:325
void setLayout(const LayoutResults &layout)
Definition Node.h:235
YGDirtiedFunc getDirtiedFunc() const
Definition Node.h:103
FloatOptional getResolvedDimension(Direction direction, Dimension dimension, float referenceLength, float ownerWidth) const
Definition Node.h:183
bool isReferenceBaseline_
Definition Node.h:312
bool hasDefiniteLength(Dimension dimension, float ownerSize)
Definition Node.h:90
bool getHasNewLayout() const
Definition Node.h:58
size_t getChildCount() const
Definition Node.h:149
Node * getChild(size_t index) const
Definition Node.h:145
bool hasBaselineFunc() const noexcept
Definition Node.h:76
bool hasNewLayout_
Definition Node.h:311
LayoutResults layout_
Definition Node.h:321
bool hasMeasureFunc() const noexcept
Definition Node.h:66
Node * getOwner() const
Definition Node.h:137
NodeType getNodeType() const
Definition Node.h:62
const Config * getConfig() const
Definition Node.h:171
Node(const Config *config)
LayoutableChildren getLayoutChildren() const
Definition Node.h:153
bool hasErrata(Errata errata) const
Definition Node.h:95
void setIsReferenceBaseline(bool isReferenceBaseline)
Definition Node.h:243
const LayoutResults & getLayout() const
Definition Node.h:121
void setStyle(const Style &style)
Definition Node.h:231
size_t getLineIndex() const
Definition Node.h:125
Node & operator=(Node &&) noexcept=default
void * getContext() const
Definition Node.h:50
bool alwaysFormsContainingBlock_
Definition Node.h:314
bool isReferenceBaseline() const
Definition Node.h:129
const std::vector< Node * > & getChildren() const
Definition Node.h:141
bool hasContentsChildren() const
Definition Node.h:99
void setContext(void *context)
Definition Node.h:205
Node(const Node &node)=default
const Style & style() const
Definition Node.h:112
void setOwner(Node *owner)
Definition Node.h:247
void setLineIndex(size_t lineIndex)
Definition Node.h:239
Definition Style.h:40
Definition StyleSizeLength.h:29
Definition Benchmark.cpp:19
Direction
Definition Direction.h:18
NodeType
Definition NodeType.h:18
FlexDirection resolveDirection(const FlexDirection flexDirection, const Direction direction)
Definition FlexDirection.h:31
Dimension dimension(FlexDirection flexDirection)
Definition FlexDirection.h:105
FlexDirection
Definition FlexDirection.h:18
MeasureMode
Definition MeasureMode.h:18
Dimension
Definition Dimension.h:18
Errata
Definition Errata.h:18
Config * resolveRef(const YGConfigRef ref)
Definition Config.h:84
PhysicalEdge
Definition PhysicalEdge.h:14
Definition Node.h:29
Definition YGNode.h:183
Definition FloatOptional.h:15
constexpr bool isDefined() const
Definition FloatOptional.h:36
Definition LayoutResults.h:22