com.facebook.litho.ComponentLayout Java Examples

The following examples show how to use com.facebook.litho.ComponentLayout. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop LifecycleTracker lifecycleTracker,
    @Prop(optional = true) Size intrinsicSize) {

  int width = SizeSpec.getSize(widthSpec);
  int height = SizeSpec.getSize(heightSpec);

  if (intrinsicSize != null) {
    size.width = SizeSpec.resolveSize(widthSpec, intrinsicSize.width);
    size.height = SizeSpec.resolveSize(heightSpec, intrinsicSize.height);
  } else {
    size.width = width;
    size.height = height;
  }

  lifecycleTracker.addStep(LifecycleStep.ON_MEASURE, size);
}
 
Example #2
Source File: SizeSpecMountWrapperComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnBoundsDefined
static void onBoundsDefined(
    ComponentContext c,
    ComponentLayout layout,
    @Prop Component component,
    @State AtomicReference<ComponentTree> componentTreeRef) {
  // the updated width and height is passed down.
  int widthSpec = SizeSpec.makeSizeSpec(layout.getWidth(), SizeSpec.EXACTLY);
  int heightSpec = SizeSpec.makeSizeSpec(layout.getHeight(), SizeSpec.EXACTLY);
  final ComponentTree componentTree = getOrCreateComponentTree(c, componentTreeRef);
  // This check is also done in the setRootAndSizeSpec method, but we need to do this here since
  // it will fail if a ErrorBoundariesConfiguration.rootWrapperComponentFactory was set.
  // TODO: T60426216
  if (!componentTree.hasCompatibleLayout(widthSpec, heightSpec)) {
    componentTree.setVersionedRootAndSizeSpec(
        component,
        widthSpec,
        heightSpec,
        null,
        getTreePropWithSize(c, widthSpec, heightSpec),
        c.getLayoutVersion());
  }
}
 
Example #3
Source File: SizeSpecMountWrapperComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop Component component,
    @State AtomicReference<ComponentTree> componentTreeRef) {
  final ComponentTree componentTree = getOrCreateComponentTree(c, componentTreeRef);
  componentTree.setVersionedRootAndSizeSpec(
      component,
      widthSpec,
      heightSpec,
      size,
      getTreePropWithSize(c, widthSpec, heightSpec),
      c.getLayoutVersion());
  if (size.width < 0 || size.height < 0) {
    // if this happens it means that the componentTree was probably released in the UI Thread so
    // this measurement is not needed.
    size.width = size.height = 0;
  }
}
 
Example #4
Source File: VerticalScrollSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop Component childComponent,
    @Prop(optional = true) boolean fillViewport,
    @State ComponentTree childComponentTree,
    Output<Integer> measuredWidth,
    Output<Integer> measuredHeight) {
  measureVerticalScroll(
      c, widthSpec, heightSpec, size, childComponentTree, childComponent, fillViewport);
  measuredWidth.set(size.width);
  measuredHeight.set(size.height);
}
 
Example #5
Source File: ClockComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  final ClockView clockView = new ClockView(c.getAndroidContext());
  clockView.measure(
      MeasureUtils.getViewMeasureSpec(widthSpec), MeasureUtils.getViewMeasureSpec(heightSpec));
  size.width = clockView.getMeasuredWidth();
  size.height = clockView.getMeasuredHeight();
}
 
Example #6
Source File: FrescoVitoImage2Spec.java    From fresco with MIT License 5 votes vote down vote up
@OnBoundsDefined
static void onBoundsDefined(
    ComponentContext c, ComponentLayout layout, Output<Rect> viewportDimensions) {
  final int width = layout.getWidth();
  final int height = layout.getHeight();
  int paddingX = 0, paddingY = 0;
  if (layout.isPaddingSet()) {
    paddingX = layout.getPaddingLeft() + layout.getPaddingRight();
    paddingY = layout.getPaddingTop() + layout.getPaddingBottom();
  }

  viewportDimensions.set(new Rect(0, 0, width - paddingX, height - paddingY));
}
 
