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

Alen Mujezinovic

09/23/2023, 9:08 PM
How would I go about dynamically resizing the window when the content has changed? Using
Dp.Unspecified
on startup makes the window the right size to fit the content - but when I add or remove content, it's the wrong size
Copy code
fun main() = application {
    var switch by remember { mutableStateOf(true) }
    val windowState = rememberWindowState(
        size = DpSize(width = Dp.Unspecified, height = Dp.Unspecified)
    )
    Window(
        title = "Resizing",
        onCloseRequest = ::exitApplication,
        state = windowState,
        resizable = false,
        undecorated = false
    ) {
        when {
            switch -> Column(Modifier.width(200.dp)) {
                Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec ex ac eros interdum consectetur a et nisl. Mauris sit amet enim a odio feugiat malesuada sed nec nisl. ")
                Button({ switch = !switch }) {
                    Text("Switch")
                }
            }

            else -> Column(Modifier.width(200.dp)) {
                Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec ex ac eros interdum consectetur a et nisl. Mauris sit amet enim a odio feugiat malesuada sed nec nisl. ")
                Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec ex ac eros interdum consectetur a et nisl. Mauris sit amet enim a odio feugiat malesuada sed nec nisl. ")
                Button({ switch = !switch }) {
                    Text("Switch")
                }
            }
        }
    }
}
Setting
Dp.Unspecified
in an effect does work, but the screen flashes for a split second with a fullscreen window
Copy code
LaunchedEffect(switch) {
        delay(50.milliseconds)
        windowState.size = DpSize(width = Dp.Unspecified, height = Dp.Unspecified)
    }
And every now and then, the content just disappears
a

Alexander Maryanovsky

09/24/2023, 7:59 AM
Using Dp.Unspecified isn’t currently supported after the window has been created, but do submit a feature request.
a

Alen Mujezinovic

09/24/2023, 12:36 PM
Is there another way to adjust the size of the window when the size of the content changes?
Ah, figured it out by using
wrapContentSize
and reacting to
onSizeChanged
. This adjust the window size to the size of the content
Copy code
fun main() = application {
    var switch by remember { mutableStateOf(true) }
    val windowState: WindowState = rememberWindowState(
        size = DpSize(width = Dp.Unspecified, height = Dp.Unspecified)
    )

    Window(
        title = "Resizing",
        onCloseRequest = ::exitApplication,
        state = windowState,
        resizable = false,
        undecorated = false
    ) {
        Box(
            modifier = Modifier.wrapContentSize(unbounded = true)
                .onSizeChanged { windowState.size = DpSize(it.width.dp, (it.height + 25).dp) }
                .padding(16.dp)
        ) {
            when {
                switch -> Column(Modifier.width(200.dp)) {
                    Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec ex ac eros interdum consectetur a et nisl. Mauris sit amet enim a odio feugiat malesuada sed nec nisl. ")
                    Button({ switch = !switch }) {
                        Text("Switch")
                    }
                }

                else -> Column(Modifier.width(200.dp)) {
                    Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec ex ac eros interdum consectetur a et nisl. Mauris sit amet enim a odio feugiat malesuada sed nec nisl. ")
                    Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec ex ac eros interdum consectetur a et nisl. Mauris sit amet enim a odio feugiat malesuada sed nec nisl. ")
                    Button({ switch = !switch }) {
                        Text("Switch")
                    }
                }
            }
        }
    }
}
Hmm, no. It seems that this is wreaking havoc on the layout as soon as there are any complicated bits of content in the box
4 Views