-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support draggable rows and columns, fix scroll animation
- Loading branch information
Showing
19 changed files
with
783 additions
and
74 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
87 changes: 87 additions & 0 deletions
87
foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/draggable/Draggable.kt
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.jetbrains.jewel.foundation.lazy.draggable | ||
|
||
import androidx.compose.foundation.gestures.Orientation | ||
import androidx.compose.foundation.gestures.detectDragGestures | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.graphics.graphicsLayer | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
import androidx.compose.ui.layout.onGloballyPositioned | ||
import androidx.compose.ui.layout.positionInRoot | ||
import androidx.compose.ui.modifier.modifierLocalConsumer | ||
import androidx.compose.ui.modifier.modifierLocalOf | ||
import androidx.compose.ui.modifier.modifierLocalProvider | ||
import androidx.compose.ui.zIndex | ||
|
||
internal val ModifierLocalDraggableLayoutOffset = modifierLocalOf { Offset.Zero } | ||
|
||
public fun Modifier.draggableLayout(): Modifier = composed { | ||
var offset by remember { mutableStateOf(Offset.Zero) } | ||
|
||
this.onGloballyPositioned { | ||
offset = it.positionInRoot() | ||
}.modifierLocalProvider(ModifierLocalDraggableLayoutOffset) { | ||
offset | ||
} | ||
} | ||
|
||
public fun Modifier.draggableItem( | ||
state: LazyLayoutDraggableState<*>, | ||
key: Any, | ||
draggable: Boolean = true, | ||
orientation: Orientation? = null, | ||
): Modifier { | ||
return if (draggable) { | ||
this.draggingItem(state, key, orientation).draggableItemHandle(state, key) | ||
} else { | ||
this.draggingItem(state, key, orientation) | ||
} | ||
} | ||
|
||
private fun Modifier.draggableItemHandle( | ||
state: LazyLayoutDraggableState<*>, | ||
key: Any, | ||
): Modifier = composed { | ||
var itemOffset by remember { mutableStateOf(Offset.Zero) } | ||
var layoutOffset by remember { mutableStateOf(Offset.Zero) } | ||
|
||
onGloballyPositioned { | ||
itemOffset = it.positionInRoot() | ||
}.modifierLocalConsumer { | ||
layoutOffset = ModifierLocalDraggableLayoutOffset.current | ||
}.pointerInput(Unit) { | ||
detectDragGestures(onDrag = { change, offset -> | ||
change.consume() | ||
state.onDrag(offset) | ||
}, onDragStart = { | ||
state.onDragStart(key, it + itemOffset - layoutOffset) | ||
}, onDragEnd = { | ||
state.onDragInterrupted() | ||
}, onDragCancel = { | ||
state.onDragInterrupted() | ||
}) | ||
} | ||
} | ||
|
||
private fun Modifier.draggingItem( | ||
state: LazyLayoutDraggableState<*>, | ||
key: Any, | ||
orientation: Orientation? = null, | ||
): Modifier = composed { | ||
val dragging = state.draggingItemKey == key | ||
if (dragging) { | ||
this.then( | ||
Modifier.zIndex(2f).graphicsLayer( | ||
translationX = if (orientation == Orientation.Vertical) 0f else state.draggingItemOffsetTransformX.toFloat(), | ||
translationY = if (orientation == Orientation.Horizontal) 0f else state.draggingItemOffsetTransformY.toFloat(), | ||
), | ||
) | ||
} else { | ||
this | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...src/main/kotlin/org/jetbrains/jewel/foundation/lazy/draggable/LazyLayoutDraggableState.kt
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.jetbrains.jewel.foundation.lazy.draggable | ||
|
||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.geometry.Size | ||
|
||
public abstract class LazyLayoutDraggableState<T> { | ||
|
||
public var draggingItemOffsetTransformX: Float by mutableStateOf(0f) | ||
|
||
public var draggingItemOffsetTransformY: Float by mutableStateOf(0f) | ||
|
||
public var draggingItemKey: Any? by mutableStateOf(null) | ||
|
||
public var initialOffset: Offset = Offset.Zero | ||
|
||
public var draggingOffset: Offset = Offset.Zero | ||
|
||
internal val interactionSource: MutableInteractionSource = MutableInteractionSource() | ||
|
||
public fun onDragStart(key: Any?, offset: Offset) { | ||
draggingItemKey = key | ||
initialOffset = offset | ||
} | ||
|
||
public fun onDrag(offset: Offset) { | ||
draggingItemOffsetTransformX += offset.x | ||
draggingItemOffsetTransformY += offset.y | ||
draggingOffset += offset | ||
|
||
val draggingItem = getItemWithKey(draggingItemKey ?: return) ?: return | ||
val hoverItem = getItemAt(initialOffset + draggingOffset) | ||
|
||
if (hoverItem != null && draggingItem != hoverItem) { | ||
val changedOffset = draggingItem.offset - hoverItem.offset | ||
|
||
draggingItemOffsetTransformX += changedOffset.x | ||
draggingItemOffsetTransformY += changedOffset.y | ||
|
||
moveItem(draggingItem.index, hoverItem.index) | ||
} | ||
} | ||
|
||
public fun onDragInterrupted() { | ||
draggingItemKey = null | ||
initialOffset = Offset.Zero | ||
draggingOffset = Offset.Zero | ||
draggingItemOffsetTransformX = 0f | ||
draggingItemOffsetTransformY = 0f | ||
} | ||
|
||
public abstract fun moveItem(from: Int, to: Int) | ||
|
||
public abstract fun getItemAt(offset: Offset): T? | ||
|
||
public abstract fun getItemWithKey(key: Any): T? | ||
|
||
public abstract val T.offset: Offset | ||
|
||
public abstract val T.size: Size | ||
|
||
public abstract val T.index: Int | ||
|
||
public abstract val T.key: Any? | ||
} |
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
101 changes: 101 additions & 0 deletions
101
...tion/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/table/LazyTableDraggableState.kt
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.jetbrains.jewel.foundation.lazy.table | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.geometry.Rect | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.unit.toOffset | ||
import androidx.compose.ui.unit.toSize | ||
import androidx.compose.ui.util.fastFirstOrNull | ||
import org.jetbrains.jewel.foundation.lazy.draggable.LazyLayoutDraggableState | ||
|
||
@Composable | ||
public fun rememberLazyTableRowDraggingState( | ||
tableState: LazyTableState = rememberLazyTableState(), | ||
onMove: (Int, Int) -> Unit, | ||
): LazyTableRowDraggingState { | ||
return remember(tableState, onMove) { | ||
LazyTableRowDraggingState(tableState, onMove) | ||
} | ||
} | ||
|
||
@Composable | ||
public fun rememberLazyTableColumnDraggingState( | ||
tableState: LazyTableState = rememberLazyTableState(), | ||
onMove: (Int, Int) -> Unit, | ||
): LazyTableColumnDraggingState { | ||
return remember(tableState, onMove) { | ||
LazyTableColumnDraggingState(tableState, onMove) | ||
} | ||
} | ||
|
||
public abstract class LazyTableDraggableState( | ||
public val tableState: LazyTableState, | ||
public val onMove: (Int, Int) -> Unit, | ||
) : LazyLayoutDraggableState<LazyTableItemInfo>() { | ||
|
||
override fun getItemAt(offset: Offset): LazyTableItemInfo? { | ||
return tableState.layoutInfo.pinnedColumnsInfo.fastFirstOrNull { | ||
offset in Rect(it.offset.toOffset(), it.size.toSize()) | ||
} ?: tableState.layoutInfo.pinnedRowsInfo.fastFirstOrNull { | ||
offset in Rect(it.offset.toOffset(), it.size.toSize()) | ||
} ?: tableState.layoutInfo.pinnedItemsInfo.fastFirstOrNull { | ||
offset in Rect(it.offset.toOffset(), it.size.toSize()) | ||
} | ||
} | ||
|
||
override val LazyTableItemInfo.size: Size | ||
get() = this.size.toSize() | ||
|
||
override val LazyTableItemInfo.offset: Offset | ||
get() = this.offset.toOffset() | ||
|
||
override fun moveItem(from: Int, to: Int) { | ||
onMove(from, to) | ||
} | ||
} | ||
|
||
public class LazyTableRowDraggingState( | ||
tableState: LazyTableState, | ||
onMove: (Int, Int) -> Unit, | ||
) : LazyTableDraggableState(tableState, onMove) { | ||
|
||
override val LazyTableItemInfo.index: Int | ||
get() = this.row | ||
|
||
override val LazyTableItemInfo.key: Any? | ||
get() = (this.key as Pair<Any?, Any?>?)?.second | ||
|
||
override fun getItemWithKey(key: Any): LazyTableItemInfo? { | ||
return tableState.layoutInfo.pinnedColumnsInfo.fastFirstOrNull { | ||
(it.key as Pair<Any?, Any?>?)?.second == key | ||
} ?: tableState.layoutInfo.pinnedRowsInfo.fastFirstOrNull { | ||
(it.key as Pair<Any?, Any?>?)?.second == key | ||
} ?: tableState.layoutInfo.pinnedItemsInfo.fastFirstOrNull { | ||
(it.key as Pair<Any?, Any?>?)?.second == key | ||
} | ||
} | ||
} | ||
|
||
public class LazyTableColumnDraggingState( | ||
tableState: LazyTableState, | ||
onMove: (Int, Int) -> Unit, | ||
) : LazyTableDraggableState(tableState, onMove) { | ||
|
||
override val LazyTableItemInfo.index: Int | ||
get() = this.column | ||
|
||
override val LazyTableItemInfo.key: Any? | ||
get() = (this.key as Pair<Any?, Any?>?)?.first | ||
|
||
override fun getItemWithKey(key: Any): LazyTableItemInfo? { | ||
return tableState.layoutInfo.pinnedColumnsInfo.fastFirstOrNull { | ||
(it.key as Pair<Any?, Any?>?)?.first == key | ||
} ?: tableState.layoutInfo.pinnedRowsInfo.fastFirstOrNull { | ||
(it.key as Pair<Any?, Any?>?)?.first == key | ||
} ?: tableState.layoutInfo.pinnedItemsInfo.fastFirstOrNull { | ||
(it.key as Pair<Any?, Any?>?)?.first == key | ||
} | ||
} | ||
} |
Oops, something went wrong.