1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-09-19 15:11:42 +02:00

Add onDrop event

This commit is contained in:
Allan Wang 2023-06-22 02:11:29 -07:00
parent 3a8f47d8cb
commit bdb7fa6aa8
No known key found for this signature in database
GPG Key ID: C93E3F9C679D7A56
2 changed files with 23 additions and 8 deletions

View File

@ -27,12 +27,16 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.unit.IntSize
@Composable
fun <T> rememberDraggableState(): DraggableState<T> {
return remember { DraggableStateImpl() }
typealias DraggableComposeContent = @Composable (isDragging: Boolean) -> Unit
fun interface OnDrop<T> {
fun onDrop(dragTarget: String, dragData: T, dropTarget: String)
}
typealias DraggableComposeContent = @Composable (isDragging: Boolean) -> Unit
@Composable
fun <T> rememberDraggableState(onDrop: OnDrop<T>): DraggableState<T> {
return remember(onDrop) { DraggableStateImpl(onDrop) }
}
interface DraggableState<T> {
@ -58,7 +62,7 @@ interface DraggableState<T> {
@Composable fun rememberDropTarget(key: String): DropTargetState<T>
}
class DraggableStateImpl<T> : DraggableState<T> {
class DraggableStateImpl<T>(private val onDrop: OnDrop<T>) : DraggableState<T> {
private val activeDragTargets = mutableStateMapOf<String, DragTargetState<T>>()
private val dropTargets = mutableStateMapOf<String, DropTargetState<T>>()
@ -79,9 +83,13 @@ class DraggableStateImpl<T> : DraggableState<T> {
}
override fun onDragEnd(key: String) {
activeDragTargets.remove(key)
val dragTarget = activeDragTargets.remove(key)
for ((dropKey, dropTarget) in dropTargets) {
if (dropTarget.hoverKey == key) {
if (dragTarget != null) {
onDrop.onDrop(dragTarget = key, dragData = dragTarget.data, dropTarget = dropKey)
}
setHover(dragKey = null, dropKey)
// Check other drag targets in case one meets drag requirements
checkForDrop(dropKey)

View File

@ -100,7 +100,14 @@ fun TabSelector(
unselected: List<TabData>,
onSelect: (List<TabData>) -> Unit
) {
val draggableState = rememberDraggableState<TabData>()
val draggableState =
rememberDraggableState<TabData>(
onDrop = { _, dragData, dropTarget ->
onSelect(
selected.map { if (it.key == dropTarget) dragData else it },
)
},
)
DragContainer(modifier = modifier, draggableState = draggableState) {
Column(modifier = Modifier.statusBarsPadding()) {
@ -196,7 +203,7 @@ fun DraggingTabItem(
transition.animateColor(label = "Background") {
if (it)
MaterialTheme.colorScheme.surfaceVariant.copy(
LocalRippleTheme.current.rippleAlpha().pressedAlpha
LocalRippleTheme.current.rippleAlpha().pressedAlpha,
)
else MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.85f)
}