Skip to content

Commit

Permalink
Add offset to dragStart callback like in onDrag function
Browse files Browse the repository at this point in the history
  • Loading branch information
Walingar committed Jan 31, 2023
1 parent d231497 commit f628cba
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ sealed interface DropData {
@Composable
fun Modifier.onExternalDrag(
enabled: Boolean = true,
onDragStart: () -> Unit = {},
onDragStart: (Offset) -> Unit = {},
onDrag: (Offset) -> Unit = {},
onDragCancel: () -> Unit = {},
onDrop: (DropData) -> Unit = {},
Expand Down Expand Up @@ -163,7 +163,7 @@ private class AwtWindowDropTarget(
val isInside =
isExternalDragInsideComponent(handler.componentCoordinates, newWindowDragCoordinates)
if (isInside) {
handler.onDragStart()
handler.onDragStart(calculateOffset(handler.componentCoordinates, newWindowDragCoordinates))
}
}
windowDragCoordinates = newWindowDragCoordinates
Expand All @@ -180,7 +180,7 @@ private class AwtWindowDropTarget(
isExternalDragInsideComponent(componentCoordinates, newWindowDragCoordinates)

if (!wasDragInside && newIsDragInside) {
handler.onDragStart()
handler.onDragStart(calculateOffset(componentCoordinates, newWindowDragCoordinates))
}

if (wasDragInside && !newIsDragInside) {
Expand Down Expand Up @@ -238,7 +238,7 @@ private class AwtWindowDropTarget(
*/
fun installComponentDragHandler(
componentCoordinates: LayoutCoordinates,
onDragStart: () -> Unit,
onDragStart: (Offset) -> Unit,
onDrag: (Offset) -> Unit,
onDragCancel: () -> Unit,
onDrop: (DropData) -> Unit
Expand All @@ -248,7 +248,7 @@ private class AwtWindowDropTarget(
handlers[idsCounter] = ComponentDragHandler(componentCoordinates, onDragStart, onDrag, onDragCancel, onDrop)

if (isExternalDragInsideComponent(componentCoordinates, windowDragCoordinates)) {
onDragStart()
onDragStart(calculateOffset(componentCoordinates, windowDragCoordinates!!))
}
return idsCounter++
}
Expand Down Expand Up @@ -284,7 +284,7 @@ private class AwtWindowDropTarget(

private class ComponentDragHandler(
val componentCoordinates: LayoutCoordinates,
val onDragStart: () -> Unit,
val onDragStart: (Offset) -> Unit,
val onDrag: (Offset) -> Unit,
val onDragCancel: () -> Unit,
val onDrop: (DropData) -> Unit
Expand All @@ -301,6 +301,13 @@ private class AwtWindowDropTarget(

return componentCoordinates.boundsInWindow().contains(windowDragCoordinates)
}

private fun calculateOffset(
componentCoordinates: LayoutCoordinates,
windowDragCoordinates: Offset
): Offset {
return componentCoordinates.windowToLocal(windowDragCoordinates)
}
}
}

Expand Down

0 comments on commit f628cba

Please sign in to comment.