Skip to content

Commit

Permalink
fix off by padding in overscroll
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin-LL committed Dec 3, 2023
1 parent bcea1f8 commit 1ec8471
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,16 @@ class ReorderableLazyListState internal constructor(
draggingItemDraggedDelta += offset

val draggingItem = draggingItemLayoutInfo ?: return
// draggingItem.offset is the distance of the start of the dragging item from the top or left of the list
// state.layoutInfo.mainAxisItemSpacing is included in the offset
// (e.g. the first item's offset is 0, despite being state.layoutInfo.mainAxisItemSpacing pixels from the top or left of the list)
val startOffset = draggingItem.offset + draggingItemOffset
val endOffset = startOffset + draggingItem.size
val (contentStartOffset, contentEndOffset) = state.layoutInfo.getContentOffset(
orientation, ignoreContentPaddingForScroll
)

if (!programmaticScroller.isScrolling) {
val endOffset = startOffset + draggingItem.size
// find a target item to swap with
val targetItem = state.layoutInfo.visibleItemsInfo.find { item ->
item.offsetMiddle in startOffset..endOffset && draggingItem.index != item.index && item.key in reorderableKeys && item.offset >= contentStartOffset && item.offset + item.size <= contentEndOffset
Expand All @@ -418,9 +421,14 @@ class ReorderableLazyListState internal constructor(
}
}

// the actual distance from the top or left of the list to the top or left of the dragging item
val startOffsetWithSpacing = startOffset + state.layoutInfo.mainAxisItemSpacing
// the distance from the top or left of the list to the center of the dragging item handle
val handleOffset = startOffsetWithSpacing + draggingItemHandleOffset

// check if the handle center is in the scroll threshold
val distanceFromStart = (startOffset + draggingItemHandleOffset) - contentStartOffset
val distanceFromEnd = contentEndOffset - (startOffset + draggingItemHandleOffset)
val distanceFromStart = handleOffset - contentStartOffset
val distanceFromEnd = contentEndOffset - handleOffset

if (distanceFromStart < scrollThreshold) {
programmaticScroller.start(
Expand Down Expand Up @@ -523,6 +531,7 @@ internal class ReorderableItemScopeImpl(
) = composed {
var handleOffset = remember { 0f }
var handleSize = remember { 0 }
val localDensity = LocalDensity.current
onGloballyPositioned {
handleOffset = when (orientation) {
Orientation.Vertical -> it.positionInRoot().y
Expand Down Expand Up @@ -611,7 +620,8 @@ fun LazyItemScope.ReorderableItem(
reorderableLazyListState,
key,
orientation,
{ itemPosition }).content(dragging)
{ itemPosition },
).content(dragging)
}

LaunchedEffect(enabled) {
Expand Down

0 comments on commit 1ec8471

Please sign in to comment.