RCTShadowView.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. #import "RCTShadowView.h"
  8. #import "RCTConvert.h"
  9. #import "RCTI18nUtil.h"
  10. #import "RCTLayout.h"
  11. #import "RCTLog.h"
  12. #import "RCTShadowView+Layout.h"
  13. #import "RCTUtils.h"
  14. #import "UIView+Private.h"
  15. #import "UIView+React.h"
  16. typedef void (^RCTActionBlock)(RCTShadowView *shadowViewSelf, id value);
  17. typedef void (^RCTResetActionBlock)(RCTShadowView *shadowViewSelf);
  18. typedef NS_ENUM(unsigned int, meta_prop_t) {
  19. META_PROP_LEFT,
  20. META_PROP_TOP,
  21. META_PROP_RIGHT,
  22. META_PROP_BOTTOM,
  23. META_PROP_START,
  24. META_PROP_END,
  25. META_PROP_HORIZONTAL,
  26. META_PROP_VERTICAL,
  27. META_PROP_ALL,
  28. META_PROP_COUNT,
  29. };
  30. @implementation RCTShadowView
  31. {
  32. NSDictionary *_lastParentProperties;
  33. NSMutableArray<RCTShadowView *> *_reactSubviews;
  34. BOOL _recomputePadding;
  35. BOOL _recomputeMargin;
  36. BOOL _recomputeBorder;
  37. YGValue _paddingMetaProps[META_PROP_COUNT];
  38. YGValue _marginMetaProps[META_PROP_COUNT];
  39. YGValue _borderMetaProps[META_PROP_COUNT];
  40. }
  41. + (YGConfigRef)yogaConfig
  42. {
  43. static YGConfigRef yogaConfig;
  44. static dispatch_once_t onceToken;
  45. dispatch_once(&onceToken, ^{
  46. yogaConfig = YGConfigNew();
  47. YGConfigSetPointScaleFactor(yogaConfig, RCTScreenScale());
  48. YGConfigSetUseLegacyStretchBehaviour(yogaConfig, true);
  49. });
  50. return yogaConfig;
  51. }
  52. @synthesize reactTag = _reactTag;
  53. // YogaNode API
  54. static void RCTPrint(YGNodeRef node)
  55. {
  56. RCTShadowView *shadowView = (__bridge RCTShadowView *)YGNodeGetContext(node);
  57. printf("%s(%lld), ", shadowView.viewName.UTF8String, (long long)shadowView.reactTag.integerValue);
  58. }
  59. #define RCT_SET_YGVALUE(ygvalue, setter, ...) \
  60. switch (ygvalue.unit) { \
  61. case YGUnitAuto: \
  62. case YGUnitUndefined: \
  63. setter(__VA_ARGS__, YGUndefined); \
  64. break; \
  65. case YGUnitPoint: \
  66. setter(__VA_ARGS__, ygvalue.value); \
  67. break; \
  68. case YGUnitPercent: \
  69. setter##Percent(__VA_ARGS__, ygvalue.value); \
  70. break; \
  71. }
  72. #define RCT_SET_YGVALUE_AUTO(ygvalue, setter, ...) \
  73. switch (ygvalue.unit) { \
  74. case YGUnitAuto: \
  75. setter##Auto(__VA_ARGS__); \
  76. break; \
  77. case YGUnitUndefined: \
  78. setter(__VA_ARGS__, YGUndefined); \
  79. break; \
  80. case YGUnitPoint: \
  81. setter(__VA_ARGS__, ygvalue.value); \
  82. break; \
  83. case YGUnitPercent: \
  84. setter##Percent(__VA_ARGS__, ygvalue.value); \
  85. break; \
  86. }
  87. static void RCTProcessMetaPropsPadding(const YGValue metaProps[META_PROP_COUNT], YGNodeRef node) {
  88. if (![[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL]) {
  89. RCT_SET_YGVALUE(metaProps[META_PROP_START], YGNodeStyleSetPadding, node, YGEdgeStart);
  90. RCT_SET_YGVALUE(metaProps[META_PROP_END], YGNodeStyleSetPadding, node, YGEdgeEnd);
  91. RCT_SET_YGVALUE(metaProps[META_PROP_LEFT], YGNodeStyleSetPadding, node, YGEdgeLeft);
  92. RCT_SET_YGVALUE(metaProps[META_PROP_RIGHT], YGNodeStyleSetPadding, node, YGEdgeRight);
  93. } else {
  94. YGValue start = metaProps[META_PROP_START].unit == YGUnitUndefined ? metaProps[META_PROP_LEFT] : metaProps[META_PROP_START];
  95. YGValue end = metaProps[META_PROP_END].unit == YGUnitUndefined ? metaProps[META_PROP_RIGHT] : metaProps[META_PROP_END];
  96. RCT_SET_YGVALUE(start, YGNodeStyleSetPadding, node, YGEdgeStart);
  97. RCT_SET_YGVALUE(end, YGNodeStyleSetPadding, node, YGEdgeEnd);
  98. }
  99. RCT_SET_YGVALUE(metaProps[META_PROP_TOP], YGNodeStyleSetPadding, node, YGEdgeTop);
  100. RCT_SET_YGVALUE(metaProps[META_PROP_BOTTOM], YGNodeStyleSetPadding, node, YGEdgeBottom);
  101. RCT_SET_YGVALUE(metaProps[META_PROP_HORIZONTAL], YGNodeStyleSetPadding, node, YGEdgeHorizontal);
  102. RCT_SET_YGVALUE(metaProps[META_PROP_VERTICAL], YGNodeStyleSetPadding, node, YGEdgeVertical);
  103. RCT_SET_YGVALUE(metaProps[META_PROP_ALL], YGNodeStyleSetPadding, node, YGEdgeAll);
  104. }
  105. static void RCTProcessMetaPropsMargin(const YGValue metaProps[META_PROP_COUNT], YGNodeRef node) {
  106. if (![[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL]) {
  107. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_START], YGNodeStyleSetMargin, node, YGEdgeStart);
  108. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_END], YGNodeStyleSetMargin, node, YGEdgeEnd);
  109. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_LEFT], YGNodeStyleSetMargin, node, YGEdgeLeft);
  110. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_RIGHT], YGNodeStyleSetMargin, node, YGEdgeRight);
  111. } else {
  112. YGValue start = metaProps[META_PROP_START].unit == YGUnitUndefined ? metaProps[META_PROP_LEFT] : metaProps[META_PROP_START];
  113. YGValue end = metaProps[META_PROP_END].unit == YGUnitUndefined ? metaProps[META_PROP_RIGHT] : metaProps[META_PROP_END];
  114. RCT_SET_YGVALUE_AUTO(start, YGNodeStyleSetMargin, node, YGEdgeStart);
  115. RCT_SET_YGVALUE_AUTO(end, YGNodeStyleSetMargin, node, YGEdgeEnd);
  116. }
  117. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_TOP], YGNodeStyleSetMargin, node, YGEdgeTop);
  118. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_BOTTOM], YGNodeStyleSetMargin, node, YGEdgeBottom);
  119. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_HORIZONTAL], YGNodeStyleSetMargin, node, YGEdgeHorizontal);
  120. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_VERTICAL], YGNodeStyleSetMargin, node, YGEdgeVertical);
  121. RCT_SET_YGVALUE_AUTO(metaProps[META_PROP_ALL], YGNodeStyleSetMargin, node, YGEdgeAll);
  122. }
  123. static void RCTProcessMetaPropsBorder(const YGValue metaProps[META_PROP_COUNT], YGNodeRef node) {
  124. if (![[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL]) {
  125. YGNodeStyleSetBorder(node, YGEdgeStart, metaProps[META_PROP_START].value);
  126. YGNodeStyleSetBorder(node, YGEdgeEnd, metaProps[META_PROP_END].value);
  127. YGNodeStyleSetBorder(node, YGEdgeLeft, metaProps[META_PROP_LEFT].value);
  128. YGNodeStyleSetBorder(node, YGEdgeRight, metaProps[META_PROP_RIGHT].value);
  129. } else {
  130. const float start = YGFloatIsUndefined(metaProps[META_PROP_START].value) ? metaProps[META_PROP_LEFT].value : metaProps[META_PROP_START].value;
  131. const float end = YGFloatIsUndefined(metaProps[META_PROP_END].value) ? metaProps[META_PROP_RIGHT].value : metaProps[META_PROP_END].value;
  132. YGNodeStyleSetBorder(node, YGEdgeStart, start);
  133. YGNodeStyleSetBorder(node, YGEdgeEnd, end);
  134. }
  135. YGNodeStyleSetBorder(node, YGEdgeTop, metaProps[META_PROP_TOP].value);
  136. YGNodeStyleSetBorder(node, YGEdgeBottom, metaProps[META_PROP_BOTTOM].value);
  137. YGNodeStyleSetBorder(node, YGEdgeHorizontal, metaProps[META_PROP_HORIZONTAL].value);
  138. YGNodeStyleSetBorder(node, YGEdgeVertical, metaProps[META_PROP_VERTICAL].value);
  139. YGNodeStyleSetBorder(node, YGEdgeAll, metaProps[META_PROP_ALL].value);
  140. }
  141. - (CGRect)measureLayoutRelativeToAncestor:(RCTShadowView *)ancestor
  142. {
  143. CGPoint offset = CGPointZero;
  144. NSInteger depth = 30; // max depth to search
  145. RCTShadowView *shadowView = self;
  146. while (depth && shadowView && shadowView != ancestor) {
  147. offset.x += shadowView.layoutMetrics.frame.origin.x;
  148. offset.y += shadowView.layoutMetrics.frame.origin.y;
  149. shadowView = shadowView->_superview;
  150. depth--;
  151. }
  152. if (ancestor != shadowView) {
  153. return CGRectNull;
  154. }
  155. return (CGRect){offset, self.layoutMetrics.frame.size};
  156. }
  157. - (BOOL)viewIsDescendantOf:(RCTShadowView *)ancestor
  158. {
  159. NSInteger depth = 30; // max depth to search
  160. RCTShadowView *shadowView = self;
  161. while (depth && shadowView && shadowView != ancestor) {
  162. shadowView = shadowView->_superview;
  163. depth--;
  164. }
  165. return ancestor == shadowView;
  166. }
  167. - (instancetype)init
  168. {
  169. if (self = [super init]) {
  170. for (unsigned int ii = 0; ii < META_PROP_COUNT; ii++) {
  171. _paddingMetaProps[ii] = YGValueUndefined;
  172. _marginMetaProps[ii] = YGValueUndefined;
  173. _borderMetaProps[ii] = YGValueUndefined;
  174. }
  175. _intrinsicContentSize = CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric);
  176. _newView = YES;
  177. _reactSubviews = [NSMutableArray array];
  178. _yogaNode = YGNodeNewWithConfig([[self class] yogaConfig]);
  179. YGNodeSetContext(_yogaNode, (__bridge void *)self);
  180. YGNodeSetPrintFunc(_yogaNode, RCTPrint);
  181. }
  182. return self;
  183. }
  184. - (BOOL)isReactRootView
  185. {
  186. return RCTIsReactRootView(self.reactTag);
  187. }
  188. - (void)dealloc
  189. {
  190. YGNodeFree(_yogaNode);
  191. }
  192. - (BOOL)canHaveSubviews
  193. {
  194. return YES;
  195. }
  196. - (BOOL)isYogaLeafNode
  197. {
  198. return NO;
  199. }
  200. - (void)insertReactSubview:(RCTShadowView *)subview atIndex:(NSInteger)atIndex
  201. {
  202. RCTAssert(self.canHaveSubviews, @"Attempt to insert subview inside leaf view.");
  203. [_reactSubviews insertObject:subview atIndex:atIndex];
  204. if (![self isYogaLeafNode]) {
  205. YGNodeInsertChild(_yogaNode, subview.yogaNode, (uint32_t)atIndex);
  206. }
  207. subview->_superview = self;
  208. }
  209. - (void)removeReactSubview:(RCTShadowView *)subview
  210. {
  211. subview->_superview = nil;
  212. [_reactSubviews removeObject:subview];
  213. if (![self isYogaLeafNode]) {
  214. YGNodeRemoveChild(_yogaNode, subview.yogaNode);
  215. }
  216. }
  217. - (NSArray<RCTShadowView *> *)reactSubviews
  218. {
  219. return _reactSubviews;
  220. }
  221. - (RCTShadowView *)reactSuperview
  222. {
  223. return _superview;
  224. }
  225. #pragma mark - Layout
  226. - (void)layoutWithMinimumSize:(CGSize)minimumSize
  227. maximumSize:(CGSize)maximumSize
  228. layoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection
  229. layoutContext:(RCTLayoutContext)layoutContext
  230. {
  231. YGNodeRef yogaNode = _yogaNode;
  232. CGSize oldMinimumSize = (CGSize){
  233. RCTCoreGraphicsFloatFromYogaValue(YGNodeStyleGetMinWidth(yogaNode), 0.0),
  234. RCTCoreGraphicsFloatFromYogaValue(YGNodeStyleGetMinHeight(yogaNode), 0.0)
  235. };
  236. if (!CGSizeEqualToSize(oldMinimumSize, minimumSize)) {
  237. YGNodeStyleSetMinWidth(yogaNode, RCTYogaFloatFromCoreGraphicsFloat(minimumSize.width));
  238. YGNodeStyleSetMinHeight(yogaNode, RCTYogaFloatFromCoreGraphicsFloat(minimumSize.height));
  239. }
  240. YGNodeCalculateLayout(
  241. yogaNode,
  242. RCTYogaFloatFromCoreGraphicsFloat(maximumSize.width),
  243. RCTYogaFloatFromCoreGraphicsFloat(maximumSize.height),
  244. RCTYogaLayoutDirectionFromUIKitLayoutDirection(layoutDirection)
  245. );
  246. RCTAssert(!YGNodeIsDirty(yogaNode), @"Attempt to get layout metrics from dirtied Yoga node.");
  247. if (!YGNodeGetHasNewLayout(yogaNode)) {
  248. return;
  249. }
  250. YGNodeSetHasNewLayout(yogaNode, false);
  251. RCTLayoutMetrics layoutMetrics = RCTLayoutMetricsFromYogaNode(yogaNode);
  252. layoutContext.absolutePosition.x += layoutMetrics.frame.origin.x;
  253. layoutContext.absolutePosition.y += layoutMetrics.frame.origin.y;
  254. [self layoutWithMetrics:layoutMetrics
  255. layoutContext:layoutContext];
  256. [self layoutSubviewsWithContext:layoutContext];
  257. }
  258. - (void)layoutWithMetrics:(RCTLayoutMetrics)layoutMetrics
  259. layoutContext:(RCTLayoutContext)layoutContext
  260. {
  261. if (!RCTLayoutMetricsEqualToLayoutMetrics(self.layoutMetrics, layoutMetrics)) {
  262. self.layoutMetrics = layoutMetrics;
  263. [layoutContext.affectedShadowViews addObject:self];
  264. }
  265. }
  266. - (void)layoutSubviewsWithContext:(RCTLayoutContext)layoutContext
  267. {
  268. RCTLayoutMetrics layoutMetrics = self.layoutMetrics;
  269. if (layoutMetrics.displayType == RCTDisplayTypeNone) {
  270. return;
  271. }
  272. for (RCTShadowView *childShadowView in _reactSubviews) {
  273. YGNodeRef childYogaNode = childShadowView.yogaNode;
  274. RCTAssert(!YGNodeIsDirty(childYogaNode), @"Attempt to get layout metrics from dirtied Yoga node.");
  275. if (!YGNodeGetHasNewLayout(childYogaNode)) {
  276. continue;
  277. }
  278. YGNodeSetHasNewLayout(childYogaNode, false);
  279. RCTLayoutMetrics childLayoutMetrics = RCTLayoutMetricsFromYogaNode(childYogaNode);
  280. layoutContext.absolutePosition.x += childLayoutMetrics.frame.origin.x;
  281. layoutContext.absolutePosition.y += childLayoutMetrics.frame.origin.y;
  282. [childShadowView layoutWithMetrics:childLayoutMetrics
  283. layoutContext:layoutContext];
  284. // Recursive call.
  285. [childShadowView layoutSubviewsWithContext:layoutContext];
  286. }
  287. }
  288. - (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize
  289. {
  290. YGNodeRef clonedYogaNode = YGNodeClone(self.yogaNode);
  291. YGNodeRef constraintYogaNode = YGNodeNewWithConfig([[self class] yogaConfig]);
  292. YGNodeInsertChild(constraintYogaNode, clonedYogaNode, 0);
  293. YGNodeStyleSetMinWidth(constraintYogaNode, RCTYogaFloatFromCoreGraphicsFloat(minimumSize.width));
  294. YGNodeStyleSetMinHeight(constraintYogaNode, RCTYogaFloatFromCoreGraphicsFloat(minimumSize.height));
  295. YGNodeStyleSetMaxWidth(constraintYogaNode, RCTYogaFloatFromCoreGraphicsFloat(maximumSize.width));
  296. YGNodeStyleSetMaxHeight(constraintYogaNode, RCTYogaFloatFromCoreGraphicsFloat(maximumSize.height));
  297. YGNodeCalculateLayout(
  298. constraintYogaNode,
  299. YGUndefined,
  300. YGUndefined,
  301. RCTYogaLayoutDirectionFromUIKitLayoutDirection(self.layoutMetrics.layoutDirection)
  302. );
  303. CGSize measuredSize = (CGSize){
  304. RCTCoreGraphicsFloatFromYogaFloat(YGNodeLayoutGetWidth(constraintYogaNode)),
  305. RCTCoreGraphicsFloatFromYogaFloat(YGNodeLayoutGetHeight(constraintYogaNode)),
  306. };
  307. YGNodeRemoveChild(constraintYogaNode, clonedYogaNode);
  308. YGNodeFree(constraintYogaNode);
  309. YGNodeFree(clonedYogaNode);
  310. return measuredSize;
  311. }
  312. - (NSNumber *)reactTagAtPoint:(CGPoint)point
  313. {
  314. for (RCTShadowView *shadowView in _reactSubviews) {
  315. if (CGRectContainsPoint(shadowView.layoutMetrics.frame, point)) {
  316. CGPoint relativePoint = point;
  317. CGPoint origin = shadowView.layoutMetrics.frame.origin;
  318. relativePoint.x -= origin.x;
  319. relativePoint.y -= origin.y;
  320. return [shadowView reactTagAtPoint:relativePoint];
  321. }
  322. }
  323. return self.reactTag;
  324. }
  325. - (NSString *)description
  326. {
  327. NSString *description = super.description;
  328. description = [[description substringToIndex:description.length - 1] stringByAppendingFormat:@"; viewName: %@; reactTag: %@; frame: %@>", self.viewName, self.reactTag, NSStringFromCGRect(self.layoutMetrics.frame)];
  329. return description;
  330. }
  331. - (void)addRecursiveDescriptionToString:(NSMutableString *)string atLevel:(NSUInteger)level
  332. {
  333. for (NSUInteger i = 0; i < level; i++) {
  334. [string appendString:@" | "];
  335. }
  336. [string appendString:self.description];
  337. [string appendString:@"\n"];
  338. for (RCTShadowView *subview in _reactSubviews) {
  339. [subview addRecursiveDescriptionToString:string atLevel:level + 1];
  340. }
  341. }
  342. - (NSString *)recursiveDescription
  343. {
  344. NSMutableString *description = [NSMutableString string];
  345. [self addRecursiveDescriptionToString:description atLevel:0];
  346. return description;
  347. }
  348. // Margin
  349. #define RCT_MARGIN_PROPERTY(prop, metaProp) \
  350. - (void)setMargin##prop:(YGValue)value \
  351. { \
  352. _marginMetaProps[META_PROP_##metaProp] = value; \
  353. _recomputeMargin = YES; \
  354. } \
  355. - (YGValue)margin##prop \
  356. { \
  357. return _marginMetaProps[META_PROP_##metaProp]; \
  358. }
  359. RCT_MARGIN_PROPERTY(, ALL)
  360. RCT_MARGIN_PROPERTY(Vertical, VERTICAL)
  361. RCT_MARGIN_PROPERTY(Horizontal, HORIZONTAL)
  362. RCT_MARGIN_PROPERTY(Top, TOP)
  363. RCT_MARGIN_PROPERTY(Left, LEFT)
  364. RCT_MARGIN_PROPERTY(Bottom, BOTTOM)
  365. RCT_MARGIN_PROPERTY(Right, RIGHT)
  366. RCT_MARGIN_PROPERTY(Start, START)
  367. RCT_MARGIN_PROPERTY(End, END)
  368. // Padding
  369. #define RCT_PADDING_PROPERTY(prop, metaProp) \
  370. - (void)setPadding##prop:(YGValue)value \
  371. { \
  372. _paddingMetaProps[META_PROP_##metaProp] = value; \
  373. _recomputePadding = YES; \
  374. } \
  375. - (YGValue)padding##prop \
  376. { \
  377. return _paddingMetaProps[META_PROP_##metaProp]; \
  378. }
  379. RCT_PADDING_PROPERTY(, ALL)
  380. RCT_PADDING_PROPERTY(Vertical, VERTICAL)
  381. RCT_PADDING_PROPERTY(Horizontal, HORIZONTAL)
  382. RCT_PADDING_PROPERTY(Top, TOP)
  383. RCT_PADDING_PROPERTY(Left, LEFT)
  384. RCT_PADDING_PROPERTY(Bottom, BOTTOM)
  385. RCT_PADDING_PROPERTY(Right, RIGHT)
  386. RCT_PADDING_PROPERTY(Start, START)
  387. RCT_PADDING_PROPERTY(End, END)
  388. // Border
  389. #define RCT_BORDER_PROPERTY(prop, metaProp) \
  390. - (void)setBorder##prop##Width:(float)value \
  391. { \
  392. _borderMetaProps[META_PROP_##metaProp].value = value; \
  393. _recomputeBorder = YES; \
  394. } \
  395. - (float)border##prop##Width \
  396. { \
  397. return _borderMetaProps[META_PROP_##metaProp].value; \
  398. }
  399. RCT_BORDER_PROPERTY(, ALL)
  400. RCT_BORDER_PROPERTY(Top, TOP)
  401. RCT_BORDER_PROPERTY(Left, LEFT)
  402. RCT_BORDER_PROPERTY(Bottom, BOTTOM)
  403. RCT_BORDER_PROPERTY(Right, RIGHT)
  404. RCT_BORDER_PROPERTY(Start, START)
  405. RCT_BORDER_PROPERTY(End, END)
  406. // Dimensions
  407. #define RCT_DIMENSION_PROPERTY(setProp, getProp, cssProp) \
  408. - (void)set##setProp:(YGValue)value \
  409. { \
  410. RCT_SET_YGVALUE_AUTO(value, YGNodeStyleSet##cssProp, _yogaNode); \
  411. } \
  412. - (YGValue)getProp \
  413. { \
  414. return YGNodeStyleGet##cssProp(_yogaNode); \
  415. }
  416. #define RCT_MIN_MAX_DIMENSION_PROPERTY(setProp, getProp, cssProp) \
  417. - (void)set##setProp:(YGValue)value \
  418. { \
  419. RCT_SET_YGVALUE(value, YGNodeStyleSet##cssProp, _yogaNode); \
  420. } \
  421. - (YGValue)getProp \
  422. { \
  423. return YGNodeStyleGet##cssProp(_yogaNode); \
  424. }
  425. RCT_DIMENSION_PROPERTY(Width, width, Width)
  426. RCT_DIMENSION_PROPERTY(Height, height, Height)
  427. RCT_MIN_MAX_DIMENSION_PROPERTY(MinWidth, minWidth, MinWidth)
  428. RCT_MIN_MAX_DIMENSION_PROPERTY(MinHeight, minHeight, MinHeight)
  429. RCT_MIN_MAX_DIMENSION_PROPERTY(MaxWidth, maxWidth, MaxWidth)
  430. RCT_MIN_MAX_DIMENSION_PROPERTY(MaxHeight, maxHeight, MaxHeight)
  431. // Position
  432. #define RCT_POSITION_PROPERTY(setProp, getProp, edge) \
  433. - (void)set##setProp:(YGValue)value \
  434. { \
  435. RCT_SET_YGVALUE(value, YGNodeStyleSetPosition, _yogaNode, edge); \
  436. } \
  437. - (YGValue)getProp \
  438. { \
  439. return YGNodeStyleGetPosition(_yogaNode, edge); \
  440. }
  441. RCT_POSITION_PROPERTY(Top, top, YGEdgeTop)
  442. RCT_POSITION_PROPERTY(Bottom, bottom, YGEdgeBottom)
  443. RCT_POSITION_PROPERTY(Start, start, YGEdgeStart)
  444. RCT_POSITION_PROPERTY(End, end, YGEdgeEnd)
  445. - (void)setLeft:(YGValue)value
  446. {
  447. YGEdge edge = [[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL] ? YGEdgeStart : YGEdgeLeft;
  448. RCT_SET_YGVALUE(value, YGNodeStyleSetPosition, _yogaNode, edge);
  449. }
  450. - (YGValue)left
  451. {
  452. YGEdge edge = [[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL] ? YGEdgeStart : YGEdgeLeft;
  453. return YGNodeStyleGetPosition(_yogaNode, edge);
  454. }
  455. - (void)setRight:(YGValue)value
  456. {
  457. YGEdge edge = [[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL] ? YGEdgeEnd : YGEdgeRight;
  458. RCT_SET_YGVALUE(value, YGNodeStyleSetPosition, _yogaNode, edge);
  459. }
  460. - (YGValue)right
  461. {
  462. YGEdge edge = [[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL] ? YGEdgeEnd : YGEdgeRight;
  463. return YGNodeStyleGetPosition(_yogaNode, edge);
  464. }
  465. // Size
  466. - (CGSize)size
  467. {
  468. YGValue width = YGNodeStyleGetWidth(_yogaNode);
  469. YGValue height = YGNodeStyleGetHeight(_yogaNode);
  470. return CGSizeMake(
  471. width.unit == YGUnitPoint ? width.value : NAN,
  472. height.unit == YGUnitPoint ? height.value : NAN
  473. );
  474. }
  475. - (void)setSize:(CGSize)size
  476. {
  477. YGNodeStyleSetWidth(_yogaNode, size.width);
  478. YGNodeStyleSetHeight(_yogaNode, size.height);
  479. }
  480. // IntrinsicContentSize
  481. static inline YGSize RCTShadowViewMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
  482. {
  483. RCTShadowView *shadowView = (__bridge RCTShadowView *)YGNodeGetContext(node);
  484. CGSize intrinsicContentSize = shadowView->_intrinsicContentSize;
  485. // Replace `UIViewNoIntrinsicMetric` (which equals `-1`) with zero.
  486. intrinsicContentSize.width = MAX(0, intrinsicContentSize.width);
  487. intrinsicContentSize.height = MAX(0, intrinsicContentSize.height);
  488. YGSize result;
  489. switch (widthMode) {
  490. case YGMeasureModeUndefined:
  491. result.width = intrinsicContentSize.width;
  492. break;
  493. case YGMeasureModeExactly:
  494. result.width = width;
  495. break;
  496. case YGMeasureModeAtMost:
  497. result.width = MIN(width, intrinsicContentSize.width);
  498. break;
  499. }
  500. switch (heightMode) {
  501. case YGMeasureModeUndefined:
  502. result.height = intrinsicContentSize.height;
  503. break;
  504. case YGMeasureModeExactly:
  505. result.height = height;
  506. break;
  507. case YGMeasureModeAtMost:
  508. result.height = MIN(height, intrinsicContentSize.height);
  509. break;
  510. }
  511. return result;
  512. }
  513. - (void)setIntrinsicContentSize:(CGSize)intrinsicContentSize
  514. {
  515. if (CGSizeEqualToSize(_intrinsicContentSize, intrinsicContentSize)) {
  516. return;
  517. }
  518. _intrinsicContentSize = intrinsicContentSize;
  519. if (CGSizeEqualToSize(_intrinsicContentSize, CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric))) {
  520. YGNodeSetMeasureFunc(_yogaNode, NULL);
  521. } else {
  522. YGNodeSetMeasureFunc(_yogaNode, RCTShadowViewMeasure);
  523. }
  524. YGNodeMarkDirty(_yogaNode);
  525. }
  526. // Local Data
  527. - (void)setLocalData:(__unused NSObject *)localData
  528. {
  529. // Do nothing by default.
  530. }
  531. // Flex
  532. - (void)setFlexBasis:(YGValue)value
  533. {
  534. RCT_SET_YGVALUE_AUTO(value, YGNodeStyleSetFlexBasis, _yogaNode);
  535. }
  536. - (YGValue)flexBasis
  537. {
  538. return YGNodeStyleGetFlexBasis(_yogaNode);
  539. }
  540. #define RCT_STYLE_PROPERTY(setProp, getProp, cssProp, type) \
  541. - (void)set##setProp:(type)value \
  542. { \
  543. YGNodeStyleSet##cssProp(_yogaNode, value); \
  544. } \
  545. - (type)getProp \
  546. { \
  547. return YGNodeStyleGet##cssProp(_yogaNode); \
  548. }
  549. RCT_STYLE_PROPERTY(Flex, flex, Flex, float)
  550. RCT_STYLE_PROPERTY(FlexGrow, flexGrow, FlexGrow, float)
  551. RCT_STYLE_PROPERTY(FlexShrink, flexShrink, FlexShrink, float)
  552. RCT_STYLE_PROPERTY(FlexDirection, flexDirection, FlexDirection, YGFlexDirection)
  553. RCT_STYLE_PROPERTY(JustifyContent, justifyContent, JustifyContent, YGJustify)
  554. RCT_STYLE_PROPERTY(AlignSelf, alignSelf, AlignSelf, YGAlign)
  555. RCT_STYLE_PROPERTY(AlignItems, alignItems, AlignItems, YGAlign)
  556. RCT_STYLE_PROPERTY(AlignContent, alignContent, AlignContent, YGAlign)
  557. RCT_STYLE_PROPERTY(Position, position, PositionType, YGPositionType)
  558. RCT_STYLE_PROPERTY(FlexWrap, flexWrap, FlexWrap, YGWrap)
  559. RCT_STYLE_PROPERTY(Overflow, overflow, Overflow, YGOverflow)
  560. RCT_STYLE_PROPERTY(Display, display, Display, YGDisplay)
  561. RCT_STYLE_PROPERTY(Direction, direction, Direction, YGDirection)
  562. RCT_STYLE_PROPERTY(AspectRatio, aspectRatio, AspectRatio, float)
  563. - (void)didUpdateReactSubviews
  564. {
  565. // Does nothing by default
  566. }
  567. - (void)didSetProps:(__unused NSArray<NSString *> *)changedProps
  568. {
  569. if (_recomputePadding) {
  570. RCTProcessMetaPropsPadding(_paddingMetaProps, _yogaNode);
  571. }
  572. if (_recomputeMargin) {
  573. RCTProcessMetaPropsMargin(_marginMetaProps, _yogaNode);
  574. }
  575. if (_recomputeBorder) {
  576. RCTProcessMetaPropsBorder(_borderMetaProps, _yogaNode);
  577. }
  578. _recomputeMargin = NO;
  579. _recomputePadding = NO;
  580. _recomputeBorder = NO;
  581. }
  582. @end