Q: how do I size a window to fit its contents in J...
# compose-desktop
n
Q: how do I size a window to fit its contents in Jetpack compose? (Eg What is the equivalent of Swing’s pack method?)
c
I'm not an expert, so I could be wrong, but Compose works the opposite way to Swing: components adapt themselves to the size of the window (this is much more performant), so I don't think it's possible to implement this
n
I guess that makes sense on a hand-held device where the UI usually fills the (small) screen. But makes for an awkward experience on desktop without hard-coding a window size, which duplicates size information known by the window contents. I’ll keep exploring…
a
Pass
Dp.Undefined
as the window size
n
You mean
Dp.Unspecified
? That doesn’t work — the window is created much too large.
a
Copy code
fun main() = singleWindowApplication(
    state = WindowState(size = DpSize.Unspecified)
){
    Box(
        Modifier.size(200.dp, 300.dp)
    )
}
works for me
You can even “pack” on each axis individually:
Copy code
fun main() = singleWindowApplication(
    state = WindowState(width = Dp.Unspecified, height = 200.dp)
){
    Box(
        Modifier.size(200.dp, 300.dp)
    )
}
n
Which version of Jetpack compose are you using?
a
1.3.0
There’s a known bug where this doesn’t work for undecorated windows: https://github.com/JetBrains/compose-jb/issues/2676
n
My window is decorated.
I get different layouts if I add the wrapContentSize modifier to the children in the window content. None of them change the size of the window yet,. but they do affect the size of intermediate containers.
I’ll experiment with various modifiers and see if I can get it to work
Dp.Unspecified seems to be the way to go, but I need to be careful about how layout info is specified / propagated within the window
Thank you
a
np