Example #7
Source File: FrescoVitoImage2Spec.java    From fresco with MIT License 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop(optional = true, resType = ResType.FLOAT) float imageAspectRatio) {
  MeasureUtils.measureWithAspectRatio(widthSpec, heightSpec, imageAspectRatio, size);
}
 
Example #8
Source File: TestMount.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
protected void onBoundsDefined(ComponentContext c, ComponentLayout layout) {
  Output<Integer> boundsDefinedOutputTmp = new Output<>();
  TestMountSpec.onBoundsDefined(
      (ComponentContext) c,
      (ComponentLayout) layout,
      (Object) prop3,
      (char[]) prop4,
      (Long) measureOutput,
      (Output<Integer>) boundsDefinedOutputTmp);
  boundsDefinedOutput = boundsDefinedOutputTmp.get();
}
 
Example #9
Source File: TestMount.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
protected void onMeasure(
    ComponentContext context, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  Output<Long> measureOutputTmp = new Output<>();
  TestMountSpec.onMeasure(
      (ComponentContext) context,
      (ComponentLayout) layout,
      (int) widthSpec,
      (int) heightSpec,
      (Size) size,
      (Output<Long>) measureOutputTmp);
  measureOutput = measureOutputTmp.get();
}
 
Example #10
Source File: TestMountSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnBoundsDefined
static void onBoundsDefined(
    ComponentContext c,
    ComponentLayout layout,
    @Prop Object prop3,
    @Prop char[] prop4,
    @FromMeasure Long measureOutput,
    Output<Integer> boundsDefinedOutput) {}
 
Example #11
Source File: TestMountSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext context,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    Output<Long> measureOutput) {}
 
Example #12
Source File: MountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnBoundsDefined
static void onBoundsDefined(
    ComponentContext c, ComponentLayout layout, @Prop LifecycleTracker lifecycleTracker) {
  final Rect bounds =
      new Rect(
          layout.getX(),
          layout.getY(),
          layout.getX() + layout.getWidth(),
          layout.getY() + layout.getHeight());
  lifecycleTracker.addStep(LifecycleStep.ON_BOUNDS_DEFINED, bounds);
}
 
Example #13
Source File: PreallocatedMountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop List<LifecycleStep.StepInfo> steps) {

  steps.add(new StepInfo(LifecycleStep.ON_MEASURE));
  size.width = 600;
  size.height = 800;
}
 
Example #14
Source File: FrescoImageSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
protected static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop(optional = true, resType = ResType.FLOAT) float imageAspectRatio) {
  MeasureUtils.measureWithAspectRatio(widthSpec, heightSpec, imageAspectRatio, size);
}
 
Example #15
Source File: TestViewComponent.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
protected void onMeasure(
    ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  int width = SizeSpec.getSize(widthSpec);
  int height = SizeSpec.getSize(heightSpec);

  size.height = height;
  size.width = width;

  onMeasureCalled();
}
 
Example #16
Source File: ProgressSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  if (SizeSpec.getMode(widthSpec) == SizeSpec.UNSPECIFIED
      && SizeSpec.getMode(heightSpec) == SizeSpec.UNSPECIFIED) {
    size.width = DEFAULT_SIZE;
    size.height = DEFAULT_SIZE;
  } else {
    MeasureUtils.measureWithEqualDimens(widthSpec, heightSpec, size);
  }
}
 
Example #17
Source File: RecyclerSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size measureOutput,
    @Prop Binder<RecyclerView> binder) {

  binder.measure(
      measureOutput,
      widthSpec,
      heightSpec,
      (binder.canMeasure() || binder.isWrapContent()) ? Recycler.onRemeasure(c) : null);
}
 
Example #18
Source File: TestDrawableComponent.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
protected void onMeasure(
    ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  int width = SizeSpec.getSize(widthSpec);
  int height = SizeSpec.getSize(heightSpec);
  onMeasureCalled();

  size.width = measuredWidth != -1 ? SizeSpec.resolveSize(widthSpec, measuredWidth) : width;
  size.height = measuredHeight != -1 ? SizeSpec.resolveSize(heightSpec, measuredHeight) : height;
}
 
