propsConversions.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <react/components/view/conversions.h>
  9. #include <react/core/propsConversions.h>
  10. namespace facebook {
  11. namespace react {
  12. static inline YGStyle::Dimensions convertRawProp(
  13. RawProps const &rawProps,
  14. char const *widthName,
  15. char const *heightName,
  16. YGStyle::Dimensions const &sourceValue,
  17. YGStyle::Dimensions const &defaultValue) {
  18. auto dimensions = defaultValue;
  19. dimensions[YGDimensionWidth] = convertRawProp(
  20. rawProps,
  21. widthName,
  22. sourceValue[YGDimensionWidth],
  23. defaultValue[YGDimensionWidth]);
  24. dimensions[YGDimensionHeight] = convertRawProp(
  25. rawProps,
  26. heightName,
  27. sourceValue[YGDimensionHeight],
  28. defaultValue[YGDimensionWidth]);
  29. return dimensions;
  30. }
  31. static inline YGStyle::Edges convertRawProp(
  32. RawProps const &rawProps,
  33. char const *prefix,
  34. char const *suffix,
  35. YGStyle::Edges const &sourceValue,
  36. YGStyle::Edges const &defaultValue) {
  37. auto result = defaultValue;
  38. result[YGEdgeLeft] = convertRawProp(
  39. rawProps,
  40. "Left",
  41. sourceValue[YGEdgeLeft],
  42. defaultValue[YGEdgeLeft],
  43. prefix,
  44. suffix);
  45. result[YGEdgeTop] = convertRawProp(
  46. rawProps,
  47. "Top",
  48. sourceValue[YGEdgeTop],
  49. defaultValue[YGEdgeTop],
  50. prefix,
  51. suffix);
  52. result[YGEdgeRight] = convertRawProp(
  53. rawProps,
  54. "Right",
  55. sourceValue[YGEdgeRight],
  56. defaultValue[YGEdgeRight],
  57. prefix,
  58. suffix);
  59. result[YGEdgeBottom] = convertRawProp(
  60. rawProps,
  61. "Bottom",
  62. sourceValue[YGEdgeBottom],
  63. defaultValue[YGEdgeBottom],
  64. prefix,
  65. suffix);
  66. result[YGEdgeStart] = convertRawProp(
  67. rawProps,
  68. "Start",
  69. sourceValue[YGEdgeStart],
  70. defaultValue[YGEdgeStart],
  71. prefix,
  72. suffix);
  73. result[YGEdgeEnd] = convertRawProp(
  74. rawProps,
  75. "End",
  76. sourceValue[YGEdgeEnd],
  77. defaultValue[YGEdgeEnd],
  78. prefix,
  79. suffix);
  80. result[YGEdgeHorizontal] = convertRawProp(
  81. rawProps,
  82. "Horizontal",
  83. sourceValue[YGEdgeHorizontal],
  84. defaultValue[YGEdgeHorizontal],
  85. prefix,
  86. suffix);
  87. result[YGEdgeVertical] = convertRawProp(
  88. rawProps,
  89. "Vertical",
  90. sourceValue[YGEdgeVertical],
  91. defaultValue[YGEdgeVertical],
  92. prefix,
  93. suffix);
  94. result[YGEdgeAll] = convertRawProp(
  95. rawProps,
  96. "",
  97. sourceValue[YGEdgeAll],
  98. defaultValue[YGEdgeAll],
  99. prefix,
  100. suffix);
  101. return result;
  102. }
  103. static inline YGStyle::Edges convertRawProp(
  104. RawProps const &rawProps,
  105. YGStyle::Edges const &sourceValue,
  106. YGStyle::Edges const &defaultValue) {
  107. auto result = defaultValue;
  108. result[YGEdgeLeft] = convertRawProp(
  109. rawProps, "left", sourceValue[YGEdgeLeft], defaultValue[YGEdgeLeft]);
  110. result[YGEdgeTop] = convertRawProp(
  111. rawProps, "top", sourceValue[YGEdgeTop], defaultValue[YGEdgeTop]);
  112. result[YGEdgeRight] = convertRawProp(
  113. rawProps, "right", sourceValue[YGEdgeRight], defaultValue[YGEdgeRight]);
  114. result[YGEdgeBottom] = convertRawProp(
  115. rawProps,
  116. "bottom",
  117. sourceValue[YGEdgeBottom],
  118. defaultValue[YGEdgeBottom]);
  119. result[YGEdgeStart] = convertRawProp(
  120. rawProps, "start", sourceValue[YGEdgeStart], defaultValue[YGEdgeStart]);
  121. result[YGEdgeEnd] = convertRawProp(
  122. rawProps, "end", sourceValue[YGEdgeEnd], defaultValue[YGEdgeEnd]);
  123. return result;
  124. }
  125. static inline YGStyle convertRawProp(
  126. RawProps const &rawProps,
  127. YGStyle const &sourceValue) {
  128. auto yogaStyle = YGStyle{};
  129. yogaStyle.direction() = convertRawProp(
  130. rawProps, "direction", sourceValue.direction(), yogaStyle.direction());
  131. yogaStyle.flexDirection() = convertRawProp(
  132. rawProps,
  133. "flexDirection",
  134. sourceValue.flexDirection(),
  135. yogaStyle.flexDirection());
  136. yogaStyle.justifyContent() = convertRawProp(
  137. rawProps,
  138. "justifyContent",
  139. sourceValue.justifyContent(),
  140. yogaStyle.justifyContent());
  141. yogaStyle.alignContent() = convertRawProp(
  142. rawProps,
  143. "alignContent",
  144. sourceValue.alignContent(),
  145. yogaStyle.alignContent());
  146. yogaStyle.alignItems() = convertRawProp(
  147. rawProps, "alignItems", sourceValue.alignItems(), yogaStyle.alignItems());
  148. yogaStyle.alignSelf() = convertRawProp(
  149. rawProps, "alignSelf", sourceValue.alignSelf(), yogaStyle.alignSelf());
  150. yogaStyle.positionType() = convertRawProp(
  151. rawProps,
  152. "position",
  153. sourceValue.positionType(),
  154. yogaStyle.positionType());
  155. yogaStyle.flexWrap() = convertRawProp(
  156. rawProps, "flexWrap", sourceValue.flexWrap(), yogaStyle.flexWrap());
  157. yogaStyle.overflow() = convertRawProp(
  158. rawProps, "overflow", sourceValue.overflow(), yogaStyle.overflow());
  159. yogaStyle.display() = convertRawProp(
  160. rawProps, "display", sourceValue.display(), yogaStyle.display());
  161. yogaStyle.flex() =
  162. convertRawProp(rawProps, "flex", sourceValue.flex(), yogaStyle.flex());
  163. yogaStyle.flexGrow() = convertRawProp(
  164. rawProps, "flexGrow", sourceValue.flexGrow(), yogaStyle.flexGrow());
  165. yogaStyle.flexShrink() = convertRawProp(
  166. rawProps, "flexShrink", sourceValue.flexShrink(), yogaStyle.flexShrink());
  167. yogaStyle.flexBasis() = convertRawProp(
  168. rawProps, "flexBasis", sourceValue.flexBasis(), yogaStyle.flexBasis());
  169. yogaStyle.margin() = convertRawProp(
  170. rawProps, "margin", "", sourceValue.margin(), yogaStyle.margin());
  171. yogaStyle.position() =
  172. convertRawProp(rawProps, sourceValue.position(), yogaStyle.position());
  173. yogaStyle.padding() = convertRawProp(
  174. rawProps, "padding", "", sourceValue.padding(), yogaStyle.padding());
  175. yogaStyle.border() = convertRawProp(
  176. rawProps, "border", "Width", sourceValue.border(), yogaStyle.border());
  177. yogaStyle.dimensions() = convertRawProp(
  178. rawProps,
  179. "width",
  180. "height",
  181. sourceValue.dimensions(),
  182. yogaStyle.dimensions());
  183. yogaStyle.minDimensions() = convertRawProp(
  184. rawProps,
  185. "minWidth",
  186. "minHeight",
  187. sourceValue.minDimensions(),
  188. yogaStyle.minDimensions());
  189. yogaStyle.maxDimensions() = convertRawProp(
  190. rawProps,
  191. "maxWidth",
  192. "maxHeight",
  193. sourceValue.maxDimensions(),
  194. yogaStyle.maxDimensions());
  195. yogaStyle.aspectRatio() = convertRawProp(
  196. rawProps,
  197. "aspectRatio",
  198. sourceValue.aspectRatio(),
  199. yogaStyle.aspectRatio());
  200. return yogaStyle;
  201. }
  202. template <typename T>
  203. static inline CascadedRectangleCorners<T> convertRawProp(
  204. RawProps const &rawProps,
  205. char const *prefix,
  206. char const *suffix,
  207. CascadedRectangleCorners<T> const &sourceValue,
  208. CascadedRectangleCorners<T> const &defaultValue) {
  209. CascadedRectangleCorners<T> result;
  210. result.topLeft = convertRawProp(
  211. rawProps,
  212. "TopLeft",
  213. sourceValue.topLeft,
  214. defaultValue.topLeft,
  215. prefix,
  216. suffix);
  217. result.topRight = convertRawProp(
  218. rawProps,
  219. "TopRight",
  220. sourceValue.topRight,
  221. defaultValue.topRight,
  222. prefix,
  223. suffix);
  224. result.bottomLeft = convertRawProp(
  225. rawProps,
  226. "BottomLeft",
  227. sourceValue.bottomLeft,
  228. defaultValue.bottomLeft,
  229. prefix,
  230. suffix);
  231. result.bottomRight = convertRawProp(
  232. rawProps,
  233. "BottomRight",
  234. sourceValue.bottomRight,
  235. defaultValue.bottomRight,
  236. prefix,
  237. suffix);
  238. result.topStart = convertRawProp(
  239. rawProps,
  240. "TopStart",
  241. sourceValue.topStart,
  242. defaultValue.topStart,
  243. prefix,
  244. suffix);
  245. result.topEnd = convertRawProp(
  246. rawProps,
  247. "TopEnd",
  248. sourceValue.topEnd,
  249. defaultValue.topEnd,
  250. prefix,
  251. suffix);
  252. result.bottomStart = convertRawProp(
  253. rawProps,
  254. "BottomStart",
  255. sourceValue.bottomStart,
  256. defaultValue.bottomStart,
  257. prefix,
  258. suffix);
  259. result.bottomEnd = convertRawProp(
  260. rawProps,
  261. "BottomEnd",
  262. sourceValue.bottomEnd,
  263. defaultValue.bottomEnd,
  264. prefix,
  265. suffix);
  266. result.all = convertRawProp(
  267. rawProps, "", sourceValue.all, defaultValue.all, prefix, suffix);
  268. return result;
  269. }
  270. template <typename T>
  271. static inline CascadedRectangleEdges<T> convertRawProp(
  272. RawProps const &rawProps,
  273. char const *prefix,
  274. char const *suffix,
  275. CascadedRectangleEdges<T> const &sourceValue,
  276. CascadedRectangleEdges<T> const &defaultValue) {
  277. CascadedRectangleEdges<T> result;
  278. result.left = convertRawProp(
  279. rawProps, "Left", sourceValue.left, defaultValue.left, prefix, suffix);
  280. result.right = convertRawProp(
  281. rawProps, "Right", sourceValue.right, defaultValue.right, prefix, suffix);
  282. result.top = convertRawProp(
  283. rawProps, "Top", sourceValue.top, defaultValue.top, prefix, suffix);
  284. result.bottom = convertRawProp(
  285. rawProps,
  286. "Bottom",
  287. sourceValue.bottom,
  288. defaultValue.bottom,
  289. prefix,
  290. suffix);
  291. result.start = convertRawProp(
  292. rawProps, "Start", sourceValue.start, defaultValue.start, prefix, suffix);
  293. result.end = convertRawProp(
  294. rawProps, "End", sourceValue.end, defaultValue.end, prefix, suffix);
  295. result.horizontal = convertRawProp(
  296. rawProps,
  297. "Horizontal",
  298. sourceValue.horizontal,
  299. defaultValue.horizontal,
  300. prefix,
  301. suffix);
  302. result.vertical = convertRawProp(
  303. rawProps,
  304. "Vertical",
  305. sourceValue.vertical,
  306. defaultValue.vertical,
  307. prefix,
  308. suffix);
  309. result.all = convertRawProp(
  310. rawProps, "", sourceValue.all, defaultValue.all, prefix, suffix);
  311. return result;
  312. }
  313. } // namespace react
  314. } // namespace facebook