Skip to content

Commit

Permalink
Convert SquooshListWidget.kt
Browse files Browse the repository at this point in the history
Fixes #1939
  • Loading branch information
rylin8 committed Dec 23, 2024
1 parent 78a308f commit 24da3f7
Showing 1 changed file with 51 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ import com.android.designcompose.common.NodeQuery
import com.android.designcompose.getCustomComposable
import com.android.designcompose.getScrollCallbacks
import com.android.designcompose.itemSpacingAbs
import com.android.designcompose.proto.getDim
import com.android.designcompose.proto.nodeStyle
import com.android.designcompose.proto.type
import com.android.designcompose.serdegen.GridLayoutType
import com.android.designcompose.serdegen.GridSpan
import com.android.designcompose.serdegen.ItemSpacingType
import com.android.designcompose.serdegen.ViewStyle
import com.android.designcompose.definition.layout.GridSpan
import com.android.designcompose.definition.layout.GridLayoutType
import com.android.designcompose.definition.view.ViewStyle
import com.android.designcompose.utils.pointsAsDp

// For a list widget whose layout is set to horizontal or vertical, add the widget's children into
Expand All @@ -70,10 +66,10 @@ private fun addRowColumnContent(
var count = content.count

var overflowNodeId: String? = null
if (style.nodeStyle.max_children.isPresent && style.nodeStyle.max_children.get() < count) {
count = style.nodeStyle.max_children.get()
if (style.nodeStyle.overflow_node_id.isPresent)
overflowNodeId = style.nodeStyle.overflow_node_id.get()
if (style.nodeStyle.hasMaxChildren() && style.nodeStyle.maxChildren < count) {
count = style.nodeStyle.maxChildren
if (style.nodeStyle.hasOverflowNodeId())
overflowNodeId = style.nodeStyle.overflowNodeId
}

var previousReplacementChild: SquooshResolvedNode? = null
Expand All @@ -88,8 +84,8 @@ private fun addRowColumnContent(
if (customComposable != null) {
customComposable(
Modifier,
style.nodeStyle.overflow_node_name.get(),
NodeQuery.NodeId(style.nodeStyle.overflow_node_id.get()),
style.nodeStyle.overflowNodeName,
NodeQuery.NodeId(style.nodeStyle.overflowNodeId),
listOf(), // parentComponents,
null,
)
Expand Down Expand Up @@ -128,12 +124,12 @@ private fun getSpan(
gridSpanContent.forEach { item ->
// If not looking for a variant, just find a node name match
if (nodeData.variantProperties.isEmpty()) {
if (nodeData.nodeName == item.node_name)
return LazyContentSpan(span = item.span, maxLineSpan = item.max_span)
if (nodeData.nodeName == item.nodeName)
return LazyContentSpan(span = item.span, maxLineSpan = item.maxSpan)
} else {
var spanFound: LazyContentSpan? = null
var matchesLeft = nodeData.variantProperties.size
item.node_variant.forEach {
item.nodeVariantMap.forEach {
val property = it.key.trim()
val value = it.value.trim()
val variantPropertyValue = nodeData.variantProperties[property]
Expand All @@ -146,15 +142,15 @@ private fun getSpan(
// variant properties then we are done. Otherwise, this is a possible match, and
// save it in spanFound. If we don't have any exact matches, return spanFound
if (matchesLeft == 0) {
if (nodeData.variantProperties.size == item.node_variant.size) {
if (nodeData.variantProperties.size == item.nodeVariantCount) {
val span =
if (item.max_span) LazyContentSpan(maxLineSpan = true)
if (item.maxSpan) LazyContentSpan(maxLineSpan = true)
else LazyContentSpan(span = item.span)
SpanCache.setSpan(nodeData, span)
return span
} else
spanFound =
LazyContentSpan(span = item.span, maxLineSpan = item.max_span)
LazyContentSpan(span = item.span, maxLineSpan = item.maxSpan)
}
}
}
Expand Down Expand Up @@ -183,15 +179,13 @@ private fun calculateCellsCrossAxisSizeImpl(

// Given the grid layout type and main axis size, return the number of columns/rows
private fun calculateColumnRowCount(layoutInfo: LayoutInfoGrid, gridMainAxisSize: Int): Int {
val count: Int
if (
layoutInfo.layout is GridLayoutType.FixedColumns ||
layoutInfo.layout is GridLayoutType.FixedRows
val count = if (
layoutInfo.layout == GridLayoutType.GRID_LAYOUT_TYPE_FIXED_COLUMNS ||
layoutInfo.layout == GridLayoutType.GRID_LAYOUT_TYPE_FIXED_ROWS
) {
count = layoutInfo.numColumnsRows
layoutInfo.numColumnsRows
} else {
count =
gridMainAxisSize /
gridMainAxisSize /
(layoutInfo.minColumnRowSize + itemSpacingAbs(layoutInfo.mainAxisSpacing))
}
return if (count > 0) count else 1
Expand Down Expand Up @@ -226,8 +220,7 @@ private fun addGridContent(
// for the first frame and then in another on the second frame after the main axis
// size has been set.
val showInitContent =
(gridMainAxisSize <= 0 &&
layoutInfo.mainAxisSpacing.type() is ItemSpacingType.Auto)
(gridMainAxisSize <= 0 && layoutInfo.mainAxisSpacing.hasAuto())

if (showInitContent)
items(
Expand All @@ -243,12 +236,12 @@ private fun addGridContent(
var count = lContent.count
var overflowNodeId: String? = null
if (
style.nodeStyle.max_children.isPresent &&
style.nodeStyle.max_children.get() < count
style.nodeStyle.hasMaxChildren() &&
style.nodeStyle.maxChildren < count
) {
count = style.nodeStyle.max_children.get()
if (style.nodeStyle.overflow_node_id.isPresent)
overflowNodeId = style.nodeStyle.overflow_node_id.get()
count = style.nodeStyle.maxChildren
if (style.nodeStyle.hasOverflowNodeId())
overflowNodeId = style.nodeStyle.overflowNodeId
}
items(
count,
Expand Down Expand Up @@ -282,8 +275,8 @@ private fun addGridContent(
if (customComposable != null) {
customComposable(
Modifier,
style.nodeStyle.overflow_node_name.get(),
NodeQuery.NodeId(style.nodeStyle.overflow_node_id.get()),
style.nodeStyle.overflowNodeName,
NodeQuery.NodeId(style.nodeStyle.overflowNodeId),
listOf(), // parentComps,
null,
)
Expand All @@ -305,12 +298,11 @@ private fun addGridContent(
}

if (
layoutInfo.layout is GridLayoutType.FixedColumns ||
layoutInfo.layout is GridLayoutType.AutoColumns
layoutInfo.layout == GridLayoutType.GRID_LAYOUT_TYPE_FIXED_COLUMNS ||
layoutInfo.layout == GridLayoutType.GRID_LAYOUT_TYPE_AUTO_COLUMNS
) {
val columnCount = calculateColumnRowCount(layoutInfo, gridMainAxisSize)
val horizontalSpacing =
(layoutInfo.mainAxisSpacing.type() as? ItemSpacingType.Fixed)?.value ?: 0
val horizontalSpacing = if (layoutInfo.mainAxisSpacing.hasFixed()) layoutInfo.mainAxisSpacing.fixed else 0
val verticalSpacing = layoutInfo.crossAxisSpacing
LazyVerticalGrid(
state = lazyGridState,
Expand All @@ -331,36 +323,31 @@ private fun addGridContent(
},
horizontalArrangement =
Arrangement.spacedBy(
(when (val spacing = layoutInfo.mainAxisSpacing.type()) {
is ItemSpacingType.Fixed -> spacing.value
is ItemSpacingType.Auto -> {
if (columnCount > 1)
(gridMainAxisSize -
(spacing.value.height * columnCount)) /
(columnCount - 1)
else spacing.value.width
}
else -> horizontalSpacing
})
.dp
if (layoutInfo.mainAxisSpacing.hasFixed())
layoutInfo.mainAxisSpacing.fixed.dp
else if (layoutInfo.mainAxisSpacing.hasAuto()) {
if (columnCount > 1)
((gridMainAxisSize - (layoutInfo.mainAxisSpacing.auto.height * columnCount)) / (columnCount - 1)).dp
else layoutInfo.mainAxisSpacing.auto.width.dp
}
else horizontalSpacing.dp
),
verticalArrangement = Arrangement.spacedBy(verticalSpacing.dp),
userScrollEnabled = layoutInfo.scrollingEnabled,
contentPadding =
PaddingValues(
layoutInfo.padding.start.getDim().pointsAsDp(density),
layoutInfo.padding.top.getDim().pointsAsDp(density),
layoutInfo.padding.end.getDim().pointsAsDp(density),
layoutInfo.padding.bottom.getDim().pointsAsDp(density),
layoutInfo.padding.start.pointsAsDp(density),
layoutInfo.padding.top.pointsAsDp(density),
layoutInfo.padding.end.pointsAsDp(density),
layoutInfo.padding.bottom.pointsAsDp(density),
),
) {
lazyItemContent()
}
} else {
val rowCount = calculateColumnRowCount(layoutInfo, gridMainAxisSize)
val horizontalSpacing = layoutInfo.crossAxisSpacing
val verticalSpacing =
(layoutInfo.mainAxisSpacing.type() as? ItemSpacingType.Fixed)?.value ?: 0
val verticalSpacing = if (layoutInfo.mainAxisSpacing.hasFixed()) layoutInfo.mainAxisSpacing.fixed else 0

LazyHorizontalGrid(
state = lazyGridState,
Expand All @@ -382,17 +369,13 @@ private fun addGridContent(
horizontalArrangement = Arrangement.spacedBy(horizontalSpacing.dp),
verticalArrangement =
Arrangement.spacedBy(
(when (val spacing = layoutInfo.mainAxisSpacing.type()) {
is ItemSpacingType.Fixed -> spacing.value
is ItemSpacingType.Auto -> {
if (rowCount > 1)
(gridMainAxisSize - (spacing.value.height * rowCount)) /
(rowCount - 1)
else spacing.value.width
}
else -> verticalSpacing
})
.dp
if (layoutInfo.mainAxisSpacing.hasFixed())
layoutInfo.mainAxisSpacing.fixed.dp
else if (layoutInfo.mainAxisSpacing.hasAuto()) {
if (rowCount > 1)
((gridMainAxisSize - (layoutInfo.mainAxisSpacing.auto.height * rowCount)) / (rowCount - 1)).dp
else layoutInfo.mainAxisSpacing.auto.width.dp
} else verticalSpacing.dp
),
userScrollEnabled = layoutInfo.scrollingEnabled,
) {
Expand Down

0 comments on commit 24da3f7

Please sign in to comment.