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.96k stars 1.16k forks source link

SwingPanel doesn't change its size on window resize #731

Open igordmn opened 3 years ago

igordmn commented 3 years ago

Compose 0.4.0, 0.4.0-build174 (everything is ok in 0.4.0-build173)

  1. run this code:
import androidx.compose.desktop.SwingPanel
import androidx.compose.desktop.Window
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import java.awt.Color
import javax.swing.JPanel

fun main() = Window {
    SwingPanel(
        factory = {
            JPanel().apply {
                background = Color.RED
            }
        },
        modifier = Modifier.fillMaxSize()
    )
}
  1. try to resize the window

Expected: panel is resized Actual: size of the panel doesn't change

The problem lies in Modifier.onGloballyPositioned that is used by SwingPanel. This code also works in 0.4.0-build173:

import androidx.compose.desktop.Window
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.onGloballyPositioned

fun main() = Window {
    Layout(
        content = {},
        modifier = Modifier.fillMaxSize().onGloballyPositioned { childCoordinates ->
            val coordinates = childCoordinates.parentCoordinates!!
            val size = coordinates.size
            println("Size ${size.width}")
        },
        measurePolicy = { _, _ ->
            layout(0, 0) {}
        }
    )
}
igordmn commented 3 years ago

It seems this code works:

import androidx.compose.desktop.SwingPanel
import androidx.compose.desktop.Window
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import java.awt.Color
import javax.swing.JPanel
fun main() = Window {
    Box(
        modifier = Modifier.fillMaxSize().background(color = androidx.compose.ui.graphics.Color.Green )
    ) {
        SwingPanel(
            factory = {
                JPanel().apply {
                    background = Color.RED
                }
            }
        )
    }
}

But doesn't work when we add Modifier.fillMaxSize() to the SwingPanel

okushnikov commented 4 weeks ago

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