如何在JetPack上制作两个Windows撰写桌面并从窗户到另一个窗口?

发布于 2025-01-30 05:15:19 字数 969 浏览 3 评论 0 原文

例如,当我单击按钮时,如何在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")
                }
            }
        }
    }
}

How can I make two windows on jetpack compose desktop and going from window to another when I click button for example?

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")
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2025-02-06 05:15:19

要创建多个窗口,您只需要拥有多个窗口 Composable。查看文档部分。

要以编程方式切换在Windows之间,您可以在窗口上使用 window.tofront()应该成为最高的: window frame> frame> frame> 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")
                }
            }
        }
    }
}

To create multiple windows, you simply need to have multiple Window composables. Check out Open and close multiple windows documentation section for example.

To switch between windows programmatically, you can use window.toFront() on the window that should become topmost: window is property available in FrameWindowScope inside Window.content.

Here's an example how it can be done with two window "types". You can replace type with any other identifier.

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")
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文