Merge "Add a `maxSpan` property to `LazyGridLayoutInfo` to provide the maximum number of spans that an item can occupy on a line." into androidx-main
diff --git a/compose/foundation/foundation/api/current.ignore b/compose/foundation/foundation/api/current.ignore
index 816f82c..dab733e 100644
--- a/compose/foundation/foundation/api/current.ignore
+++ b/compose/foundation/foundation/api/current.ignore
@@ -1,3 +1,5 @@
 // Baseline format: 1.0
 AddedAbstractMethod: androidx.compose.foundation.lazy.grid.LazyGridItemInfo#getSpan():
     Added method androidx.compose.foundation.lazy.grid.LazyGridItemInfo.getSpan()
+AddedAbstractMethod: androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo#getMaxSpan():
+    Added method androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo.getMaxSpan()
diff --git a/compose/foundation/foundation/api/current.txt b/compose/foundation/foundation/api/current.txt
index 5b61221..ea8512e 100644
--- a/compose/foundation/foundation/api/current.txt
+++ b/compose/foundation/foundation/api/current.txt
@@ -1005,6 +1005,7 @@
     method public int getAfterContentPadding();
     method public int getBeforeContentPadding();
     method public int getMainAxisItemSpacing();
+    method public int getMaxSpan();
     method public androidx.compose.foundation.gestures.Orientation getOrientation();
     method public boolean getReverseLayout();
     method public int getTotalItemsCount();
@@ -1015,6 +1016,7 @@
     property public abstract int afterContentPadding;
     property public abstract int beforeContentPadding;
     property public abstract int mainAxisItemSpacing;
+    property public abstract int maxSpan;
     property public abstract androidx.compose.foundation.gestures.Orientation orientation;
     property public abstract boolean reverseLayout;
     property public abstract int totalItemsCount;
diff --git a/compose/foundation/foundation/api/restricted_current.ignore b/compose/foundation/foundation/api/restricted_current.ignore
index 816f82c..dab733e 100644
--- a/compose/foundation/foundation/api/restricted_current.ignore
+++ b/compose/foundation/foundation/api/restricted_current.ignore
@@ -1,3 +1,5 @@
 // Baseline format: 1.0
 AddedAbstractMethod: androidx.compose.foundation.lazy.grid.LazyGridItemInfo#getSpan():
     Added method androidx.compose.foundation.lazy.grid.LazyGridItemInfo.getSpan()
+AddedAbstractMethod: androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo#getMaxSpan():
+    Added method androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo.getMaxSpan()
diff --git a/compose/foundation/foundation/api/restricted_current.txt b/compose/foundation/foundation/api/restricted_current.txt
index c5a2237..95b89c4 100644
--- a/compose/foundation/foundation/api/restricted_current.txt
+++ b/compose/foundation/foundation/api/restricted_current.txt
@@ -1007,6 +1007,7 @@
     method public int getAfterContentPadding();
     method public int getBeforeContentPadding();
     method public int getMainAxisItemSpacing();
+    method public int getMaxSpan();
     method public androidx.compose.foundation.gestures.Orientation getOrientation();
     method public boolean getReverseLayout();
     method public int getTotalItemsCount();
@@ -1017,6 +1018,7 @@
     property public abstract int afterContentPadding;
     property public abstract int beforeContentPadding;
     property public abstract int mainAxisItemSpacing;
+    property public abstract int maxSpan;
     property public abstract androidx.compose.foundation.gestures.Orientation orientation;
     property public abstract boolean reverseLayout;
     property public abstract int totalItemsCount;
diff --git a/compose/foundation/foundation/integration-tests/lazy-tests/src/androidTest/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfoTest.kt b/compose/foundation/foundation/integration-tests/lazy-tests/src/androidTest/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfoTest.kt
index 1489d04..dd3a3b8 100644
--- a/compose/foundation/foundation/integration-tests/lazy-tests/src/androidTest/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfoTest.kt
+++ b/compose/foundation/foundation/integration-tests/lazy-tests/src/androidTest/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfoTest.kt
@@ -527,6 +527,23 @@
         rule.runOnIdle { assertThat(firstItemOffset).isEqualTo(-1) }
     }
 
+    @Test
+    fun maxSpan_returnsNumberOfSlotsPerLine() {
+        val state = LazyGridState()
+        rule.setContent {
+            LazyGrid(
+                cells = 4,
+                modifier = Modifier.mainAxisSize(itemSizeDp * 4).crossAxisSize(itemSizeDp * 2),
+                state = state,
+                reverseLayout = reverseLayout,
+            ) {
+                items(8) { Box(Modifier.requiredSize(itemSizeDp)) }
+            }
+        }
+
+        rule.runOnIdle { assertThat(state.layoutInfo.maxSpan).isEqualTo(4) }
+    }
+
     fun LazyGridLayoutInfo.assertVisibleItems(
         count: Int,
         cells: Int,
diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemInfo.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemInfo.kt
index 26986e0a9..3873cf9 100644
--- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemInfo.kt
+++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemInfo.kt
@@ -62,6 +62,9 @@
     /**
      * The horizontal span of the item if it's in a [LazyVerticalGrid] or the vertical span if the
      * item is in a [LazyHorizontalGrid].
+     *
+     * Note, [LazyGridLayoutInfo.maxSpan] can be used to get the maximum number of spans in a line,
+     * e.g., to check if the item is filling the whole line.
      */
     val span: Int
 
diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo.kt
index f2a9ac7..d971eab 100644
--- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo.kt
+++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfo.kt
@@ -78,4 +78,12 @@
 
     /** The spacing between lines in the direction of scrolling. */
     val mainAxisItemSpacing: Int
+
+    /**
+     * The max line span an item can occupy. This will be the number of columns in vertical grids or
+     * the number of rows in horizontal grids.
+     *
+     * For example if [LazyVerticalGrid] has 3 columns this value will be 3 for each cell.
+     */
+    val maxSpan: Int
 }
diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridMeasureResult.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridMeasureResult.kt
index 932e6d4..f3b8ce0 100644
--- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridMeasureResult.kt
+++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridMeasureResult.kt
@@ -76,6 +76,9 @@
     override val beforeContentPadding: Int
         get() = -viewportStartOffset
 
+    override val maxSpan: Int
+        get() = slotsPerLine
+
     /**
      * Creates a new layout info with applying a scroll [delta] for this layout info. In some cases
      * we can apply small scroll deltas by just changing the offsets for each [visibleItemsInfo].