Skip to content

Instantly share code, notes, and snippets.

@landarskiy
Last active December 17, 2022 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landarskiy/863c620989fa14639ca37e69e31eeba9 to your computer and use it in GitHub Desktop.
Save landarskiy/863c620989fa14639ca37e69e31eeba9 to your computer and use it in GitHub Desktop.
@Composable
public fun GridPad(
cells: GridPadCells, modifier: Modifier = Modifier, content: GridPadScope.() -> Unit
) {
val scopeContent: GridPadScopeImpl = GridPadScopeImpl(cells).apply(content)
Layout(modifier = modifier, content = {
scopeContent.data.forEach {
it.item(GridPadItemScopeImpl)
}
}) { measurables, constraints ->
// ...
val cellPlaces = calculateCellPlaces(cells, width = constraints.maxWidth, height = constraints.maxHeight)
// list of measured children
val placeables = measurables.mapIndexed { index, measurable ->
val contentMetaInfo = scopeContent.data[index]
// calculate the actual maximum cell width in pixels based on the span size
val maxWidth = (0 until contentMetaInfo.columnSpan).sumOf {
cellPlaces[contentMetaInfo.row][contentMetaInfo.column + it].width
}
// calculate the actual maximum cell height in pixels based on the span size
val maxHeight = (0 until contentMetaInfo.rowSpan).sumOf {
cellPlaces[contentMetaInfo.row + it][contentMetaInfo.column].height
}
// measurement of children with cell boundary constraints, not the parent
measurable.measure(
constraints.copy(
minWidth = min(constraints.minWidth, maxWidth),
maxWidth = maxWidth,
minHeight = min(constraints.minHeight, maxHeight),
maxHeight = maxHeight
)
)
}
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment