Skip to content

Commit

Permalink
Java: Expose extra functionality on CSSNode
Browse files Browse the repository at this point in the history
This change is needed to support inline views on React Native
for Android.
  • Loading branch information
Adam Comella committed Jul 6, 2016
1 parent 6e05325 commit ef3397b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
11 changes: 8 additions & 3 deletions src/java/src/com/facebook/csslayout/CSSNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* A CSS Node. It has a style object you can manipulate at {@link #style}. After calling
* {@link #calculateLayout()}, {@link #layout} will be filled with the results of the layout.
* {@link #calculateLayout(CSSLayoutContext, float, float)}, {@link #layout} will be filled with the results of the layout.
*/
public class CSSNode {

Expand Down Expand Up @@ -71,6 +71,11 @@ public static interface MeasureFunction {
private LayoutState mLayoutState = LayoutState.DIRTY;
private boolean mIsTextNode = false;

public static final Iterable<CSSNode> NO_CSS_NODES = new ArrayList<CSSNode>(0);
public Iterable<CSSNode> getChildrenIterable() {
return mChildren == null ? NO_CSS_NODES : mChildren;
}

public int getChildCount() {
return mChildren == null ? 0 : mChildren.size();
}
Expand Down Expand Up @@ -146,8 +151,8 @@ public boolean isTextNode() {
/**
* Performs the actual layout and saves the results in {@link #layout}
*/
public void calculateLayout(CSSLayoutContext layoutContext) {
LayoutEngine.layoutNode(layoutContext, this, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED, null);
public void calculateLayout(CSSLayoutContext layoutContext, float availableWidth, float availableHeight) {
LayoutEngine.layoutNode(layoutContext, this, availableWidth, availableHeight, null);
}

/**
Expand Down
32 changes: 16 additions & 16 deletions src/java/tests/com/facebook/csslayout/LayoutCachingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public void testCachesFullTree() {
root.addChildAt(c1, 1);
c0.addChildAt(c0c0, 0);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
assertTreeHasNewLayout(true, root);
markLayoutAppliedForTree(root);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
assertTrue(root.hasNewLayout());
assertTreeHasNewLayout(false, c0);
assertTreeHasNewLayout(false, c1);
Expand All @@ -70,12 +70,12 @@ public void testInvalidatesCacheWhenChildAdded() {
c0.addChildAt(c0c0, 0);
c0c0.addChildAt(c1c0, 0);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
markLayoutAppliedForTree(root);

c0.addChildAt(c0c1, 1);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
assertTrue(root.hasNewLayout());
assertTrue(c0.hasNewLayout());
assertTrue(c0c1.hasNewLayout());
Expand All @@ -97,11 +97,11 @@ public void testInvalidatesCacheWhenEnumPropertyChanges() {
root.addChildAt(c1, 1);
c0.addChildAt(c0c0, 0);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
markLayoutAppliedForTree(root);

c1.setAlignSelf(CSSAlign.CENTER);
root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);

assertTrue(root.hasNewLayout());
assertTrue(c1.hasNewLayout());
Expand All @@ -121,11 +121,11 @@ public void testInvalidatesCacheWhenFloatPropertyChanges() {
root.addChildAt(c1, 1);
c0.addChildAt(c0c0, 0);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
markLayoutAppliedForTree(root);

c1.setMargin(Spacing.LEFT, 10);
root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);

assertTrue(root.hasNewLayout());
assertTrue(c1.hasNewLayout());
Expand All @@ -147,11 +147,11 @@ public void testInvalidatesFullTreeWhenParentWidthChanges() {
c0.addChildAt(c0c0, 0);
c1.addChildAt(c1c0, 0);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
markLayoutAppliedForTree(root);

c0.setStyleWidth(200);
root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);

assertTrue(root.hasNewLayout());
assertTrue(c0.hasNewLayout());
Expand All @@ -173,11 +173,11 @@ public void testDoesNotInvalidateCacheWhenPropertyIsTheSame() {
c0.addChildAt(c0c0, 0);
root.setStyleWidth(200);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
markLayoutAppliedForTree(root);

root.setStyleWidth(200);
root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);

assertTrue(root.hasNewLayout());
assertTreeHasNewLayout(false, c0);
Expand All @@ -195,11 +195,11 @@ public void testInvalidateCacheWhenHeightChangesPosition() {
root.addChildAt(c1, 1);
c1.addChildAt(c1c0, 0);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
markLayoutAppliedForTree(root);

c0.setStyleHeight(100);
root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);

assertTrue(root.hasNewLayout());
assertTrue(c0.hasNewLayout());
Expand All @@ -218,7 +218,7 @@ public void testInvalidatesOnNewMeasureFunction() {
root.addChildAt(c1, 1);
c0.addChildAt(c0c0, 0);

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
markLayoutAppliedForTree(root);

c1.setMeasureFunction(new CSSNode.MeasureFunction() {
Expand All @@ -229,7 +229,7 @@ public void measure(CSSNode node, float width, CSSMeasureMode widthMode, float h
}
});

root.calculateLayout(layoutContext);
root.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);

assertTrue(root.hasNewLayout());
assertTrue(c1.hasNewLayout());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public TestCSSNode getChildAt(int i) {

private static void test(String message, CSSNode style, CSSNode expectedLayout) {
CSSLayoutContext layoutContext = new CSSLayoutContext();
style.calculateLayout(layoutContext);
style.calculateLayout(layoutContext, CSSConstants.UNDEFINED, CSSConstants.UNDEFINED);
assertLayoutsEqual(message, style, expectedLayout);
}

Expand Down

0 comments on commit ef3397b

Please sign in to comment.