compose-multiplatform
compose-multiplatform copied to clipboard
Min/Max size window
Is it possible to limit the size of a resizable window?
Maybe this would help
fun main() {
application {
val winState = rememberWindowState(size = DpSize(500.dp,500.dp))
Window(::exitApplication,winState) {
LaunchedEffect(winState.size) {
if(winState.size.width < 400.dp)
winState.size = DpSize(400.dp,winState.size.height)
}
Text(winState.size.toString())
}
}
}
But it sometimes has a bug like this: the winState.size.width is not the real width
Update: the best practice is https://github.com/JetBrains/compose-jb/issues/2285#issuecomment-1246224606 by succlz123
Would be nice to add this to the windowState api.
Window(...){
window.minimumSize = Dimension(1080,720)
}
Give it a try.
Yes it works. but I noticed another problem, maximumSize does not work, it does not limit window scaling in any way
Window(...){ window.minimumSize = Dimension(1080,720) }
Give it a try.
On Windows 11, compose 1.4.0, kotlin 1.8.20, scale 200%.
window.minimumSize = Dimension(1080,720)
sets the window to the passed size but the window can be resized to half of the passed size
sets the window to the passed size but the window can be resized to half of the passed size
window.minimumSize
is Swing API, not compose. Therefore it's values are (system; logical) pixels, not Compose dp
.
@m-sasha
window.minimumSize
is Swing API, not compose
I am aware of that but there does not seem to be way to set minimum size in compose.
And window.minimumSize
does not seem to respect the system scale.
Hi All,
I faced the same challenge while implementing transparent and undecorated window with custom resize by dragging composable in the right bottom corner. I worked out the translation from dp to pixels out with LocalDensity
.
fun main() {
application {
val winState = rememberWindowState(size = DpSize(800.dp, 600.dp) * 1.2f)
Window(::exitApplication,winState) {
with (LocalDensity.current) {
val minSize = DpSize(800.dp, 600.dp).toSize()
window.minimumSize = java.awt.Dimension(minSize.width.toInt(), minSize.height.toInt())
}
}
}
}
Since it is a Swing API, we only need to set it once. Check this out.
Window(...) {
setMinimumSize(
width = 360.dp,
height = 640.dp,
)
}
@Composable
fun FrameWindowScope.setMinimumSize(
width: Dp = Dp.Unspecified,
height: Dp = Dp.Unspecified,
) {
val density = LocalDensity.current
LaunchedEffect(density) {
window.minimumSize = with(density) {
Dimension(width.toPx().toInt(), height.toPx().toInt())
}
}
}
@Composable
fun FrameWindowScope.setMinimumSize(size: Dp = Dp.Unspecified): Unit =
setMinimumSize(
width = size,
height = size,
)
@Composable
fun FrameWindowScope.setMinimumSize(size: DpSize = DpSize.Unspecified): Unit =
setMinimumSize(
width = size.width,
height = size.height,
)