Skip to content

Commit

Permalink
fix excessive recompositions in SelectableLazyColumn
Browse files Browse the repository at this point in the history
reference #723

closes JetBrains/intellij-community#2905

GitOrigin-RevId: c7cf1e0405ac1894c7318627381dfb39dd0e0b20
  • Loading branch information
hamen authored and intellij-monorepo-bot committed Jan 15, 2025
1 parent fd417a6 commit 9dc70b1
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ 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.FocusDirection
Expand Down Expand Up @@ -59,19 +57,20 @@ public fun SelectableLazyColumn(
content: SelectableLazyListScope.() -> Unit,
) {
val scope = rememberCoroutineScope()
val container = SelectableLazyListScopeContainer().apply(content)
val container = remember(content) { SelectableLazyListScopeContainer().apply(content) }

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

val latestOnSelectedIndexesChanged = rememberUpdatedState(onSelectedIndexesChange)
LaunchedEffect(state, container) {
snapshotFlow { state.selectedKeys }
.collect { selectedKeys ->
val indices = selectedKeys.mapNotNull { key -> container.getKeyIndex(key) }
latestOnSelectedIndexesChanged.value.invoke(indices)
}
var lastSelectedKeys by remember { mutableStateOf(state.selectedKeys) }
LaunchedEffect(state.selectedKeys, onSelectedIndexesChange, container) {
if (lastSelectedKeys == state.selectedKeys) return@LaunchedEffect

val indices = state.selectedKeys.mapNotNull { key -> container.getKeyIndex(key) }
lastSelectedKeys = state.selectedKeys
onSelectedIndexesChange(indices)
}

val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }
LazyColumn(
Expand Down

0 comments on commit 9dc70b1

Please sign in to comment.