Moh*_*leh 4 desktop-application kotlin compose-desktop
例如,如何在 jetpack 上创建两个窗口组成桌面,并在单击按钮时从一个窗口转到另一个窗口?
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")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要创建多个窗口,您只需要拥有多个Window可组合项即可。例如,请查看打开和关闭多个窗口文档部分。
要以编程方式在窗口之间切换,您可以window.toFront()在应该成为最顶层的窗口上使用:windowis property available in FrameWindowScopeinside Window.content。
这是一个如何使用两种窗口“类型”来完成此操作的示例。您可以将 type 替换为任何其他标识符。
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")
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3154 次 |
| 最近记录: |