2 * Copyright (c) 2014-present, Facebook, Inc.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
21 /** Large positive number signifies that the property(float) is undefined.
22 *Earlier we used to have YGundefined as NAN, but the downside of this is that
23 *we can't use -ffast-math compiler flag as it assumes all floating-point
24 *calculation involve and result into finite numbers. For more information
25 *regarding -ffast-math compiler flag in clang, have a look at
26 *https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math
28 #define YGUndefined 10E20F
35 typedef struct YGSize {
40 typedef struct YGValue {
45 extern const YGValue YGValueUndefined;
46 extern const YGValue YGValueAuto;
48 typedef struct YGConfig *YGConfigRef;
50 typedef struct YGNode* YGNodeRef;
52 typedef YGSize (*YGMeasureFunc)(YGNodeRef node,
54 YGMeasureMode widthMode,
56 YGMeasureMode heightMode);
57 typedef float (*YGBaselineFunc)(YGNodeRef node, const float width, const float height);
58 typedef void (*YGDirtiedFunc)(YGNodeRef node);
59 typedef void (*YGPrintFunc)(YGNodeRef node);
60 typedef int (*YGLogger)(const YGConfigRef config,
66 *YGCloneNodeFunc)(YGNodeRef oldNode, YGNodeRef owner, int childIndex);
69 WIN_EXPORT YGNodeRef YGNodeNew(void);
70 WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config);
71 WIN_EXPORT YGNodeRef YGNodeClone(const YGNodeRef node);
72 WIN_EXPORT void YGNodeFree(const YGNodeRef node);
73 WIN_EXPORT void YGNodeFreeRecursive(const YGNodeRef node);
74 WIN_EXPORT void YGNodeReset(const YGNodeRef node);
75 WIN_EXPORT int32_t YGNodeGetInstanceCount(void);
77 WIN_EXPORT void YGNodeInsertChild(const YGNodeRef node,
78 const YGNodeRef child,
79 const uint32_t index);
81 // This function inserts the child YGNodeRef as a children of the node received
82 // by parameter and set the Owner of the child object to null. This function is
83 // expected to be called when using Yoga in persistent mode in order to share a
84 // YGNodeRef object as a child of two different Yoga trees. The child YGNodeRef
85 // is expected to be referenced from its original owner and from a clone of its
87 WIN_EXPORT void YGNodeInsertSharedChild(
89 const YGNodeRef child,
90 const uint32_t index);
91 WIN_EXPORT void YGNodeRemoveChild(const YGNodeRef node, const YGNodeRef child);
92 WIN_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef node);
93 WIN_EXPORT YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index);
94 WIN_EXPORT YGNodeRef YGNodeGetOwner(const YGNodeRef node);
95 WIN_EXPORT uint32_t YGNodeGetChildCount(const YGNodeRef node);
96 WIN_EXPORT void YGNodeSetChildren(
97 YGNodeRef const owner,
98 const YGNodeRef children[],
99 const uint32_t count);
101 WIN_EXPORT void YGNodeCalculateLayout(const YGNodeRef node,
102 const float availableWidth,
103 const float availableHeight,
104 const YGDirection ownerDirection);
106 // Mark a node as dirty. Only valid for nodes with a custom measure function
108 // YG knows when to mark all other nodes as dirty but because nodes with
110 // depends on information not known to YG they must perform this dirty
112 WIN_EXPORT void YGNodeMarkDirty(const YGNodeRef node);
114 // This function marks the current node and all its descendants as dirty. This function is added to test yoga benchmarks.
115 // This function is not expected to be used in production as calling `YGCalculateLayout` will cause the recalculation of each and every node.
116 WIN_EXPORT void YGNodeMarkDirtyAndPropogateToDescendants(const YGNodeRef node);
118 WIN_EXPORT void YGNodePrint(const YGNodeRef node, const YGPrintOptions options);
120 WIN_EXPORT bool YGFloatIsUndefined(const float value);
122 WIN_EXPORT bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode,
124 const YGMeasureMode heightMode,
126 const YGMeasureMode lastWidthMode,
127 const float lastWidth,
128 const YGMeasureMode lastHeightMode,
129 const float lastHeight,
130 const float lastComputedWidth,
131 const float lastComputedHeight,
132 const float marginRow,
133 const float marginColumn,
134 const YGConfigRef config);
136 WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode);
138 #define YG_NODE_PROPERTY(type, name, paramName) \
139 WIN_EXPORT void YGNodeSet##name(const YGNodeRef node, type paramName); \
140 WIN_EXPORT type YGNodeGet##name(const YGNodeRef node);
142 #define YG_NODE_STYLE_PROPERTY(type, name, paramName) \
143 WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, const type paramName); \
144 WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node);
146 #define YG_NODE_STYLE_PROPERTY_UNIT(type, name, paramName) \
147 WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, const float paramName); \
148 WIN_EXPORT void YGNodeStyleSet##name##Percent(const YGNodeRef node, const float paramName); \
149 WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node);
151 #define YG_NODE_STYLE_PROPERTY_UNIT_AUTO(type, name, paramName) \
152 YG_NODE_STYLE_PROPERTY_UNIT(type, name, paramName) \
153 WIN_EXPORT void YGNodeStyleSet##name##Auto(const YGNodeRef node);
155 #define YG_NODE_STYLE_EDGE_PROPERTY(type, name, paramName) \
156 WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, \
158 const type paramName); \
159 WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge);
161 #define YG_NODE_STYLE_EDGE_PROPERTY_UNIT(type, name, paramName) \
162 WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, \
164 const float paramName); \
165 WIN_EXPORT void YGNodeStyleSet##name##Percent(const YGNodeRef node, \
167 const float paramName); \
168 WIN_EXPORT WIN_STRUCT(type) YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge);
170 #define YG_NODE_STYLE_EDGE_PROPERTY_UNIT_AUTO(type, name) \
171 WIN_EXPORT void YGNodeStyleSet##name##Auto(const YGNodeRef node, const YGEdge edge);
173 #define YG_NODE_LAYOUT_PROPERTY(type, name) \
174 WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node);
176 #define YG_NODE_LAYOUT_EDGE_PROPERTY(type, name) \
177 WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge);
179 void* YGNodeGetContext(YGNodeRef node);
180 void YGNodeSetContext(YGNodeRef node, void* context);
181 YGMeasureFunc YGNodeGetMeasureFunc(YGNodeRef node);
182 void YGNodeSetMeasureFunc(YGNodeRef node, YGMeasureFunc measureFunc);
183 YGBaselineFunc YGNodeGetBaselineFunc(YGNodeRef node);
184 void YGNodeSetBaselineFunc(YGNodeRef node, YGBaselineFunc baselineFunc);
185 YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeRef node);
186 void YGNodeSetDirtiedFunc(YGNodeRef node, YGDirtiedFunc dirtiedFunc);
187 YGPrintFunc YGNodeGetPrintFunc(YGNodeRef node);
188 void YGNodeSetPrintFunc(YGNodeRef node, YGPrintFunc printFunc);
189 bool YGNodeGetHasNewLayout(YGNodeRef node);
190 void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout);
191 YGNodeType YGNodeGetNodeType(YGNodeRef node);
192 void YGNodeSetNodeType(YGNodeRef node, YGNodeType nodeType);
193 bool YGNodeIsDirty(YGNodeRef node);
194 bool YGNodeLayoutGetDidUseLegacyFlag(const YGNodeRef node);
196 YG_NODE_STYLE_PROPERTY(YGDirection, Direction, direction);
197 YG_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection);
198 YG_NODE_STYLE_PROPERTY(YGJustify, JustifyContent, justifyContent);
199 YG_NODE_STYLE_PROPERTY(YGAlign, AlignContent, alignContent);
200 YG_NODE_STYLE_PROPERTY(YGAlign, AlignItems, alignItems);
201 YG_NODE_STYLE_PROPERTY(YGAlign, AlignSelf, alignSelf);
202 YG_NODE_STYLE_PROPERTY(YGPositionType, PositionType, positionType);
203 YG_NODE_STYLE_PROPERTY(YGWrap, FlexWrap, flexWrap);
204 YG_NODE_STYLE_PROPERTY(YGOverflow, Overflow, overflow);
205 YG_NODE_STYLE_PROPERTY(YGDisplay, Display, display);
206 YG_NODE_STYLE_PROPERTY(float, Flex, flex);
207 YG_NODE_STYLE_PROPERTY(float, FlexGrow, flexGrow);
208 YG_NODE_STYLE_PROPERTY(float, FlexShrink, flexShrink);
209 YG_NODE_STYLE_PROPERTY_UNIT_AUTO(YGValue, FlexBasis, flexBasis);
211 YG_NODE_STYLE_EDGE_PROPERTY_UNIT(YGValue, Position, position);
212 YG_NODE_STYLE_EDGE_PROPERTY_UNIT(YGValue, Margin, margin);
213 YG_NODE_STYLE_EDGE_PROPERTY_UNIT_AUTO(YGValue, Margin);
214 YG_NODE_STYLE_EDGE_PROPERTY_UNIT(YGValue, Padding, padding);
215 YG_NODE_STYLE_EDGE_PROPERTY(float, Border, border);
217 YG_NODE_STYLE_PROPERTY_UNIT_AUTO(YGValue, Width, width);
218 YG_NODE_STYLE_PROPERTY_UNIT_AUTO(YGValue, Height, height);
219 YG_NODE_STYLE_PROPERTY_UNIT(YGValue, MinWidth, minWidth);
220 YG_NODE_STYLE_PROPERTY_UNIT(YGValue, MinHeight, minHeight);
221 YG_NODE_STYLE_PROPERTY_UNIT(YGValue, MaxWidth, maxWidth);
222 YG_NODE_STYLE_PROPERTY_UNIT(YGValue, MaxHeight, maxHeight);
224 // Yoga specific properties, not compatible with flexbox specification
225 // Aspect ratio control the size of the undefined dimension of a node.
226 // Aspect ratio is encoded as a floating point value width/height. e.g. A value of 2 leads to a node
227 // with a width twice the size of its height while a value of 0.5 gives the opposite effect.
229 // - On a node with a set width/height aspect ratio control the size of the unset dimension
230 // - On a node with a set flex basis aspect ratio controls the size of the node in the cross axis if
232 // - On a node with a measure function aspect ratio works as though the measure function measures
234 // - On a node with flex grow/shrink aspect ratio controls the size of the node in the cross axis if
236 // - Aspect ratio takes min/max dimensions into account
237 YG_NODE_STYLE_PROPERTY(float, AspectRatio, aspectRatio);
239 YG_NODE_LAYOUT_PROPERTY(float, Left);
240 YG_NODE_LAYOUT_PROPERTY(float, Top);
241 YG_NODE_LAYOUT_PROPERTY(float, Right);
242 YG_NODE_LAYOUT_PROPERTY(float, Bottom);
243 YG_NODE_LAYOUT_PROPERTY(float, Width);
244 YG_NODE_LAYOUT_PROPERTY(float, Height);
245 YG_NODE_LAYOUT_PROPERTY(YGDirection, Direction);
246 YG_NODE_LAYOUT_PROPERTY(bool, HadOverflow);
247 bool YGNodeLayoutGetDidLegacyStretchFlagAffectLayout(const YGNodeRef node);
249 // Get the computed values for these nodes after performing layout. If they were set using
250 // point values then the returned value will be the same as YGNodeStyleGetXXX. However if
251 // they were set using a percentage value then the returned value is the computed value used
253 YG_NODE_LAYOUT_EDGE_PROPERTY(float, Margin);
254 YG_NODE_LAYOUT_EDGE_PROPERTY(float, Border);
255 YG_NODE_LAYOUT_EDGE_PROPERTY(float, Padding);
257 WIN_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger);
258 WIN_EXPORT void YGLog(const YGNodeRef node, YGLogLevel level, const char *message, ...);
259 WIN_EXPORT void YGLogWithConfig(const YGConfigRef config, YGLogLevel level, const char *format, ...);
260 WIN_EXPORT void YGAssert(const bool condition, const char *message);
261 WIN_EXPORT void YGAssertWithNode(const YGNodeRef node, const bool condition, const char *message);
262 WIN_EXPORT void YGAssertWithConfig(const YGConfigRef config,
263 const bool condition,
264 const char *message);
265 // Set this to number of pixels in 1 point to round calculation results
266 // If you want to avoid rounding - set PointScaleFactor to 0
267 WIN_EXPORT void YGConfigSetPointScaleFactor(const YGConfigRef config, const float pixelsInPoint);
268 void YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(
269 const YGConfigRef config,
270 const bool shouldDiffLayout);
272 // Yoga previously had an error where containers would take the maximum space possible instead of
274 // like they are supposed to. In practice this resulted in implicit behaviour similar to align-self:
276 // Because this was such a long-standing bug we must allow legacy users to switch back to this
278 WIN_EXPORT void YGConfigSetUseLegacyStretchBehaviour(const YGConfigRef config,
279 const bool useLegacyStretchBehaviour);
282 WIN_EXPORT YGConfigRef YGConfigNew(void);
283 WIN_EXPORT void YGConfigFree(const YGConfigRef config);
284 WIN_EXPORT void YGConfigCopy(const YGConfigRef dest, const YGConfigRef src);
285 WIN_EXPORT int32_t YGConfigGetInstanceCount(void);
287 WIN_EXPORT void YGConfigSetExperimentalFeatureEnabled(const YGConfigRef config,
288 const YGExperimentalFeature feature,
290 WIN_EXPORT bool YGConfigIsExperimentalFeatureEnabled(const YGConfigRef config,
291 const YGExperimentalFeature feature);
293 // Using the web defaults is the prefered configuration for new projects.
294 // Usage of non web defaults should be considered as legacy.
295 WIN_EXPORT void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled);
296 WIN_EXPORT bool YGConfigGetUseWebDefaults(const YGConfigRef config);
298 WIN_EXPORT void YGConfigSetCloneNodeFunc(const YGConfigRef config,
299 const YGCloneNodeFunc callback);
301 // Export only for C#
302 WIN_EXPORT YGConfigRef YGConfigGetDefault(void);
304 WIN_EXPORT void YGConfigSetContext(const YGConfigRef config, void *context);
305 WIN_EXPORT void *YGConfigGetContext(const YGConfigRef config);
307 WIN_EXPORT float YGRoundValueToPixelGrid(
309 const float pointScaleFactor,
310 const bool forceCeil,
311 const bool forceFloor);
317 #include <functional>
320 // Calls f on each node in the tree including the given node argument.
321 extern void YGTraversePreOrder(YGNodeRef const node, std::function<void(YGNodeRef node)>&& f);
323 extern void YGNodeSetChildren(
324 YGNodeRef const owner,
325 const std::vector<YGNodeRef>& children);