Skip to content

Commit

Permalink
Fixes #254 - SelectableLazyColumn multi selection bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fscarponi committed Nov 26, 2023
1 parent 07dd720 commit ca37203
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public interface PointerEventActions {
key: Any,
allKeys: List<SelectableLazyListKey>,
selectableLazyListState: SelectableLazyListState,
selectionMode: SelectionMode,
)

public fun onExtendSelectionToKey(
Expand Down Expand Up @@ -83,7 +84,7 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions {

pointerEvent.keyboardModifiers.isMultiSelectionKeyPressed -> {
Log.i("ctrl pressed on click")
toggleKeySelection(key, allKeys, selectableLazyListState)
toggleKeySelection(key, allKeys, selectableLazyListState, selectionMode)
}

else -> {
Expand All @@ -99,13 +100,23 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions {
key: Any,
allKeys: List<SelectableLazyListKey>,
selectableLazyListState: SelectableLazyListState,
selectionMode: SelectionMode,
) {
if (selectableLazyListState.selectedKeys.contains(key)) {
selectableLazyListState.selectedKeys =
selectableLazyListState.selectedKeys.toMutableList().also { it.remove(key) }
} else {
selectableLazyListState.selectedKeys =
selectableLazyListState.selectedKeys.toMutableList().also { it.add(key) }
when (selectionMode) {
SelectionMode.None -> return
SelectionMode.Single -> {
selectableLazyListState.selectedKeys = listOf(key)
}

SelectionMode.Multiple -> {
if (selectableLazyListState.selectedKeys.contains(key)) {
selectableLazyListState.selectedKeys =
selectableLazyListState.selectedKeys.toMutableList().also { it.remove(key) }
} else {
selectableLazyListState.selectedKeys =
selectableLazyListState.selectedKeys.toMutableList().also { it.add(key) }
}
}
}
selectableLazyListState.lastActiveItemIndex = allKeys.indexOfFirst { it == key }
}
Expand Down Expand Up @@ -171,7 +182,7 @@ public class DefaultTreeViewPointerEventAction(
pointerEvent.keyboardModifiers.isMultiSelectionKeyPressed -> {
Log.t("multi selection pressed")
selectableLazyListState.lastKeyEventUsedMouse = false
super.toggleKeySelection(key, allKeys, selectableLazyListState)
super.toggleKeySelection(key, allKeys, selectableLazyListState, selectionMode)
}

else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import org.jetbrains.jewel.foundation.lazy.SelectableLazyColumn
import org.jetbrains.jewel.foundation.lazy.SelectionMode
import org.jetbrains.jewel.foundation.lazy.rememberSelectableLazyListState
import org.jetbrains.jewel.foundation.lazy.tree.buildTree
import org.jetbrains.jewel.foundation.theme.JewelTheme
Expand Down Expand Up @@ -70,6 +71,7 @@ fun SelectableLazyColumnSample() {
modifier = Modifier.size(200.dp, 200.dp),
) {
SelectableLazyColumn(
selectionMode = SelectionMode.Multiple,
modifier = Modifier.focusable(interactionSource = interactionSource),
state = state,
content = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ private fun LinkImpl(
.clickable(
onClick = {
linkState = linkState.copy(visited = true)
println("clicked Link")
onClick()
},
enabled = enabled,
Expand Down

0 comments on commit ca37203

Please sign in to comment.