Kotlin Multiplatorm min window size

Unfortunately, it seems that Kotlin Multiplatform doesn’t support setting minimum window size for the desktop right now, however, there is a workaround.

In this article we will find an answer to the following questions:

  • Kotlin Multiplatform set window size
  • Kotlin Multiplatform how to set minimum window size
  • min widow size Kotlin Multiplatform desktop

Suppose we have some window with size:

fun main() = application {
    val state = rememberWindowState(
        position = WindowPosition(Alignment.Center),
        size = DpSize(750.dp, 590.dp),
    )

    Window(
        onCloseRequest = ::exitApplication,
        title = "MyTitle",
        state = state,
    ) {
        App()
    }
}

So the idea is to add listener for size changes and adjust it given known min width and height:

Window(
    onCloseRequest = ::exitApplication,
    title = "MyTitle",
    state = state,
) {
    App()

    LaunchedEffect(state) {
        snapshotFlow { state.size }
            .onEach { newSize ->
                val clampedWidth = maxOf(newSize.width, 400.dp)
                val clampedHeight = maxOf(newSize.height, 590.dp)
                state.size = DpSize(clampedWidth, clampedHeight)
            }
            .launchIn(this)
    }
}

That’s all!


Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at promark33@gmail.com.

If I saved your day, you can support me 🤝