JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.91k stars 1.16k forks source link

ImageComposeScene. `scene.render` doesn't work if it is called inside `singleWindowApplication` #1394

Open igordmn opened 2 years ago

igordmn commented 2 years ago

Compose 1.0.0-beta5

This code works:

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.ui.ImageComposeScene
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.window.singleWindowApplication
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.ExperimentalTime

@OptIn(ExperimentalTime::class, androidx.compose.ui.ExperimentalComposeUiApi::class)
fun main() {
    val scene = ImageComposeScene(100, 100) {
        Box(Modifier.fillMaxSize().background(Color.White)) {
            CircularProgressIndicator()
        }
    }
    scene.render() // start animation

    val f0 = scene.render(milliseconds(0L)).toComposeImageBitmap()
    val f100 = scene.render(milliseconds(100L)).toComposeImageBitmap()
    val f200 = scene.render(milliseconds(200L)).toComposeImageBitmap()
    val f300 = scene.render(milliseconds(300L)).toComposeImageBitmap()
    val f400 = scene.render(milliseconds(400L)).toComposeImageBitmap()
    val f500 = scene.render(milliseconds(500L)).toComposeImageBitmap()

    singleWindowApplication {
        Row {
            Image(f0, "image")
            Image(f100, "image")
            Image(f200, "image")
            Image(f300, "image")
            Image(f400, "image")
            Image(f500, "image")
        }
    }
}

And this doesn't:

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.ui.ImageComposeScene
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.window.singleWindowApplication
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.ExperimentalTime

@OptIn(ExperimentalTime::class, androidx.compose.ui.ExperimentalComposeUiApi::class)
fun main() {
    val scene = ImageComposeScene(100, 100) {
        Box(Modifier.fillMaxSize().background(Color.White)) {
            CircularProgressIndicator()
        }
    }
    scene.render() // start animation

    singleWindowApplication {
        Row {
            Image(scene.render(milliseconds(0L)).toComposeImageBitmap(), "image")
            Image(scene.render(milliseconds(100L)).toComposeImageBitmap(), "image")
            Image(scene.render(milliseconds(200L)).toComposeImageBitmap(), "image")
            Image(scene.render(milliseconds(300L)).toComposeImageBitmap(), "image")
            Image(scene.render(milliseconds(400L)).toComposeImageBitmap(), "image")
            Image(scene.render(milliseconds(500L)).toComposeImageBitmap(), "image")
        }
    }
}
igordmn commented 2 years ago

Still reproduces after https://github.com/JetBrains/androidx/pull/199

okushnikov commented 3 weeks ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.