Would anyone know how to make Dialog and AlertDial...
# compose-desktop
c
Would anyone know how to make Dialog and AlertDialog adjust the size of the window according to the size of the content? And how to show the Dialog on the same monitor as the main window?
i
Currently only with some small "hacks":
Copy code
import androidx.compose.desktop.AppWindowAmbient
import androidx.compose.desktop.Window
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.AmbientDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.DesktopDialogProperties
import androidx.compose.ui.window.Dialog

fun main() = Window {
    Dialog(
        onDismissRequest = { },
        properties = DesktopDialogProperties(
            undecorated = true,
            size = IntSize.Zero
        ),
    ) {
        val window = AppWindowAmbient.current!!
        val density = AmbientDensity.current
        Box(
            Modifier
                .sizeIn(maxWidth = Dp.Infinity, maxHeight = Dp.Infinity)
                .onSizeChanged {
                    with(density) {
                        window.setSize(
                            it.width.toDp().value.toInt(),
                            it.height.toDp().value.toInt()
                        )
                        window.window.isResizable = false
                    }
                }
        ) {
            Text("Text", style = TextStyle(fontSize = 300.sp))
        }
    }
}
For main Window we use something similar: https://kotlinlang.slack.com/archives/C01D6HTPATV/p1605028146409100?thread_ts=1605021187.408600&cid=C01D6HTPATV
c
Thanks for answering. I'll try your hack. Do you think that this will be addressed later on or should I open an issue? By the way, this hack won't work with AlertDialog as we don't have an compose lambda where to use the hack, right?
@Igor Demin The second question was about when the main Window is moved to a secondary monitor then all the others Window or Dialog opened after that continues to show in the main monitor. Do You know if there is a solution for this or should I open an issue.
i
should I open an issue
It will be better to open an issue, so people can track a progress.
this hack won't work with AlertDialog as we don't have an compose lambda where to use the hack, right
Probably yes, it isn't working, because we don't have access to the whole content.
all the others Window or Dialog opened after that continues to show in the main monitor.
It looks like a bug. We can bypass it though:
Copy code
val parentWindow = AppWindowAmbient.current!!.window
Dialog(
    onDismissRequest = { },
) {
    onActive {
        AppWindowAmbient.current!!.window.setLocationRelativeTo(parentWindow)
    }
}
👍 1