Status Update
Comments
an...@google.com <an...@google.com>
ap...@google.com <ap...@google.com> #2
Branch: androidx-main
commit b1b6e7ab35e639fa74efeecabb31e496a130cc3e
Author: Matvei Malkov <malkov@google.com>
Date: Wed May 19 19:01:23 2021
Make detectDragGestures, detectVerticalGestures and detectHorizontaGestures to consume position change automatically.
This will allow this functions to fulfill the ordering contract when the start should be called before first onDrag. Also this removed the implicit contract where we people have to consume explicitly, making this high level functions easier to use.
Fixes: 185096350
Fixes: 187320697
Test: added new
Change-Id: I42fc4a6529f73db228ae671097d10a0cda0d834b
Relnote: now detectDragGesures, detectVerticalGestures and detectHozirontalGestures will consume the position change automatically, no need to call change.consumePositionChange in the onDrag callbacks
M compose/animation/animation/integration-tests/animation-demos/src/main/java/androidx/compose/animation/demos/SpringChainDemo.kt
M compose/foundation/foundation/integration-tests/foundation-demos/src/main/java/androidx/compose/foundation/demos/SuspendingGesturesDemo.kt
M compose/foundation/foundation/samples/src/main/java/androidx/compose/foundation/samples/DragGestureDetectorSamples.kt
M compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/gestures/DragGestureDetector.kt
M compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/LongPressTextDragObserver.kt
M compose/foundation/foundation/src/test/kotlin/androidx/compose/foundation/gestures/DragGestureDetectorTest.kt
M compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/gestures/Gestures.kt
M compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/gestures/LongPressDragGestureDetectorDemo.kt
M compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/gestures/PopupDragDemo.kt
M compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/gestures/TouchSlopDragGestureDetectorDemo.kt
ch...@gmail.com <ch...@gmail.com> #3
I have been trying to implement zoomable composable in a scrollable layout like LazyColumn. When the zoomable composable is zoomed, user can drag to pan. But once the panning reaches the edge of the content, the subsequent pan will scroll the LazyColumn. Here's my code that was working prior to beta08:
@ExperimentalFoundationApi
@Composable
fun ZoomableComposable(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
val width = remember { mutableStateOf(0f) }
val height = remember { mutableStateOf(0f) }
val offsetX = remember { mutableStateOf(0f) }
val offsetY = remember { mutableStateOf(0f) }
val scale = remember { mutableStateOf(1f) }
val state = rememberTransformableState { zoomChange, offsetChange, rotationChange ->
scale.value *= zoomChange
scale.value = maxOf(1f, minOf(3f, scale.value))
if (scale.value == 1f) {
offsetX.value = 0f
offsetY.value = 0f
}
}
Box(
modifier = modifier
.clip(RectangleShape)
.onGloballyPositioned { coordinates ->
// This will be the size of the Box.
width.value = coordinates.size.width.toFloat()
height.value = coordinates.size.height.toFloat()
}
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
Log.d("detectDragGestures", "scale: ${scale.value}")
offsetX.value += dragAmount.x
offsetY.value += dragAmount.y
var consumeChange = true
if (offsetX.value <= width.value * (1f - scale.value) / 2f) {
offsetX.value = width.value * (1f - scale.value) / 2f
consumeChange = false
} else if (offsetX.value >= width.value * (scale.value - 1f) / 2f) {
offsetX.value = width.value * (scale.value - 1f) / 2f
consumeChange = false
}
if (offsetY.value <= height.value * (1f - scale.value) / 2f) {
offsetY.value = height.value * (1f - scale.value) / 2f
consumeChange = false
} else if (offsetY.value >= height.value * (scale.value - 1f) / 2f) {
offsetY.value = height.value * (scale.value - 1f) / 2f
consumeChange = false
}
if (consumeChange) {
change.consumeAllChanges()
}
}
}
.graphicsLayer(
scaleX = scale.value,
scaleY = scale.value,
translationX = offsetX.value,
translationY = offsetY.value
)
.transformable(state = state)
) {
content()
}
}
I would like to know how I can disable auto consume of the changes. Thank you.
ma...@google.com <ma...@google.com> #4
There's no way to not to consume pointer input events in the detectDragGestures
since beta08. If you need to not to consume changes, you can use lower level blocks such as awaitTouchSlop
or drag()
.
ch...@gmail.com <ch...@gmail.com> #5
Thank you for your reply. Is there any documentation or sample that I can refer to?
ma...@google.com <ma...@google.com> #6
Sure, I think the implementation of detectDragGestures
, which is ~25 LOC might be a good start:
Description
Steps to reproduce: Run the following code:
Drag anywhere in the Box.
Check the log, we can find that onDrag is always called once before onDragStart get called.
Example: