DEV Community

Cover image for Compose for Desktop: Window party tricks
ColtonIdle
ColtonIdle

Posted on

Compose for Desktop: Window party tricks

Just a random collection of window tricks I've accumulated while trying to build a desktop app.

1. Size the window to your content

Sometimes you just want your window to match the size of your content. I've found this to be true for settings windows for example. Simply use:

val state = rememberWindowState(
    size = DpSize(Dp.Unspecified, Dp.Unspecified)
)
Enter fullscreen mode Exit fullscreen mode

Make sure your root layouts don't have fillMaxSize() or else this won't work. You can optionally also make the Window non-resizable via resizable = false on the Window.

If you need the window size to adjust to your content size, checkout https://gist.github.com/ColtonIdle/df23e3dc8e72569a28a4c64197bed14c

More info: https://kotlinlang.slack.com/archives/C01D6HTPATV/p1740892851067859

2. Start with the window centered

Use:

Window(
state = rememberWindowState(position = WindowPosition(Alignment.Center))
Enter fullscreen mode Exit fullscreen mode

(Barely) More info: https://kotlinlang.slack.com/archives/C01D6HTPATV/p1739326963488559

3. Remove title bar

Use:

fun main() = application {
    Window(
        onCloseRequest = ::exitApplication,
        title = ""
    ) {
        Box(Modifier.fillMaxSize().background(Color.Red))
        LaunchedEffect(window.rootPane) {
            with(window.rootPane) {
                putClientProperty("apple.awt.transparentTitleBar", true)
                putClientProperty("apple.awt.fullWindowContent", true)
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

More info:
https://kotlinlang.slack.com/archives/C01D6HTPATV/p1739449034315049

4. Bring window to front and into focus

If you need to bring it to the foreground and focussed

Desktop.getDesktop().requestForeground(true)
Enter fullscreen mode Exit fullscreen mode

More info:
https://github.com/JetBrains/compose-multiplatform/issues/4231#issuecomment-1952205605
and
https://kotlinlang.slack.com/archives/C01D6HTPATV/p1739152698052949

Top comments (0)