From c8b67c6fdc4fe411b0638e1eb255de3eb68cc918 Mon Sep 17 00:00:00 2001 From: Fabrizio Scarponi <36624359+fscarponi@users.noreply.github.com> Date: Tue, 9 Apr 2024 12:49:29 +0200 Subject: [PATCH] Fix TAB key event swallowing from SelectableLazyColumn key event handler (#348) --- .../jewel/foundation/lazy/SelectableLazyColumn.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt b/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt index 415a9c7b9..6f00f1e74 100644 --- a/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt +++ b/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/SelectableLazyColumn.kt @@ -20,12 +20,19 @@ import androidx.compose.runtime.setValue import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusDirection import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.focus.onFocusChanged +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEventType +import androidx.compose.ui.input.key.isShiftPressed +import androidx.compose.ui.input.key.key import androidx.compose.ui.input.key.onPreviewKeyEvent +import androidx.compose.ui.input.key.type import androidx.compose.ui.input.pointer.PointerEventType import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.unit.dp import kotlinx.coroutines.launch import org.jetbrains.jewel.foundation.lazy.SelectableLazyListScopeContainer.Entry @@ -67,12 +74,19 @@ public fun SelectableLazyColumn( latestOnSelectedIndexesChanged.value.invoke(indices) } } + val focusManager = LocalFocusManager.current val focusRequester = remember { FocusRequester() } LazyColumn( modifier = modifier.onFocusChanged { isFocused = it.hasFocus } .focusRequester(focusRequester) .focusable(interactionSource = interactionSource) .onPreviewKeyEvent { event -> + // Handle Tab key press to move focus to next item + if (event.type == KeyEventType.KeyDown && event.key == Key.Tab) { + val focusDirection = if (event.isShiftPressed) FocusDirection.Previous else FocusDirection.Next + focusManager.moveFocus(focusDirection) + return@onPreviewKeyEvent true + } if (state.lastActiveItemIndex != null) { val actionHandled = keyActions.handleOnKeyEvent(event, keys, state, selectionMode)