From 8317f9df46762f750bcafecf2a76290aaff6d75d Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Mon, 16 Sep 2024 08:00:14 +0100 Subject: [PATCH 1/2] Remove unnecessary code --- .../compose/dnd/DragAndDropState.kt | 28 ++----------------- .../compose/dnd/drag/CoreDraggableItem.kt | 3 +- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt index 7aeffc4..917d135 100644 --- a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt +++ b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt @@ -119,40 +119,16 @@ class DragAndDropState( * * @param state - new state */ - internal fun addOrUpdateDraggableItem( + internal fun addDraggableItem( state: DraggableItemState ) { - val key = state.key - val oldState = draggableItemMap[key] - - if (oldState != null) { - updateDraggableItem(oldState, state) - } else { - draggableItemMap[key] = state - } + draggableItemMap[state.key] = state } internal fun removeDraggableItem(key: Any) { draggableItemMap.remove(key) } - /** - * Update [DraggableItemState] - * - * @param oldState - old state - * @param newState - new state - */ - private fun updateDraggableItem( - oldState: DraggableItemState, - newState: DraggableItemState - ) { - oldState.size = newState.size - oldState.positionInRoot = newState.positionInRoot - oldState.dropTargets = newState.dropTargets - oldState.data = newState.data - oldState.key = newState.key - } - private var dragStartPositionInRoot: Offset = Offset.Zero private var dragStartOffset: Offset = Offset.Zero diff --git a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/CoreDraggableItem.kt b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/CoreDraggableItem.kt index eef1f37..c1ce6af 100644 --- a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/CoreDraggableItem.kt +++ b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/CoreDraggableItem.kt @@ -21,7 +21,6 @@ import androidx.compose.foundation.layout.Box import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.key import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset @@ -104,7 +103,7 @@ internal fun CoreDraggableItem( } DisposableEffect(key, state, draggableItemState) { - state.addOrUpdateDraggableItem(draggableItemState) + state.addDraggableItem(draggableItemState) onDispose { state.removeDraggableItem(key) From 01986678c592c255ee69be2f8ada741cbf1e0394 Mon Sep 17 00:00:00 2001 From: MohamedRejeb Date: Mon, 16 Sep 2024 11:52:29 +0100 Subject: [PATCH 2/2] Refactor dropTarget --- .../compose/dnd/DragAndDropState.kt | 4 + .../compose/dnd/drag/DropStrategy.kt | 12 +-- .../compose/dnd/drop/DropTarget.kt | 99 +++++++++++-------- .../compose/dnd/drop/DropTargetState.kt | 16 ++- 4 files changed, 74 insertions(+), 57 deletions(-) diff --git a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt index 917d135..9468c86 100644 --- a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt +++ b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/DragAndDropState.kt @@ -87,6 +87,10 @@ class DragAndDropState( * Add or update [DropTargetState] in [dropTargetMap] */ internal fun addDropTarget(dropTargetState: DropTargetState) { + if (dropTargetMap[dropTargetState.key] == dropTargetState) { + return + } + dropTargetMap[dropTargetState.key] = dropTargetState } diff --git a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/DropStrategy.kt b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/DropStrategy.kt index 9915a0a..bc256db 100644 --- a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/DropStrategy.kt +++ b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drag/DropStrategy.kt @@ -74,11 +74,6 @@ interface DropStrategy { ): DropTargetState? = dropTargets .minByOrNull { - println("it.key: ${it.key}") - println("draggedItemTopLeft: $draggedItemTopLeft") - println("draggedItemSize: $draggedItemSize") - println("it.topLeft: ${it.topLeft}") - println("it.size: ${it.size}") val p1 = Offset( x = draggedItemTopLeft.x + draggedItemSize.width / 2f, y = draggedItemTopLeft.y + draggedItemSize.height / 2f, @@ -88,12 +83,7 @@ interface DropStrategy { y = it.topLeft.y + it.size.height / 2f, ) - println("p1: $p1") - println("p2: $p2") - - MathUtils.distance2(p1 = p1, p2 = p2).also { - println("distance2: $it") - } + MathUtils.distance2(p1 = p1, p2 = p2) } } } diff --git a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drop/DropTarget.kt b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drop/DropTarget.kt index 7502d78..c3a1172 100644 --- a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drop/DropTarget.kt +++ b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drop/DropTarget.kt @@ -18,11 +18,13 @@ package com.mohamedrejeb.compose.dnd.drop import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Size import androidx.compose.ui.layout.LayoutCoordinates import androidx.compose.ui.layout.positionInRoot import androidx.compose.ui.node.LayoutAwareModifierNode import androidx.compose.ui.node.ModifierNodeElement import androidx.compose.ui.platform.InspectorInfo +import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.toSize import com.mohamedrejeb.compose.dnd.DragAndDropState import com.mohamedrejeb.compose.dnd.drag.DraggedItemState @@ -75,67 +77,82 @@ private data class DropTargetNodeElement( val onDragEnter: (state: DraggedItemState) -> Unit, val onDragExit: (state: DraggedItemState) -> Unit, ) : ModifierNodeElement>() { - override fun create(): DropTargetNode = DropTargetNode( - key = key, - state = state, - zIndex = zIndex, - dropAlignment = dropAlignment, - dropOffset = dropOffset, - dropAnimationEnabled = dropAnimationEnabled, - onDrop = onDrop, - onDragEnter = onDragEnter, - onDragExit = onDragExit, - ) + override fun create(): DropTargetNode = + DropTargetNode( + dropTargetState = DropTargetState( + key = key, + zIndex = zIndex, + size = Size.Zero, + topLeft = Offset.Zero, + dropAlignment = dropAlignment, + dropOffset = dropOffset, + dropAnimationEnabled = dropAnimationEnabled, + onDrop = onDrop, + onDragEnter = onDragEnter, + onDragExit = onDragExit, + ), + state = state, + ) override fun update(node: DropTargetNode) { - node.key = key - node.state = state - node.zIndex = zIndex - node.dropAlignment = dropAlignment - node.dropOffset = dropOffset - node.dropAnimationEnabled = dropAnimationEnabled - node.onDrop = onDrop - node.onDragEnter = onDragEnter - node.onDragExit = onDragExit + node.apply { + this.state = state + + val isKeyChanged = dropTargetState.key != key + + dropTargetState.key = key + dropTargetState.zIndex = zIndex + dropTargetState.dropAlignment = dropAlignment + dropTargetState.dropOffset = dropOffset + dropTargetState.dropAnimationEnabled = dropAnimationEnabled + dropTargetState.onDrop = onDrop + dropTargetState.onDragEnter = onDragEnter + dropTargetState.onDragExit = onDragExit + + if (isKeyChanged) { + onDetach() + onAttach() + } + } } override fun InspectorInfo.inspectableProperties() { name = "DropTarget" properties["key"] = key + properties["state"] = state properties["zIndex"] = zIndex properties["dropAlignment"] = dropAlignment properties["dropOffset"] = dropOffset properties["dropAnimationEnabled"] = dropAnimationEnabled + properties["onDrop"] = onDrop + properties["onDragEnter"] = onDragEnter + properties["onDragExit"] = onDragExit } } private data class DropTargetNode( - var key: Any, + val dropTargetState: DropTargetState, var state: DragAndDropState, - var zIndex: Float, - var dropAlignment: Alignment, - var dropOffset: Offset, - var dropAnimationEnabled: Boolean, - var onDrop: (state: DraggedItemState) -> Unit, - var onDragEnter: (state: DraggedItemState) -> Unit, - var onDragExit: (state: DraggedItemState) -> Unit, ) : Modifier.Node(), LayoutAwareModifierNode { + + private val key get() = dropTargetState.key + + override fun onAttach() { + state.addDropTarget(dropTargetState) + } + override fun onPlaced(coordinates: LayoutCoordinates) { + state.addDropTarget(dropTargetState) + val size = coordinates.size.toSize() val topLeft = coordinates.positionInRoot() - val dropTargetState = DropTargetState( - key = key, - zIndex = zIndex, - size = size, - topLeft = topLeft, - dropAlignment = dropAlignment, - dropOffset = dropOffset, - dropAnimationEnabled = dropAnimationEnabled, - onDrop = onDrop, - onDragEnter = onDragEnter, - onDragExit = onDragExit, - ) - state.addDropTarget(dropTargetState) + + dropTargetState.size = size + dropTargetState.topLeft = topLeft + } + + override fun onRemeasured(size: IntSize) { + dropTargetState.size = size.toSize() } override fun onReset() { diff --git a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drop/DropTargetState.kt b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drop/DropTargetState.kt index f143b8a..4601a07 100644 --- a/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drop/DropTargetState.kt +++ b/compose-dnd/src/commonMain/kotlin/com/mohamedrejeb/compose/dnd/drop/DropTargetState.kt @@ -21,8 +21,8 @@ import androidx.compose.ui.geometry.Size import com.mohamedrejeb.compose.dnd.drag.DraggedItemState class DropTargetState internal constructor( - val key: Any, - val zIndex: Float, + key: Any, + zIndex: Float, size: Size, topLeft: Offset, @@ -31,10 +31,16 @@ class DropTargetState internal constructor( internal var dropOffset: Offset, internal var dropAnimationEnabled: Boolean, - internal val onDrop: (state: DraggedItemState) -> Unit, - internal val onDragEnter: (state: DraggedItemState) -> Unit, - internal val onDragExit: (state: DraggedItemState) -> Unit, + internal var onDrop: (state: DraggedItemState) -> Unit, + internal var onDragEnter: (state: DraggedItemState) -> Unit, + internal var onDragExit: (state: DraggedItemState) -> Unit, ) { + var key: Any = key + internal set + + var zIndex: Float = zIndex + internal set + var size: Size = size internal set