Skip to content

Commit

Permalink
Speed up selection in SelectableLazyColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
Walingar committed Nov 1, 2023
1 parent 374f8f3 commit 266a1a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
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.FocusRequester
Expand All @@ -23,6 +26,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.flow.collectLatest
import kotlinx.coroutines.launch
import org.jetbrains.jewel.foundation.lazy.SelectableLazyListScopeContainer.Entry
import org.jetbrains.jewel.foundation.lazy.tree.DefaultSelectableLazyColumnEventAction
Expand Down Expand Up @@ -59,15 +63,12 @@ fun SelectableLazyColumn(
}
var isFocused by remember { mutableStateOf(false) }

fun evaluateIndexes(): List<Int> {
val keyToIndexMap = keys.withIndex().associateBy({ it.value.key }, { it.index })
return state.selectedKeys
.mapNotNull { selected -> keyToIndexMap[selected] }
.sorted()
}

remember(state.selectedKeys) {
onSelectedIndexesChanged(evaluateIndexes())
val latestOnSelectedIndexesChanged = rememberUpdatedState(onSelectedIndexesChanged)
LaunchedEffect(state, container) {
snapshotFlow { state.selectedKeys }.collect { selectedKeys ->
val indices = selectedKeys.map { key -> container.getKeyIndex(key) }
latestOnSelectedIndexesChanged.value.invoke(indices)
}
}
val focusRequester = remember { FocusRequester() }
LazyColumn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ internal class SelectableLazyListScopeContainer : SelectableLazyListScope {
*/
private val nonSelectableKeys = hashSetOf<Any>()

// TODO: [performance] we can get rid of that map if indices won't be used at all in the API
private val keyToIndex = hashMapOf<Any, Int>()

private val keys = mutableListOf<SelectableLazyListKey>()
private val entries = mutableListOf<Entry>()

Expand Down Expand Up @@ -101,6 +104,10 @@ internal class SelectableLazyListScopeContainer : SelectableLazyListScope {
) : Entry
}

internal fun getKeyIndex(key: Any): Int {
return keyToIndex[key] ?: error("Cannot find index of '$key'")
}

internal fun isKeySelectable(key: Any): Boolean {
return key !in nonSelectableKeys
}
Expand All @@ -111,6 +118,7 @@ internal class SelectableLazyListScopeContainer : SelectableLazyListScope {
selectable: Boolean,
content: @Composable (SelectableLazyItemScope.() -> Unit),
) {
keyToIndex[key] = keys.size
keys.add(if (selectable) Selectable(key) else NotSelectable(key))
if (!selectable) {
nonSelectableKeys.add(key)
Expand All @@ -132,6 +140,7 @@ internal class SelectableLazyListScopeContainer : SelectableLazyListScope {
if (!isSelectable) {
nonSelectableKeys.add(currentKey)
}
keyToIndex[currentKey] = keys.size
keys.add(if (isSelectable) Selectable(currentKey) else NotSelectable(currentKey))
}
entries.add(Entry.Items(count, key, contentType, itemContent))
Expand All @@ -144,6 +153,7 @@ internal class SelectableLazyListScopeContainer : SelectableLazyListScope {
selectable: Boolean,
content: @Composable (SelectableLazyItemScope.() -> Unit),
) {
keyToIndex[key] = keys.size
keys.add(if (selectable) Selectable(key) else NotSelectable(key))
if (!selectable) {
nonSelectableKeys.add(key)
Expand Down

0 comments on commit 266a1a4

Please sign in to comment.