Handle children internally in CSSLayout

Reviewed By: javache

Differential Revision: D3588205

fbshipit-source-id: 27f701a9a85cc0e86bb1d233a6bc74b529a8d4d4
This commit is contained in:
Emil Sjolander 2016-07-20 13:26:57 -07:00 committed by Facebook Github Bot 6
parent 566e0553ca
commit 0df569dc7f
7 changed files with 171 additions and 32 deletions

View File

@ -14,6 +14,7 @@
#include <stdlib.h>
#include "CSSLayout.h"
#include "CSSNodeList.h"
CSS_EXTERN_C_BEGIN
@ -85,15 +86,14 @@ typedef struct CSSStyle {
typedef struct CSSNode {
CSSStyle style;
CSSLayout layout;
int childCount;
int lineIndex;
bool shouldUpdate;
bool isTextNode;
CSSNodeListRef children;
struct CSSNode* nextChild;
CSSSize (*measure)(void *context, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode);
struct CSSNode* (*getChild)(void *context, int i);
bool (*isDirty)(void *context);
void (*print)(void *context);
void *context;

View File

@ -30,12 +30,16 @@ __forceinline const float fmaxf(const float a, const float b) {
#define POSITIVE_FLEX_IS_AUTO 0
CSSNodeRef CSSNodeNew() {
CSSNode* node = (CSSNode*)calloc(1, sizeof(*node));
CSSNodeRef node = calloc(1, sizeof(CSSNode));
assert(node != NULL);
node->children = CSSNodeListNew(4);
CSSNodeInit(node);
return node;
}
void CSSNodeFree(CSSNodeRef node) {
CSSNodeListFree(node->children);
free(node);
}
@ -84,6 +88,22 @@ void CSSNodeInit(CSSNodeRef node) {
node->layout.cached_layout.heightMeasureMode = (CSSMeasureMode)-1;
}
void CSSNodeInsertChild(CSSNodeRef node, CSSNodeRef child, unsigned int index) {
CSSNodeListInsert(node->children, child, index);
}
void CSSNodeRemoveChild(CSSNodeRef node, CSSNodeRef child) {
CSSNodeListDelete(node->children, child);
}
CSSNodeRef CSSNodeGetChild(CSSNodeRef node, unsigned int index) {
return CSSNodeListGet(node->children, index);
}
unsigned int CSSNodeChildCount(CSSNodeRef node) {
return CSSNodeListCount(node->children);
}
#define CSS_NODE_PROPERTY_IMPL(type, name, paramName, instanceName) \
void CSSNodeSet##name(CSSNodeRef node, type paramName) { \
node->instanceName = paramName;\
@ -108,9 +128,7 @@ type CSSNodeLayoutGet##name(CSSNodeRef node) { \
} \
CSS_NODE_PROPERTY_IMPL(void*, Context, context, context);
CSS_NODE_PROPERTY_IMPL(int, ChildCount, childCount, childCount);
CSS_NODE_PROPERTY_IMPL(CSSMeasureFunc, MeasureFunc, measureFunc, measure);
CSS_NODE_PROPERTY_IMPL(CSSChildFunc, ChildFunc, childFunc, getChild);
CSS_NODE_PROPERTY_IMPL(CSSIsDirtyFunc, IsDirtyFunc, isDirtyFunc, isDirty);
CSS_NODE_PROPERTY_IMPL(CSSPrintFunc, PrintFunc, printFunc, print);
CSS_NODE_PROPERTY_IMPL(bool, IsTextnode, isTextNode, isTextNode);
@ -335,10 +353,11 @@ static void print_css_node_rec(
print_number_nan("bottom", node->style.position[CSSPositionBottom]);
}
if (options & CSSPrintOptionsChildren && node->childCount > 0) {
unsigned int childCount = CSSNodeListCount(node->children);
if (options & CSSPrintOptionsChildren && childCount > 0) {
printf("children: [\n");
for (int i = 0; i < node->childCount; ++i) {
print_css_node_rec(node->getChild(node->context, i), options, level + 1);
for (unsigned int i = 0; i < childCount; ++i) {
print_css_node_rec(CSSNodeGetChild(node, i), options, level + 1);
}
indent(level);
printf("]},\n");
@ -789,7 +808,7 @@ static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableH
// For nodes with no children, use the available values if they were provided, or
// the minimum size as indicated by the padding and border sizes.
int childCount = node->childCount;
unsigned int childCount = CSSNodeListCount(node->children);
if (childCount == 0) {
node->layout.measuredDimensions[CSSDimensionWidth] = boundAxis(node, CSSFlexDirectionRow,
(widthMeasureMode == CSSMeasureModeUndefined || widthMeasureMode == CSSMeasureModeAtMost) ?
@ -861,13 +880,13 @@ static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableH
// STEP 3: DETERMINE FLEX BASIS FOR EACH ITEM
CSSNode* child;
int i;
unsigned int i;
float childWidth;
float childHeight;
CSSMeasureMode childWidthMeasureMode;
CSSMeasureMode childHeightMeasureMode;
for (i = 0; i < childCount; i++) {
child = node->getChild(node->context, i);
child = CSSNodeListGet(node->children, i);
if (performLayout) {
// Set the initial position (relative to the parent).
@ -1003,7 +1022,7 @@ static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableH
// Add items to the current line until it's full or we run out of items.
while (i < childCount) {
child = node->getChild(node->context, i);
child = CSSNodeListGet(node->children, i);
child->lineIndex = lineCount;
if (child->style.positionType != CSSPositionTypeAbsolute) {
@ -1248,7 +1267,7 @@ static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableH
float crossDim = 0;
for (i = startOfLineIndex; i < endOfLineIndex; ++i) {
child = node->getChild(node->context, i);
child = CSSNodeListGet(node->children, i);
if (child->style.positionType == CSSPositionTypeAbsolute &&
isPosDefined(child, leading[mainAxis])) {
@ -1313,7 +1332,7 @@ static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableH
// We can skip child alignment if we're just measuring the container.
if (performLayout) {
for (i = startOfLineIndex; i < endOfLineIndex; ++i) {
child = node->getChild(node->context, i);
child = CSSNodeListGet(node->children, i);
if (child->style.positionType == CSSPositionTypeAbsolute) {
// If the child is absolutely positioned and has a top/left/bottom/right
@ -1405,7 +1424,7 @@ static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableH
// compute the line's height and find the endIndex
float lineHeight = 0;
for (j = startIndex; j < childCount; ++j) {
child = node->getChild(node->context, j);
child = CSSNodeListGet(node->children, j);
if (child->style.positionType != CSSPositionTypeRelative) {
continue;
}
@ -1422,7 +1441,7 @@ static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableH
if (performLayout) {
for (j = startIndex; j < endIndex; ++j) {
child = node->getChild(node->context, j);
child = CSSNodeListGet(node->children, j);
if (child->style.positionType != CSSPositionTypeRelative) {
continue;
}
@ -1493,7 +1512,7 @@ static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableH
// Set trailing position if necessary.
if (needsMainTrailingPos || needsCrossTrailingPos) {
for (i = 0; i < childCount; ++i) {
child = node->getChild(node->context, i);
child = CSSNodeListGet(node->children, i);
if (needsMainTrailingPos) {
setTrailingPosition(node, child, mainAxis);

View File

@ -110,7 +110,6 @@ typedef struct CSSSize {
typedef struct CSSNode * CSSNodeRef;
typedef CSSSize (*CSSMeasureFunc)(void *context, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode);
typedef CSSNodeRef (*CSSChildFunc)(void *context, int i);
typedef bool (*CSSIsDirtyFunc)(void *context);
typedef void (*CSSPrintFunc)(void *context);
@ -119,6 +118,11 @@ CSSNodeRef CSSNodeNew();
void CSSNodeInit(CSSNodeRef node);
void CSSNodeFree(CSSNodeRef node);
void CSSNodeInsertChild(CSSNodeRef node, CSSNodeRef child, unsigned int index);
void CSSNodeRemoveChild(CSSNodeRef node, CSSNodeRef child);
CSSNodeRef CSSNodeGetChild(CSSNodeRef node, unsigned int index);
unsigned int CSSNodeChildCount(CSSNodeRef node);
void CSSNodeCalculateLayout(
CSSNodeRef node,
float availableWidth,
@ -141,9 +145,7 @@ type CSSNodeStyleGet##name(CSSNodeRef node);
type CSSNodeLayoutGet##name(CSSNodeRef node);
CSS_NODE_PROPERTY(void*, Context, context);
CSS_NODE_PROPERTY(int, ChildCount, childCount);
CSS_NODE_PROPERTY(CSSMeasureFunc, MeasureFunc, measureFunc);
CSS_NODE_PROPERTY(CSSChildFunc, ChildFunc, childFunc);
CSS_NODE_PROPERTY(CSSIsDirtyFunc, IsDirtyFunc, isDirtyFunc);
CSS_NODE_PROPERTY(CSSPrintFunc, PrintFunc, printFunc);
CSS_NODE_PROPERTY(bool, IsTextnode, isTextNode);

View File

@ -0,0 +1,86 @@
/**
* Copyright (c) 2014-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.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "CSSNodeList.h"
struct CSSNodeList {
int capacity;
int count;
void **items;
};
CSSNodeListRef CSSNodeListNew(unsigned int initialCapacity) {
CSSNodeListRef list = malloc(sizeof(struct CSSNodeList));
assert(list != NULL);
list->capacity = initialCapacity;
list->count = 0;
list->items = malloc(sizeof(void*) * list->capacity);
assert(list->items != NULL);
return list;
}
void CSSNodeListFree(CSSNodeListRef list) {
free(list);
}
unsigned int CSSNodeListCount(CSSNodeListRef list) {
return list->count;
}
void CSSNodeListAdd(CSSNodeListRef list, CSSNodeRef node) {
CSSNodeListInsert(list, node, list->count);
}
void CSSNodeListInsert(CSSNodeListRef list, CSSNodeRef node, unsigned int index) {
if (list->count == list->capacity) {
list->capacity *= 2;
list->items = realloc(list->items, sizeof(void*) * list->capacity);
assert(list->items != NULL);
}
for (unsigned int i = list->count; i > index; i--) {
list->items[i] = list->items[i - 1];
}
list->count++;
list->items[index] = node;
}
CSSNodeRef CSSNodeListRemove(CSSNodeListRef list, unsigned int index) {
CSSNodeRef removed = list->items[index];
list->items[index] = NULL;
for (unsigned int i = index; i < list->count - 1; i++) {
list->items[i] = list->items[i + 1];
list->items[i + 1] = NULL;
}
list->count--;
return removed;
}
CSSNodeRef CSSNodeListDelete(CSSNodeListRef list, CSSNodeRef node) {
for (unsigned int i = 0; i < list->count; i++) {
if (list->items[i] == node) {
return CSSNodeListRemove(list, i);
}
}
return NULL;
}
CSSNodeRef CSSNodeListGet(CSSNodeListRef list, unsigned int index) {
return list->items[index];
}

View File

@ -0,0 +1,30 @@
/**
* Copyright (c) 2014-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.
*/
#ifndef __CSS_NODE_LIST_H
#define __CSS_NODE_LIST_H
#include <CSSLayout/CSSLayout.h>
CSS_EXTERN_C_BEGIN
typedef struct CSSNodeList * CSSNodeListRef;
CSSNodeListRef CSSNodeListNew(unsigned int initialCapacity);
void CSSNodeListFree(CSSNodeListRef list);
unsigned int CSSNodeListCount(CSSNodeListRef list);
void CSSNodeListAdd(CSSNodeListRef list, CSSNodeRef node);
void CSSNodeListInsert(CSSNodeListRef list, CSSNodeRef node, unsigned int index);
CSSNodeRef CSSNodeListRemove(CSSNodeListRef list, unsigned int index);
CSSNodeRef CSSNodeListDelete(CSSNodeListRef list, CSSNodeRef node);
CSSNodeRef CSSNodeListGet(CSSNodeListRef list, unsigned int index);
CSS_EXTERN_C_END
#endif

View File

@ -11,6 +11,7 @@
008341F61D1DB34400876D9A /* RCTJSStackFrame.m in Sources */ = {isa = PBXBuildFile; fileRef = 008341F41D1DB34400876D9A /* RCTJSStackFrame.m */; };
131B6AF41AF1093D00FFC3E0 /* RCTSegmentedControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 131B6AF11AF1093D00FFC3E0 /* RCTSegmentedControl.m */; };
131B6AF51AF1093D00FFC3E0 /* RCTSegmentedControlManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 131B6AF31AF1093D00FFC3E0 /* RCTSegmentedControlManager.m */; };
1321C8D01D3EB50800D58318 /* CSSNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = 1321C8CE1D3EB50800D58318 /* CSSNodeList.c */; };
133683521D37ACA10077D0C3 /* CSSLayout.c in Sources */ = {isa = PBXBuildFile; fileRef = 133683441D37ACA10077D0C3 /* CSSLayout.c */; };
133CAE8E1B8E5CFD00F6AD92 /* RCTDatePicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 133CAE8D1B8E5CFD00F6AD92 /* RCTDatePicker.m */; };
13456E931ADAD2DE009F94A7 /* RCTConvert+CoreLocation.m in Sources */ = {isa = PBXBuildFile; fileRef = 13456E921ADAD2DE009F94A7 /* RCTConvert+CoreLocation.m */; };
@ -126,6 +127,8 @@
131B6AF11AF1093D00FFC3E0 /* RCTSegmentedControl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSegmentedControl.m; sourceTree = "<group>"; };
131B6AF21AF1093D00FFC3E0 /* RCTSegmentedControlManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTSegmentedControlManager.h; sourceTree = "<group>"; };
131B6AF31AF1093D00FFC3E0 /* RCTSegmentedControlManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTSegmentedControlManager.m; sourceTree = "<group>"; };
1321C8CE1D3EB50800D58318 /* CSSNodeList.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CSSNodeList.c; sourceTree = "<group>"; };
1321C8CF1D3EB50800D58318 /* CSSNodeList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSNodeList.h; sourceTree = "<group>"; };
133683441D37ACA10077D0C3 /* CSSLayout.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CSSLayout.c; sourceTree = "<group>"; };
133683451D37ACA10077D0C3 /* CSSLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSLayout.h; sourceTree = "<group>"; };
133683481D37ACA10077D0C3 /* CSSMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSMacros.h; sourceTree = "<group>"; };
@ -344,6 +347,8 @@
isa = PBXGroup;
children = (
131541CF1D3E4893006A0E08 /* CSSLayout-internal.h */,
1321C8CE1D3EB50800D58318 /* CSSNodeList.c */,
1321C8CF1D3EB50800D58318 /* CSSNodeList.h */,
133683441D37ACA10077D0C3 /* CSSLayout.c */,
133683451D37ACA10077D0C3 /* CSSLayout.h */,
133683481D37ACA10077D0C3 /* CSSMacros.h */,
@ -728,6 +733,7 @@
13A0C28A1B74F71200B29F6F /* RCTDevMenu.m in Sources */,
13BCE8091C99CB9D00DD7AAD /* RCTRootShadowView.m in Sources */,
14C2CA711B3AC63800E6CBB2 /* RCTModuleMethod.m in Sources */,
1321C8D01D3EB50800D58318 /* CSSNodeList.c in Sources */,
13CC8A821B17642100940AE7 /* RCTBorderDrawing.m in Sources */,
83CBBA511A601E3B00E9B192 /* RCTAssert.m in Sources */,
13AF20451AE707F9005F5298 /* RCTSlider.m in Sources */,

View File

@ -56,13 +56,6 @@ static void RCTPrint(void *context)
printf("%s(%zd), ", shadowView.viewName.UTF8String, shadowView.reactTag.integerValue);
}
static CSSNodeRef 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;
@ -189,9 +182,9 @@ DEFINE_PROCESS_META_PROPS(Border);
viewsWithNewFrame:(NSMutableSet<RCTShadowView *> *)viewsWithNewFrame
absolutePosition:(CGPoint)absolutePosition
{
for (int i = 0; i < CSSNodeGetChildCount(node); ++i) {
for (unsigned int i = 0; i < CSSNodeChildCount(node); ++i) {
RCTShadowView *child = (RCTShadowView *)_reactSubviews[i];
[child applyLayoutNode:RCTGetChild(CSSNodeGetContext(node), i)
[child applyLayoutNode:CSSNodeGetChild(node, i)
viewsWithNewFrame:viewsWithNewFrame
absolutePosition:absolutePosition];
}
@ -319,7 +312,6 @@ DEFINE_PROCESS_META_PROPS(Border);
_cssNode = CSSNodeNew();
CSSNodeSetContext(_cssNode, (__bridge void *)self);
CSSNodeSetChildFunc(_cssNode, RCTGetChild);
CSSNodeSetPrintFunc(_cssNode, RCTPrint);
CSSNodeSetIsDirtyFunc(_cssNode, RCTIsDirty);
}
@ -388,7 +380,9 @@ DEFINE_PROCESS_META_PROPS(Border);
- (void)insertReactSubview:(RCTShadowView *)subview atIndex:(NSInteger)atIndex
{
[_reactSubviews insertObject:subview atIndex:atIndex];
CSSNodeSetChildCount(_cssNode, [self isCSSLeafNode] ? 0 : (int)_reactSubviews.count);
if (![self isCSSLeafNode]) {
CSSNodeInsertChild(_cssNode, subview.cssNode, atIndex);
}
subview->_superview = self;
_didUpdateSubviews = YES;
[self dirtyText];
@ -404,7 +398,9 @@ DEFINE_PROCESS_META_PROPS(Border);
_didUpdateSubviews = YES;
subview->_superview = nil;
[_reactSubviews removeObject:subview];
CSSNodeSetChildCount(_cssNode, [self isCSSLeafNode] ? 0 : (int)_reactSubviews.count);
if (![self isCSSLeafNode]) {
CSSNodeRemoveChild(_cssNode, subview.cssNode);
}
}
- (NSArray<RCTShadowView *> *)reactSubviews