From f7a22bc33c3418409238e7bce2b5c9a2410ad1e4 Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Mon, 15 Aug 2016 09:15:10 -0700 Subject: [PATCH] Use single function for margin, position, padding, and border Reviewed By: lucasr Differential Revision: D3715201 fbshipit-source-id: ea81ed426f0f7853bb542355c01fc16ae4360238 --- .../UIExplorerUnitTests/RCTShadowViewTests.m | 36 +-- React/CSSLayout/CSSLayout-internal.h | 10 - React/CSSLayout/CSSLayout.c | 62 +++-- React/CSSLayout/CSSLayout.h | 47 ++-- React/Views/RCTShadowView.m | 95 ++++--- .../com/facebook/csslayout/CSSNodeJNI.java | 262 +++--------------- .../java/com/facebook/csslayout/Spacing.java | 30 +- 7 files changed, 179 insertions(+), 363 deletions(-) diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTShadowViewTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTShadowViewTests.m index 3f003dbe9..6e5dae34f 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTShadowViewTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTShadowViewTests.m @@ -56,8 +56,8 @@ RCTShadowView *centerView = [self _shadowViewWithConfig:^(CSSNodeRef node) { CSSNodeStyleSetFlex(node, 2); - CSSNodeStyleSetMarginLeft(node, 10); - CSSNodeStyleSetMarginRight(node, 10); + CSSNodeStyleSetMargin(node, CSSEdgeLeft, 10); + CSSNodeStyleSetMargin(node, CSSEdgeRight, 10); }]; RCTShadowView *rightView = [self _shadowViewWithConfig:^(CSSNodeRef node) { @@ -67,8 +67,8 @@ RCTShadowView *mainView = [self _shadowViewWithConfig:^(CSSNodeRef node) { CSSNodeStyleSetFlexDirection(node, CSSFlexDirectionRow); CSSNodeStyleSetFlex(node, 2); - CSSNodeStyleSetMarginTop(node, 10); - CSSNodeStyleSetMarginBottom(node, 10); + CSSNodeStyleSetMargin(node, CSSEdgeTop, 10); + CSSNodeStyleSetMargin(node, CSSEdgeBottom, 10); }]; [mainView insertReactSubview:leftView atIndex:0]; @@ -83,10 +83,10 @@ CSSNodeStyleSetFlex(node, 1); }]; - CSSNodeStyleSetPaddingLeft(self.parentView.cssNode, 10); - CSSNodeStyleSetPaddingTop(self.parentView.cssNode, 10); - CSSNodeStyleSetPaddingRight(self.parentView.cssNode, 10); - CSSNodeStyleSetPaddingBottom(self.parentView.cssNode, 10); + CSSNodeStyleSetPadding(self.parentView.cssNode, CSSEdgeLeft, 10); + CSSNodeStyleSetPadding(self.parentView.cssNode, CSSEdgeTop, 10); + CSSNodeStyleSetPadding(self.parentView.cssNode, CSSEdgeRight, 10); + CSSNodeStyleSetPadding(self.parentView.cssNode, CSSEdgeBottom, 10); [self.parentView insertReactSubview:headerView atIndex:0]; [self.parentView insertReactSubview:mainView atIndex:1]; @@ -111,20 +111,20 @@ RCTShadowView *centerView = [self _shadowViewWithConfig:^(CSSNodeRef node) { CSSNodeStyleSetFlex(node, 1); }]; - + RCTShadowView *mainView = [self _shadowViewWithConfig:^(CSSNodeRef node) { CSSNodeStyleSetFlex(node, 1); }]; - + [mainView insertReactSubview:centerView atIndex:0]; RCTShadowView *footerView = [self _shadowViewWithConfig:^(CSSNodeRef node) { CSSNodeStyleSetFlex(node, 1); }]; - + [self.parentView insertReactSubview:mainView atIndex:0]; [self.parentView insertReactSubview:footerView atIndex:1]; - + XCTAssertTrue([centerView viewIsDescendantOf:mainView]); XCTAssertFalse([footerView viewIsDescendantOf:mainView]); } @@ -132,8 +132,8 @@ - (void)testAssignsSuggestedWidthDimension { [self _withShadowViewWithStyle:^(CSSNodeRef node) { - CSSNodeStyleSetPositionLeft(node, 0); - CSSNodeStyleSetPositionTop(node, 0); + CSSNodeStyleSetPosition(node, CSSEdgeLeft, 0); + CSSNodeStyleSetPosition(node, CSSEdgeTop, 0); CSSNodeStyleSetHeight(node, 10); } assertRelativeLayout:CGRectMake(0, 0, 3, 10) @@ -143,8 +143,8 @@ - (void)testAssignsSuggestedHeightDimension { [self _withShadowViewWithStyle:^(CSSNodeRef node) { - CSSNodeStyleSetPositionLeft(node, 0); - CSSNodeStyleSetPositionTop(node, 0); + CSSNodeStyleSetPosition(node, CSSEdgeLeft, 0); + CSSNodeStyleSetPosition(node, CSSEdgeTop, 0); CSSNodeStyleSetWidth(node, 10); } assertRelativeLayout:CGRectMake(0, 0, 10, 4) @@ -154,8 +154,8 @@ - (void)testDoesNotOverrideDimensionStyleWithSuggestedDimensions { [self _withShadowViewWithStyle:^(CSSNodeRef node) { - CSSNodeStyleSetPositionLeft(node, 0); - CSSNodeStyleSetPositionTop(node, 0); + CSSNodeStyleSetPosition(node, CSSEdgeLeft, 0); + CSSNodeStyleSetPosition(node, CSSEdgeTop, 0); CSSNodeStyleSetWidth(node, 10); CSSNodeStyleSetHeight(node, 10); } diff --git a/React/CSSLayout/CSSLayout-internal.h b/React/CSSLayout/CSSLayout-internal.h index 67e16e5ca..e5cf8966b 100644 --- a/React/CSSLayout/CSSLayout-internal.h +++ b/React/CSSLayout/CSSLayout-internal.h @@ -62,16 +62,6 @@ typedef struct CSSStyle { float flexBasis; float margin[6]; float position[6]; - /** - * You should skip all the rules that contain negative values for the - * following attributes. For example: - * {padding: 10, paddingLeft: -5} - * should output: - * {left: 10 ...} - * the following two are incorrect: - * {left: -5 ...} - * {left: 0 ...} - */ float padding[6]; float border[6]; float dimensions[2]; diff --git a/React/CSSLayout/CSSLayout.c b/React/CSSLayout/CSSLayout.c index 29f73a966..40ee61c95 100644 --- a/React/CSSLayout/CSSLayout.c +++ b/React/CSSLayout/CSSLayout.c @@ -179,6 +179,37 @@ float CSSNodeStyleGetFlex(CSSNodeRef node) { return node->style.instanceName; \ } +#define CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(type, name, paramName, instanceName) \ + void CSSNodeStyleSet##name(CSSNodeRef node, CSSEdge edge, type paramName) { \ + switch (edge) { \ + case CSSEdgeHorizontal: \ + CSSNodeStyleSet##name(node, CSSEdgeLeft, paramName); \ + CSSNodeStyleSet##name(node, CSSEdgeRight, paramName); \ + CSSNodeStyleSet##name(node, CSSEdgeStart, paramName); \ + CSSNodeStyleSet##name(node, CSSEdgeEnd, paramName); \ + break; \ + case CSSEdgeVertical: \ + CSSNodeStyleSet##name(node, CSSEdgeTop, paramName); \ + CSSNodeStyleSet##name(node, CSSEdgeBottom, paramName); \ + break; \ + case CSSEdgeAll: \ + CSSNodeStyleSet##name(node, CSSEdgeHorizontal, paramName); \ + CSSNodeStyleSet##name(node, CSSEdgeVertical, paramName); \ + break; \ + default: \ + if (node->style.instanceName[edge] != paramName) { \ + node->style.instanceName[edge] = paramName; \ + _CSSNodeMarkDirty(node); \ + } \ + break; \ + } \ + } \ + \ + type CSSNodeStyleGet##name(CSSNodeRef node, CSSEdge edge) { \ + CSS_ASSERT(edge <= CSSEdgeEnd, "Cannot get value of compound edge"); \ + return node->style.instanceName[edge]; \ + } + #define CSS_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \ type CSSNodeLayoutGet##name(CSSNodeRef node) { \ return node->layout.instanceName; \ @@ -203,33 +234,10 @@ CSS_NODE_STYLE_PROPERTY_IMPL(float, FlexGrow, flexGrow, flexGrow); CSS_NODE_STYLE_PROPERTY_IMPL(float, FlexShrink, flexShrink, flexShrink); CSS_NODE_STYLE_PROPERTY_IMPL(float, FlexBasis, flexBasis, flexBasis); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PositionLeft, positionLeft, position[CSSPositionLeft]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PositionTop, positionTop, position[CSSPositionTop]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PositionRight, positionRight, position[CSSPositionRight]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PositionBottom, positionBottom, position[CSSPositionBottom]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PositionStart, positionStart, position[CSSPositionStart]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PositionEnd, positionEnd, position[CSSPositionEnd]); - -CSS_NODE_STYLE_PROPERTY_IMPL(float, MarginLeft, marginLeft, margin[CSSPositionLeft]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MarginTop, marginTop, margin[CSSPositionTop]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MarginRight, marginRight, margin[CSSPositionRight]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MarginBottom, marginBottom, margin[CSSPositionBottom]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MarginStart, marginStart, margin[CSSPositionStart]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, MarginEnd, marginEnd, margin[CSSPositionEnd]); - -CSS_NODE_STYLE_PROPERTY_IMPL(float, PaddingLeft, paddingLeft, padding[CSSPositionLeft]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PaddingTop, paddingTop, padding[CSSPositionTop]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PaddingRight, paddingRight, padding[CSSPositionRight]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PaddingBottom, paddingBottom, padding[CSSPositionBottom]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PaddingStart, paddingStart, padding[CSSPositionStart]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, PaddingEnd, paddingEnd, padding[CSSPositionEnd]); - -CSS_NODE_STYLE_PROPERTY_IMPL(float, BorderLeft, borderLeft, border[CSSPositionLeft]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, BorderTop, borderTop, border[CSSPositionTop]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, BorderRight, borderRight, border[CSSPositionRight]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, BorderBottom, borderBottom, border[CSSPositionBottom]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, BorderStart, borderStart, border[CSSPositionStart]); -CSS_NODE_STYLE_PROPERTY_IMPL(float, BorderEnd, BorderEnd, border[CSSPositionEnd]); +CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Position, position, position); +CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Margin, margin, margin); +CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Padding, padding, padding); +CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Border, border, border); CSS_NODE_STYLE_PROPERTY_IMPL(float, Width, width, dimensions[CSSDimensionWidth]); CSS_NODE_STYLE_PROPERTY_IMPL(float, Height, height, dimensions[CSSDimensionHeight]); diff --git a/React/CSSLayout/CSSLayout.h b/React/CSSLayout/CSSLayout.h index 795164ce1..81f86552b 100644 --- a/React/CSSLayout/CSSLayout.h +++ b/React/CSSLayout/CSSLayout.h @@ -101,6 +101,18 @@ typedef enum CSSDimension { CSSDimensionHeight, } CSSDimension; +typedef enum CSSEdge { + CSSEdgeLeft, + CSSEdgeTop, + CSSEdgeRight, + CSSEdgeBottom, + CSSEdgeStart, + CSSEdgeEnd, + CSSEdgeHorizontal, + CSSEdgeVertical, + CSSEdgeAll, +} CSSEdge; + typedef enum CSSPrintOptions { CSSPrintOptionsLayout = 1, CSSPrintOptionsStyle = 2, @@ -156,6 +168,10 @@ bool CSSValueIsUndefined(float value); void CSSNodeStyleSet##name(CSSNodeRef node, type paramName); \ type CSSNodeStyleGet##name(CSSNodeRef node); +#define CSS_NODE_STYLE_EDGE_PROPERTY(type, name, paramName) \ + void CSSNodeStyleSet##name(CSSNodeRef node, CSSEdge edge, type paramName); \ + type CSSNodeStyleGet##name(CSSNodeRef node, CSSEdge edge); + #define CSS_NODE_LAYOUT_PROPERTY(type, name) type CSSNodeLayoutGet##name(CSSNodeRef node); CSS_NODE_PROPERTY(void *, Context, context); @@ -178,33 +194,10 @@ CSS_NODE_STYLE_PROPERTY(float, FlexGrow, flexGrow); CSS_NODE_STYLE_PROPERTY(float, FlexShrink, flexShrink); CSS_NODE_STYLE_PROPERTY(float, FlexBasis, flexBasis); -CSS_NODE_STYLE_PROPERTY(float, PositionLeft, positionLeft); -CSS_NODE_STYLE_PROPERTY(float, PositionTop, positionTop); -CSS_NODE_STYLE_PROPERTY(float, PositionRight, positionRight); -CSS_NODE_STYLE_PROPERTY(float, PositionBottom, positionBottom); -CSS_NODE_STYLE_PROPERTY(float, PositionStart, positionStart); -CSS_NODE_STYLE_PROPERTY(float, PositionEnd, positionEnd); - -CSS_NODE_STYLE_PROPERTY(float, MarginLeft, marginLeft); -CSS_NODE_STYLE_PROPERTY(float, MarginTop, marginTop); -CSS_NODE_STYLE_PROPERTY(float, MarginRight, marginRight); -CSS_NODE_STYLE_PROPERTY(float, MarginBottom, marginBottom); -CSS_NODE_STYLE_PROPERTY(float, MarginStart, marginStart); -CSS_NODE_STYLE_PROPERTY(float, MarginEnd, marginEnd); - -CSS_NODE_STYLE_PROPERTY(float, PaddingLeft, paddingLeft); -CSS_NODE_STYLE_PROPERTY(float, PaddingTop, paddingTop); -CSS_NODE_STYLE_PROPERTY(float, PaddingRight, paddingRight); -CSS_NODE_STYLE_PROPERTY(float, PaddingBottom, paddingBottom); -CSS_NODE_STYLE_PROPERTY(float, PaddingStart, paddingStart); -CSS_NODE_STYLE_PROPERTY(float, PaddingEnd, paddingEnd); - -CSS_NODE_STYLE_PROPERTY(float, BorderLeft, borderLeft); -CSS_NODE_STYLE_PROPERTY(float, BorderTop, borderTop); -CSS_NODE_STYLE_PROPERTY(float, BorderRight, borderRight); -CSS_NODE_STYLE_PROPERTY(float, BorderBottom, borderBottom); -CSS_NODE_STYLE_PROPERTY(float, BorderStart, borderStart); -CSS_NODE_STYLE_PROPERTY(float, BorderEnd, borderEnd); +CSS_NODE_STYLE_EDGE_PROPERTY(float, Position, position); +CSS_NODE_STYLE_EDGE_PROPERTY(float, Margin, margin); +CSS_NODE_STYLE_EDGE_PROPERTY(float, Padding, padding); +CSS_NODE_STYLE_EDGE_PROPERTY(float, Border, border); CSS_NODE_STYLE_PROPERTY(float, Width, width); CSS_NODE_STYLE_PROPERTY(float, Height, height); diff --git a/React/Views/RCTShadowView.m b/React/Views/RCTShadowView.m index 3bbd045ea..76b262b15 100644 --- a/React/Views/RCTShadowView.m +++ b/React/Views/RCTShadowView.m @@ -60,43 +60,43 @@ static void RCTPrint(void *context) #define DEFINE_PROCESS_META_PROPS(type) \ static void RCTProcessMetaProps##type(const float metaProps[META_PROP_COUNT], CSSNodeRef node) { \ if (!CSSValueIsUndefined(metaProps[META_PROP_LEFT])) { \ - CSSNodeStyleSet##type##Start(node, metaProps[META_PROP_LEFT]); \ + CSSNodeStyleSet##type(node, CSSEdgeStart, metaProps[META_PROP_LEFT]); \ } else if (!CSSValueIsUndefined(metaProps[META_PROP_HORIZONTAL])) { \ - CSSNodeStyleSet##type##Start(node, metaProps[META_PROP_HORIZONTAL]); \ + CSSNodeStyleSet##type(node, CSSEdgeStart, metaProps[META_PROP_HORIZONTAL]); \ } else if (!CSSValueIsUndefined(metaProps[META_PROP_ALL])) { \ - CSSNodeStyleSet##type##Start(node, metaProps[META_PROP_ALL]); \ + CSSNodeStyleSet##type(node, CSSEdgeStart, metaProps[META_PROP_ALL]); \ } else { \ - CSSNodeStyleSet##type##Start(node, 0); \ + CSSNodeStyleSet##type(node, CSSEdgeStart, 0); \ } \ \ if (!CSSValueIsUndefined(metaProps[META_PROP_RIGHT])) { \ - CSSNodeStyleSet##type##End(node, metaProps[META_PROP_RIGHT]); \ + CSSNodeStyleSet##type(node, CSSEdgeEnd, metaProps[META_PROP_RIGHT]); \ } else if (!CSSValueIsUndefined(metaProps[META_PROP_HORIZONTAL])) { \ - CSSNodeStyleSet##type##End(node, metaProps[META_PROP_HORIZONTAL]); \ + CSSNodeStyleSet##type(node, CSSEdgeEnd, metaProps[META_PROP_HORIZONTAL]); \ } else if (!CSSValueIsUndefined(metaProps[META_PROP_ALL])) { \ - CSSNodeStyleSet##type##End(node, metaProps[META_PROP_ALL]); \ + CSSNodeStyleSet##type(node, CSSEdgeEnd, metaProps[META_PROP_ALL]); \ } else { \ - CSSNodeStyleSet##type##End(node, 0); \ + CSSNodeStyleSet##type(node, CSSEdgeEnd, 0); \ } \ \ if (!CSSValueIsUndefined(metaProps[META_PROP_TOP])) { \ - CSSNodeStyleSet##type##Top(node, metaProps[META_PROP_TOP]); \ + CSSNodeStyleSet##type(node, CSSEdgeTop, metaProps[META_PROP_TOP]); \ } else if (!CSSValueIsUndefined(metaProps[META_PROP_VERTICAL])) { \ - CSSNodeStyleSet##type##Top(node, metaProps[META_PROP_VERTICAL]); \ + CSSNodeStyleSet##type(node, CSSEdgeTop, metaProps[META_PROP_VERTICAL]); \ } else if (!CSSValueIsUndefined(metaProps[META_PROP_ALL])) { \ - CSSNodeStyleSet##type##Top(node, metaProps[META_PROP_ALL]); \ + CSSNodeStyleSet##type(node, CSSEdgeTop, metaProps[META_PROP_ALL]); \ } else { \ - CSSNodeStyleSet##type##Top(node, 0); \ + CSSNodeStyleSet##type(node, CSSEdgeTop, 0); \ } \ \ if (!CSSValueIsUndefined(metaProps[META_PROP_BOTTOM])) { \ - CSSNodeStyleSet##type##Bottom(node, metaProps[META_PROP_BOTTOM]); \ + CSSNodeStyleSet##type(node, CSSEdgeBottom, metaProps[META_PROP_BOTTOM]); \ } else if (!CSSValueIsUndefined(metaProps[META_PROP_VERTICAL])) { \ - CSSNodeStyleSet##type##Bottom(node, metaProps[META_PROP_VERTICAL]); \ + CSSNodeStyleSet##type(node, CSSEdgeBottom, metaProps[META_PROP_VERTICAL]); \ } else if (!CSSValueIsUndefined(metaProps[META_PROP_ALL])) { \ - CSSNodeStyleSet##type##Bottom(node, metaProps[META_PROP_ALL]); \ + CSSNodeStyleSet##type(node, CSSEdgeBottom, metaProps[META_PROP_ALL]); \ } else { \ - CSSNodeStyleSet##type##Bottom(node, 0); \ + CSSNodeStyleSet##type(node, CSSEdgeBottom, 0); \ } \ } @@ -254,8 +254,8 @@ DEFINE_PROCESS_META_PROPS(Border); CSSNodeStyleSetPositionType(_cssNode, CSSPositionTypeAbsolute); CSSNodeStyleSetWidth(_cssNode, frame.size.width); CSSNodeStyleSetHeight(_cssNode, frame.size.height); - CSSNodeStyleSetPositionLeft(_cssNode, frame.origin.x); - CSSNodeStyleSetPositionTop(_cssNode, frame.origin.y); + CSSNodeStyleSetPosition(_cssNode, CSSEdgeLeft, frame.origin.x); + CSSNodeStyleSetPosition(_cssNode, CSSEdgeTop, frame.origin.y); } CSSNodeCalculateLayout(_cssNode, frame.size.width, frame.size.height, CSSDirectionInherit); @@ -483,25 +483,25 @@ RCT_PADDING_PROPERTY(Right, RIGHT) { if (CSSNodeLayoutGetDirection(_cssNode) == CSSDirectionRTL) { return (UIEdgeInsets){ - CSSNodeStyleGetPaddingTop(_cssNode), - !CSSValueIsUndefined(CSSNodeStyleGetPaddingEnd(_cssNode)) ? - CSSNodeStyleGetPaddingEnd(_cssNode) : - CSSNodeStyleGetPaddingLeft(_cssNode), - CSSNodeStyleGetPaddingBottom(_cssNode), - !CSSValueIsUndefined(CSSNodeStyleGetPaddingStart(_cssNode)) ? - CSSNodeStyleGetPaddingStart(_cssNode) : - CSSNodeStyleGetPaddingRight(_cssNode) + CSSNodeStyleGetPadding(_cssNode, CSSEdgeTop), + !CSSValueIsUndefined(CSSNodeStyleGetPadding(_cssNode, CSSEdgeEnd)) ? + CSSNodeStyleGetPadding(_cssNode, CSSEdgeEnd) : + CSSNodeStyleGetPadding(_cssNode, CSSEdgeLeft), + CSSNodeStyleGetPadding(_cssNode, CSSEdgeBottom), + !CSSValueIsUndefined(CSSNodeStyleGetPadding(_cssNode, CSSEdgeStart)) ? + CSSNodeStyleGetPadding(_cssNode, CSSEdgeStart) : + CSSNodeStyleGetPadding(_cssNode, CSSEdgeRight) }; } else { return (UIEdgeInsets){ - CSSNodeStyleGetPaddingTop(_cssNode), - !CSSValueIsUndefined(CSSNodeStyleGetPaddingStart(_cssNode)) ? - CSSNodeStyleGetPaddingStart(_cssNode) : - CSSNodeStyleGetPaddingLeft(_cssNode), - CSSNodeStyleGetPaddingBottom(_cssNode), - !CSSValueIsUndefined(CSSNodeStyleGetPaddingEnd(_cssNode)) ? - CSSNodeStyleGetPaddingEnd(_cssNode) : - CSSNodeStyleGetPaddingRight(_cssNode) + CSSNodeStyleGetPadding(_cssNode, CSSEdgeTop), + !CSSValueIsUndefined(CSSNodeStyleGetPadding(_cssNode, CSSEdgeStart)) ? + CSSNodeStyleGetPadding(_cssNode, CSSEdgeStart) : + CSSNodeStyleGetPadding(_cssNode, CSSEdgeLeft), + CSSNodeStyleGetPadding(_cssNode, CSSEdgeBottom), + !CSSValueIsUndefined(CSSNodeStyleGetPadding(_cssNode, CSSEdgeEnd)) ? + CSSNodeStyleGetPadding(_cssNode, CSSEdgeEnd) : + CSSNodeStyleGetPadding(_cssNode, CSSEdgeRight) }; } } @@ -548,17 +548,28 @@ RCT_DIMENSION_PROPERTY(MaxHeight, maxHeight, MaxHeight) // Position -RCT_DIMENSION_PROPERTY(Top, top, PositionTop) -RCT_DIMENSION_PROPERTY(Right, right, PositionEnd) -RCT_DIMENSION_PROPERTY(Bottom, bottom, PositionBottom) -RCT_DIMENSION_PROPERTY(Left, left, PositionStart) +#define RCT_POSITION_PROPERTY(setProp, getProp, edge) \ +- (void)set##setProp:(CGFloat)value \ +{ \ + CSSNodeStyleSetPosition(_cssNode, edge, value); \ + [self dirtyText]; \ +} \ +- (CGFloat)getProp \ +{ \ + return CSSNodeStyleGetPosition(_cssNode, edge); \ +} + +RCT_POSITION_PROPERTY(Top, top, CSSEdgeTop) +RCT_POSITION_PROPERTY(Right, right, CSSEdgeEnd) +RCT_POSITION_PROPERTY(Bottom, bottom, CSSEdgeBottom) +RCT_POSITION_PROPERTY(Left, left, CSSEdgeStart) - (void)setFrame:(CGRect)frame { if (!CGRectEqualToRect(frame, _frame)) { _frame = frame; - CSSNodeStyleSetPositionLeft(_cssNode, CGRectGetMinX(frame)); - CSSNodeStyleSetPositionTop(_cssNode, CGRectGetMinY(frame)); + CSSNodeStyleSetPosition(_cssNode, CSSEdgeLeft, CGRectGetMinX(frame)); + CSSNodeStyleSetPosition(_cssNode, CSSEdgeTop, CGRectGetMinY(frame)); CSSNodeStyleSetWidth(_cssNode, CGRectGetWidth(frame)); CSSNodeStyleSetHeight(_cssNode, CGRectGetHeight(frame)); } @@ -592,8 +603,8 @@ static inline void RCTAssignSuggestedDimension(CSSNodeRef cssNode, CSSDimension - (void)setTopLeft:(CGPoint)topLeft { - CSSNodeStyleSetPositionLeft(_cssNode, topLeft.x); - CSSNodeStyleSetPositionTop(_cssNode, topLeft.y); + CSSNodeStyleSetPosition(_cssNode, CSSEdgeLeft, topLeft.x); + CSSNodeStyleSetPosition(_cssNode, CSSEdgeTop, topLeft.y); } - (void)setSize:(CGSize)size diff --git a/ReactAndroid/src/main/java/com/facebook/csslayout/CSSNodeJNI.java b/ReactAndroid/src/main/java/com/facebook/csslayout/CSSNodeJNI.java index 90c8e982f..8e75da945 100644 --- a/ReactAndroid/src/main/java/com/facebook/csslayout/CSSNodeJNI.java +++ b/ReactAndroid/src/main/java/com/facebook/csslayout/CSSNodeJNI.java @@ -293,140 +293,46 @@ public class CSSNodeJNI implements CSSNodeAPI { jni_CSSNodeStyleSetFlex(mNativePointer, flex); } - private native float jni_CSSNodeStyleGetMarginLeft(int nativePointer); - private native float jni_CSSNodeStyleGetMarginTop(int nativePointer); - private native float jni_CSSNodeStyleGetMarginRight(int nativePointer); - private native float jni_CSSNodeStyleGetMarginBottom(int nativePointer); - private native float jni_CSSNodeStyleGetMarginStart(int nativePointer); - private native float jni_CSSNodeStyleGetMarginEnd(int nativePointer); + private native float jni_CSSNodeStyleGetMargin(int nativePointer, int edge); @Override public Spacing getMargin() { assertNativeInstance(); Spacing margin = new Spacing(); - margin.set(Spacing.LEFT, jni_CSSNodeStyleGetMarginLeft(mNativePointer)); - margin.set(Spacing.TOP, jni_CSSNodeStyleGetMarginTop(mNativePointer)); - margin.set(Spacing.RIGHT, jni_CSSNodeStyleGetMarginRight(mNativePointer)); - margin.set(Spacing.BOTTOM, jni_CSSNodeStyleGetMarginBottom(mNativePointer)); - margin.set(Spacing.START, jni_CSSNodeStyleGetMarginStart(mNativePointer)); - margin.set(Spacing.END, jni_CSSNodeStyleGetMarginEnd(mNativePointer)); + margin.set(Spacing.LEFT, jni_CSSNodeStyleGetMargin(Spacing.LEFT, mNativePointer)); + margin.set(Spacing.TOP, jni_CSSNodeStyleGetMargin(Spacing.TOP, mNativePointer)); + margin.set(Spacing.RIGHT, jni_CSSNodeStyleGetMargin(Spacing.RIGHT, mNativePointer)); + margin.set(Spacing.BOTTOM, jni_CSSNodeStyleGetMargin(Spacing.BOTTOM, mNativePointer)); + margin.set(Spacing.START, jni_CSSNodeStyleGetMargin(Spacing.START, mNativePointer)); + margin.set(Spacing.END, jni_CSSNodeStyleGetMargin(Spacing.END, mNativePointer)); return margin; } - private native void jni_CSSNodeStyleSetMarginLeft(int nativePointer, float marginLeft); - private native void jni_CSSNodeStyleSetMarginTop(int nativePointer, float marginTop); - private native void jni_CSSNodeStyleSetMarginRight(int nativePointer, float marginRight); - private native void jni_CSSNodeStyleSetMarginBottom(int nativePointer, float marginBottom); - private native void jni_CSSNodeStyleSetMarginStart(int nativePointer, float marginStart); - private native void jni_CSSNodeStyleSetMarginEnd(int nativePointer, float marginEnd); + private native void jni_CSSNodeStyleSetMargin(int nativePointer, int edge, float margin); @Override public void setMargin(int spacingType, float margin) { assertNativeInstance(); - switch (spacingType) { - case Spacing.LEFT: - jni_CSSNodeStyleSetMarginLeft(mNativePointer, margin); - break; - case Spacing.TOP: - jni_CSSNodeStyleSetMarginTop(mNativePointer, margin); - break; - case Spacing.RIGHT: - jni_CSSNodeStyleSetMarginRight(mNativePointer, margin); - break; - case Spacing.BOTTOM: - jni_CSSNodeStyleSetMarginBottom(mNativePointer, margin); - break; - case Spacing.START: - jni_CSSNodeStyleSetMarginStart(mNativePointer, margin); - break; - case Spacing.END: - jni_CSSNodeStyleSetMarginEnd(mNativePointer, margin); - break; - case Spacing.HORIZONTAL: - jni_CSSNodeStyleSetMarginLeft(mNativePointer, margin); - jni_CSSNodeStyleSetMarginRight(mNativePointer, margin); - jni_CSSNodeStyleSetMarginStart(mNativePointer, margin); - jni_CSSNodeStyleSetMarginEnd(mNativePointer, margin); - break; - case Spacing.VERTICAL: - jni_CSSNodeStyleSetMarginTop(mNativePointer, margin); - jni_CSSNodeStyleSetMarginBottom(mNativePointer, margin); - break; - case Spacing.ALL: - jni_CSSNodeStyleSetMarginLeft(mNativePointer, margin); - jni_CSSNodeStyleSetMarginRight(mNativePointer, margin); - jni_CSSNodeStyleSetMarginStart(mNativePointer, margin); - jni_CSSNodeStyleSetMarginEnd(mNativePointer, margin); - jni_CSSNodeStyleSetMarginTop(mNativePointer, margin); - jni_CSSNodeStyleSetMarginBottom(mNativePointer, margin); - break; - } + jni_CSSNodeStyleSetMargin(mNativePointer, spacingType, margin); } - private native float jni_CSSNodeStyleGetPaddingLeft(int nativePointer); - private native float jni_CSSNodeStyleGetPaddingTop(int nativePointer); - private native float jni_CSSNodeStyleGetPaddingRight(int nativePointer); - private native float jni_CSSNodeStyleGetPaddingBottom(int nativePointer); - private native float jni_CSSNodeStyleGetPaddingStart(int nativePointer); - private native float jni_CSSNodeStyleGetPaddingEnd(int nativePointer); + private native float jni_CSSNodeStyleGetPadding(int nativePointer, int edge); @Override public Spacing getPadding() { assertNativeInstance(); Spacing padding = new Spacing(); - padding.set(Spacing.LEFT, jni_CSSNodeStyleGetPaddingLeft(mNativePointer)); - padding.set(Spacing.TOP, jni_CSSNodeStyleGetPaddingTop(mNativePointer)); - padding.set(Spacing.RIGHT, jni_CSSNodeStyleGetPaddingRight(mNativePointer)); - padding.set(Spacing.BOTTOM, jni_CSSNodeStyleGetPaddingBottom(mNativePointer)); - padding.set(Spacing.START, jni_CSSNodeStyleGetPaddingStart(mNativePointer)); - padding.set(Spacing.END, jni_CSSNodeStyleGetPaddingEnd(mNativePointer)); + padding.set(Spacing.LEFT, jni_CSSNodeStyleGetPadding(Spacing.LEFT, mNativePointer)); + padding.set(Spacing.TOP, jni_CSSNodeStyleGetPadding(Spacing.TOP, mNativePointer)); + padding.set(Spacing.RIGHT, jni_CSSNodeStyleGetPadding(Spacing.RIGHT, mNativePointer)); + padding.set(Spacing.BOTTOM, jni_CSSNodeStyleGetPadding(Spacing.BOTTOM, mNativePointer)); + padding.set(Spacing.START, jni_CSSNodeStyleGetPadding(Spacing.START, mNativePointer)); + padding.set(Spacing.END, jni_CSSNodeStyleGetPadding(Spacing.END, mNativePointer)); return padding; } - private native void jni_CSSNodeStyleSetPaddingLeft(int nativePointer, float paddingLeft); - private native void jni_CSSNodeStyleSetPaddingTop(int nativePointer, float paddingTop); - private native void jni_CSSNodeStyleSetPaddingRight(int nativePointer, float paddingRight); - private native void jni_CSSNodeStyleSetPaddingBottom(int nativePointer, float paddingBottom); - private native void jni_CSSNodeStyleSetPaddingStart(int nativePointer, float paddingStart); - private native void jni_CSSNodeStyleSetPaddingEnd(int nativePointer, float paddingEnd); + private native void jni_CSSNodeStyleSetPadding(int nativePointer, int edge, float padding); @Override public void setPadding(int spacingType, float padding) { assertNativeInstance(); - switch (spacingType) { - case Spacing.LEFT: - jni_CSSNodeStyleSetPaddingLeft(mNativePointer, padding); - break; - case Spacing.TOP: - jni_CSSNodeStyleSetPaddingTop(mNativePointer, padding); - break; - case Spacing.RIGHT: - jni_CSSNodeStyleSetPaddingRight(mNativePointer, padding); - break; - case Spacing.BOTTOM: - jni_CSSNodeStyleSetPaddingBottom(mNativePointer, padding); - break; - case Spacing.START: - jni_CSSNodeStyleSetPaddingStart(mNativePointer, padding); - break; - case Spacing.END: - jni_CSSNodeStyleSetPaddingEnd(mNativePointer, padding); - break; - case Spacing.HORIZONTAL: - jni_CSSNodeStyleSetPaddingLeft(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingRight(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingStart(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingEnd(mNativePointer, padding); - break; - case Spacing.VERTICAL: - jni_CSSNodeStyleSetPaddingTop(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingBottom(mNativePointer, padding); - break; - case Spacing.ALL: - jni_CSSNodeStyleSetPaddingLeft(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingRight(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingStart(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingEnd(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingTop(mNativePointer, padding); - jni_CSSNodeStyleSetPaddingBottom(mNativePointer, padding); - break; - } + jni_CSSNodeStyleSetPadding(mNativePointer, spacingType, padding); } @Override @@ -434,138 +340,46 @@ public class CSSNodeJNI implements CSSNodeAPI { // TODO } - private native float jni_CSSNodeStyleGetBorderLeft(int nativePointer); - private native float jni_CSSNodeStyleGetBorderTop(int nativePointer); - private native float jni_CSSNodeStyleGetBorderRight(int nativePointer); - private native float jni_CSSNodeStyleGetBorderBottom(int nativePointer); - private native float jni_CSSNodeStyleGetBorderStart(int nativePointer); - private native float jni_CSSNodeStyleGetBorderEnd(int nativePointer); + private native float jni_CSSNodeStyleGetBorder(int nativePointer, int edge); @Override public Spacing getBorder() { assertNativeInstance(); Spacing border = new Spacing(); - border.set(Spacing.LEFT, jni_CSSNodeStyleGetBorderLeft(mNativePointer)); - border.set(Spacing.TOP, jni_CSSNodeStyleGetBorderTop(mNativePointer)); - border.set(Spacing.RIGHT, jni_CSSNodeStyleGetBorderRight(mNativePointer)); - border.set(Spacing.BOTTOM, jni_CSSNodeStyleGetBorderBottom(mNativePointer)); - border.set(Spacing.START, jni_CSSNodeStyleGetBorderStart(mNativePointer)); - border.set(Spacing.END, jni_CSSNodeStyleGetBorderEnd(mNativePointer)); + border.set(Spacing.LEFT, jni_CSSNodeStyleGetBorder(Spacing.LEFT, mNativePointer)); + border.set(Spacing.TOP, jni_CSSNodeStyleGetBorder(Spacing.TOP, mNativePointer)); + border.set(Spacing.RIGHT, jni_CSSNodeStyleGetBorder(Spacing.RIGHT, mNativePointer)); + border.set(Spacing.BOTTOM, jni_CSSNodeStyleGetBorder(Spacing.BOTTOM, mNativePointer)); + border.set(Spacing.START, jni_CSSNodeStyleGetBorder(Spacing.START, mNativePointer)); + border.set(Spacing.END, jni_CSSNodeStyleGetBorder(Spacing.END, mNativePointer)); return border; } - private native void jni_CSSNodeStyleSetBorderLeft(int nativePointer, float borderLeft); - private native void jni_CSSNodeStyleSetBorderTop(int nativePointer, float borderTop); - private native void jni_CSSNodeStyleSetBorderRight(int nativePointer, float borderRight); - private native void jni_CSSNodeStyleSetBorderBottom(int nativePointer, float borderBottom); - private native void jni_CSSNodeStyleSetBorderStart(int nativePointer, float borderStart); - private native void jni_CSSNodeStyleSetBorderEnd(int nativePointer, float borderEnd); + private native void jni_CSSNodeStyleSetBorder(int nativePointer, int edge, float border); @Override public void setBorder(int spacingType, float border) { assertNativeInstance(); - switch (spacingType) { - case Spacing.LEFT: - jni_CSSNodeStyleSetBorderLeft(mNativePointer, border); - break; - case Spacing.TOP: - jni_CSSNodeStyleSetBorderTop(mNativePointer, border); - break; - case Spacing.RIGHT: - jni_CSSNodeStyleSetBorderRight(mNativePointer, border); - break; - case Spacing.BOTTOM: - jni_CSSNodeStyleSetBorderBottom(mNativePointer, border); - break; - case Spacing.START: - jni_CSSNodeStyleSetBorderStart(mNativePointer, border); - break; - case Spacing.END: - jni_CSSNodeStyleSetBorderEnd(mNativePointer, border); - break; - case Spacing.HORIZONTAL: - jni_CSSNodeStyleSetBorderLeft(mNativePointer, border); - jni_CSSNodeStyleSetBorderRight(mNativePointer, border); - jni_CSSNodeStyleSetBorderStart(mNativePointer, border); - jni_CSSNodeStyleSetBorderEnd(mNativePointer, border); - break; - case Spacing.VERTICAL: - jni_CSSNodeStyleSetBorderTop(mNativePointer, border); - jni_CSSNodeStyleSetBorderBottom(mNativePointer, border); - break; - case Spacing.ALL: - jni_CSSNodeStyleSetBorderLeft(mNativePointer, border); - jni_CSSNodeStyleSetBorderRight(mNativePointer, border); - jni_CSSNodeStyleSetBorderStart(mNativePointer, border); - jni_CSSNodeStyleSetBorderEnd(mNativePointer, border); - jni_CSSNodeStyleSetBorderTop(mNativePointer, border); - jni_CSSNodeStyleSetBorderBottom(mNativePointer, border); - break; - } + jni_CSSNodeStyleSetBorder(mNativePointer, spacingType, border); } - private native float jni_CSSNodeStyleGetPositionLeft(int nativePointer); - private native float jni_CSSNodeStyleGetPositionTop(int nativePointer); - private native float jni_CSSNodeStyleGetPositionRight(int nativePointer); - private native float jni_CSSNodeStyleGetPositionBottom(int nativePointer); - private native float jni_CSSNodeStyleGetPositionStart(int nativePointer); - private native float jni_CSSNodeStyleGetPositionEnd(int nativePointer); + private native float jni_CSSNodeStyleGetPosition(int nativePointer, int edge); @Override public Spacing getPosition() { + assertNativeInstance(); Spacing position = new Spacing(); - position.set(Spacing.LEFT, jni_CSSNodeStyleGetPositionLeft(mNativePointer)); - position.set(Spacing.TOP, jni_CSSNodeStyleGetPositionTop(mNativePointer)); - position.set(Spacing.RIGHT, jni_CSSNodeStyleGetPositionRight(mNativePointer)); - position.set(Spacing.BOTTOM, jni_CSSNodeStyleGetPositionBottom(mNativePointer)); - position.set(Spacing.START, jni_CSSNodeStyleGetPositionStart(mNativePointer)); - position.set(Spacing.END, jni_CSSNodeStyleGetPositionEnd(mNativePointer)); + position.set(Spacing.LEFT, jni_CSSNodeStyleGetPosition(Spacing.LEFT, mNativePointer)); + position.set(Spacing.TOP, jni_CSSNodeStyleGetPosition(Spacing.TOP, mNativePointer)); + position.set(Spacing.RIGHT, jni_CSSNodeStyleGetPosition(Spacing.RIGHT, mNativePointer)); + position.set(Spacing.BOTTOM, jni_CSSNodeStyleGetPosition(Spacing.BOTTOM, mNativePointer)); + position.set(Spacing.START, jni_CSSNodeStyleGetPosition(Spacing.START, mNativePointer)); + position.set(Spacing.END, jni_CSSNodeStyleGetPosition(Spacing.END, mNativePointer)); return position; } - private native void jni_CSSNodeStyleSetPositionLeft(int nativePointer, float position); - private native void jni_CSSNodeStyleSetPositionTop(int nativePointer, float position); - private native void jni_CSSNodeStyleSetPositionRight(int nativePointer, float position); - private native void jni_CSSNodeStyleSetPositionBottom(int nativePointer, float position); - private native void jni_CSSNodeStyleSetPositionStart(int nativePointer, float position); - private native void jni_CSSNodeStyleSetPositionEnd(int nativePointer, float position); + private native void jni_CSSNodeStyleSetPosition(int nativePointer, int edge, float position); @Override public void setPosition(int spacingType, float position) { - switch (spacingType) { - case Spacing.LEFT: - jni_CSSNodeStyleSetPositionLeft(mNativePointer, position); - break; - case Spacing.TOP: - jni_CSSNodeStyleSetPositionTop(mNativePointer, position); - break; - case Spacing.RIGHT: - jni_CSSNodeStyleSetPositionRight(mNativePointer, position); - break; - case Spacing.BOTTOM: - jni_CSSNodeStyleSetPositionBottom(mNativePointer, position); - break; - case Spacing.START: - jni_CSSNodeStyleSetPositionStart(mNativePointer, position); - break; - case Spacing.END: - jni_CSSNodeStyleSetPositionEnd(mNativePointer, position); - break; - case Spacing.HORIZONTAL: - jni_CSSNodeStyleSetPositionLeft(mNativePointer, position); - jni_CSSNodeStyleSetPositionRight(mNativePointer, position); - jni_CSSNodeStyleSetPositionStart(mNativePointer, position); - jni_CSSNodeStyleSetPositionEnd(mNativePointer, position); - break; - case Spacing.VERTICAL: - jni_CSSNodeStyleSetPositionTop(mNativePointer, position); - jni_CSSNodeStyleSetPositionBottom(mNativePointer, position); - break; - case Spacing.ALL: - jni_CSSNodeStyleSetPositionLeft(mNativePointer, position); - jni_CSSNodeStyleSetPositionRight(mNativePointer, position); - jni_CSSNodeStyleSetPositionStart(mNativePointer, position); - jni_CSSNodeStyleSetPositionEnd(mNativePointer, position); - jni_CSSNodeStyleSetPositionTop(mNativePointer, position); - jni_CSSNodeStyleSetPositionBottom(mNativePointer, position); - break; - } + assertNativeInstance(); + jni_CSSNodeStyleSetPosition(mNativePointer, spacingType, position); } private native float jni_CSSNodeStyleGetWidth(int nativePointer); diff --git a/ReactAndroid/src/main/java/com/facebook/csslayout/Spacing.java b/ReactAndroid/src/main/java/com/facebook/csslayout/Spacing.java index 25a555fee..af46cbd4a 100644 --- a/ReactAndroid/src/main/java/com/facebook/csslayout/Spacing.java +++ b/ReactAndroid/src/main/java/com/facebook/csslayout/Spacing.java @@ -37,22 +37,22 @@ public class Spacing { */ public static final int BOTTOM = 3; /** - * Spacing type that represents vertical direction (top and bottom). E.g. {@code marginVertical}. + * Spacing type that represents start direction e.g. left in left-to-right, right in right-to-left. */ - public static final int VERTICAL = 4; + public static final int START = 4; + /** + * Spacing type that represents end direction e.g. right in left-to-right, left in right-to-left. + */ + public static final int END = 5; /** * Spacing type that represents horizontal direction (left and right). E.g. * {@code marginHorizontal}. */ - public static final int HORIZONTAL = 5; + public static final int HORIZONTAL = 6; /** - * Spacing type that represents start direction e.g. left in left-to-right, right in right-to-left. + * Spacing type that represents vertical direction (top and bottom). E.g. {@code marginVertical}. */ - public static final int START = 6; - /** - * Spacing type that represents end direction e.g. right in left-to-right, left in right-to-left. - */ - public static final int END = 7; + public static final int VERTICAL = 7; /** * Spacing type that represents all directions (left, top, right, bottom). E.g. {@code margin}. */ @@ -63,10 +63,10 @@ public class Spacing { 2, /*TOP*/ 4, /*RIGHT*/ 8, /*BOTTOM*/ - 16, /*VERTICAL*/ - 32, /*HORIZONTAL*/ - 64, /*START*/ - 128, /*END*/ + 16, /*START*/ + 32, /*END*/ + 64, /*HORIZONTAL*/ + 128, /*VERTICAL*/ 256, /*ALL*/ }; @@ -211,11 +211,11 @@ public class Spacing { defaultValue, defaultValue, defaultValue, - defaultValue, - defaultValue, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED, defaultValue, + defaultValue, + defaultValue, }; } }