我有一些正好 500x500 像素的内容,我想在窗口中显示它们。
如果我通过
state = WindowState(...)
设置窗口大小,似乎会创建一个尺寸实际上稍小的窗口,因此内容会被切断。
快速测试应用程序:
fun main() = application {
Window(onCloseRequest = ::exitApplication, state = WindowState(width = 500.dp, height = 500.dp), resizable = false) {
Surface(color = Color.Black, modifier = Modifier.fillMaxSize()) {
Canvas(modifier = Modifier.fillMaxSize()) {
withTransform(transformBlock = {
scale(500.0f, 500.0f, pivot = Offset.Zero)
}) {
drawLine(color = Color.Blue, start = Offset(0.0f, 0.0f), end = Offset(1.0f, 1.0f))
drawLine(color = Color.Blue, start = Offset(1.0f, 0.0f), end = Offset(0.0f, 1.0f))
}
}
}
}
}
结果是这个外部尺寸为 486 x 493 的窗口:
如何执行与 Swing 的
pack()
等效的操作以使窗口适合内容?
绘制的窗口标题是窗口大小选定区域的一部分,可以将其关闭并使用组合元素来实现自己的标题栏
未装饰= true
简单
application {
val state = rememberWindowState(
position = WindowPosition(Alignment.Center), size = DpSize(1280.dp, 768.dp)
)
Window(
// Hide default window title
undecorated = true
)
{
AppView()
}
// You custom title bar
AppWindowTitleBar(
{ state.isMinimized = !state.isMinimized },
{ exitApplication() }
)
}
@Composable
fun WindowScope.AppWindowTitleBar(onMinimize: () -> Unit, onClose: () -> Unit) {
AppDraggableArea()
Box(Modifier.fillMaxWidth()) {
Row(Modifier.align(Alignment.TopEnd).padding(horizontal = 8.dp)) {
IconButton(onClick = onMinimize) {
// minimize button
}
IconButton(onClick = onClose) {
// close button
}
}
}
}
@Composable
private fun WindowScope.AppDraggableArea() = WindowDraggableArea {
Box(
Modifier.fillMaxWidth().height(18.dp)/*.offset(y = 2.dp)*/
.padding(horizontal = 160.dp)
.shadow(4.dp, RoundedCornerShape(4.dp, 4.dp, 12.dp, 12.dp), ambientColor = Color.White)
.background(Colors.DarkBlue)
)
}
很简单
val windowState = rememberWindowState(size = DpSize.Unspecified)
Window(onCloseRequest = ::exitApplication, state = windowState) {...}
在此处的文档中找到答案: https://github.com/JetBrains/compose-multiplatform/tree/master/tutorials/Window_API_new
fun main() = application {
val state = rememberWindowState(placement = WindowPlacement.Maximized)
Window(onCloseRequest = ::exitApplication, state) { }
}
快乐编码:)