compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

Min/Max size window

Open denchic45 opened this issue 2 years ago • 4 comments

Is it possible to limit the size of a resizable window?

denchic45 avatar Aug 31 '22 12:08 denchic45

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

Winterreisender avatar Sep 03 '22 13:09 Winterreisender

Would be nice to add this to the windowState api.

Burtan avatar Sep 04 '22 09:09 Burtan

        Window(...){
            window.minimumSize = Dimension(1080,720)
        }

Give it a try.

succlz123 avatar Sep 14 '22 04:09 succlz123

Yes it works. but I noticed another problem, maximumSize does not work, it does not limit window scaling in any way

denchic45 avatar Sep 14 '22 05:09 denchic45

        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

DuchGhast avatar May 08 '23 07:05 DuchGhast

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 avatar May 08 '23 08:05 m-sasha

@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.

DuchGhast avatar May 08 '23 08:05 DuchGhast

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

tterebko avatar Dec 31 '23 17:12 tterebko

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,
    )

Omico avatar Mar 19 '24 10:03 Omico