Skip to content

Commit

Permalink
Fix default layout params for compose snapshots (#1681)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-powell authored Nov 7, 2024
1 parent 1908c6a commit c5f3b2d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package app.cash.paparazzi.plugin.test

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import app.cash.paparazzi.Paparazzi
import org.junit.Rule
import org.junit.Test
Expand All @@ -14,4 +21,18 @@ class ComposeTest {
HelloPaparazzi()
}
}

@Test
fun composeDefaultLayoutParams() {
paparazzi.snapshot {
Box(
modifier = Modifier
.background(Color.Cyan)
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
Text("Hello")
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion paparazzi/src/main/java/app/cash/paparazzi/PaparazziSdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.View.NO_ID
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import androidx.activity.setViewTreeOnBackPressedDispatcherOwner
import androidx.annotation.LayoutRes
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -162,7 +163,9 @@ public class PaparazziSdk @JvmOverloads constructor(
public fun <V : View> inflate(@LayoutRes layoutId: Int): V = layoutInflater.inflate(layoutId, null) as V

public fun snapshot(composable: @Composable () -> Unit) {
val hostView = ComposeView(context)
val hostView = ComposeView(context).apply {
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
}
hostView.setContent(composable)

snapshot(hostView)
Expand Down

0 comments on commit c5f3b2d

Please sign in to comment.