如何在创建后以编程方式调整组合窗口的大小

huangapple go评论71阅读模式
英文:

How to resize a compose window programmatically after it has been created

问题

Is there a way to resize a Compose Multiplatform desktop window after it has been created?

我已经尝试通过windowState进行设置(这是有道理的,因为它说对初始值的更改不会在创建后更改或重新创建状态),以及通过window.size进行设置。
这两种方法都没有产生任何效果。

fun main() = application {
    val windowState = rememberWindowState()

    Window(
        onCloseRequest = ::exitApplication,
        state = windowState
    ) {
        windowState.size = DpSize(1000.dp, 1000.dp) // 不起作用
        window.size = Dimension(1000, 1000) // 也不起作用
    }
}
英文:

Is there a way to resize a Compose Multiplatform desktop window after it has been created?

I already tried setting it through the windowState (which makes sense that it doesn't work as it says changes to the initial values won't change or recreate the state after it has been created) and through window.size.
Both approaches lead to nothing.

fun main() = application {
    val windowState = rememberWindowState()

    Window(
        onCloseRequest = ::exitApplication,
        state = windowState
    ) {
        windowState.size = DpSize(1000.dp, 1000.dp) // not working
        window.size = Dimension(1000, 1000) // also not working
    }
}

答案1

得分: 2

不应该直接从可组合函数中更改组合状态。而是应该使用副作用 - 详细信息请查看文档

LaunchedEffect(Unit) {
    windowState.size = DpSize(1000.dp, 1000.dp)
}
英文:

You shouldn't change compose state directly from composable functions. Use side effects instead - check out documentation for more details

LaunchedEffect(Unit) {
    windowState.size = DpSize(1000.dp, 1000.dp)
}

答案2

得分: -1

我找到了设置大小在我的情况下不起作用的原因。
设置大小的调用太靠近窗口的创建。
如果在调用之前加入1000毫秒的延迟(使用LaunchedEffect或协程范围),两种方法都有效。

英文:

I found the reason why setting the size didn't work in the my case.
The calls to set the size were to close to the creation of the window.
Both methods are working if i put a delay of 1000ms before the calls (either with LaunchedEffect or with a coroutine scope).

huangapple
  • 本文由 发表于 2023年7月4日 22:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定