Skip to content

Commit 51f9db0

Browse files
committed
Add new requireFirstDownUnconsumed param
1 parent 73192ff commit 51f9db0

6 files changed

Lines changed: 32 additions & 8 deletions

File tree

compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ import kotlinx.coroutines.launch
4040
* Remember [DragAndDropState]
4141
* @param dragAfterLongPress if true, drag will start after long press, otherwise drag will start after simple press
4242
* This parameter is applied to all [DraggableItem]s. If you want to change it for a specific item, use [DraggableItem] parameter.
43+
* @param requireFirstDownUnconsumed if true, the first down event should be unconsumed
4344
* @param T type of the data that is dragged
4445
* @return [DragAndDropState]
4546
* @see DragAndDropState
4647
*/
4748
@Composable
4849
fun <T> rememberDragAndDropState(
4950
dragAfterLongPress: Boolean = false,
51+
requireFirstDownUnconsumed: Boolean = false,
5052
): DragAndDropState<T> {
5153
return remember {
5254
DragAndDropState(
5355
dragAfterLongPress = dragAfterLongPress,
56+
requireFirstDownUnconsumed = requireFirstDownUnconsumed,
5457
)
5558
}
5659
}
@@ -59,11 +62,13 @@ fun <T> rememberDragAndDropState(
5962
* State of the drag and drop
6063
* @param dragAfterLongPress if true, drag will start after long press, otherwise drag will start after simple press
6164
* This parameter is applied to all [DraggableItem]s. If you want to change it for a specific item, use [DraggableItem] parameter.
65+
* @param requireFirstDownUnconsumed if true, the first down event should be unconsumed
6266
* @param T type of the data that is dragged
6367
*/
6468
@Stable
6569
class DragAndDropState<T>(
6670
internal val dragAfterLongPress: Boolean = false,
71+
internal val requireFirstDownUnconsumed: Boolean = false,
6772
) {
6873
/**
6974
* If true, drag and drop is enabled

compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/CoreDraggableItem.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ internal fun <T> CoreDraggableItem(
5555
key: Any,
5656
data: T,
5757
state: DragAndDropState<T>,
58-
enabled: Boolean = true,
59-
dragAfterLongPress: Boolean = state.dragAfterLongPress,
60-
dropTargets: List<Any> = emptyList(),
61-
dropStrategy: DropStrategy = DropStrategy.SurfacePercentage,
62-
dropAnimationSpec: AnimationSpec<Offset> = SpringSpec(),
63-
sizeDropAnimationSpec: AnimationSpec<Size> = SpringSpec(),
58+
enabled: Boolean,
59+
dragAfterLongPress: Boolean,
60+
requireFirstDownUnconsumed: Boolean,
61+
dropTargets: List<Any>,
62+
dropStrategy: DropStrategy,
63+
dropAnimationSpec: AnimationSpec<Offset>,
64+
sizeDropAnimationSpec: AnimationSpec<Size>,
6465
draggableContent: @Composable () -> Unit,
6566
content: @Composable () -> Unit,
6667
) {
@@ -118,12 +119,20 @@ internal fun <T> CoreDraggableItem(
118119
.onSizeChanged {
119120
draggableItemState.size = it.toSize()
120121
}
121-
.pointerInput(key, enabled, state, state.enabled, dragAfterLongPress) {
122+
.pointerInput(
123+
key,
124+
enabled,
125+
state,
126+
state.enabled,
127+
dragAfterLongPress,
128+
requireFirstDownUnconsumed,
129+
) {
122130
detectDragStartGesture(
123131
key = key,
124132
state = state,
125133
enabled = enabled && state.enabled,
126134
dragAfterLongPress = dragAfterLongPress,
135+
requireFirstDownUnconsumed = requireFirstDownUnconsumed,
127136
)
128137
},
129138
) {

compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/DraggableItem.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.mohamedrejeb.compose.dnd.DragAndDropState
3434
* @param state - state of the drag and drop
3535
* @param enabled - whether the drag and drop is enabled
3636
* @param dragAfterLongPress if true, drag will start after long press, otherwise drag will start after simple press
37+
* @param requireFirstDownUnconsumed if true, the first down event must be unconsumed to start the drag
3738
* @param dropTargets - list of drop targets ids to which this item can be dropped, if empty, item can be dropped to any drop target
3839
* @param dropStrategy - strategy to determine the drop target
3940
* @param dropAnimationSpec - animation spec for the position drop animation
@@ -49,6 +50,7 @@ fun <T> DraggableItem(
4950
state: DragAndDropState<T>,
5051
enabled: Boolean = true,
5152
dragAfterLongPress: Boolean = state.dragAfterLongPress,
53+
requireFirstDownUnconsumed: Boolean = state.requireFirstDownUnconsumed,
5254
dropTargets: List<Any> = emptyList(),
5355
dropStrategy: DropStrategy = DropStrategy.SurfacePercentage,
5456
dropAnimationSpec: AnimationSpec<Offset> = SpringSpec(),
@@ -76,6 +78,7 @@ fun <T> DraggableItem(
7678
state = state,
7779
enabled = enabled,
7880
dragAfterLongPress = dragAfterLongPress,
81+
requireFirstDownUnconsumed = requireFirstDownUnconsumed,
7982
dropTargets = dropTargets,
8083
dropStrategy = dropStrategy,
8184
dropAnimationSpec = dropAnimationSpec,

compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/gesture/DragGesture.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ internal suspend fun <T> PointerInputScope.detectDragStartGesture(
3131
state: DragAndDropState<T>,
3232
enabled: Boolean,
3333
dragAfterLongPress: Boolean,
34+
requireFirstDownUnconsumed: Boolean,
3435
) = coroutineScope {
3536
if (!enabled) return@coroutineScope
3637

3738
awaitEachGesture {
38-
val down = awaitFirstDown(requireUnconsumed = true, pass = PointerEventPass.Main)
39+
val down = awaitFirstDown(requireUnconsumed = requireFirstDownUnconsumed, pass = PointerEventPass.Main)
3940
var drag: PointerInputChange?
4041
if (dragAfterLongPress) {
4142
drag = awaitLongPressOrCancellation(down.id)

compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/reorder/ReorderState.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,21 @@ fun <T> rememberReorderState(
4242
* State of the reorder
4343
*
4444
* @param dragAfterLongPress if true, drag will start after long press, otherwise drag will start after simple press
45+
* @param requireFirstDownUnconsumed if true, the first down event must be unconsumed to start the drag
4546
* This parameter is applied to all [ReorderableItem]s. If you want to change it for a specific item, use [ReorderableItem] parameter.
4647
*/
4748
@Stable
4849
class ReorderState<T>(
4950
dragAfterLongPress: Boolean = false,
51+
requireFirstDownUnconsumed: Boolean = false,
5052
) {
5153
/**
5254
* State of the drag and drop
5355
*/
5456
@ExperimentalDndApi
5557
val dndState = DragAndDropState<T>(
5658
dragAfterLongPress = dragAfterLongPress,
59+
requireFirstDownUnconsumed = requireFirstDownUnconsumed,
5760
)
5861

5962
@OptIn(ExperimentalDndApi::class)

compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/reorder/ReorderableItem.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.mohamedrejeb.compose.dnd.drop.dropTarget
3838
* @param zIndex The z-index of the item.
3939
* @param enabled Whether the reorder is enabled.
4040
* @param dragAfterLongPress if true, drag will start after long press, otherwise drag will start after simple press
41+
* @param requireFirstDownUnconsumed if true, the first down event must be unconsumed to start the drag
4142
* @param dropTargets - list of drop targets ids to which this item can be dropped, if empty, item can be dropped to any drop target
4243
* @param dropStrategy - strategy to determine the drop target
4344
* @param onDrop The action to perform when an item is dropped onto the target.
@@ -61,6 +62,7 @@ fun <T> ReorderableItem(
6162
zIndex: Float = 0f,
6263
enabled: Boolean = true,
6364
dragAfterLongPress: Boolean = state.dndState.dragAfterLongPress,
65+
requireFirstDownUnconsumed: Boolean = state.dndState.requireFirstDownUnconsumed,
6466
dropTargets: List<Any> = emptyList(),
6567
dropStrategy: DropStrategy = DropStrategy.SurfacePercentage,
6668
onDrop: (state: DraggedItemState<T>) -> Unit = {},
@@ -99,6 +101,7 @@ fun <T> ReorderableItem(
99101
state = state.dndState,
100102
enabled = enabled,
101103
dragAfterLongPress = dragAfterLongPress,
104+
requireFirstDownUnconsumed = requireFirstDownUnconsumed,
102105
dropTargets = dropTargets,
103106
dropStrategy = dropStrategy,
104107
dropAnimationSpec = dropAnimationSpec,

0 commit comments

Comments
 (0)