Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mahozad committed Feb 11, 2024
1 parent 4606c42 commit 4c345c4
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.math.roundToInt

// The compose-multiplatform-core implementation for Android:
// https://github.com/JetBrains/compose-multiplatform-core/blob/jb-main/compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/AndroidImageHelpers.android.kt
// https://github.com/JetBrains/compose-multiplatform-core/blob/jb-main/compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/android/WindowCapture.android.kt

actual fun initializeeeeeee(): Unit = TODO()

actual fun setContenttt(content: @Composable () -> Unit): Unit = TODO()

actual fun captureToImageeee(content: @Composable () -> Unit): ImageBitmap {
error("Use the Activity.captureToImageeee instead")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// https://github.com/JetBrains/compose-multiplatform-core/pull/589
// https://developer.android.com/jetpack/compose/migrate/interoperability-apis/compose-in-views
// https://github.com/JetBrains/compose-multiplatform/issues/3972#event-11759307640
// The compose-multiplatform-core implementation for Android:
// https://github.com/JetBrains/compose-multiplatform-core/blob/jb-main/compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/AndroidImageHelpers.android.kt
// https://github.com/JetBrains/compose-multiplatform-core/blob/jb-main/compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/android/WindowCapture.android.kt

// Similar libraries:
// https://github.com/PatilShreyas/Capturable
Expand Down Expand Up @@ -47,10 +50,4 @@ package ir.mahozad.multiplatform.comshot
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.ImageBitmap

internal const val NanoSecondsPerMilliSecond = 1_000_000L

expect fun initializeeeeeee()

expect fun setContenttt(content: @Composable () -> Unit)

expect fun captureToImageeee(content: @Composable () -> Unit): ImageBitmap
Original file line number Diff line number Diff line change
@@ -1,57 +1,42 @@
package ir.mahozad.multiplatform.comshot

import androidx.compose.runtime.Composable
import androidx.compose.ui.ImageComposeScene
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeCanvas
import androidx.compose.ui.graphics.asComposeImageBitmap
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.renderComposeScene
import androidx.compose.ui.scene.ComposeScene
import androidx.compose.ui.scene.MultiLayerComposeScene
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.test.runDesktopComposeUiTest
import androidx.compose.ui.test.runSkikoComposeUiTest
import androidx.compose.ui.unit.*
import kotlinx.coroutines.Dispatchers
import org.jetbrains.skia.Bitmap
import org.jetbrains.skia.Surface
import org.jetbrains.skiko.toBufferedImage

private val surface = Surface.makeRasterN32Premul(800, 600)
@OptIn(InternalComposeUiApi::class)
private lateinit var scene: ComposeScene

@OptIn(InternalComposeUiApi::class)
private fun render(timeMillis: Long) {
scene.render(
surface.canvas.asComposeCanvas(),
timeMillis * NanoSecondsPerMilliSecond
)
}

@OptIn(InternalComposeUiApi::class)
private fun createUi() = MultiLayerComposeScene(
layoutDirection = LayoutDirection.Ltr,
invalidate = { render(0) },
// density = Density(1f),
// coroutineContext = Dispatchers.Unconfined,
// composeSceneContext = TestComposeSceneContext(),
).also {
it.boundsInWindow = IntSize(600, 400).toIntRect()
}

@OptIn(InternalComposeUiApi::class)
actual fun initializeeeeeee() {
scene = createUi()
}

@OptIn(InternalComposeUiApi::class)
actual fun setContenttt(content: @Composable () -> Unit) {
scene.setContent(content)
}
// private val surface = Surface.makeRasterN32Premul(800, 600)
// @OptIn(InternalComposeUiApi::class)
// private lateinit var scene: ComposeScene
//
// @OptIn(InternalComposeUiApi::class)
// private fun render(timeMillis: Long) {
// scene.render(
// surface.canvas.asComposeCanvas(),
// timeMillis * NanoSecondsPerMilliSecond
// )
// }
//
// @OptIn(InternalComposeUiApi::class)
// private fun createUi() = MultiLayerComposeScene(
// layoutDirection = LayoutDirection.Ltr,
// invalidate = { render(0) },
// // density = Density(1f),
// // coroutineContext = Dispatchers.Unconfined,
// // composeSceneContext = TestComposeSceneContext(),
// ).also {
// it.boundsInWindow = IntSize(600, 400).toIntRect()
// }
//
// @OptIn(InternalComposeUiApi::class)
// actual fun initializeeeeeee() {
// scene = createUi()
// }
//
// @OptIn(InternalComposeUiApi::class)
// actual fun setContenttt(content: @Composable () -> Unit) {
// scene.setContent(content)
// }

@OptIn(ExperimentalTestApi::class)
actual fun captureToImageeee(content: @Composable () -> Unit): ImageBitmap {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
package ir.mahozad.multiplatform.comshot

import androidx.compose.runtime.Composable
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeCanvas
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.scene.ComposeScene
import androidx.compose.ui.scene.MultiLayerComposeScene
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.toIntRect
import org.jetbrains.skia.Surface

private val surface = Surface.makeRasterN32Premul(800, 600)
@OptIn(InternalComposeUiApi::class)
private lateinit var scene: ComposeScene

@OptIn(InternalComposeUiApi::class)
private fun render(timeMillis: Long) {
scene.render(
surface.canvas.asComposeCanvas(),
timeMillis * NanoSecondsPerMilliSecond
)
}

@OptIn(InternalComposeUiApi::class)
private fun createUi() = MultiLayerComposeScene(
layoutDirection = LayoutDirection.Ltr,
invalidate = { render(0) },
// density = Density(1f),
// coroutineContext = Dispatchers.Unconfined,
// composeSceneContext = TestComposeSceneContext(),
).also {
it.boundsInWindow = IntSize(600, 400).toIntRect()
}

@OptIn(InternalComposeUiApi::class)
actual fun initializeeeeeee() {
scene = createUi()
}

@OptIn(InternalComposeUiApi::class)
actual fun setContenttt(content: @Composable () -> Unit) {
scene.setContent(content)
}
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.runSkikoComposeUiTest

@OptIn(ExperimentalTestApi::class)
actual fun captureToImageeee(content: @Composable () -> Unit): ImageBitmap {
// waitForIdle()
return surface.makeImageSnapshot().toComposeImageBitmap()
var image: ImageBitmap? = null
runSkikoComposeUiTest {
setContent(content)
image = captureToImage()
}
return image!!
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,19 @@
package ir.mahozad.multiplatform.comshot

import androidx.compose.runtime.Composable
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeCanvas
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.scene.ComposeScene
import androidx.compose.ui.scene.MultiLayerComposeScene
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.toIntRect
import org.jetbrains.skia.Surface

private val surface = Surface.makeRasterN32Premul(800, 600)
@OptIn(InternalComposeUiApi::class)
private lateinit var scene: ComposeScene

@OptIn(InternalComposeUiApi::class)
private fun render(timeMillis: Long) {
scene.render(
surface.canvas.asComposeCanvas(),
timeMillis * NanoSecondsPerMilliSecond
)
}

@OptIn(InternalComposeUiApi::class)
private fun createUi() = MultiLayerComposeScene(
layoutDirection = LayoutDirection.Ltr,
invalidate = { render(0) },
// density = Density(1f),
// coroutineContext = Dispatchers.Unconfined,
// composeSceneContext = TestComposeSceneContext(),
).also {
it.boundsInWindow = IntSize(600, 400).toIntRect()
}

@OptIn(InternalComposeUiApi::class)
actual fun initializeeeeeee() {
scene = createUi()
}

@OptIn(InternalComposeUiApi::class)
actual fun setContenttt(content: @Composable () -> Unit) {
scene.setContent(content)
}
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.runSkikoComposeUiTest

@OptIn(ExperimentalTestApi::class)
actual fun captureToImageeee(content: @Composable () -> Unit): ImageBitmap {
// waitForIdle()
return surface.makeImageSnapshot().toComposeImageBitmap()
var image: ImageBitmap? = null
runSkikoComposeUiTest {
setContent(content)
image = captureToImage()
}
return image!!
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
package ir.mahozad.multiplatform.comshot

import androidx.compose.runtime.Composable
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeCanvas
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.scene.ComposeScene
import androidx.compose.ui.scene.MultiLayerComposeScene
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.toIntRect
import org.jetbrains.skia.Surface

private val surface = Surface.makeRasterN32Premul(800, 600)
@OptIn(InternalComposeUiApi::class)
private lateinit var scene: ComposeScene

@OptIn(InternalComposeUiApi::class)
private fun render(timeMillis: Long) {
scene.render(
surface.canvas.asComposeCanvas(),
timeMillis * NanoSecondsPerMilliSecond
)
}

@OptIn(InternalComposeUiApi::class)
private fun createUi() = MultiLayerComposeScene(
layoutDirection = LayoutDirection.Ltr,
invalidate = { render(0) },
// density = Density(1f),
// coroutineContext = Dispatchers.Unconfined,
// composeSceneContext = TestComposeSceneContext(),
).also {
it.boundsInWindow = IntSize(600, 400).toIntRect()
}

@OptIn(InternalComposeUiApi::class)
actual fun initializeeeeeee() {
scene = createUi()
}

@OptIn(InternalComposeUiApi::class)
actual fun setContenttt(content: @Composable () -> Unit) {
scene.setContent(content)
}
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.runSkikoComposeUiTest

@OptIn(ExperimentalTestApi::class)
actual fun captureToImageeee(content: @Composable () -> Unit): ImageBitmap {
// waitForIdle()
return surface.makeImageSnapshot().toComposeImageBitmap()
var image: ImageBitmap? = null
runSkikoComposeUiTest {
setContent(content)
image = captureToImage()
}
return image!!
}
4 changes: 0 additions & 4 deletions showcase/shared/src/commonMain/kotlin/comshot/showcase/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.unit.dp
import comshot.showcase.shared.generated.resources.Res
import ir.mahozad.multiplatform.comshot.captureToImageeee
import ir.mahozad.multiplatform.comshot.initializeeeeeee
import ir.mahozad.multiplatform.comshot.setContenttt
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource

Expand Down Expand Up @@ -42,8 +40,6 @@ fun App() {
// WavySlider(value, { value = it })
Button(
onClick = {
initializeeeeeee()
setContenttt(content = composable)
image = captureToImageeee(composable)
i++
// ImageIO.write(image.toAwtImage(), "PNG", Path("output.png").outputStream())
Expand Down

0 comments on commit 4c345c4

Please sign in to comment.