com.facebook.litho.annotations.OnMeasure Java Examples

The following examples show how to use com.facebook.litho.annotations.OnMeasure. 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: 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 #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: 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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: TextInputSpec.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,
    @Prop(optional = true, resType = ResType.STRING) CharSequence hint,
    @Prop(optional = true, resType = ResType.DRAWABLE) Drawable inputBackground,
    @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowRadius,
    @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDx,
    @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDy,
    @Prop(optional = true, resType = ResType.COLOR) int shadowColor,
    @Prop(optional = true) ColorStateList textColorStateList,
    @Prop(optional = true) ColorStateList hintColorStateList,
    @Prop(optional = true, resType = ResType.COLOR) Integer highlightColor,
    @Prop(optional = true, resType = ResType.DIMEN_TEXT) int textSize,
    @Prop(optional = true) Typeface typeface,
    @Prop(optional = true) int textAlignment,
    @Prop(optional = true) int gravity,
    @Prop(optional = true) boolean editable,
    @Prop(optional = true) int inputType,
    @Prop(optional = true) int imeOptions,
    @Prop(optional = true, varArg = "inputFilter") List<InputFilter> inputFilters,
    @Prop(optional = true) boolean multiline,
    @Prop(optional = true) TextUtils.TruncateAt ellipsize,
    @Prop(optional = true) int minLines,
    @Prop(optional = true) int maxLines,
    @Prop(optional = true) int cursorDrawableRes,
    @Prop(optional = true, resType = ResType.STRING) CharSequence error,
    @Prop(optional = true, resType = ResType.DRAWABLE) Drawable errorDrawable,
    @State AtomicReference<CharSequence> savedText,
    @State int measureSeqNumber) {

  // The height should be the measured height of EditText with relevant params
  final EditText forMeasure = new ForMeasureEditText(c.getAndroidContext());
  // If text contains Spans, we don't want it to be mutable for the measurement case
  CharSequence text = savedText.get();
  if (text instanceof Spannable) {
    text = text.toString();
  }
  setParams(
      forMeasure,
      hint,
      getBackgroundOrDefault(
          c, inputBackground == UNSET_DRAWABLE ? forMeasure.getBackground() : inputBackground),
      shadowRadius,
      shadowDx,
      shadowDy,
      shadowColor,
      textColorStateList,
      hintColorStateList,
      highlightColor,
      textSize,
      typeface,
      textAlignment,
      gravity,
      editable,
      inputType,
      imeOptions,
      inputFilters,
      multiline,
      ellipsize,
      minLines,
      maxLines,
      cursorDrawableRes,
      forMeasure.getMovementMethod(),
      // onMeasure happens:
      // 1. After initState before onMount: savedText = initText.
      // 2. After onMount before onUnmount: savedText preserved from underlying editText.
      text,
      error,
      errorDrawable);
  forMeasure.measure(
      MeasureUtils.getViewMeasureSpec(widthSpec), MeasureUtils.getViewMeasureSpec(heightSpec));

  size.height = forMeasure.getMeasuredHeight();

  // For width we always take all available space, or collapse to 0 if unspecified.
  if (SizeSpec.getMode(widthSpec) == SizeSpec.UNSPECIFIED) {
    size.width = 0;
  } else {
    size.width = Math.min(SizeSpec.getSize(widthSpec), forMeasure.getMeasuredWidth());
  }
}
 
Example #14
Source File: MountSpecWorkingRangeTesterSpec.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 #15
Source File: ShouldUseGlobalPoolTrueMountSpecLifecycleTesterSpec.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 #16
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 #17
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 #18
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 #19
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);
}