Dirk Hoffmann
05/24/2021, 11:54 AMScaffold
.
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 -->)Dirk Hoffmann
05/24/2021, 11:55 AMfun 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 }
Dirk Hoffmann
05/24/2021, 11:56 AMDirk Hoffmann
05/24/2021, 12:00 PMmainContentWidthMax: 654
measured width: main: 395 (overall: 395)
Igor Demin
05/25/2021, 8:53 AMstill the surface is "eating up" all available width and heightThis is because you define width that is equal to `constraints.maxWidth`:
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
).Dirk Hoffmann
05/25/2021, 4:42 PM