diff --git a/core/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt b/core/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt index 44e582d8e..2e50e999d 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt @@ -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 @@ -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 @@ -48,6 +50,7 @@ fun SelectableLazyColumn( interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, content: SelectableLazyListScope.() -> Unit, ) { + val scope = rememberCoroutineScope() val container = SelectableLazyListScopeContainer() .apply(content) @@ -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 }, diff --git a/core/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyListState.kt b/core/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyListState.kt index 9768b435d..4bd726ba7 100644 --- a/core/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyListState.kt +++ b/core/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyListState.kt @@ -19,6 +19,7 @@ val SelectableLazyListState.visibleItemsRange get() = firstVisibleItemIndex..firstVisibleItemIndex + layoutInfo.visibleItemsInfo.size interface SelectableScope { + var selectedKeys: List } @@ -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), @@ -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) } } }