https://kotlinlang.org logo
#compose-desktop
Title
# compose-desktop
n

natpryce

02/20/2023, 10:50 AM
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

CLOVIS

02/20/2023, 10:52 AM
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

natpryce

02/20/2023, 11:31 AM
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

Alexander Maryanovsky

02/20/2023, 12:49 PM
Pass
Dp.Undefined
as the window size
n

natpryce

02/20/2023, 3:28 PM
You mean
Dp.Unspecified
? That doesn’t work — the window is created much too large.
a

Alexander Maryanovsky

02/20/2023, 3:33 PM
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

natpryce

02/20/2023, 3:34 PM
Which version of Jetpack compose are you using?
a

Alexander Maryanovsky

02/20/2023, 3:34 PM
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

natpryce

02/20/2023, 3:39 PM
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

Alexander Maryanovsky

02/20/2023, 3:42 PM
np
11 Views