Utils.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the LICENSE
  5. * file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include "YGNode.h"
  9. #include "Yoga-internal.h"
  10. #include "CompactValue.h"
  11. // This struct is an helper model to hold the data for step 4 of flexbox algo,
  12. // which is collecting the flex items in a line.
  13. //
  14. // - itemsOnLine: Number of items which can fit in a line considering the
  15. // available Inner dimension, the flex items computed flexbasis and their
  16. // margin. It may be different than the difference between start and end
  17. // indicates because we skip over absolute-positioned items.
  18. //
  19. // - sizeConsumedOnCurrentLine: It is accumulation of the dimensions and margin
  20. // of all the children on the current line. This will be used in order to
  21. // either set the dimensions of the node if none already exist or to compute
  22. // the remaining space left for the flexible children.
  23. //
  24. // - totalFlexGrowFactors: total flex grow factors of flex items which are to be
  25. // layed in the current line
  26. //
  27. // - totalFlexShrinkFactors: total flex shrink factors of flex items which are
  28. // to be layed in the current line
  29. //
  30. // - endOfLineIndex: Its the end index of the last flex item which was examined
  31. // and it may or may not be part of the current line(as it may be absolutely
  32. // positioned or inculding it may have caused to overshoot availableInnerDim)
  33. //
  34. // - relativeChildren: Maintain a vector of the child nodes that can shrink
  35. // and/or grow.
  36. struct YGCollectFlexItemsRowValues {
  37. uint32_t itemsOnLine;
  38. float sizeConsumedOnCurrentLine;
  39. float totalFlexGrowFactors;
  40. float totalFlexShrinkScaledFactors;
  41. uint32_t endOfLineIndex;
  42. std::vector<YGNodeRef> relativeChildren;
  43. float remainingFreeSpace;
  44. // The size of the mainDim for the row after considering size, padding, margin
  45. // and border of flex items. This is used to calculate maxLineDim after going
  46. // through all the rows to decide on the main axis size of owner.
  47. float mainDim;
  48. // The size of the crossDim for the row after considering size, padding,
  49. // margin and border of flex items. Used for calculating containers crossSize.
  50. float crossDim;
  51. };
  52. bool YGValueEqual(const YGValue& a, const YGValue& b);
  53. inline bool YGValueEqual(
  54. facebook::yoga::detail::CompactValue a,
  55. facebook::yoga::detail::CompactValue b) {
  56. return YGValueEqual((YGValue) a, (YGValue) b);
  57. }
  58. // This custom float equality function returns true if either absolute
  59. // difference between two floats is less than 0.0001f or both are undefined.
  60. bool YGFloatsEqual(const float a, const float b);
  61. float YGFloatMax(const float a, const float b);
  62. YGFloatOptional YGFloatOptionalMax(
  63. const YGFloatOptional op1,
  64. const YGFloatOptional op2);
  65. float YGFloatMin(const float a, const float b);
  66. // This custom float comparision function compares the array of float with
  67. // YGFloatsEqual, as the default float comparision operator will not work(Look
  68. // at the comments of YGFloatsEqual function).
  69. template <std::size_t size>
  70. bool YGFloatArrayEqual(
  71. const std::array<float, size>& val1,
  72. const std::array<float, size>& val2) {
  73. bool areEqual = true;
  74. for (std::size_t i = 0; i < size && areEqual; ++i) {
  75. areEqual = YGFloatsEqual(val1[i], val2[i]);
  76. }
  77. return areEqual;
  78. }
  79. // This function returns 0 if YGFloatIsUndefined(val) is true and val otherwise
  80. float YGFloatSanitize(const float val);
  81. YGFlexDirection YGFlexDirectionCross(
  82. const YGFlexDirection flexDirection,
  83. const YGDirection direction);
  84. inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) {
  85. return flexDirection == YGFlexDirectionRow ||
  86. flexDirection == YGFlexDirectionRowReverse;
  87. }
  88. inline YGFloatOptional YGResolveValue(
  89. const YGValue value,
  90. const float ownerSize) {
  91. switch (value.unit) {
  92. case YGUnitPoint:
  93. return YGFloatOptional{value.value};
  94. case YGUnitPercent:
  95. return YGFloatOptional{value.value * ownerSize * 0.01f};
  96. default:
  97. return YGFloatOptional{};
  98. }
  99. }
  100. inline YGFloatOptional YGResolveValue(
  101. yoga::detail::CompactValue value,
  102. float ownerSize) {
  103. return YGResolveValue((YGValue) value, ownerSize);
  104. }
  105. inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) {
  106. return flexDirection == YGFlexDirectionColumn ||
  107. flexDirection == YGFlexDirectionColumnReverse;
  108. }
  109. inline YGFlexDirection YGResolveFlexDirection(
  110. const YGFlexDirection flexDirection,
  111. const YGDirection direction) {
  112. if (direction == YGDirectionRTL) {
  113. if (flexDirection == YGFlexDirectionRow) {
  114. return YGFlexDirectionRowReverse;
  115. } else if (flexDirection == YGFlexDirectionRowReverse) {
  116. return YGFlexDirectionRow;
  117. }
  118. }
  119. return flexDirection;
  120. }
  121. inline YGFloatOptional YGResolveValueMargin(
  122. yoga::detail::CompactValue value,
  123. const float ownerSize) {
  124. return value.isAuto() ? YGFloatOptional{0} : YGResolveValue(value, ownerSize);
  125. }