DEV Community

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

Posted on • Edited 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

5. Customize UI stuff on Java/macOS: https://developer.apple.com/library/archive/technotes/tn2007/tn2196.html

Source: https://kotlinlang.slack.com/archives/C01D6HTPATV/p1741607313291729

6. Custom title bar

Apparently some stuff changed in later JDK versions?

See: https://gist.github.com/adrientetar/b039998a0752261d2ee7db9ade3e7c15

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please consider leaving a ❤️ or a kind comment on this post if it was useful to you!

Thanks!