I am trying to do my own Custom Layout following t...
# compose-desktop
d
I am trying to do my own Custom Layout following the source code of
Scaffold
. On putting in only unbounded content Components (like just a single
Text()
still the surface is "eating up" all available width and height. Why is this? (fully self-contained minimal executable code in the thread -->)
Copy code
fun main() {

    Window("Jetpack Composed Desktop Sandbox", size = IntSize(600, 800)) {
        DesktopTheme {
            MaterialTheme {
                Box {
                    FramedContentMin(Modifier.border(2.dp, Color.Blue)) {
                        Text("00 Schnederpelzes Pelzig", Modifier.border(1.dp, Color.Green))
                    }
                }
            }
        }
    }
}

@Composable
fun FramedContentMin(
    modifier: Modifier = Modifier,
    mainContent: @Composable (PaddingValues) -> Unit
) {
    Surface(modifier) {
        FramedContentLayoutMin(
            mainContent = mainContent
        )
    }
}

@Composable
private fun FramedContentLayoutMin(
    mainContent: @Composable (PaddingValues) -> Unit
) {
    SubcomposeLayout { constraints ->
        val layoutWidth = constraints.maxWidth
        val layoutHeight = constraints.maxHeight

        val looseConstraints = constraints.copy(minWidth = 0, minHeight = 0)

        val topPanelHeight = 0
        val leftPanelWidth = 0
        val rightPanelWidth = 0
        val bottomPanelHeight = 0

        val middlePanelsHeight = (layoutHeight - topPanelHeight - bottomPanelHeight).coerceAtLeast(0)

        layout(layoutWidth, layoutHeight) {
            val mainContentWidthMax = (layoutWidth - leftPanelWidth - rightPanelWidth).coerceAtLeast(0)
            println("mainContentWidthMax: ${mainContentWidthMax}")
            val mainContentPlaceables = subcompose(FramedContentLayoutContent.MainContent) {
                val innerPadding = PaddingValues(start = leftPanelWidth.toDp(), bottom = bottomPanelHeight.toDp(), end = rightPanelWidth.toDp())
                mainContent(innerPadding)
            }.map { it.measure(looseConstraints.copy(maxHeight = middlePanelsHeight, maxWidth = mainContentWidthMax)) }
            val mainContentWidth = mainContentPlaceables.maxByOrNull { it.width }?.width ?: 0
            val mainContentHeight = mainContentPlaceables.maxByOrNull { it.height }?.height ?: 0
            println("measured width: main: ${mainContentWidth} (overall: ${leftPanelWidth + mainContentWidth + rightPanelWidth})")

            mainContentPlaceables.forEach {
                it.place(leftPanelWidth, topPanelHeight)
            }
        }
    }
}

private enum class FramedContentLayoutContent { TopPanel, BottomPanel, MainContent, LeftPanel, RightPanel }
message has been deleted
mainContentWidthMax: 654
measured width: main: 395 (overall: 395)
i
still the surface is "eating up" all available width and height
This is because you define width that is equal to `constraints.maxWidth`:
Copy code
val layoutWidth = constraints.maxWidth
...
layout(layoutWidth, layoutHeight)
If you need surface to wrap the width/height of the content, you should pass
mainContentWidth
there (you can use
subcompose
before
layout
).
d
ah, cool ... another learning step just got taken ... thanx!