0

Is there a way to maximize or minimize programmatically the window with Jetpack Compose Desktop ?

I'm thinking about that :

fun main() {
    application {
        Window(
            title = "My Application",
            focusable = true,
            onCloseRequest = { exitApplication() }
        ) {
            MyMaterialTheme {
                Button(onClick = { ...Minimize or Maximize... }) {
                    Text("Test")
                }
            }
        }
    }
}
  • 2
    [Here](https://github.com/JetBrains/compose-jb/blob/master/tutorials/Window_API_new/README.md#changing-the-state-maximized-minimized-fullscreen-size-position-of-the-window) – bylazy Feb 27 '23 at 18:11
  • Thank you so much ! I I’m gonna write a clean response, tomorrow. – Benjamin Jalon Feb 27 '23 at 18:30

2 Answers2

1

Yes, You can do it by this:


fun main() {
    val state = rememberWindowState()

    application {
        Window(
            state = stateAddress
        ) {
            MyMaterialTheme {
                Button(onClick = { state.isMinimized = state.isMinimized.not() }) {
                    Text("Frms")
                }
            }
        }
    }
}
  • As for maximization, the source code is not explicitly written out. I recommend getting the screen size and setting it by state – Frank Miles May 14 '23 at 14:43
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney May 14 '23 at 19:07
0

enter image description here

Desktop custom window title bar

AppWindowTitleBar.kt

@Composable
fun WindowScope.AppWindowTitleBar(
    state: WindowState,
    onClose: () -> Unit,
    onMinimize: () -> Unit = { state.isMinimized = state.isMinimized.not() },
    onMaximize: () -> Unit = {
        state.placement = if (state.placement == WindowPlacement.Maximized)
            WindowPlacement.Floating else WindowPlacement.Maximized
    },
) {
    Box(Modifier.fillMaxWidth()) {
        Row(Modifier.align(Alignment.TopEnd).padding(horizontal = 8.dp)) {
            IconButton(onClick = onMinimize) { }
            val isFloating = state.placement == WindowPlacement.Floating
            IconButton(onClick = onMaximize) {
             if (isFloating) // Icon Floating or Maximized
            }
            IconButton(onClick = onClose) { }
        }
    }
}

App.kt

fun main() {
    application {
        val state = rememberWindowState(
            position = WindowPosition(Alignment.Center), size = DpSize(1280.dp, 768.dp)
        )
        PreComposeWindow(
            state = state,
            onCloseRequest = { exitApplication() },
            undecorated = true, //transparent window must be undecorated
        ) {
            App() // Main view
            // 
            AppWindowTitleBar(state, onClose = { exitApplication() })
        }
    }
}
QiXi
  • 59
  • 5