this is something I've been fighting for ages, but...
# compose-desktop
t
this is something I've been fighting for ages, but is there a reliable way to have a window size to fit the contents and also update its size when the contents update?
Example which sometimes resizes but other times does not
Copy code
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.material.darkColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState

class Settings {
    val doTheThing = mutableStateOf(false)
    val reallyDoTheThing = mutableStateOf(false)
}

@Composable
fun SwitchRow(
    state: MutableState<Boolean>,
    label: String,
) {
    Row(verticalAlignment = Alignment.CenterVertically) {
        Switch(checked = state.value, onCheckedChange = { state.value = it })
        Text(text = label)
    }
}

fun main() = application {
    val settings = remember { Settings() }
    val windowState = rememberWindowState(size = DpSize.Unspecified)
    LaunchedEffect(settings.doTheThing.value) {
        windowState.size = DpSize.Unspecified
    }
    Window(
        state = windowState,
        onCloseRequest = ::exitApplication,
    ) {
        MaterialTheme(colors = darkColors()) {
            Surface {
                Column(
                    modifier = Modifier.padding(16.dp)
                ) {
                    SwitchRow(settings.doTheThing, "Do the thing")
                    if (settings.doTheThing.value) {
                        SwitchRow(settings.reallyDoTheThing, "Really do the thing")
                    }
                }
            }
        }
    }
}
throwing in a delay(10) seems to make it work but I hate that sort of solution LOL
heck even delay(1) does the job
oh it does randomly fail to resize even with the workaround too. on Windows