Example #19
Source File: ImageSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnBoundsDefined
static void onBoundsDefined(
    ComponentContext c,
    ComponentLayout layout,
    @Prop(resType = ResType.DRAWABLE) Drawable drawable,
    @Prop(optional = true) ScaleType scaleType,
    Output<DrawableMatrix> drawableMatrix,
    Output<Integer> drawableWidth,
    Output<Integer> drawableHeight) {

  final int horizontalPadding = layout.getPaddingLeft() + layout.getPaddingRight();
  final int verticalPadding = layout.getPaddingTop() + layout.getPaddingBottom();

  if (ScaleType.FIT_XY == scaleType
      || drawable == null
      || drawable.getIntrinsicWidth() <= 0
      || drawable.getIntrinsicHeight() <= 0) {
    drawableMatrix.set(null);
    drawableWidth.set(layout.getWidth() - horizontalPadding);
    drawableHeight.set(layout.getHeight() - verticalPadding);
  } else {
    final DrawableMatrix matrix =
        DrawableMatrix.create(
            drawable,
            scaleType,
            layout.getWidth() - horizontalPadding,
            layout.getHeight() - verticalPadding);

    drawableMatrix.set(matrix);
    drawableWidth.set(drawable.getIntrinsicWidth());
    drawableHeight.set(drawable.getIntrinsicHeight());
  }
}
 
Example #20
Source File: ImageSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop(resType = ResType.DRAWABLE) Drawable drawable) {
  if (drawable == null
      || drawable.getIntrinsicWidth() <= 0
      || drawable.getIntrinsicHeight() <= 0) {
    size.width = 0;
    size.height = 0;
    return;
  }

  final int intrinsicHeight = drawable.getIntrinsicHeight();
  final int intrinsicWidth = drawable.getIntrinsicWidth();

  if (SizeSpec.getMode(widthSpec) == UNSPECIFIED && SizeSpec.getMode(heightSpec) == UNSPECIFIED) {
    size.width = intrinsicWidth;
    size.height = intrinsicHeight;
    return;
  }

  final float aspectRatio = intrinsicWidth / (float) intrinsicHeight;
  MeasureUtils.measureWithAspectRatio(
      widthSpec, heightSpec, intrinsicWidth, intrinsicHeight, aspectRatio, size);
}
 
Example #21
Source File: VerticalScrollSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnBoundsDefined
static void onBoundsDefined(
    ComponentContext c,
    ComponentLayout layout,
    @Prop Component childComponent,
    @Prop(optional = true) boolean fillViewport,
    @State ComponentTree childComponentTree,
    @FromMeasure Integer measuredWidth,
    @FromMeasure Integer measuredHeight) {

  final int layoutWidth = layout.getWidth() - layout.getPaddingLeft() - layout.getPaddingRight();
  final int layoutHeight =
      layout.getHeight() - layout.getPaddingTop() - layout.getPaddingBottom();

  if (measuredWidth != null
      && measuredWidth == layoutWidth
      && (!fillViewport || (measuredHeight != null && measuredHeight == layoutHeight))) {
    // If we're not filling the viewport, then we always measure the height with unspecified, so
    // we just need to check that the width matches.
    return;
  }

  measureVerticalScroll(
      c,
      SizeSpec.makeSizeSpec(layout.getWidth(), EXACTLY),
      SizeSpec.makeSizeSpec(layout.getHeight(), EXACTLY),
      new Size(),
      childComponentTree,
      childComponent,
      fillViewport);
}
 
