compose-multiplatform: View events are triggered on non-main thread?

Hello! I’m observing an interesting behaviour. The following code:

println(Thread.currentThread().toString())

    Window("Title") {
        Surface(modifier = Modifier.fillMaxSize()) {
            MaterialTheme {
                DesktopTheme {
                    Button(onClick = { println(Thread.currentThread().toString()) }) {
                        Text("Button")
                    }
                }
            }
        }
    }

prints:

Thread[main,5,main]
Thread[AWT-EventQueue-0 @coroutine#1,6,main]

Is it expected? My concern is that some libraries have requirements that they are accessed from the same thread as they were initialised. From my point of view, the event queue should be looped from the original main thread.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Yeah, if you really need to run on AWT, using DisposableEffect doesn’t really sound like the right thing, I’d do something like:

    val viewModel = run {
        var viewModel: MyViewModel? = null
        SwingUtilities.invokeAndWait { viewModel = MyViewModel() }
        viewModel!!
    }