All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.peanuuutz.fork.ui.foundation.layout.BoxWithConstraints.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 The Android Open Source Project
 * Modifications Copyright 2022 Peanuuutz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.peanuuutz.fork.ui.foundation.layout

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import net.peanuuutz.fork.ui.ui.layout.Alignment
import net.peanuuutz.fork.ui.ui.layout.Constraints
import net.peanuuutz.fork.ui.ui.layout.SubLayout
import net.peanuuutz.fork.ui.ui.modifier.Modifier

@Composable
fun BoxWithConstraints(
    modifier: Modifier = Modifier,
    contentAlignment: Alignment = Alignment.TopLeft,
    propagateMinConstraints: Boolean = false,
    content: @Composable BoxWithConstraintsScope.() -> Unit
) {
    val measurePolicy = rememberBoxMeasurePolicy(
        contentAlignment = contentAlignment,
        propagateMinConstraints = propagateMinConstraints
    )

    SubLayout(modifier = modifier) { constraints ->
        val scope = BoxWithConstraintsScopeImpl(constraints)
        val measurables = layout(Unit) {
            scope.content()
        }
        measurePolicy.measure(measurables, constraints)
    }
}

@Immutable
sealed interface BoxWithConstraintsScope : BoxScope {
    val constraints: Constraints

    val minWidth: Int
        get() = constraints.minWidth

    val maxWidth: Int
        get() = constraints.maxWidth

    val minHeight: Int
        get() = constraints.minHeight

    val maxHeight: Int
        get() = constraints.maxHeight
}

// ======== Internal ========

private class BoxWithConstraintsScopeImpl(
    override val constraints: Constraints
) : BoxWithConstraintsScope, BoxScope by BoxScopeImpl




© 2015 - 2025 Weber Informatics LLC | Privacy Policy