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

Update SelectableLazyColumn for key separation and update Gradle wrapper #216

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
107 changes: 63 additions & 44 deletions foundation/api/foundation.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface SelectableColumnOnKeyEvent {
fun onSelectFirstItem(allKeys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
val firstSelectable = allKeys.withIndex().firstOrNull { it.value is Selectable }
if (firstSelectable != null) {
state.selectedKeys = listOf(firstSelectable.value.key)
state.selectedKeys = listOf(firstSelectable.value.value)
state.lastActiveItemIndex = firstSelectable.index
}
}
Expand All @@ -30,7 +30,7 @@ interface SelectableColumnOnKeyEvent {
while (iterator.hasPrevious()) {
val previous = iterator.previous()
if (previous is Selectable) {
add(previous.key)
add(previous.value)
state.lastActiveItemIndex = (iterator.previousIndex() + 1).coerceAtMost(keys.size)
}
}
Expand Down Expand Up @@ -64,7 +64,7 @@ interface SelectableColumnOnKeyEvent {
val list = mutableListOf<Any>(state.selectedKeys)
keys.subList(it, keys.lastIndex).forEachIndexed { index, selectableLazyListKey ->
if (selectableLazyListKey is Selectable) {
list.add(selectableLazyListKey.key)
list.add(selectableLazyListKey.value)
}
state.lastActiveItemIndex = index
}
Expand All @@ -85,7 +85,7 @@ interface SelectableColumnOnKeyEvent {
.reversed()
.firstOrNull { it.value is Selectable }
?.let { (index, selectableKey) ->
state.selectedKeys = listOf(selectableKey.key)
state.selectedKeys = listOf(selectableKey.value)
state.lastActiveItemIndex = index
}
}
Expand All @@ -104,7 +104,7 @@ interface SelectableColumnOnKeyEvent {
.reversed()
.firstOrNull { it.value is Selectable }
?.let { (index, selectableKey) ->
state.selectedKeys = state.selectedKeys + selectableKey.key
state.selectedKeys = state.selectedKeys + selectableKey.value
state.lastActiveItemIndex = index
}
}
Expand All @@ -121,7 +121,7 @@ interface SelectableColumnOnKeyEvent {
.dropWhile { it.index <= lastActiveIndex }
.firstOrNull { it.value is Selectable }
?.let { (index, selectableKey) ->
state.selectedKeys = listOf(selectableKey.key)
state.selectedKeys = listOf(selectableKey.value)
state.lastActiveItemIndex = index
}
}
Expand All @@ -139,7 +139,7 @@ interface SelectableColumnOnKeyEvent {
.dropWhile { it.index <= lastActiveIndex }
.firstOrNull { it.value is Selectable }
?.let { (index, selectableKey) ->
state.selectedKeys = state.selectedKeys + selectableKey.key
state.selectedKeys = state.selectedKeys + selectableKey.value
state.lastActiveItemIndex = index
}
}
Expand All @@ -151,7 +151,7 @@ interface SelectableColumnOnKeyEvent {
fun onScrollPageUpAndSelectItem(keys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
val visibleSize = state.layoutInfo.visibleItemsInfo.size
val targetIndex = max((state.lastActiveItemIndex ?: 0) - visibleSize, 0)
state.selectedKeys = listOf(keys[targetIndex].key)
state.selectedKeys = listOf(keys[targetIndex].value)
state.lastActiveItemIndex = targetIndex
}

Expand All @@ -166,7 +166,7 @@ interface SelectableColumnOnKeyEvent {
.withIndex()
.filter { it.value is Selectable }
.let {
state.selectedKeys + it.map { selectableKey -> selectableKey.value.key }
state.selectedKeys + it.map { selectableKey -> selectableKey.value.value }
}
state.selectedKeys = newSelectionList
state.lastActiveItemIndex = targetIndex
Expand All @@ -178,7 +178,7 @@ interface SelectableColumnOnKeyEvent {
fun onScrollPageDownAndSelectItem(keys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
val visibleSize = state.layoutInfo.visibleItemsInfo.size
val targetIndex = min((state.lastActiveItemIndex ?: 0) + visibleSize, keys.lastIndex)
state.selectedKeys = listOf(keys[targetIndex].key)
state.selectedKeys = listOf(keys[targetIndex].value)
state.lastActiveItemIndex = targetIndex
}

Expand All @@ -192,7 +192,7 @@ interface SelectableColumnOnKeyEvent {
keys.subList(state.lastActiveItemIndex ?: 0, targetIndex)
.filterIsInstance<Selectable>()
.let {
state.selectedKeys + it.map { selectableKey -> selectableKey.key }
state.selectedKeys + it.map { selectableKey -> selectableKey.value }
}
state.selectedKeys = newSelectionList
state.lastActiveItemIndex = targetIndex
Expand All @@ -210,7 +210,7 @@ interface SelectableColumnOnKeyEvent {
* Select All
*/
fun onSelectAll(keys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
state.selectedKeys = keys.filterIsInstance<Selectable>().map { it.key }
state.selectedKeys = keys.filterIsInstance<Selectable>().map { it.value }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.jewel.foundation.lazy

import androidx.compose.foundation.focusGroup
import androidx.compose.foundation.focusable
import androidx.compose.foundation.gestures.FlingBehavior
import androidx.compose.foundation.gestures.ScrollableDefaults
Expand Down Expand Up @@ -54,13 +55,13 @@ fun SelectableLazyColumn(
val container = SelectableLazyListScopeContainer()
.apply(content)

val keys = remember(container) {
container.getKeys()
val selectionKeys = remember(container) {
container.getSelectionKeys()
}
var isFocused by remember { mutableStateOf(false) }

fun evaluateIndexes(): List<Int> {
val keyToIndexMap = keys.withIndex().associateBy({ it.value.key }, { it.index })
val keyToIndexMap = selectionKeys.withIndex().associateBy({ it.value.value }, { it.index })
return state.selectedKeys
.mapNotNull { selected -> keyToIndexMap[selected] }
.sorted()
Expand All @@ -72,13 +73,16 @@ fun SelectableLazyColumn(
val focusRequester = remember { FocusRequester() }
LazyColumn(
modifier = modifier
.onFocusChanged { isFocused = it.hasFocus }
.onFocusChanged {
isFocused = it.isFocused || it.hasFocus
}
.focusGroup()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be any focusable elements inside SelectableLazyColumn, otherwise tab behavior will work incorrectly, so we need to check only isFocused state and don't make it focusGroup

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably I'm missing something here, the only selectable element is the LazyColumn (due SEL is a wrapper).
if you are going to remove focusable from the LC, then you will have no way to send keybindings event to the component.
BTW in most use cases, I doubt that users will not put something clickable inside the SLC, and if I'm not mind wrong, we cannot prevent that
Probably we can accept that and find a way to handle TAB key-event properly by moving in some way the focus to the next element

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I agree with @Walingar. On the other hand we need to warn the user to not use focusable or clickable modifiers on items inside the list. A solution could be to add such modifiers with all their overloads as extension members of SelectableLazyListScope and mark such modifiers as errors using annotations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how that scales. I reckon a Lint check would the right way to go, but since we don't have that, a Detekt check would be good as well.

.focusRequester(focusRequester)
.focusable(interactionSource = interactionSource)
.onPreviewKeyEvent { event ->
if (state.lastActiveItemIndex != null) {
val actionHandled = keyActions
.handleOnKeyEvent(event, keys, state, selectionMode)
.handleOnKeyEvent(event, selectionKeys, state, selectionMode)
.invoke(event)
if (actionHandled) {
scope.launch {
Expand All @@ -99,21 +103,21 @@ fun SelectableLazyColumn(
) {
container.getEntries().forEach { entry ->
when (entry) {
is Entry.Item -> item(entry.key, entry.contentType) {
is Entry.Item -> item(entry.uiKey, entry.contentType) {
val itemScope = SelectableLazyItemScope(
isSelected = entry.key in state.selectedKeys,
isSelected = entry.selectionKey in state.selectedKeys,
isActive = isFocused,
)
if (keys.any { it.key == entry.key && it is SelectableLazyListKey.Selectable }) {
if (selectionKeys.any { it.value == entry.selectionKey && it is SelectableLazyListKey.Selectable }) {
Box(
modifier = Modifier.selectable(
requester = focusRequester,
keybindings = keyActions.keybindings,
actionHandler = pointerEventActions,
selectionMode = selectionMode,
selectableState = state,
allKeys = keys,
itemKey = entry.key,
allKeys = selectionKeys,
itemKey = entry.selectionKey,
),
) {
entry.content.invoke(itemScope)
Expand All @@ -125,20 +129,21 @@ fun SelectableLazyColumn(

is Entry.Items -> items(
count = entry.count,
key = { entry.key(entry.startIndex + it) },
key = { entry.uiKey(entry.startIndex + it) },
contentType = { entry.contentType(it) },
) { index ->
val itemScope = SelectableLazyItemScope(entry.key(index) in state.selectedKeys, isFocused)
if (keys.any { it.key == entry.key(index) && it is SelectableLazyListKey.Selectable }) {
val itemScope =
SelectableLazyItemScope(entry.selectionKey(index) in state.selectedKeys, isFocused)
if (selectionKeys.any { it.value == entry.selectionKey(index) && it is SelectableLazyListKey.Selectable }) {
Box(
modifier = Modifier.selectable(
requester = focusRequester,
keybindings = keyActions.keybindings,
actionHandler = pointerEventActions,
selectionMode = selectionMode,
selectableState = state,
allKeys = keys,
itemKey = entry.key(index),
allKeys = selectionKeys,
itemKey = entry.selectionKey(index),
),
) {
entry.itemContent.invoke(itemScope, index)
Expand All @@ -148,23 +153,23 @@ fun SelectableLazyColumn(
}
}

is Entry.StickyHeader -> stickyHeader(entry.key, entry.contentType) {
val itemScope = SelectableLazyItemScope(entry.key in state.selectedKeys, isFocused)
if (keys.any { it.key == entry.key && it is SelectableLazyListKey.Selectable }) {
is Entry.StickyHeader -> stickyHeader(entry.uiKey, entry.contentType) {
val itemScope = SelectableLazyItemScope(entry.selectionKey in state.selectedKeys, isFocused)
if (selectionKeys.any { it.value == entry.selectionKey && it is SelectableLazyListKey.Selectable }) {
Box(
modifier = Modifier.selectable(
keybindings = keyActions.keybindings,
actionHandler = pointerEventActions,
selectionMode = selectionMode,
selectableState = state,
allKeys = keys,
itemKey = entry.key,
allKeys = selectionKeys,
itemKey = entry.selectionKey,
),
) {
entry.content.invoke(itemScope)
}
} else {
SelectableLazyItemScope(entry.key in state.selectedKeys, isFocused).apply {
SelectableLazyItemScope(entry.selectionKey in state.selectedKeys, isFocused).apply {
entry.content.invoke(itemScope)
}
}
Expand All @@ -182,7 +187,7 @@ private fun Modifier.selectable(
selectableState: SelectableLazyListState,
allKeys: List<SelectableLazyListKey>,
itemKey: Any,
) = this.pointerInput(allKeys, itemKey) {
) = pointerInput(allKeys, itemKey) {
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent()
Expand Down
Loading