gpt4 book ai didi

kotlin - 如何让 Jetpack 上的两个窗口组成桌面并从一个窗口转到另一个窗口?

转载 作者:行者123 更新时间:2023-12-05 04:29:32 26 4
gpt4 key购买 nike

例如,如何在 Jetpack Compose 桌面上制作两个窗口,并在单击按钮时从一个窗口切换到另一个窗口?

fun main() = application {
Window(
onCloseRequest = ::exitApplication,
title = "Products Manager",
state = rememberWindowState(width = 700.dp, height = 600.dp)
) {
val count = remember { mutableStateOf(0) }
MaterialTheme {
Column(Modifier.fillMaxSize(), Arrangement.spacedBy(5.dp)) {
Button(modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = {
count.value++
}) {
Text(if (count.value == 0) "Hello World" else "Clicked ${count.value}!")
}
Button(modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = {
count.value = 0
}) {
Text("Reset")
}
}
}
}
}

最佳答案

要创建多个窗口,您只需要有多个 Window可组合物。查看Open and close multiple windows例如文档部分。

要以编程方式在窗口之间切换,您可以使用 window.toFront()在应该成为最顶层的窗口上:window FrameWindowScope 有特性吗里面Window.content .

这是一个如何使用两种窗口“类型”完成的示例。您可以将类型替换为任何其他标识符。

enum class WindowTypes {
First,
Second,
}

fun main() = application {
val windowFocusRequestSharedFlow = remember { MutableSharedFlow<WindowTypes>() }

WindowTypes.values().forEach { windowType ->
key(windowType) {
Window(
title = windowType.toString(),
onCloseRequest = ::exitApplication,
) {
LaunchedEffect(Unit) {
windowFocusRequestSharedFlow
.filter { it == windowType }
.collect {
window.toFront()
}
}
val scope = rememberCoroutineScope()
Button({
scope.launch {
val windowTypeToFocus = WindowTypes.values().run {
get((indexOf(windowType) + 1) % count())
}
windowFocusRequestSharedFlow.emit(windowTypeToFocus)
}
}) {
Text("next window")
}
}
}
}
}

关于kotlin - 如何让 Jetpack 上的两个窗口组成桌面并从一个窗口转到另一个窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72297718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com