Prevent Scrolling using DPAD when `userScrollEnabled=false`
When a LazyList is created with `userScrollEnabled=false`,
disable scrolling with DPAD. Scrolling using state will still work.
fixes: 277607437
Test: Updated tests
Change-Id: I38416f5643cf03c91950c948321835519382d29d
diff --git a/tv/tv-foundation/src/androidTest/java/androidx/tv/foundation/lazy/list/LazyListTest.kt b/tv/tv-foundation/src/androidTest/java/androidx/tv/foundation/lazy/list/LazyListTest.kt
index 51f87fd..afc1361 100644
--- a/tv/tv-foundation/src/androidTest/java/androidx/tv/foundation/lazy/list/LazyListTest.kt
+++ b/tv/tv-foundation/src/androidTest/java/androidx/tv/foundation/lazy/list/LazyListTest.kt
@@ -1489,15 +1489,24 @@
userScrollEnabled = false,
) {
items(5) {
- Spacer(Modifier.size(itemSize).testTag("$it"))
+ Box(
+ modifier = Modifier
+ .size(itemSize)
+ .border(2.dp, Color.Blue)
+ .testTag("$it")
+ .focusable()
+ ) {
+ BasicText("$it")
+ }
}
}
}
- rule.keyPress(1)
+ rule.onNodeWithTag("2").performSemanticsAction(SemanticsActions.RequestFocus)
+ rule.keyPress(2)
- rule.onNodeWithTag("1")
- .assertStartPositionInRootIsEqualTo(itemSize)
+ rule.onNodeWithTag("1").assertIsDisplayed()
+ rule.onNodeWithTag("3").assertIsNotDisplayed()
}
@Test
diff --git a/tv/tv-foundation/src/main/java/androidx/tv/foundation/ContentInViewModifier.kt b/tv/tv-foundation/src/main/java/androidx/tv/foundation/ContentInViewModifier.kt
index 6c22786..716e8f8 100644
--- a/tv/tv-foundation/src/main/java/androidx/tv/foundation/ContentInViewModifier.kt
+++ b/tv/tv-foundation/src/main/java/androidx/tv/foundation/ContentInViewModifier.kt
@@ -59,7 +59,8 @@
private val orientation: Orientation,
private val scrollState: ScrollableState,
private val reverseDirection: Boolean,
- private val pivotOffsets: PivotOffsets
+ private val pivotOffsets: PivotOffsets,
+ private val userScrollEnabled: Boolean
) : BringIntoViewResponder,
OnRemeasuredModifier,
OnPlacedModifier {
@@ -352,6 +353,8 @@
trailingEdgeOfItemRequestingFocus: Float,
containerSize: Float
): Float {
+ if (!userScrollEnabled) return 0f
+
val sizeOfItemRequestingFocus =
abs(trailingEdgeOfItemRequestingFocus - leadingEdgeOfItemRequestingFocus)
val childSmallerThanParent = sizeOfItemRequestingFocus <= containerSize
diff --git a/tv/tv-foundation/src/main/java/androidx/tv/foundation/ScrollableWithPivot.kt b/tv/tv-foundation/src/main/java/androidx/tv/foundation/ScrollableWithPivot.kt
index 635441e..f7f95a3 100644
--- a/tv/tv-foundation/src/main/java/androidx/tv/foundation/ScrollableWithPivot.kt
+++ b/tv/tv-foundation/src/main/java/androidx/tv/foundation/ScrollableWithPivot.kt
@@ -79,9 +79,15 @@
factory = {
val coroutineScope = rememberCoroutineScope()
val keepFocusedChildInViewModifier =
- remember(coroutineScope, orientation, state, reverseDirection, pivotOffsets) {
+ remember(coroutineScope, orientation, state, reverseDirection, pivotOffsets, enabled) {
ContentInViewModifier(
- coroutineScope, orientation, state, reverseDirection, pivotOffsets)
+ scope = coroutineScope,
+ orientation = orientation,
+ scrollState = state,
+ reverseDirection = reverseDirection,
+ pivotOffsets = pivotOffsets,
+ userScrollEnabled = enabled
+ )
}
Modifier