Skip to content

Commit

Permalink
Move LayoutResult out of Node class
Browse files Browse the repository at this point in the history
Summary: Moving LayoutResult out of the Node class.

Reviewed By: adityasharat

Differential Revision: D42929851

fbshipit-source-id: 3bda9016975c645e4f28f3550e496e3101c98651
  • Loading branch information
zielinskimz authored and facebook-github-bot committed Mar 23, 2023
1 parent 5a58dfa commit 43cff46
Show file tree
Hide file tree
Showing 53 changed files with 282 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.litho.LithoRenderUnit.UpdateState;
import com.facebook.rendercore.LayoutContext;
import com.facebook.rendercore.Node;
import com.facebook.rendercore.RenderTreeNode;

/**
* This object will host the data associated with the component which is generated during the
* measure pass, for example: the {@link InterStagePropsContainer}, and the {@link UpdateState}. It
* will be created in {@link Node.LayoutResult#calculateLayout(LayoutContext, int, int)}. This
* object will be returned by {@link Node.LayoutResult#getLayoutData()}, then written to the layout
* data in {@link RenderTreeNode} during reduce.
* will be created in {@link LayoutResult#calculateLayout(LayoutContext, int, int)}. This object
* will be returned by {@link LayoutResult#getLayoutData()}, then written to the layout data in
* {@link RenderTreeNode} during reduce.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class LithoLayoutData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import com.facebook.rendercore.LayoutContext;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.MeasureResult;
import com.facebook.rendercore.Mountable;
import com.facebook.rendercore.Node.LayoutResult;
import com.facebook.rendercore.RenderUnit;
import com.facebook.rendercore.primitives.Primitive;
import com.facebook.rendercore.utils.MeasureSpecUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import android.graphics.Rect;
import androidx.annotation.Nullable;
import com.facebook.rendercore.Node;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.RenderTreeNode;
import com.facebook.rendercore.RenderUnit;
import com.facebook.rendercore.extensions.LayoutResultVisitor;
Expand Down Expand Up @@ -187,7 +187,7 @@ public static class Visitor implements LayoutResultVisitor<Results> {
@Override
public void visit(
final @Nullable RenderTreeNode parent,
final Node.LayoutResult result,
final LayoutResult result,
final Rect bounds,
final int x,
final int y,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import android.widget.TextView;
import androidx.test.core.app.ApplicationProvider;
import com.facebook.rendercore.HostView;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.MountState;
import com.facebook.rendercore.Node.LayoutResult;
import com.facebook.rendercore.RenderState;
import com.facebook.rendercore.RenderTree;
import com.facebook.rendercore.RenderTreeNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.facebook.rendercore.primitives

import com.facebook.rendercore.LayoutResult
import com.facebook.rendercore.MeasureResult
import com.facebook.rendercore.Node
import com.facebook.rendercore.RenderUnit
import com.facebook.rendercore.primitives.utils.hasEquivalentFields

Expand Down Expand Up @@ -156,12 +156,12 @@ class PrimitiveLayoutResult(
widthSpec: Int,
heightSpec: Int,
renderUnit: RenderUnit<*>?
): Node.LayoutResult {
return object : Node.LayoutResult {
): LayoutResult {
return object : LayoutResult {
override fun getRenderUnit(): RenderUnit<*>? = renderUnit
override fun getLayoutData(): Any? = this@PrimitiveLayoutResult.layoutData
override fun getChildrenCount(): Int = 0
override fun getChildAt(index: Int): Node.LayoutResult {
override fun getChildAt(index: Int): LayoutResult {
throw UnsupportedOperationException("A PrimitiveLayoutResult has no children")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.facebook.rendercore.primitives
import android.graphics.drawable.Drawable
import android.view.View
import com.facebook.rendercore.LayoutContext
import com.facebook.rendercore.LayoutResult
import com.facebook.rendercore.Node

/**
Expand Down Expand Up @@ -47,7 +48,7 @@ class Primitive<ContentType : Any>(
context: LayoutContext<Any?>,
widthSpec: Int,
heightSpec: Int
): Node.LayoutResult {
): LayoutResult {
val layoutScope = LayoutScope(context)
return with(layoutBehavior) {
layoutScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.util.Pair;
import android.view.View;
import androidx.annotation.Nullable;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.Node;
import com.facebook.rendercore.RenderResult;
import com.facebook.rendercore.RenderState;
Expand Down Expand Up @@ -59,7 +60,7 @@ public interface NodeBuilderPart {
/** Supply the root Node of the tree. */
LayoutBuilderPart withNode(Node rootNode);

LayoutBuilderPart withLayoutResult(Node.LayoutResult result);
LayoutBuilderPart withLayoutResult(LayoutResult result);
}

