-
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
Open
VeselyJan92
wants to merge
2
commits into
aclassen:main
Choose a base branch
from
VeselyJan92:compose_gradle_upgrade
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−264
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,46 +15,44 @@ | |
*/ | ||
package org.burnoutcrew.reorderable | ||
|
||
import androidx.compose.foundation.gestures.awaitDragOrCancellation | ||
import androidx.compose.foundation.gestures.awaitEachGesture | ||
import androidx.compose.foundation.gestures.awaitFirstDown | ||
import androidx.compose.foundation.gestures.forEachGesture | ||
import androidx.compose.foundation.gestures.awaitLongPressOrCancellation | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.input.pointer.AwaitPointerEventScope | ||
import androidx.compose.ui.input.pointer.PointerId | ||
import androidx.compose.ui.input.pointer.PointerInputChange | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
import androidx.compose.ui.layout.onGloballyPositioned | ||
import androidx.compose.ui.layout.positionInWindow | ||
|
||
fun Modifier.detectReorder(state: ReorderableState<*>) = | ||
this.then( | ||
Modifier.pointerInput(Unit) { | ||
forEachGesture { | ||
awaitPointerEventScope { | ||
val down = awaitFirstDown(requireUnconsumed = false) | ||
var drag: PointerInputChange? | ||
var overSlop = Offset.Zero | ||
do { | ||
drag = awaitPointerSlopOrCancellation(down.id, down.type) { change, over -> | ||
change.consume() | ||
overSlop = over | ||
} | ||
} while (drag != null && !drag.isConsumed) | ||
if (drag != null) { | ||
state.interactions.trySend(StartDrag(down.id, overSlop)) | ||
} | ||
} | ||
} | ||
} | ||
) | ||
|
||
|
||
fun Modifier.detectReorderAfterLongPress(state: ReorderableState<*>) = | ||
this.then( | ||
Modifier.pointerInput(Unit) { | ||
forEachGesture { | ||
val down = awaitPointerEventScope { | ||
awaitFirstDown(requireUnconsumed = false) | ||
} | ||
awaitLongPressOrCancellation(down)?.also { | ||
state.interactions.trySend(StartDrag(down.id)) | ||
} | ||
fun Modifier.detectReorder(state: ReorderableState<*>) = detect(state){ | ||
awaitDragOrCancellation(it) | ||
} | ||
|
||
fun Modifier.detectReorderAfterLongPress(state: ReorderableState<*>) = detect(state) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
)
}
} |
||
awaitLongPressOrCancellation(it) | ||
} | ||
|
||
|
||
private fun Modifier.detect(state: ReorderableState<*>, detect: suspend AwaitPointerEventScope.(PointerId)->PointerInputChange?) = composed { | ||
|
||
val itemPosition = remember { mutableStateOf(Offset.Zero) } | ||
|
||
Modifier.onGloballyPositioned { itemPosition.value = it.positionInWindow() }.pointerInput(Unit) { | ||
awaitEachGesture { | ||
val down = awaitFirstDown(requireUnconsumed = false) | ||
val start = detect(down.id) | ||
|
||
if (start != null) { | ||
val relativePosition = itemPosition.value - state.layoutWindowPosition.value + start.position | ||
state.onDragStart(relativePosition.x.toInt(), relativePosition.y.toInt()) | ||
} | ||
} | ||
) | ||
} | ||
} |
165 changes: 0 additions & 165 deletions
165
reorderable/src/commonMain/kotlin/org/burnoutcrew/reorderable/DragGesture.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,69 +15,38 @@ | |
*/ | ||
package org.burnoutcrew.reorderable | ||
|
||
import androidx.compose.foundation.gestures.awaitEachGesture | ||
import androidx.compose.foundation.gestures.awaitFirstDown | ||
import androidx.compose.foundation.gestures.drag | ||
import androidx.compose.foundation.gestures.forEachGesture | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.input.pointer.PointerId | ||
import androidx.compose.ui.input.pointer.PointerInputChange | ||
import androidx.compose.ui.input.pointer.PointerInputScope | ||
import androidx.compose.ui.input.pointer.changedToUp | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
import androidx.compose.ui.input.pointer.positionChange | ||
import androidx.compose.ui.util.fastFirstOrNull | ||
import androidx.compose.ui.layout.onGloballyPositioned | ||
import androidx.compose.ui.layout.positionInWindow | ||
|
||
fun Modifier.reorderable( | ||
state: ReorderableState<*> | ||
) = then( | ||
Modifier.pointerInput(Unit) { | ||
forEachGesture { | ||
val dragStart = state.interactions.receive() | ||
val down = awaitPointerEventScope { | ||
currentEvent.changes.fastFirstOrNull { it.id == dragStart.id } | ||
} | ||
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 commentThe 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()
} |
||
awaitEachGesture { | ||
val down = awaitFirstDown(requireUnconsumed = false) | ||
|
||
val dragResult = drag(down.id) { | ||
if (state.draggingItemIndex != null){ | ||
state.onDrag(it.positionChange().x.toInt(), it.positionChange().y.toInt()) | ||
it.consume() | ||
} | ||
detectDrag( | ||
down.id, | ||
onDragEnd = { | ||
state.onDragCanceled() | ||
}, | ||
onDragCancel = { | ||
state.onDragCanceled() | ||
}, | ||
onDrag = { change, dragAmount -> | ||
change.consume() | ||
state.onDrag(dragAmount.x.toInt(), dragAmount.y.toInt()) | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
internal suspend fun PointerInputScope.detectDrag( | ||
down: PointerId, | ||
onDragEnd: () -> Unit = { }, | ||
onDragCancel: () -> Unit = { }, | ||
onDrag: (change: PointerInputChange, dragAmount: Offset) -> Unit, | ||
) { | ||
awaitPointerEventScope { | ||
if ( | ||
drag(down) { | ||
onDrag(it, it.positionChange()) | ||
it.consume() | ||
} | ||
) { | ||
// consume up if we quit drag gracefully with the up | ||
currentEvent.changes.forEach { | ||
if (it.changedToUp()) it.consume() | ||
if (dragResult) { | ||
// consume up if we quit drag gracefully with the up | ||
currentEvent.changes.forEach { | ||
if (it.changedToUp()) it.consume() | ||
} | ||
} | ||
onDragEnd() | ||
} else { | ||
onDragCancel() | ||
|
||
state.onDragCanceled() | ||
} | ||
} | ||
} | ||
|
||
internal data class StartDrag(val id: PointerId, val offset: Offset? = null) | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.