Skip to content

Commit

Permalink
Closes #399: Support "Hug Contents" for list widgets.
Browse files Browse the repository at this point in the history
For rows and columns, let the Row() or Column() size itself based off its children, then inform the layout manager of this size so that the parent can resize if it is an autolayout parent.

This also fixes an issue where the modifier passed into DesignView was ignored for nodes that were laid out by the layout manager.
  • Loading branch information
rylin8 committed Oct 12, 2023
1 parent aa43277 commit 799b7ae
Showing 1 changed file with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ import androidx.compose.ui.geometry.toRect
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.withSaveLayer
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.android.designcompose.serdegen.Dimension
import com.android.designcompose.serdegen.GridLayoutType
import com.android.designcompose.serdegen.GridSpan
import com.android.designcompose.serdegen.ItemSpacing
Expand Down Expand Up @@ -193,9 +195,19 @@ internal fun DesignFrame(
if (style.overflow_node_id.isPresent)
overflowNodeId = style.overflow_node_id.get()
}
val rowSizeModifier = Modifier.layoutSizeToModifier(layout)

// If the widget is set to hug contents, don't give Row() a size and let it size
// itself. Then when the size is determined, inform the layout manager. Otherwise,
// get the fixed size from the layout manager and use it in a Modifier.
val hugContents = view.style.width is Dimension.Auto
val rowModifier =
if (hugContents)
Modifier.onSizeChanged {
LayoutManager.setNodeSize(layoutId, it.width, it.height)
}
else Modifier.layoutSizeToModifier(layout)
Row(
rowSizeModifier
rowModifier
.then(layoutInfo.selfModifier)
.then(m)
.then(layoutInfo.marginModifier),
Expand Down Expand Up @@ -241,9 +253,19 @@ internal fun DesignFrame(
if (style.overflow_node_id.isPresent)
overflowNodeId = style.overflow_node_id.get()
}
val columnSizeModifier = Modifier.layoutSizeToModifier(layout)

// If the widget is set to hug contents, don't give Column() a size and let it size
// itself. Then when the size is determined, inform the layout manager. Otherwise,
// get the fixed size from the layout manager and use it in a Modifier.
val hugContents = view.style.height is Dimension.Auto
val columnModifier =
if (hugContents)
Modifier.onSizeChanged {
LayoutManager.setNodeSize(layoutId, it.width, it.height)
}
else Modifier.layoutSizeToModifier(layout)
Column(
columnSizeModifier
columnModifier
.then(layoutInfo.selfModifier)
.then(m)
.then(layoutInfo.marginModifier),
Expand Down Expand Up @@ -594,7 +616,8 @@ internal fun DesignFrame(
} else {
// Otherwise, use our custom layout to render the frame and to place its children
m = m.then(Modifier.layoutStyle(name, layoutId))
DesignFrameLayout(m, name, layoutId, layoutState) {
val frameModifier = layoutInfo.selfModifier.then(m)
DesignFrameLayout(frameModifier, name, layoutId, layoutState) {
Box(Modifier) // Need this for some reason
content()
}
Expand Down

0 comments on commit 799b7ae

Please sign in to comment.