Search code examples
android-jetpack-composedesktop-application

Jetpack Compose Desktop Change Window Title


With a Compose Desktop application the Window Title is set in the main function call. For example:

fun main() = application {
val state = rememberWindowState(
    placement = WindowPlacement.Floating,
    position = WindowPosition(Alignment.Center),
    isMinimized = false,
    width = 800.dp,
    height = 600.dp
)

Window(
    title = "Test Application",
    resizable = true,
    state = state,
    icon = painterResource("drawable/logo.png"),
    onCloseRequest = ::exitApplication
) {
    App()
}

}

I would like to add to this programatically, e.g. when the user logs in, the title would read:

Test Application - User Name

Is this possible?


Solution

  • This is done by remembering a mutable String value and set this as a title. It looks like this:

    fun main() = application {
        val state = rememberWindowState(
            placement = WindowPlacement.Floating,
            position = WindowPosition(Alignment.Center),
            isMinimized = false,
           width = 800.dp,
           height = 600.dp
        )
        var windowTitle by remember { mutableStateOf("Test Application") }
    
        Window(
            title = windowTitle,
            resizable = true,
            state = state,
            icon = painterResource("drawable/logo.png"),
            onCloseRequest = ::exitApplication
        ) {
            App()
        }
    }
    

    later you can then change the title by just calling

    windowTitle = "Test Application - $userName"