YGNode.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc.
  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. #pragma once
  8. #include <stdio.h>
  9. #include "YGConfig.h"
  10. #include "YGLayout.h"
  11. #include "YGStyle.h"
  12. #include "Yoga-internal.h"
  13. struct YGNode {
  14. private:
  15. void* context_;
  16. YGPrintFunc print_;
  17. bool hasNewLayout_;
  18. YGNodeType nodeType_;
  19. YGMeasureFunc measure_;
  20. YGBaselineFunc baseline_;
  21. YGDirtiedFunc dirtied_;
  22. YGStyle style_;
  23. YGLayout layout_;
  24. uint32_t lineIndex_;
  25. YGNodeRef owner_;
  26. YGVector children_;
  27. YGConfigRef config_;
  28. bool isDirty_;
  29. std::array<YGValue, 2> resolvedDimensions_;
  30. YGFloatOptional relativePosition(
  31. const YGFlexDirection& axis,
  32. const float& axisSize) const;
  33. public:
  34. YGNode();
  35. ~YGNode();
  36. explicit YGNode(const YGConfigRef newConfig);
  37. YGNode(const YGNode& node);
  38. YGNode& operator=(const YGNode& node);
  39. YGNode(
  40. void* context,
  41. YGPrintFunc print,
  42. bool hasNewLayout,
  43. YGNodeType nodeType,
  44. YGMeasureFunc measure,
  45. YGBaselineFunc baseline,
  46. YGDirtiedFunc dirtied,
  47. YGStyle style,
  48. const YGLayout& layout,
  49. uint32_t lineIndex,
  50. YGNodeRef owner,
  51. const YGVector& children,
  52. YGConfigRef config,
  53. bool isDirty,
  54. std::array<YGValue, 2> resolvedDimensions);
  55. // Getters
  56. void* getContext() const {
  57. return context_;
  58. }
  59. YGPrintFunc getPrintFunc() const {
  60. return print_;
  61. }
  62. bool getHasNewLayout() const {
  63. return hasNewLayout_;
  64. }
  65. YGNodeType getNodeType() const {
  66. return nodeType_;
  67. }
  68. YGMeasureFunc getMeasure() const {
  69. return measure_;
  70. }
  71. YGBaselineFunc getBaseline() const {
  72. return baseline_;
  73. }
  74. YGDirtiedFunc getDirtied() const {
  75. return dirtied_;
  76. }
  77. // For Performance reasons passing as reference.
  78. YGStyle& getStyle() {
  79. return style_;
  80. }
  81. const YGStyle& getStyle() const {
  82. return style_;
  83. }
  84. // For Performance reasons passing as reference.
  85. YGLayout& getLayout() {
  86. return layout_;
  87. }
  88. const YGLayout& getLayout() const {
  89. return layout_;
  90. }
  91. uint32_t getLineIndex() const {
  92. return lineIndex_;
  93. }
  94. // returns the YGNodeRef that owns this YGNode. An owner is used to identify
  95. // the YogaTree that a YGNode belongs to.
  96. // This method will return the parent of the YGNode when a YGNode only belongs
  97. // to one YogaTree or nullptr when the YGNode is shared between two or more
  98. // YogaTrees.
  99. YGNodeRef getOwner() const {
  100. return owner_;
  101. }
  102. // Deprecated, use getOwner() instead.
  103. YGNodeRef getParent() const {
  104. return getOwner();
  105. }
  106. YGVector getChildren() const {
  107. return children_;
  108. }
  109. uint32_t getChildrenCount() const {
  110. return static_cast<uint32_t>(children_.size());
  111. }
  112. YGNodeRef getChild(uint32_t index) const {
  113. return children_.at(index);
  114. }
  115. YGConfigRef getConfig() const {
  116. return config_;
  117. }
  118. bool isDirty() const {
  119. return isDirty_;
  120. }
  121. std::array<YGValue, 2> getResolvedDimensions() const {
  122. return resolvedDimensions_;
  123. }
  124. YGValue getResolvedDimension(int index) const {
  125. return resolvedDimensions_[index];
  126. }
  127. // Methods related to positions, margin, padding and border
  128. YGFloatOptional getLeadingPosition(const YGFlexDirection& axis,
  129. const float& axisSize) const;
  130. bool isLeadingPositionDefined(const YGFlexDirection& axis) const;
  131. bool isTrailingPosDefined(const YGFlexDirection& axis) const;
  132. YGFloatOptional getTrailingPosition(
  133. const YGFlexDirection& axis,
  134. const float& axisSize) const;
  135. YGFloatOptional getLeadingMargin(
  136. const YGFlexDirection& axis,
  137. const float& widthSize) const;
  138. YGFloatOptional getTrailingMargin(
  139. const YGFlexDirection& axis,
  140. const float& widthSize) const;
  141. float getLeadingBorder(const YGFlexDirection& flexDirection) const;
  142. float getTrailingBorder(const YGFlexDirection& flexDirection) const;
  143. YGFloatOptional getLeadingPadding(
  144. const YGFlexDirection& axis,
  145. const float& widthSize) const;
  146. YGFloatOptional getTrailingPadding(
  147. const YGFlexDirection& axis,
  148. const float& widthSize) const;
  149. YGFloatOptional getLeadingPaddingAndBorder(
  150. const YGFlexDirection& axis,
  151. const float& widthSize) const;
  152. YGFloatOptional getTrailingPaddingAndBorder(
  153. const YGFlexDirection& axis,
  154. const float& widthSize) const;
  155. YGFloatOptional getMarginForAxis(
  156. const YGFlexDirection& axis,
  157. const float& widthSize) const;
  158. // Setters
  159. void setContext(void* context) {
  160. context_ = context;
  161. }
  162. void setPrintFunc(YGPrintFunc printFunc) {
  163. print_ = printFunc;
  164. }
  165. void setHasNewLayout(bool hasNewLayout) {
  166. hasNewLayout_ = hasNewLayout;
  167. }
  168. void setNodeType(YGNodeType nodeType) {
  169. nodeType_ = nodeType;
  170. }
  171. void setStyleFlexDirection(YGFlexDirection direction) {
  172. style_.flexDirection = direction;
  173. }
  174. void setStyleAlignContent(YGAlign alignContent) {
  175. style_.alignContent = alignContent;
  176. }
  177. void setMeasureFunc(YGMeasureFunc measureFunc);
  178. void setBaseLineFunc(YGBaselineFunc baseLineFunc) {
  179. baseline_ = baseLineFunc;
  180. }
  181. void setDirtiedFunc(YGDirtiedFunc dirtiedFunc) {
  182. dirtied_ = dirtiedFunc;
  183. }
  184. void setStyle(const YGStyle& style) {
  185. style_ = style;
  186. }
  187. void setLayout(const YGLayout& layout) {
  188. layout_ = layout;
  189. }
  190. void setLineIndex(uint32_t lineIndex) {
  191. lineIndex_ = lineIndex;
  192. }
  193. void setOwner(YGNodeRef owner) {
  194. owner_ = owner;
  195. }
  196. void setChildren(const YGVector& children) {
  197. children_ = children;
  198. }
  199. // TODO: rvalue override for setChildren
  200. void setConfig(YGConfigRef config) {
  201. config_ = config;
  202. }
  203. void setDirty(bool isDirty);
  204. void setLayoutLastOwnerDirection(YGDirection direction);
  205. void setLayoutComputedFlexBasis(const YGFloatOptional& computedFlexBasis);
  206. void setLayoutComputedFlexBasisGeneration(
  207. uint32_t computedFlexBasisGeneration);
  208. void setLayoutMeasuredDimension(float measuredDimension, int index);
  209. void setLayoutHadOverflow(bool hadOverflow);
  210. void setLayoutDimension(float dimension, int index);
  211. void setLayoutDirection(YGDirection direction);
  212. void setLayoutMargin(float margin, int index);
  213. void setLayoutBorder(float border, int index);
  214. void setLayoutPadding(float padding, int index);
  215. void setLayoutPosition(float position, int index);
  216. void setPosition(
  217. const YGDirection direction,
  218. const float mainSize,
  219. const float crossSize,
  220. const float ownerWidth);
  221. void setAndPropogateUseLegacyFlag(bool useLegacyFlag);
  222. void setLayoutDoesLegacyFlagAffectsLayout(bool doesLegacyFlagAffectsLayout);
  223. void setLayoutDidUseLegacyFlag(bool didUseLegacyFlag);
  224. void markDirtyAndPropogateDownwards();
  225. // Other methods
  226. YGValue marginLeadingValue(const YGFlexDirection axis) const;
  227. YGValue marginTrailingValue(const YGFlexDirection axis) const;
  228. YGValue resolveFlexBasisPtr() const;
  229. void resolveDimension();
  230. YGDirection resolveDirection(const YGDirection ownerDirection);
  231. void clearChildren();
  232. /// Replaces the occurrences of oldChild with newChild
  233. void replaceChild(YGNodeRef oldChild, YGNodeRef newChild);
  234. void replaceChild(YGNodeRef child, uint32_t index);
  235. void insertChild(YGNodeRef child, uint32_t index);
  236. /// Removes the first occurrence of child
  237. bool removeChild(YGNodeRef child);
  238. void removeChild(uint32_t index);
  239. void cloneChildrenIfNeeded();
  240. void markDirtyAndPropogate();
  241. float resolveFlexGrow();
  242. float resolveFlexShrink();
  243. bool isNodeFlexible();
  244. bool didUseLegacyFlag();
  245. bool isLayoutTreeEqualToNode(const YGNode& node) const;
  246. };