-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compose 1.4, AGP 8.0, refactored forEachGesture with awaitEachGesture #257
base: main
Are you sure you want to change the base?
Conversation
…, refactored onDragStart detection
Looks good |
if (down != null && state.onDragStart(down.position.x.toInt(), down.position.y.toInt())) { | ||
dragStart.offset?.apply { | ||
state.onDrag(x.toInt(), y.toInt()) | ||
Modifier.onGloballyPositioned { state.layoutWindowPosition.value = it.positionInWindow()}.pointerInput(Unit) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fun Modifier.reorderable(
state: ReorderableState<*>
): Modifier = this then Modifier.onGloballyPositioned {
state.layoutWindowPosition = it.positionInWindow()
}
awaitLongPressOrCancellation(down)?.also { | ||
state.interactions.trySend(StartDrag(down.id)) | ||
} | ||
fun Modifier.detectReorder(state: ReorderableState<*>) = detect(state){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fun Modifier.detectReorder(state: ReorderableState<*>): Modifier = composed {
var itemPosition by remember { mutableStateOf(Offset.Zero) }
Modifier.onGloballyPositioned {
itemPosition = it.positionInWindow()
}.pointerInput(Unit) {
detectDragGestures(
onDragStart = { offset ->
val relativePosition = itemPosition - state.layoutWindowPosition + offset
state.onDragStart(relativePosition.x.toInt(), relativePosition.y.toInt())
},
onDrag = { change, dragAmount ->
change.consume()
state.onDrag(dragAmount.x.toInt(), dragAmount.y.toInt())
},
onDragCancel = state::onDragCanceled,
onDragEnd = state::onDragCanceled
)
}
}
awaitDragOrCancellation(it) | ||
} | ||
|
||
fun Modifier.detectReorderAfterLongPress(state: ReorderableState<*>) = detect(state) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fun Modifier.detectReorderAfterLongPress(state: ReorderableState<*>): Modifier = composed {
var itemPosition by remember { mutableStateOf(Offset.Zero) }
Modifier.onGloballyPositioned {
itemPosition = it.positionInWindow()
}.pointerInput(Unit) {
detectDragGesturesAfterLongPress(
onDragStart = { offset ->
val relativePosition = itemPosition - state.layoutWindowPosition + offset
state.onDragStart(relativePosition.x.toInt(), relativePosition.y.toInt())
},
onDrag = { change, dragAmount ->
change.consume()
state.onDrag(dragAmount.x.toInt(), dragAmount.y.toInt())
},
onDragCancel = state::onDragCanceled,
onDragEnd = state::onDragCanceled
)
}
}
You can simplify code in Modifiers using |
This PR was copied to bpappin#3 |
Updates:
Need to be tested and reviewed. Gradle 8.0 could potentionally break something.