Свернуть/развернуть окно в Jetpack Compose

Есть ли способ программно развернуть или свернуть окно с помощью Jetpack Compose Desktop?

Я думаю об этом:

      fun main() {
    application {
        Window(
            title = "My Application",
            focusable = true,
            onCloseRequest = { exitApplication() }
        ) {
            MyMaterialTheme {
                Button(onClick = { ...Minimize or Maximize... }) {
                    Text("Test")
                }
            }
        }
    }
}

2 ответа

Да, вы можете сделать это следующим образом:

      
fun main() {
    val state = rememberWindowState()

    application {
        Window(
            state = stateAddress
        ) {
            MyMaterialTheme {
                Button(onClick = { state.isMinimized = state.isMinimized.not() }) {
                    Text("Frms")
                }
            }
        }
    }
}

Строка заголовка пользовательского окна рабочего стола

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() })
        }
    }
}
Другие вопросы по тегам