2015-03-23 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
#import "RCTShadowView.h"
|
|
|
|
|
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTSparseArray.h"
|
|
|
|
#import "RCTUtils.h"
|
2015-05-28 16:29:27 +00:00
|
|
|
#import "UIView+React.h"
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
typedef void (^RCTActionBlock)(RCTShadowView *shadowViewSelf, id value);
|
|
|
|
typedef void (^RCTResetActionBlock)(RCTShadowView *shadowViewSelf);
|
|
|
|
|
|
|
|
const NSString *const RCTBackgroundColorProp = @"backgroundColor";
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
META_PROP_LEFT,
|
|
|
|
META_PROP_TOP,
|
|
|
|
META_PROP_RIGHT,
|
|
|
|
META_PROP_BOTTOM,
|
|
|
|
META_PROP_HORIZONTAL,
|
|
|
|
META_PROP_VERTICAL,
|
|
|
|
META_PROP_ALL,
|
|
|
|
META_PROP_COUNT,
|
|
|
|
} meta_prop_t;
|
|
|
|
|
|
|
|
@implementation RCTShadowView
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
RCTUpdateLifecycle _propagationLifecycle;
|
|
|
|
RCTUpdateLifecycle _textLifecycle;
|
2015-01-30 01:10:49 +00:00
|
|
|
NSDictionary *_lastParentProperties;
|
|
|
|
NSMutableArray *_reactSubviews;
|
|
|
|
BOOL _recomputePadding;
|
|
|
|
BOOL _recomputeMargin;
|
2015-07-30 13:20:28 +00:00
|
|
|
BOOL _recomputeBorder;
|
2015-03-02 19:36:55 +00:00
|
|
|
float _paddingMetaProps[META_PROP_COUNT];
|
|
|
|
float _marginMetaProps[META_PROP_COUNT];
|
2015-07-30 13:20:28 +00:00
|
|
|
float _borderMetaProps[META_PROP_COUNT];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize reactTag = _reactTag;
|
|
|
|
|
|
|
|
// css_node api
|
|
|
|
|
|
|
|
static void RCTPrint(void *context)
|
|
|
|
{
|
|
|
|
RCTShadowView *shadowView = (__bridge RCTShadowView *)context;
|
2015-03-17 20:42:44 +00:00
|
|
|
printf("%s(%zd), ", shadowView.viewName.UTF8String, shadowView.reactTag.integerValue);
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static css_node_t *RCTGetChild(void *context, int i)
|
|
|
|
{
|
|
|
|
RCTShadowView *shadowView = (__bridge RCTShadowView *)context;
|
|
|
|
RCTShadowView *child = [shadowView reactSubviews][i];
|
|
|
|
return child->_cssNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool RCTIsDirty(void *context)
|
|
|
|
{
|
|
|
|
RCTShadowView *shadowView = (__bridge RCTShadowView *)context;
|
2015-02-19 01:39:09 +00:00
|
|
|
return [shadowView isLayoutDirty];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enforces precedence rules, e.g. marginLeft > marginHorizontal > margin.
|
|
|
|
static void RCTProcessMetaProps(const float metaProps[META_PROP_COUNT], float style[CSS_POSITION_COUNT]) {
|
|
|
|
style[CSS_LEFT] = !isUndefined(metaProps[META_PROP_LEFT]) ? metaProps[META_PROP_LEFT]
|
|
|
|
: !isUndefined(metaProps[META_PROP_HORIZONTAL]) ? metaProps[META_PROP_HORIZONTAL]
|
|
|
|
: !isUndefined(metaProps[META_PROP_ALL]) ? metaProps[META_PROP_ALL]
|
|
|
|
: 0;
|
|
|
|
style[CSS_RIGHT] = !isUndefined(metaProps[META_PROP_RIGHT]) ? metaProps[META_PROP_RIGHT]
|
|
|
|
: !isUndefined(metaProps[META_PROP_HORIZONTAL]) ? metaProps[META_PROP_HORIZONTAL]
|
|
|
|
: !isUndefined(metaProps[META_PROP_ALL]) ? metaProps[META_PROP_ALL]
|
|
|
|
: 0;
|
|
|
|
style[CSS_TOP] = !isUndefined(metaProps[META_PROP_TOP]) ? metaProps[META_PROP_TOP]
|
|
|
|
: !isUndefined(metaProps[META_PROP_VERTICAL]) ? metaProps[META_PROP_VERTICAL]
|
|
|
|
: !isUndefined(metaProps[META_PROP_ALL]) ? metaProps[META_PROP_ALL]
|
|
|
|
: 0;
|
|
|
|
style[CSS_BOTTOM] = !isUndefined(metaProps[META_PROP_BOTTOM]) ? metaProps[META_PROP_BOTTOM]
|
|
|
|
: !isUndefined(metaProps[META_PROP_VERTICAL]) ? metaProps[META_PROP_VERTICAL]
|
|
|
|
: !isUndefined(metaProps[META_PROP_ALL]) ? metaProps[META_PROP_ALL]
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)fillCSSNode:(css_node_t *)node
|
|
|
|
{
|
|
|
|
node->children_count = (int)_reactSubviews.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The absolute stuff is so that we can take into account our absolute position when rounding in order to
|
|
|
|
// snap to the pixel grid. For example, say you have the following structure:
|
|
|
|
//
|
|
|
|
// +--------+---------+--------+
|
|
|
|
// | |+-------+| |
|
|
|
|
// | || || |
|
|
|
|
// | |+-------+| |
|
|
|
|
// +--------+---------+--------+
|
|
|
|
//
|
|
|
|
// Say the screen width is 320 pts so the three big views will get the following x bounds from our layout system:
|
|
|
|
// {0, 106.667}, {106.667, 213.333}, {213.333, 320}
|
|
|
|
//
|
|
|
|
// Assuming screen scale is 2, these numbers must be rounded to the nearest 0.5 to fit the pixel grid:
|
|
|
|
// {0, 106.5}, {106.5, 213.5}, {213.5, 320}
|
|
|
|
// You'll notice that the three widths are 106.5, 107, 106.5.
|
|
|
|
//
|
|
|
|
// This is great for the parent views but it gets trickier when we consider rounding for the subview.
|
|
|
|
//
|
|
|
|
// When we go to round the bounds for the subview in the middle, it's relative bounds are {0, 106.667}
|
|
|
|
// which gets rounded to {0, 106.5}. This will cause the subview to be one pixel smaller than it should be.
|
|
|
|
// this is why we need to pass in the absolute position in order to do the rounding relative to the screen's
|
|
|
|
// grid rather than the view's grid.
|
|
|
|
//
|
|
|
|
// After passing in the absolutePosition of {106.667, y}, we do the following calculations:
|
|
|
|
// absoluteLeft = round(absolutePosition.x + viewPosition.left) = round(106.667 + 0) = 106.5
|
|
|
|
// absoluteRight = round(absolutePosition.x + viewPosition.left + viewSize.left) + round(106.667 + 0 + 106.667) = 213.5
|
|
|
|
// width = 213.5 - 106.5 = 107
|
|
|
|
// You'll notice that this is the same width we calculated for the parent view because we've taken its position into account.
|
|
|
|
|
2015-06-01 15:34:09 +00:00
|
|
|
- (void)applyLayoutNode:(css_node_t *)node
|
|
|
|
viewsWithNewFrame:(NSMutableSet *)viewsWithNewFrame
|
|
|
|
absolutePosition:(CGPoint)absolutePosition
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
|
|
|
if (!node->layout.should_update) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
node->layout.should_update = false;
|
2015-03-02 19:36:55 +00:00
|
|
|
_layoutLifecycle = RCTUpdateLifecycleComputed;
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-02-04 00:02:36 +00:00
|
|
|
CGPoint absoluteTopLeft = {
|
2015-01-30 01:10:49 +00:00
|
|
|
RCTRoundPixelValue(absolutePosition.x + node->layout.position[CSS_LEFT]),
|
|
|
|
RCTRoundPixelValue(absolutePosition.y + node->layout.position[CSS_TOP])
|
|
|
|
};
|
2015-03-02 19:36:55 +00:00
|
|
|
|
2015-02-04 00:02:36 +00:00
|
|
|
CGPoint absoluteBottomRight = {
|
2015-01-30 01:10:49 +00:00
|
|
|
RCTRoundPixelValue(absolutePosition.x + node->layout.position[CSS_LEFT] + node->layout.dimensions[CSS_WIDTH]),
|
|
|
|
RCTRoundPixelValue(absolutePosition.y + node->layout.position[CSS_TOP] + node->layout.dimensions[CSS_HEIGHT])
|
|
|
|
};
|
2015-03-02 19:36:55 +00:00
|
|
|
|
2015-03-17 20:42:44 +00:00
|
|
|
CGRect frame = {{
|
2015-01-30 01:10:49 +00:00
|
|
|
RCTRoundPixelValue(node->layout.position[CSS_LEFT]),
|
|
|
|
RCTRoundPixelValue(node->layout.position[CSS_TOP]),
|
2015-03-17 20:42:44 +00:00
|
|
|
}, {
|
2015-01-30 01:10:49 +00:00
|
|
|
RCTRoundPixelValue(absoluteBottomRight.x - absoluteTopLeft.x),
|
|
|
|
RCTRoundPixelValue(absoluteBottomRight.y - absoluteTopLeft.y)
|
2015-03-17 20:42:44 +00:00
|
|
|
}};
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
if (!CGRectEqualToRect(frame, _frame)) {
|
|
|
|
_frame = frame;
|
|
|
|
[viewsWithNewFrame addObject:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
absolutePosition.x += node->layout.position[CSS_LEFT];
|
|
|
|
absolutePosition.y += node->layout.position[CSS_TOP];
|
|
|
|
|
|
|
|
node->layout.dimensions[CSS_WIDTH] = CSS_UNDEFINED;
|
|
|
|
node->layout.dimensions[CSS_HEIGHT] = CSS_UNDEFINED;
|
|
|
|
node->layout.position[CSS_LEFT] = 0;
|
|
|
|
node->layout.position[CSS_TOP] = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < node->children_count; ++i) {
|
|
|
|
RCTShadowView *child = (RCTShadowView *)_reactSubviews[i];
|
2015-06-01 15:34:09 +00:00
|
|
|
[child applyLayoutNode:node->get_child(node->context, i)
|
|
|
|
viewsWithNewFrame:viewsWithNewFrame
|
|
|
|
absolutePosition:absolutePosition];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:34:09 +00:00
|
|
|
- (NSDictionary *)processUpdatedProperties:(NSMutableSet *)applierBlocks
|
|
|
|
parentProperties:(NSDictionary *)parentProperties
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
2015-06-01 15:34:09 +00:00
|
|
|
// TODO: we always refresh all propagated properties when propagation is
|
|
|
|
// dirtied, but really we should track which properties have changed and
|
|
|
|
// only update those.
|
|
|
|
|
2015-05-28 16:29:27 +00:00
|
|
|
if (!_backgroundColor) {
|
2015-01-30 01:10:49 +00:00
|
|
|
UIColor *parentBackgroundColor = parentProperties[RCTBackgroundColorProp];
|
2015-05-28 16:29:27 +00:00
|
|
|
if (parentBackgroundColor) {
|
2015-01-30 01:10:49 +00:00
|
|
|
[applierBlocks addObject:^(RCTSparseArray *viewRegistry) {
|
|
|
|
UIView *view = viewRegistry[_reactTag];
|
2015-05-28 16:29:27 +00:00
|
|
|
[view reactSetInheritedBackgroundColor:parentBackgroundColor];
|
2015-01-30 01:10:49 +00:00
|
|
|
}];
|
|
|
|
}
|
2015-05-28 16:29:27 +00:00
|
|
|
} else {
|
2015-01-30 01:10:49 +00:00
|
|
|
// Update parent properties for children
|
|
|
|
NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:parentProperties];
|
|
|
|
CGFloat alpha = CGColorGetAlpha(_backgroundColor.CGColor);
|
2015-05-26 19:40:26 +00:00
|
|
|
if (alpha < 1.0) {
|
2015-05-28 16:29:27 +00:00
|
|
|
// If bg is non-opaque, don't propagate further
|
2015-01-30 01:10:49 +00:00
|
|
|
properties[RCTBackgroundColorProp] = [UIColor clearColor];
|
|
|
|
} else {
|
|
|
|
properties[RCTBackgroundColorProp] = _backgroundColor;
|
|
|
|
}
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
return parentProperties;
|
|
|
|
}
|
|
|
|
|
2015-06-01 15:34:09 +00:00
|
|
|
- (void)collectUpdatedProperties:(NSMutableSet *)applierBlocks
|
|
|
|
parentProperties:(NSDictionary *)parentProperties
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
if (_propagationLifecycle == RCTUpdateLifecycleComputed && [parentProperties isEqualToDictionary:_lastParentProperties]) {
|
2015-01-30 01:10:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-02 19:36:55 +00:00
|
|
|
_propagationLifecycle = RCTUpdateLifecycleComputed;
|
2015-01-30 01:10:49 +00:00
|
|
|
_lastParentProperties = parentProperties;
|
2015-06-01 15:34:09 +00:00
|
|
|
NSDictionary *nextProps = [self processUpdatedProperties:applierBlocks parentProperties:parentProperties];
|
2015-01-30 01:10:49 +00:00
|
|
|
for (RCTShadowView *child in _reactSubviews) {
|
|
|
|
[child collectUpdatedProperties:applierBlocks parentProperties:nextProps];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (void)collectRootUpdatedFrames:(NSMutableSet *)viewsWithNewFrame
|
|
|
|
parentConstraint:(__unused CGSize)parentConstraint
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
|
|
|
[self fillCSSNode:_cssNode];
|
|
|
|
layoutNode(_cssNode, CSS_UNDEFINED);
|
2015-03-02 19:36:55 +00:00
|
|
|
[self applyLayoutNode:_cssNode viewsWithNewFrame:viewsWithNewFrame absolutePosition:CGPointZero];
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
- (CGRect)measureLayoutRelativeToAncestor:(RCTShadowView *)ancestor
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
2015-06-01 15:34:09 +00:00
|
|
|
CGPoint offset = CGPointZero;
|
2015-03-25 02:34:12 +00:00
|
|
|
NSInteger depth = 30; // max depth to search
|
|
|
|
RCTShadowView *shadowView = self;
|
|
|
|
while (depth && shadowView && shadowView != ancestor) {
|
2015-06-01 15:34:09 +00:00
|
|
|
offset.x += shadowView.frame.origin.x;
|
|
|
|
offset.y += shadowView.frame.origin.y;
|
2015-01-30 01:10:49 +00:00
|
|
|
shadowView = shadowView->_superview;
|
2015-03-25 02:34:12 +00:00
|
|
|
depth--;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
if (ancestor != shadowView) {
|
|
|
|
return CGRectNull;
|
|
|
|
}
|
2015-06-01 15:34:09 +00:00
|
|
|
return (CGRect){offset, self.frame.size};
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-03-02 19:36:55 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
_frame = CGRectMake(0, 0, CSS_UNDEFINED, CSS_UNDEFINED);
|
|
|
|
|
|
|
|
for (int ii = 0; ii < META_PROP_COUNT; ii++) {
|
|
|
|
_paddingMetaProps[ii] = CSS_UNDEFINED;
|
|
|
|
_marginMetaProps[ii] = CSS_UNDEFINED;
|
2015-07-30 13:20:28 +00:00
|
|
|
_borderMetaProps[ii] = CSS_UNDEFINED;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_newView = YES;
|
2015-03-02 19:36:55 +00:00
|
|
|
_layoutLifecycle = RCTUpdateLifecycleUninitialized;
|
|
|
|
_propagationLifecycle = RCTUpdateLifecycleUninitialized;
|
|
|
|
_textLifecycle = RCTUpdateLifecycleUninitialized;
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
_reactSubviews = [NSMutableArray array];
|
|
|
|
|
|
|
|
_cssNode = new_css_node();
|
|
|
|
_cssNode->context = (__bridge void *)self;
|
|
|
|
_cssNode->print = RCTPrint;
|
|
|
|
_cssNode->get_child = RCTGetChild;
|
|
|
|
_cssNode->is_dirty = RCTIsDirty;
|
|
|
|
[self fillCSSNode:_cssNode];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
- (BOOL)isReactRootView
|
|
|
|
{
|
|
|
|
return RCTIsReactRootView(self.reactTag);
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
free_css_node(_cssNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dirtyLayout
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
if (_layoutLifecycle != RCTUpdateLifecycleDirtied) {
|
|
|
|
_layoutLifecycle = RCTUpdateLifecycleDirtied;
|
2015-01-30 01:10:49 +00:00
|
|
|
[_superview dirtyLayout];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:39:09 +00:00
|
|
|
- (BOOL)isLayoutDirty
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
return _layoutLifecycle != RCTUpdateLifecycleComputed;
|
2015-02-19 01:39:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
- (void)dirtyPropagation
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
if (_propagationLifecycle != RCTUpdateLifecycleDirtied) {
|
|
|
|
_propagationLifecycle = RCTUpdateLifecycleDirtied;
|
2015-01-30 01:10:49 +00:00
|
|
|
[_superview dirtyPropagation];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:39:09 +00:00
|
|
|
- (BOOL)isPropagationDirty
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
return _propagationLifecycle != RCTUpdateLifecycleComputed;
|
2015-02-19 01:39:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
- (void)dirtyText
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
if (_textLifecycle != RCTUpdateLifecycleDirtied) {
|
|
|
|
_textLifecycle = RCTUpdateLifecycleDirtied;
|
2015-01-30 01:10:49 +00:00
|
|
|
[_superview dirtyText];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isTextDirty
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
return _textLifecycle != RCTUpdateLifecycleComputed;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTextComputed
|
|
|
|
{
|
2015-03-02 19:36:55 +00:00
|
|
|
_textLifecycle = RCTUpdateLifecycleComputed;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)insertReactSubview:(RCTShadowView *)subview atIndex:(NSInteger)atIndex
|
|
|
|
{
|
|
|
|
[_reactSubviews insertObject:subview atIndex:atIndex];
|
|
|
|
_cssNode->children_count = (int)[_reactSubviews count];
|
|
|
|
subview->_superview = self;
|
|
|
|
[self dirtyText];
|
|
|
|
[self dirtyLayout];
|
|
|
|
[self dirtyPropagation];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeReactSubview:(RCTShadowView *)subview
|
|
|
|
{
|
|
|
|
[subview dirtyText];
|
|
|
|
[subview dirtyLayout];
|
|
|
|
[subview dirtyPropagation];
|
|
|
|
subview->_superview = nil;
|
|
|
|
[_reactSubviews removeObject:subview];
|
|
|
|
_cssNode->children_count = (int)[_reactSubviews count];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)reactSubviews
|
|
|
|
{
|
|
|
|
return _reactSubviews;
|
|
|
|
}
|
|
|
|
|
2015-03-06 17:54:10 +00:00
|
|
|
- (RCTShadowView *)reactSuperview
|
|
|
|
{
|
|
|
|
return _superview;
|
|
|
|
}
|
|
|
|
|
2015-02-06 23:45:28 +00:00
|
|
|
- (NSNumber *)reactTagAtPoint:(CGPoint)point
|
|
|
|
{
|
|
|
|
for (RCTShadowView *shadowView in _reactSubviews) {
|
|
|
|
if (CGRectContainsPoint(shadowView.frame, point)) {
|
|
|
|
CGPoint relativePoint = point;
|
|
|
|
CGPoint origin = shadowView.frame.origin;
|
|
|
|
relativePoint.x -= origin.x;
|
|
|
|
relativePoint.y -= origin.y;
|
|
|
|
return [shadowView reactTagAtPoint:relativePoint];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self.reactTag;
|
|
|
|
}
|
|
|
|
|
2015-06-15 19:05:04 +00:00
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
NSString *description = super.description;
|
2015-07-17 10:53:15 +00:00
|
|
|
description = [[description substringToIndex:description.length - 1] stringByAppendingFormat:@"; viewName: %@; reactTag: %@; frame: %@>", self.viewName, self.reactTag, NSStringFromCGRect(self.frame)];
|
|
|
|
return description;
|
2015-06-15 19:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addRecursiveDescriptionToString:(NSMutableString *)string atLevel:(NSUInteger)level
|
|
|
|
{
|
|
|
|
for (NSUInteger i = 0; i < level; i++) {
|
|
|
|
[string appendString:@" | "];
|
|
|
|
}
|
|
|
|
|
|
|
|
[string appendString:self.description];
|
|
|
|
[string appendString:@"\n"];
|
|
|
|
|
|
|
|
for (RCTShadowView *subview in _reactSubviews) {
|
|
|
|
[subview addRecursiveDescriptionToString:string atLevel:level + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)recursiveDescription
|
|
|
|
{
|
|
|
|
NSMutableString *description = [NSMutableString string];
|
|
|
|
[self addRecursiveDescriptionToString:description atLevel:0];
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
// Margin
|
|
|
|
|
|
|
|
#define RCT_MARGIN_PROPERTY(prop, metaProp) \
|
|
|
|
- (void)setMargin##prop:(CGFloat)value \
|
|
|
|
{ \
|
|
|
|
_marginMetaProps[META_PROP_##metaProp] = value; \
|
|
|
|
_recomputeMargin = YES; \
|
|
|
|
} \
|
|
|
|
- (CGFloat)margin##prop \
|
|
|
|
{ \
|
|
|
|
return _marginMetaProps[META_PROP_##metaProp]; \
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_MARGIN_PROPERTY(, ALL)
|
|
|
|
RCT_MARGIN_PROPERTY(Vertical, VERTICAL)
|
|
|
|
RCT_MARGIN_PROPERTY(Horizontal, HORIZONTAL)
|
|
|
|
RCT_MARGIN_PROPERTY(Top, TOP)
|
|
|
|
RCT_MARGIN_PROPERTY(Left, LEFT)
|
|
|
|
RCT_MARGIN_PROPERTY(Bottom, BOTTOM)
|
|
|
|
RCT_MARGIN_PROPERTY(Right, RIGHT)
|
|
|
|
|
|
|
|
// Padding
|
|
|
|
|
|
|
|
#define RCT_PADDING_PROPERTY(prop, metaProp) \
|
|
|
|
- (void)setPadding##prop:(CGFloat)value \
|
|
|
|
{ \
|
|
|
|
_paddingMetaProps[META_PROP_##metaProp] = value; \
|
|
|
|
_recomputePadding = YES; \
|
|
|
|
} \
|
|
|
|
- (CGFloat)padding##prop \
|
|
|
|
{ \
|
2015-02-19 01:46:39 +00:00
|
|
|
return _paddingMetaProps[META_PROP_##metaProp]; \
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_PADDING_PROPERTY(, ALL)
|
|
|
|
RCT_PADDING_PROPERTY(Vertical, VERTICAL)
|
|
|
|
RCT_PADDING_PROPERTY(Horizontal, HORIZONTAL)
|
|
|
|
RCT_PADDING_PROPERTY(Top, TOP)
|
|
|
|
RCT_PADDING_PROPERTY(Left, LEFT)
|
|
|
|
RCT_PADDING_PROPERTY(Bottom, BOTTOM)
|
|
|
|
RCT_PADDING_PROPERTY(Right, RIGHT)
|
|
|
|
|
2015-02-19 01:46:39 +00:00
|
|
|
- (UIEdgeInsets)paddingAsInsets
|
|
|
|
{
|
|
|
|
return (UIEdgeInsets){
|
|
|
|
_cssNode->style.padding[CSS_TOP],
|
|
|
|
_cssNode->style.padding[CSS_LEFT],
|
|
|
|
_cssNode->style.padding[CSS_BOTTOM],
|
|
|
|
_cssNode->style.padding[CSS_RIGHT]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
// Border
|
|
|
|
|
2015-02-19 01:46:39 +00:00
|
|
|
#define RCT_BORDER_PROPERTY(prop, metaProp) \
|
2015-03-26 13:32:01 +00:00
|
|
|
- (void)setBorder##prop##Width:(CGFloat)value \
|
2015-02-19 01:46:39 +00:00
|
|
|
{ \
|
2015-07-30 13:20:28 +00:00
|
|
|
_borderMetaProps[META_PROP_##metaProp] = value; \
|
|
|
|
_recomputeBorder = YES; \
|
2015-02-19 01:46:39 +00:00
|
|
|
} \
|
2015-03-26 13:32:01 +00:00
|
|
|
- (CGFloat)border##prop##Width \
|
2015-02-19 01:46:39 +00:00
|
|
|
{ \
|
2015-07-30 13:20:28 +00:00
|
|
|
return _borderMetaProps[META_PROP_##metaProp]; \
|
2015-02-19 01:46:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 13:20:28 +00:00
|
|
|
RCT_BORDER_PROPERTY(, ALL)
|
2015-02-19 01:46:39 +00:00
|
|
|
RCT_BORDER_PROPERTY(Top, TOP)
|
|
|
|
RCT_BORDER_PROPERTY(Left, LEFT)
|
|
|
|
RCT_BORDER_PROPERTY(Bottom, BOTTOM)
|
|
|
|
RCT_BORDER_PROPERTY(Right, RIGHT)
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
// Dimensions
|
|
|
|
|
|
|
|
#define RCT_DIMENSIONS_PROPERTY(setProp, getProp, cssProp) \
|
|
|
|
- (void)set##setProp:(CGFloat)value \
|
|
|
|
{ \
|
|
|
|
_cssNode->style.dimensions[CSS_##cssProp] = value; \
|
|
|
|
[self dirtyLayout]; \
|
|
|
|
} \
|
|
|
|
- (CGFloat)getProp \
|
|
|
|
{ \
|
|
|
|
return _cssNode->style.dimensions[CSS_##cssProp]; \
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_DIMENSIONS_PROPERTY(Width, width, WIDTH)
|
|
|
|
RCT_DIMENSIONS_PROPERTY(Height, height, HEIGHT)
|
|
|
|
|
|
|
|
// Position
|
|
|
|
|
|
|
|
#define RCT_POSITION_PROPERTY(setProp, getProp, cssProp) \
|
|
|
|
- (void)set##setProp:(CGFloat)value \
|
|
|
|
{ \
|
|
|
|
_cssNode->style.position[CSS_##cssProp] = value; \
|
|
|
|
[self dirtyLayout]; \
|
|
|
|
} \
|
|
|
|
- (CGFloat)getProp \
|
|
|
|
{ \
|
|
|
|
return _cssNode->style.position[CSS_##cssProp]; \
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_POSITION_PROPERTY(Top, top, TOP)
|
|
|
|
RCT_POSITION_PROPERTY(Right, right, RIGHT)
|
|
|
|
RCT_POSITION_PROPERTY(Bottom, bottom, BOTTOM)
|
|
|
|
RCT_POSITION_PROPERTY(Left, left, LEFT)
|
|
|
|
|
|
|
|
- (void)setFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
_cssNode->style.position[CSS_LEFT] = CGRectGetMinX(frame);
|
|
|
|
_cssNode->style.position[CSS_TOP] = CGRectGetMinY(frame);
|
|
|
|
_cssNode->style.dimensions[CSS_WIDTH] = CGRectGetWidth(frame);
|
|
|
|
_cssNode->style.dimensions[CSS_HEIGHT] = CGRectGetHeight(frame);
|
|
|
|
[self dirtyLayout];
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:39:09 +00:00
|
|
|
- (void)setTopLeft:(CGPoint)topLeft
|
|
|
|
{
|
|
|
|
_cssNode->style.position[CSS_LEFT] = topLeft.x;
|
|
|
|
_cssNode->style.position[CSS_TOP] = topLeft.y;
|
|
|
|
[self dirtyLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSize:(CGSize)size
|
|
|
|
{
|
|
|
|
_cssNode->style.dimensions[CSS_WIDTH] = size.width;
|
|
|
|
_cssNode->style.dimensions[CSS_HEIGHT] = size.height;
|
|
|
|
[self dirtyLayout];
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
// Flex
|
|
|
|
|
|
|
|
#define RCT_STYLE_PROPERTY(setProp, getProp, cssProp, type) \
|
|
|
|
- (void)set##setProp:(type)value \
|
|
|
|
{ \
|
|
|
|
_cssNode->style.cssProp = value; \
|
|
|
|
[self dirtyLayout]; \
|
|
|
|
} \
|
|
|
|
- (type)getProp \
|
|
|
|
{ \
|
|
|
|
return _cssNode->style.cssProp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_STYLE_PROPERTY(Flex, flex, flex, CGFloat)
|
|
|
|
RCT_STYLE_PROPERTY(FlexDirection, flexDirection, flex_direction, css_flex_direction_t)
|
|
|
|
RCT_STYLE_PROPERTY(JustifyContent, justifyContent, justify_content, css_justify_t)
|
|
|
|
RCT_STYLE_PROPERTY(AlignSelf, alignSelf, align_self, css_align_t)
|
|
|
|
RCT_STYLE_PROPERTY(AlignItems, alignItems, align_items, css_align_t)
|
2015-08-06 22:44:15 +00:00
|
|
|
RCT_STYLE_PROPERTY(Position, position, position_type, css_position_type_t)
|
2015-01-30 01:10:49 +00:00
|
|
|
RCT_STYLE_PROPERTY(FlexWrap, flexWrap, flex_wrap, css_wrap_type_t)
|
|
|
|
|
|
|
|
- (void)setBackgroundColor:(UIColor *)color
|
|
|
|
{
|
|
|
|
_backgroundColor = color;
|
|
|
|
[self dirtyPropagation];
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
- (void)updateLayout
|
2015-02-19 01:39:09 +00:00
|
|
|
{
|
|
|
|
if (_recomputePadding) {
|
|
|
|
RCTProcessMetaProps(_paddingMetaProps, _cssNode->style.padding);
|
|
|
|
}
|
|
|
|
if (_recomputeMargin) {
|
|
|
|
RCTProcessMetaProps(_marginMetaProps, _cssNode->style.margin);
|
|
|
|
}
|
2015-07-30 13:20:28 +00:00
|
|
|
if (_recomputeBorder) {
|
|
|
|
RCTProcessMetaProps(_borderMetaProps, _cssNode->style.border);
|
|
|
|
}
|
|
|
|
if (_recomputePadding || _recomputeMargin || _recomputeBorder) {
|
2015-02-19 01:39:09 +00:00
|
|
|
[self dirtyLayout];
|
|
|
|
}
|
|
|
|
[self fillCSSNode:_cssNode];
|
|
|
|
_recomputeMargin = NO;
|
|
|
|
_recomputePadding = NO;
|
2015-07-30 13:20:28 +00:00
|
|
|
_recomputeBorder = NO;
|
2015-02-19 01:39:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
@end
|