public interface LayoutBuilderPart {
Expand Down Expand Up @@ -104,7 +105,7 @@ public LayoutBuilderPart withNode(Node rootNode) {
}

@Override
public LayoutBuilderPart withLayoutResult(Node.LayoutResult result) {
public LayoutBuilderPart withLayoutResult(LayoutResult result) {
mRootNode = new SimpleWrapperNode(result);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import android.view.View;
import androidx.annotation.Nullable;
import com.facebook.rendercore.Node.LayoutResult;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.RenderUnit;
import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.facebook.rendercore.testing;

import com.facebook.rendercore.LayoutContext;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.Node;

public class SimpleWrapperNode implements Node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import android.graphics.Rect;
import androidx.annotation.Nullable;
import com.facebook.rendercore.Node.LayoutResult;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.RenderTreeNode;
import com.facebook.rendercore.extensions.LayoutResultVisitor;
import com.facebook.rendercore.testing.TestLayoutResultVisitor.Result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.facebook.rendercore.testing;

import com.facebook.rendercore.LayoutContext;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.Node;
import com.facebook.rendercore.RenderUnit;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import android.graphics.Rect;
import androidx.annotation.Nullable;
import androidx.collection.ArraySet;
import com.facebook.rendercore.Node;
import com.facebook.rendercore.LayoutResult;
import com.facebook.rendercore.RenderTreeNode;
import com.facebook.rendercore.RenderUnit;
import com.facebook.rendercore.extensions.LayoutResultVisitor;
Expand Down Expand Up @@ -104,7 +104,7 @@ public Visitor(VisibilityOutput.Factory factory) {
@Override
public void visit(
final @Nullable RenderTreeNode parent,
final Node.LayoutResult layoutResult,
final LayoutResult layoutResult,
final Rect bounds,
final int x,
final int y,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import android.graphics.Rect;
import androidx.annotation.Nullable;
import com.facebook.rendercore.Function;
import com.facebook.rendercore.Node.LayoutResult;
import com.facebook.rendercore.LayoutResult;

/**
* Stores information about a node which has registered visibility handlers for. The information is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.facebook.rendercore;

import static com.facebook.rendercore.Node.LayoutResult;
import static com.facebook.yoga.YogaEdge.BOTTOM;
import static com.facebook.yoga.YogaEdge.LEFT;
import static com.facebook.yoga.YogaEdge.RIGHT;
Expand Down Expand Up @@ -355,7 +354,7 @@ public int getChildrenCount() {
}

@Override
public Node.LayoutResult getChildAt(int index) {
public LayoutResult getChildAt(int index) {
if (mDelegate != null) {
return mDelegate.getChildAt(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public final class LayoutCache {
static final class CachedData {
private final Map<Node<?>, Node.LayoutResult> mCacheByNode = new HashMap<>();
private final Map<Node<?>, LayoutResult> mCacheByNode = new HashMap<>();
private final LongSparseArray<Object> mCacheById = new LongSparseArray<>();
}

Expand All @@ -46,11 +46,11 @@ public LayoutCache() {
}

@Nullable
public Node.LayoutResult get(Node<?> node) {
public LayoutResult get(Node<?> node) {
return mReadCache == null ? null : mReadCache.mCacheByNode.get(node);
}

public void put(Node<?> node, Node.LayoutResult layout) {
public void put(Node<?> node, LayoutResult layout) {
mWriteCache.mCacheByNode.put(node, layout);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.rendercore;

import androidx.annotation.Nullable;
import androidx.annotation.Px;

/**
* Represents the result of a Layout pass. A LayoutResult has a reference to its originating Node
* and all the layout information needed to position the content of such Node.
*/
public interface LayoutResult {

/** @return the RenderUnit that should be rendered by this layout result. */
@Nullable
RenderUnit<?> getRenderUnit();

/**
* @return layout specific data that was generated during the layout pass that created this
* LayoutResult.
*/
@Nullable
Object getLayoutData();

/** @return the number of children of this LayoutResult. */
int getChildrenCount();

/** @return the LayoutResult for the given child index */
LayoutResult getChildAt(int index);

/** @return the resolved X position for the Node */
@Px
int getXForChildAtIndex(int index);

/** @return the resolved Y position for the Node */
@Px
int getYForChildAtIndex(int index);

/** @return the resolved width for the Node */
@Px
int getWidth();

/** @return the resolved height for the Node */
@Px
int getHeight();

/** @return the resolved top padding for the Node */
@Px
int getPaddingTop();

/** @return the resolved right padding for the Node */
@Px
int getPaddingRight();

/** @return the resolved bottom padding for the Node */
@Px
int getPaddingBottom();

/** @return the resolved left padding for the Node */
@Px
int getPaddingLeft();

/** @return the width measurement that generated this LayoutResult */
int getWidthSpec();

/** @return the height measurement that generated this LayoutResult */
int getHeightSpec();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import androidx.annotation.Nullable;

public class MountableLayoutResult implements Node.LayoutResult {
public class MountableLayoutResult implements LayoutResult {

private final @Nullable RenderUnit<?> mRenderUnit;
private final int mMeasuredWidth;
Expand Down Expand Up @@ -69,7 +69,7 @@ public final int getChildrenCount() {
}

@Override
public final Node.LayoutResult getChildAt(int index) {
public final LayoutResult getChildAt(int index) {
throw new IllegalArgumentException("A MountableLayoutResult has no children");
}

Expand Down
Loading

0 comments on commit 43cff46

Please sign in to comment.