Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SelectableLazyList scrolling logic and event handling #179

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -22,6 +23,7 @@ import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import org.jetbrains.jewel.foundation.lazy.SelectableLazyListScopeContainer.Entry
import org.jetbrains.jewel.foundation.tree.DefaultSelectableLazyColumnEventAction
import org.jetbrains.jewel.foundation.tree.DefaultSelectableLazyColumnKeyActions
Expand All @@ -48,6 +50,7 @@ fun SelectableLazyColumn(
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: SelectableLazyListScope.() -> Unit,
) {
val scope = rememberCoroutineScope()
val container = SelectableLazyListScopeContainer()
.apply(content)

Expand All @@ -74,7 +77,16 @@ fun SelectableLazyColumn(
.focusable(interactionSource = interactionSource)
.onPreviewKeyEvent { event ->
if (state.lastActiveItemIndex != null) {
keyActions.handleOnKeyEvent(event, keys, state, selectionMode).invoke(event)
val actionHandled = keyActions
.handleOnKeyEvent(event, keys, state, selectionMode)
.invoke(event)
if (actionHandled) {
scope.launch {
state.lastActiveItemIndex?.let {
state.scrollToItem(it)
}
}
}
}
true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ val SelectableLazyListState.visibleItemsRange
get() = firstVisibleItemIndex..firstVisibleItemIndex + layoutInfo.visibleItemsInfo.size

interface SelectableScope {

var selectedKeys: List<Any>
}

Expand All @@ -41,16 +42,14 @@ class SelectableLazyListState(
* @param itemIndex The index of the item to focus on.
* @param animateScroll Whether to animate the scroll to the focused item.
* @param scrollOffset The scroll offset for the focused item.
* @param skipScroll Whether to skip the scroll to the focused item.
*/
suspend fun scrollToItem(
itemIndex: Int,
animateScroll: Boolean = false,
scrollOffset: Int = 0,
skipScroll: Boolean = false,
) {
val visibleRange = visibleItemsRange.drop(2).dropLast(4)
if (!skipScroll && itemIndex !in visibleRange && visibleRange.isNotEmpty()) {
if (itemIndex !in visibleRange && visibleRange.isNotEmpty()) {
when {
itemIndex < visibleRange.first() -> lazyListState.scrollToItem(
max(0, itemIndex - 2),
Expand All @@ -59,7 +58,7 @@ class SelectableLazyListState(
)

itemIndex > visibleRange.last() -> {
lazyListState.scrollToItem(max(itemIndex - (visibleRange.size + 1), 0), animateScroll, 0)
lazyListState.scrollToItem(max(itemIndex - (visibleRange.size + 2), 0), animateScroll, 0)
}
}
}
Expand Down