Java Code Examples for com.facebook.litho.annotations.ResType#DRAWABLE

The following examples show how to use com.facebook.litho.annotations.ResType#DRAWABLE . 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: SpinnerSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateLayout
static Component onCreateLayout(
    ComponentContext c,
    @State String selection,
    @State boolean isShowingDropDown,
    @Prop(resType = ResType.DIMEN_TEXT, optional = true) float selectedTextSize,
    @Prop(resType = ResType.COLOR, optional = true) int selectedTextColor,
    @Prop(resType = ResType.DRAWABLE, optional = true) @Nullable Drawable caret) {
  caret = caret == null ? new CaretDrawable(c.getAndroidContext(), DEFAULT_CARET_COLOR) : caret;
  selectedTextSize =
      selectedTextSize == -1
          ? spToPx(c.getAndroidContext(), DEFAULT_TEXT_SIZE_SP)
          : selectedTextSize;

  return Row.create(c)
      .minHeightDip(SPINNER_HEIGHT)
      .justifyContent(YogaJustify.SPACE_BETWEEN)
      .paddingDip(START, MARGIN_SMALL)
      .backgroundAttr(android.R.attr.selectableItemBackground)
      .clickHandler(Spinner.onClick(c))
      .child(createSelectedItemText(c, selection, (int) selectedTextSize, selectedTextColor))
      .child(createCaret(c, caret, isShowingDropDown))
      .accessibilityRole(AccessibilityRole.DROP_DOWN_LIST)
      .build();
}
 
Example 2
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 3
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 4
Source File: ImageSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnMount
static void onMount(
    ComponentContext c,
    MatrixDrawable matrixDrawable,
    @Prop(resType = ResType.DRAWABLE) Drawable drawable,
    @FromBoundsDefined DrawableMatrix drawableMatrix) {
  matrixDrawable.mount(drawable, drawableMatrix);
}
 
Example 5
Source File: ImageSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnUnmount
static void onUnmount(
    ComponentContext c,
    MatrixDrawable convertDrawable,
    @Prop(resType = ResType.DRAWABLE) Drawable drawable) {
  convertDrawable.unmount();
}
 
Example 6
Source File: ImageSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@ShouldUpdate(onMount = true)
static boolean shouldUpdate(
    @Prop(optional = true) Diff<ScaleType> scaleType,
    @Prop(resType = ResType.DRAWABLE) Diff<Drawable> drawable) {
  return scaleType.getNext() != scaleType.getPrevious()
      || !DrawableUtils.isEquivalentTo(drawable.getNext(), drawable.getPrevious());
}
 
Example 7
Source File: ProgressSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnPrepare
static void onPrepare(
    ComponentContext c,
    @Prop(optional = true, resType = ResType.DRAWABLE) Drawable indeterminateDrawable,
    Output<Drawable> resolvedIndeterminateDrawable) {
  if (indeterminateDrawable != null) {
    resolvedIndeterminateDrawable.set(indeterminateDrawable);
  } else {
    resolvedIndeterminateDrawable.set(
        getStyledIndeterminateDrawable(c, android.R.attr.progressBarStyle));
  }
}
 
Example 8
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 9
Source File: TextInputSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnMount
static void onMount(
    final ComponentContext c,
    EditTextWithEventHandlers editText,
    @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) int minLines,
    @Prop(optional = true) int maxLines,
    @Prop(optional = true) TextUtils.TruncateAt ellipsize,
    @Prop(optional = true) int cursorDrawableRes,
    @Prop(optional = true) MovementMethod movementMethod,
    @Prop(optional = true, resType = ResType.STRING) CharSequence error,
    @Prop(optional = true, resType = ResType.DRAWABLE) Drawable errorDrawable,
    @State AtomicReference<CharSequence> savedText,
    @State AtomicReference<EditTextWithEventHandlers> mountedView) {
  mountedView.set(editText);

  setParams(
      editText,
      hint,
      getBackgroundOrDefault(c, inputBackground),
      shadowRadius,
      shadowDx,
      shadowDy,
      shadowColor,
      textColorStateList,
      hintColorStateList,
      highlightColor,
      textSize,
      typeface,
      textAlignment,
      gravity,
      editable,
      inputType,
      imeOptions,
      inputFilters,
      multiline,
      ellipsize,
      minLines,
      maxLines,
      cursorDrawableRes,
      movementMethod,
      // onMount happens:
      // 1. After initState: savedText = initText.
      // 2. After onUnmount: savedText preserved from underlying editText.
      savedText.get(),
      error,
      errorDrawable);
  editText.setTextState(savedText);
}