primitives.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) Facebook, Inc. and its 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. #pragma once
  8. #include <folly/Optional.h>
  9. #include <react/graphics/Color.h>
  10. #include <react/graphics/Geometry.h>
  11. #include <array>
  12. #include <cmath>
  13. namespace facebook {
  14. namespace react {
  15. enum class PointerEventsMode { Auto, None, BoxNone, BoxOnly };
  16. enum class BackfaceVisibility { Auto, Visible, Hidden };
  17. enum class BorderStyle { Solid, Dotted, Dashed };
  18. template <typename T>
  19. struct CascadedRectangleEdges {
  20. using Counterpart = RectangleEdges<T>;
  21. using OptionalT = folly::Optional<T>;
  22. OptionalT left{};
  23. OptionalT top{};
  24. OptionalT right{};
  25. OptionalT bottom{};
  26. OptionalT start{};
  27. OptionalT end{};
  28. OptionalT horizontal{};
  29. OptionalT vertical{};
  30. OptionalT all{};
  31. Counterpart resolve(bool isRTL, T defaults) const {
  32. const auto leading = isRTL ? end : start;
  33. const auto trailing = isRTL ? start : end;
  34. const auto horizontalOrAllOrDefault =
  35. horizontal.value_or(all.value_or(defaults));
  36. const auto verticalOrAllOrDefault =
  37. vertical.value_or(all.value_or(defaults));
  38. return {
  39. /* .left = */ left.value_or(leading.value_or(horizontalOrAllOrDefault)),
  40. /* .top = */ top.value_or(verticalOrAllOrDefault),
  41. /* .right = */
  42. right.value_or(trailing.value_or(horizontalOrAllOrDefault)),
  43. /* .bottom = */ bottom.value_or(verticalOrAllOrDefault),
  44. };
  45. }
  46. bool operator==(const CascadedRectangleEdges<T> &rhs) const {
  47. return std::tie(
  48. this->left,
  49. this->top,
  50. this->right,
  51. this->bottom,
  52. this->start,
  53. this->end,
  54. this->horizontal,
  55. this->vertical,
  56. this->all) ==
  57. std::tie(
  58. rhs.left,
  59. rhs.top,
  60. rhs.right,
  61. rhs.bottom,
  62. rhs.start,
  63. rhs.end,
  64. rhs.horizontal,
  65. rhs.vertical,
  66. rhs.all);
  67. }
  68. bool operator!=(const CascadedRectangleEdges<T> &rhs) const {
  69. return !(*this == rhs);
  70. }
  71. };
  72. template <typename T>
  73. struct CascadedRectangleCorners {
  74. using Counterpart = RectangleCorners<T>;
  75. using OptionalT = folly::Optional<T>;
  76. OptionalT topLeft{};
  77. OptionalT topRight{};
  78. OptionalT bottomLeft{};
  79. OptionalT bottomRight{};
  80. OptionalT topStart{};
  81. OptionalT topEnd{};
  82. OptionalT bottomStart{};
  83. OptionalT bottomEnd{};
  84. OptionalT all{};
  85. Counterpart resolve(bool isRTL, T defaults) const {
  86. const auto topLeading = isRTL ? topEnd : topStart;
  87. const auto topTrailing = isRTL ? topStart : topEnd;
  88. const auto bottomLeading = isRTL ? bottomEnd : bottomStart;
  89. const auto bottomTrailing = isRTL ? bottomStart : bottomEnd;
  90. return {
  91. /* .topLeft = */ topLeft.value_or(
  92. topLeading.value_or(all.value_or(defaults))),
  93. /* .topRight = */
  94. topRight.value_or(topTrailing.value_or(all.value_or(defaults))),
  95. /* .bottomLeft = */
  96. bottomLeft.value_or(bottomLeading.value_or(all.value_or(defaults))),
  97. /* .bottomRight = */
  98. bottomRight.value_or(bottomTrailing.value_or(all.value_or(defaults))),
  99. };
  100. }
  101. bool operator==(const CascadedRectangleCorners<T> &rhs) const {
  102. return std::tie(
  103. this->topLeft,
  104. this->topRight,
  105. this->bottomLeft,
  106. this->bottomRight,
  107. this->topStart,
  108. this->topEnd,
  109. this->bottomStart,
  110. this->bottomEnd,
  111. this->all) ==
  112. std::tie(
  113. rhs.topLeft,
  114. rhs.topRight,
  115. rhs.bottomLeft,
  116. rhs.bottomRight,
  117. rhs.topStart,
  118. rhs.topEnd,
  119. rhs.bottomStart,
  120. rhs.bottomEnd,
  121. rhs.all);
  122. }
  123. bool operator!=(const CascadedRectangleCorners<T> &rhs) const {
  124. return !(*this == rhs);
  125. }
  126. };
  127. using BorderWidths = RectangleEdges<Float>;
  128. using BorderStyles = RectangleEdges<BorderStyle>;
  129. using BorderColors = RectangleEdges<SharedColor>;
  130. using BorderRadii = RectangleCorners<Float>;
  131. using CascadedBorderWidths = CascadedRectangleEdges<Float>;
  132. using CascadedBorderStyles = CascadedRectangleEdges<BorderStyle>;
  133. using CascadedBorderColors = CascadedRectangleEdges<SharedColor>;
  134. using CascadedBorderRadii = CascadedRectangleCorners<Float>;
  135. struct BorderMetrics {
  136. BorderColors borderColors{};
  137. BorderWidths borderWidths{};
  138. BorderRadii borderRadii{};
  139. BorderStyles borderStyles{};
  140. bool operator==(const BorderMetrics &rhs) const {
  141. return std::tie(
  142. this->borderColors,
  143. this->borderWidths,
  144. this->borderRadii,
  145. this->borderStyles) ==
  146. std::tie(
  147. rhs.borderColors,
  148. rhs.borderWidths,
  149. rhs.borderRadii,
  150. rhs.borderStyles);
  151. }
  152. bool operator!=(const BorderMetrics &rhs) const {
  153. return !(*this == rhs);
  154. }
  155. };
  156. } // namespace react
  157. } // namespace facebook