l

    loloof64

    1 year ago
    Hi ! Is there a way to get available size somewhere in the Composable tree ? (See the comment) Because in a Composable I made, I could not rely on a Modifier, I really needed to get the size as a parameter.
    @Composable
    fun GameScreen(topLevelNavController: NavController) {
        GameScreenTheme {
            Scaffold(
                topBar = {
                    TopAppBar(
                        title = {
                            Text(
                                stringResource(id = R.string.game_page),
                            )
                        },
                        navigationIcon = {
                            IconButton(onClick = {
                                topLevelNavController.popBackStack()
                            }) {
                                Icon(Icons.Filled.ArrowBack)
                            }
                        }
                    )
                },
            ) {
                // Is there a way to get available width and height ?
            }
        }
    }
    b

    Bradleycorn

    1 year ago
    for the whole screen, or just the width and height of the composable?
    Adam Powell

    Adam Powell

    1 year ago
    There are a few ways to go about this depending on use case. What would you like to do with this information?
    More specifically, the
    WithConstraints
    composable defers composition of its contents until layout time when the sizing constraints are available, and you can compose different UI depending on the size. This is a bit expensive, but it's often the right way to do certain kinds of responsive UIs.
    l

    loloof64

    1 year ago
    @Bradleycorn Just for the nested composable, where I put the comment 😃
    @Adam Powell In fact I would like to give the same size to both width and height. But i want it to be the minimum of the available size.
    I'll try WithConstraints. In fact, I just need to compute it on device rotation.
    Adam Powell

    Adam Powell

    1 year ago
    you want to give the same size to both width and height? You might be looking for
    Modifier.aspectRatio(1f)
    then
    WithConstraints
    is way overkill for that 🙂
    l

    loloof64

    1 year ago
    Thank you. So I'll try with aspectRatio of 1. and fill size 😃
    Adam Powell

    Adam Powell

    1 year ago
    try just the aspectRatio first
    l

    loloof64

    1 year ago
    ok
    It does not work. I think because I must set a size to my Composable manually (I could not do other way) :
    {
                Column(
                    modifier = Modifier.fillMaxSize().aspectRatio(1f)
                ) {
                    StaticChessBoard(size = 300.dp)
                }
    
            }
    The StaticChessBoard had to be, for technical reasons, to be given a size manually. Because I had to compute the font size based on the board size.
    I managed using WithConstraints:
    Column(
                    modifier = Modifier.fillMaxSize().aspectRatio(1f)
                ) {
                    WithConstraints {
                        val availableWidth = with(DensityAmbient.current) {constraints.maxWidth.toDp()}
                        val availableHeight = with(DensityAmbient.current) {constraints.maxHeight.toDp()}
                        
                        val minSize = if (availableWidth < availableHeight) availableWidth else availableHeight
                        
                        StaticChessBoard(size = minSize)
                    }
                }
    Andrey Kulikov

    Andrey Kulikov

    1 year ago
    you can also just use properties like maxWidth from the WithConstraintsScope instead of calculating availableWidth/availableHeight manually
    Adam Powell

    Adam Powell

    1 year ago
    that, and I would probably push the use of
    WithConstraints
    down into the part of your code where you need to do font sizing rather than forcing
    StaticChessBoard
    to accept a size as a parameter
    which would then let you use the standard
    Modifier.aspectRatio
    for calling
    StaticChessBoard
    l

    loloof64

    1 year ago
    Yes, you're right. I'm gonna use WithConstraints only for the font computation directly inside the ChessBoard.
    Adam Powell

    Adam Powell

    1 year ago
    Try to avoid using more than one WithConstraints if you can
    l

    loloof64

    1 year ago
    Yes, I'll restrict its usage at maximum 😃