Example #22
Source File: HorizontalScrollSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnBoundsDefined
static void onBoundsDefined(
    ComponentContext context,
    ComponentLayout layout,
    @Prop Component contentProps,
    @Prop(optional = true) boolean fillViewport,
    @State ComponentTree childComponentTree,
    @FromMeasure Integer measuredComponentWidth,
    @FromMeasure Integer measuredComponentHeight,
    Output<Integer> componentWidth,
    Output<Integer> componentHeight,
    Output<YogaDirection> layoutDirection) {

  final int layoutWidth = layout.getWidth() - layout.getPaddingLeft() - layout.getPaddingRight();

  // If onMeasure() has been called, this means the content component already
  // has a defined size, no need to calculate it again.
  if (measuredComponentWidth != null && measuredComponentHeight != null) {
    componentWidth.set(Math.max(measuredComponentWidth, fillViewport ? layoutWidth : 0));
    componentHeight.set(measuredComponentHeight);
  } else {
    final int measuredWidth;
    final int measuredHeight;

    Size contentSize = new Size();
    childComponentTree.setRootAndSizeSpec(
        contentProps,
        SizeSpec.makeSizeSpec(0, UNSPECIFIED),
        SizeSpec.makeSizeSpec(layout.getHeight(), EXACTLY),
        contentSize);

    measuredWidth = Math.max(contentSize.width, fillViewport ? layoutWidth : 0);
    measuredHeight = contentSize.height;

    componentWidth.set(measuredWidth);
    componentHeight.set(measuredHeight);
  }

  layoutDirection.set(layout.getResolvedLayoutDirection());
}
 
Example #23
Source File: HorizontalScrollSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext context,
    ComponentLayout layout,
    int widthSpec,
    int heightSpec,
    Size size,
    @Prop Component contentProps,
    @State ComponentTree childComponentTree,
    Output<Integer> measuredComponentWidth,
    Output<Integer> measuredComponentHeight) {

  final int measuredWidth;
  final int measuredHeight;

  final Size contentSize = new Size();

  // Measure the component with undefined width spec, as the contents of the
  // hscroll have unlimited horizontal space.
  childComponentTree.setRootAndSizeSpec(
      contentProps, SizeSpec.makeSizeSpec(0, UNSPECIFIED), heightSpec, contentSize);
  contentProps.measure(context, SizeSpec.makeSizeSpec(0, UNSPECIFIED), heightSpec, contentSize);

  measuredWidth = contentSize.width;
  measuredHeight = contentSize.height;

  measuredComponentWidth.set(measuredWidth);
  measuredComponentHeight.set(measuredHeight);

  // If size constraints were not explicitly defined, just fallback to the
  // component dimensions instead.
  size.width =
      SizeSpec.getMode(widthSpec) == UNSPECIFIED ? measuredWidth : SizeSpec.getSize(widthSpec);
  size.height = measuredHeight;
}
 
Example #24
Source File: ShouldUseGlobalPoolFalseMountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  size.width = 600;
  size.height = 800;
}
 
Example #25
Source File: TestViewComponent.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
protected void onBoundsDefined(ComponentContext c, ComponentLayout layout) {
  onDefineBoundsCalled();
}
 
Example #26
Source File: PicassoImageSpec.java    From litho-picasso with MIT License 4 votes vote down vote up
@OnMeasure
static void onMeasureLayout(ComponentContext c, ComponentLayout layout, int widthSpec,
    int heightSpec, Size size) {
  MeasureUtils.measureWithEqualDimens(widthSpec, heightSpec, size);
}
 
Example #27
Source File: GlideImageSpec.java    From litho-glide with MIT License 4 votes vote down vote up
@OnMeasure
static void onMeasureLayout(ComponentContext c, ComponentLayout layout, int widthSpec,
    int heightSpec, Size size,
    @Prop(optional = true, resType = ResType.FLOAT) float imageAspectRatio) {
  MeasureUtils.measureWithAspectRatio(widthSpec, heightSpec, imageAspectRatio, size);
}
 
Example #28
Source File: TestDrawableComponent.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
protected void onBoundsDefined(ComponentContext c, ComponentLayout layout) {
  onDefineBoundsCalled();
}
 
Example #29
Source File: MountSpecTriggerTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnMeasure
static void onMeasure(
    ComponentContext context, ComponentLayout layout, int widthSpec, int heightSpec, Size size) {
  size.width = 600;
  size.height = 800;
}
 
Example #30
Source File: PreallocatedMountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnBoundsDefined
static void onBoundsDefined(
    ComponentContext c, ComponentLayout layout, @Prop List<LifecycleStep.StepInfo> steps) {
  steps.add(new StepInfo(LifecycleStep.ON_BOUNDS_DEFINED));
}