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.79k stars 1.14k forks source link

SwingPanel OpenGL content freeze while TextField is focused #1072

Open yaroslavkulinich opened 3 years ago

yaroslavkulinich commented 3 years ago

I have the WorldWind Java 3D map (OpenGL) working in separate SwingPanel and regular Composable with TextField displayed side-by-side.

The problem is simple to reproduce. When I try to edit any of my TextFields, the map (inside SwingPanel) freezes and I can't do anything with it, even resize correctly (stretched picture effect when change window size).

After I remove focus from TextField (navigate to another composable for example) - everything becomes fine.

akurasov commented 3 years ago

@yaroslavkulinich, thanks for reporting. What is your environment (OS, graphics hardware)? What happens with CPU consumption in these moments?

yaroslavkulinich commented 3 years ago

MacOS Big Sur, 16 GB, Intel Iris Plus Graphics 1536 (MacBook Pro 2020). Nothing special with CPU consumption.

yaroslavkulinich commented 3 years ago

Video with described behavior.

akurasov commented 3 years ago

Thank you! Can I get your code somewhere?

yaroslavkulinich commented 3 years ago

Repository with the project running on video. Launched with adopt-openjdk-16.0.2

yaroslavkulinich commented 3 years ago

Main.kt

fun main() = application {
    androidx.compose.ui.window.Window(onCloseRequest = ::exitApplication) {
        App()
    }
}

@Composable
@Preview
fun App() {
    DesktopMaterialTheme {
        Row {
            SwingPanel(
                modifier = Modifier.fillMaxSize().weight(1f),
                factory = {
                    JPanel().apply {
                        layout = BoxLayout(this, BoxLayout.Y_AXIS)
                        val ww = WorldWindowGLCanvas().apply {
                            model = BasicModel()
                        }
                        add(ww)
                    }
                }
            )
            Column(Modifier.width(200.dp).fillMaxWidth()) {
                val textFieldState = remember { mutableStateOf("") }
                val focusManager = LocalFocusManager.current

                TextField(
                    value = textFieldState.value,
                    onValueChange = { textFieldState.value = it },
                    label = { Text("TextField") }
                )

                Spacer(Modifier.height(4.dp))

                Button(
                    modifier = Modifier.fillMaxWidth(),
                    onClick = { focusManager.clearFocus() }
                ) {
                    Text("Clear focus")
                }
            }
        }
    }
}
akurasov commented 3 years ago

Could you try to replace main with

fun main() = SwingUtilities.invokeLater {
    ComposeWindow().apply {
        size = Dimension(300, 300)
        defaultCloseOperation = JFrame.DISPOSE_ON_CLOSE
        setContent {
            App()
        }
        isVisible = true
    }
}

and see if the issue persist?

yaroslavkulinich commented 3 years ago

@akurasov Wow! Great! Issue gone!

yaroslavkulinich commented 3 years ago

@akurasov Thank you!

akurasov commented 3 years ago

It means, issue iss related with known problem of high CPU consumption during animation. This specific case will be fixed soon (probably next Compose build). What I proposed is a temporary workaround.

okushnikov commented 1 month ago

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