/** A component that doesn't render anything. */ @LayoutSpec class EmptyComponentSpec { @OnCreateLayout static @Nullable Component onCreateLayout(ComponentContext c) { return null; } }
element.getAnnotation(LayoutSpec.class).value(), DelegateMethodExtractor.getDelegateMethods( element, JavadocExtractor.getClassJavadoc(elements, element), JavadocExtractor.getPropJavadocs(elements, element), element.getAnnotation(LayoutSpec.class).isPublic(), dependencyInjectionHelper, element.getAnnotation(LayoutSpec.class).isPureRender(), SpecElementTypeDeterminator.determine(element), element,
@Test public void testCreateWithSpecifiedName() { when(mLayoutSpec.value()).thenReturn("TestComponentName"); LayoutSpecModel layoutSpecModel = mFactory.create( mElements, mTypes, mTypeElement, mMessager, RunMode.normal(), mDependencyInjectionHelper, null); assertThat(layoutSpecModel.getSpecName()).isEqualTo("TestSpec"); assertThat(layoutSpecModel.getComponentName()).isEqualTo("TestComponentName"); assertThat(layoutSpecModel.getSpecTypeName().toString()).isEqualTo(TEST_QUALIFIED_SPEC_NAME); assertThat(layoutSpecModel.getComponentTypeName().toString()) .isEqualTo("com.facebook.litho.TestComponentName"); } }
@LayoutSpec public class PossiblyCrashingSubTitleComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c, @Prop(resType = STRING) String subtitle) { if (Math.random() >= 0.7) { throw new RuntimeException("Oh no, a random error!"); } return Text.create(c).text(subtitle).textSizeDip(18).build(); } }
/** A simple, "Hello, world" Component for learning the basics of LayoutSpecs. */ @LayoutSpec public class LearningLayoutSpecsComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c) { return Text.create(c).text("Hello, World!").textSizeDip(50).build(); } }
@LayoutSpec public class TestCrasherOnCreateLayoutWithSizeSpecSpec { @OnCreateLayoutWithSizeSpec protected static Component onCreateLayoutWithSizeSpec( ComponentContext c, final int widthSpec, final int heightSpec) { throw new RuntimeException("onCreateLayoutWithSizeSpec crash"); } }
@LayoutSpec public class TestCrasherOnCreateLayoutSpec { @OnCreateLayout public static Component onCreateLayout(ComponentContext c) { throw new RuntimeException("onCreateLayout crash"); } }
@LayoutSpec public class TitleComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c, @Prop(resType = STRING) String title) { return Text.create(c) .text(title) .textStyle(BOLD) .textSizeDip(24) .positionDip(YogaEdge.BOTTOM, 16) .build(); } }
/** * <h2>End goal</h2> * Renders a "story card" with a header and message. You should also implement a togglable "saved" * state. */ @LayoutSpec class StoryCardComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c) { return Text.create(c, 0, R.style.hello_world).textRes(R.string.hello_world).build(); } }
@LayoutSpec public class FeedItemCardSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c, @Prop final Artist artist) { return Column.create(c) .paddingDip(VERTICAL, 8) .paddingDip(HORIZONTAL, 16) .child(Card.create(c).content(FeedItemComponent.create(c).artist(artist))) .build(); } }
/** Simple row that displays a single line of text and responds to click events. */ @LayoutSpec public class ExamplesRowComponentSpec { @OnCreateLayout static Component onCreateLayout( ComponentContext c, @Prop String text, @Prop EventHandler<ClickEvent> clickEventHandler) { return Text.create(c) .text(text) .textSizeSp(25) .clickHandler(clickEventHandler) .build(); } }
@LayoutSpec public class PlaygroundComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c) { return Column.create(c) .backgroundColor(Color.WHITE) .child( Text.create(c) .textSizeSp(20) .text("Playground sample")) .build(); } }
/** * Renders the approximate end state for the lab activity. This wraps the {@link * StoryCardComponent}, provides its props, and also fills the rest of the screen with gray. */ @LayoutSpec class LithoLabEndComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c) { return RecyclerCollectionComponent.create(c) .section(StoryCardsWithHeaderSection.create(new SectionContext(c)).build()) .build(); } }
@LayoutSpec public class ActionsComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c) { return Row.create(c) .backgroundColor(0xDDFFFFFF) .positionType(YogaPositionType.ABSOLUTE) .positionDip(YogaEdge.RIGHT, 4) .positionDip(YogaEdge.TOP, 4) .paddingDip(YogaEdge.ALL, 2) .child(FavouriteButton.create(c)) .build(); } }
@LayoutSpec public class AllBorderSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c) { return Row.create(c) .child( Text.create(c) .textSizeSp(20) .text("This component has all borders specified to the same color + width")) .border( Border.create(c).color(YogaEdge.ALL, NiceColor.BLUE).widthDip(YogaEdge.ALL, 5).build()) .build(); } }
@LayoutSpec public class FooterComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c, @Prop(resType = STRING) String text) { return Column.create(c) .paddingDip(YogaEdge.ALL, 8) .child( Text.create(c) .text(text) .textSizeDip(14) .textColor(GRAY) .textStyle(ITALIC)) .build(); } }
@LayoutSpec class LithoLabMiddleComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c) { return Column.create(c) .backgroundRes(android.R.color.darker_gray) .child( StoryCardComponent.create(c) .content( "This is some test content. It should fill at least one line. " + "This is a story card. You can interact with the menu button " + "and save button.")) .build(); } }
/** * Introduction to basic {@literal @}Props usage. Make your own Component with {@literal @}Props and * see what is generated. and how to interact with it. */ @LayoutSpec public class LearningPropsComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c, @Prop String text1, @Prop String text2) { return Column.create(c) .child(Text.create(c).text(text1).textSizeDip(50)) .child( Text.create(c).text(text2).textColorRes(android.R.color.holo_green_dark).textSizeSp(30)) .build(); } }
@LayoutSpec public class TitleComponentSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c, @Prop(resType = STRING) String title) { return Text.create(c) .text(title) .textStyle(BOLD) .textSizeDip(24) .backgroundColor(0xDDFFFFFF) .positionType(YogaPositionType.ABSOLUTE) .positionDip(YogaEdge.BOTTOM, 4) .positionDip(YogaEdge.LEFT, 4) .paddingDip(YogaEdge.HORIZONTAL, 6) .build(); } }
@LayoutSpec public class DashPathEffectBorderSpec { @OnCreateLayout static Component onCreateLayout(ComponentContext c) { return Row.create(c) .child(Text.create(c).textSizeSp(20).text("This component has a dash path effect applied")) .border( Border.create(c) .color(YogaEdge.ALL, NiceColor.BLUE) .widthDip(YogaEdge.ALL, 5) .dashEffect(new float[] {10f, 5f}, 0f) .build()) .build(); } }