Git Product home page Git Product logo

Comments (4)

ataulm avatar ataulm commented on July 28, 2024 1

How's this?

@Composable
private fun LayoutElementPreview(
    element: LayoutElement,
    @ColorInt windowBackgroundColor: Int = android.graphics.Color.BLACK,
    tileResourcesFn: ResourceBuilders.Resources.Builder.() -> Unit = {}
) {
    val root = LayoutElementBuilders.Box.Builder()
        .setModifiers(
            ModifiersBuilders.Modifiers.Builder().setBackground(
                ModifiersBuilders.Background.Builder()
                    .setColor(ColorBuilders.argb(windowBackgroundColor))
                    .build()
            ).build()
        )
        .setHorizontalAlignment(LayoutElementBuilders.HORIZONTAL_ALIGN_CENTER)
        .setVerticalAlignment(LayoutElementBuilders.VERTICAL_ALIGN_CENTER)
        .setHeight(DimensionBuilders.ExpandedDimensionProp.Builder().build())
        .setWidth(DimensionBuilders.ExpandedDimensionProp.Builder().build())
        .addContent(element)
        .build()

    LayoutRootPreview(root = root, tileResourcesFn)
}

@Composable
private fun LayoutRootPreview(
    root: LayoutElement,
    tileResourcesFn: ResourceBuilders.Resources.Builder.() -> Unit = {}
) {
    val tile = TileBuilders.Tile.Builder()
        .setResourcesVersion("1")
        .setTimeline(singleEntryTimeline(root))
        .build()

    val tileResources = ResourceBuilders.Resources.Builder()
        .apply(tileResourcesFn)
        .build()

    TilePreview(tile, tileResources)
}

from horologist.

ataulm avatar ataulm commented on July 28, 2024

The issue is the usage of LayoutPreview which is only intended to showcase smaller components - it has its own root layout set (a Box) so it doesn't expect another root layout to be passed in.

Would we need to have two separate Previews, e.g. a LayoutRootPreview (this works)?

@Composable
private fun LayoutRootPreview(rootLayout: LayoutElementBuilders.LayoutElement) {
    val tile =
        TileBuilders.Tile.Builder()
            .setResourcesVersion("1")
            .setTimeline(
                TimelineBuilders.Timeline.Builder().addTimelineEntry(
                    TimelineBuilders.TimelineEntry.Builder().setLayout(
                        LayoutElementBuilders.Layout.Builder().setRoot(
                            rootLayout
                        ).build()
                    ).build()
                ).build()
            ).build()

    val tileResources = ResourceBuilders.Resources.Builder()
        .build()
    TilePreview(tile, tileResources)
}

from horologist.

yschimke avatar yschimke commented on July 28, 2024

@ataulm Do I leave the PR to you, no rush from my side. And good to know that the approach works with all component types.

from horologist.

ataulm avatar ataulm commented on July 28, 2024

Addressed in #240

from horologist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.