Would this be a good way to run JavaFX and Compose...
# compose-desktop
m
Would this be a good way to run JavaFX and Compose Desktop in the same application?
Copy code
import kotlinx.coroutines.*

import androidx.compose.desktop.*
import androidx.compose.material.*
import androidx.compose.runtime.*

fun main(args: Array<String>) = runBlocking {
    val javaFxJob = GlobalScope.launch {
        launch<HelloWorld>(args)
    }

    val composeDesktopJob = GlobalScope.launch {
        Window {
            var text by remember { mutableStateOf("Hello, World!") }

            MaterialTheme {
                Button(onClick = {
                    text = "Hello, Desktop!"
                }) {
                    Text(text)
                }
            }
        }
    }

    javaFxJob.join()
    composeDesktopJob.join()
}
I'm getting crashes half of the time, unless I separate the startups of the two toolkits with a
delay()
, so I guess there is something wrong. Otherwise it seems to work. This happens with both 0.3.0-build133 and 0.2.0-build132.
i
Can you show stacktrace of the crash?
m
I will document the problem in a few hours. Thanks, @Igor Demin.
Let me know if you need a MCVE.
Thanks.
i
It seems it crashes when we initialize JavaFX. One guess - JavaFX and Swing (we use it inside Compose) use common class for initialization that isn't thread safe. In this case we need to synchronize initialization or just call both inits from one thread
m
Was this fixed? Should I open an issue on the tracker?
i
Probably there is no easy fix on Compose side. JavaFx / Compose(Swing) use the common class GtkApplication to init application. Probably it is not thread safe. Without some common mutex provided by JDK we can't control when JavaFx is started. Just don't launch JavaFX/Compose in parallel threads (i.e. remove GlobalScope.launch)
👍 1
m
Ok. Thanks!