From ca3720378d394d8c862c34ff5971c6ba49cd17dd Mon Sep 17 00:00:00 2001 From: fscarponi Date: Sun, 26 Nov 2023 20:52:12 +0000 Subject: [PATCH] Fixes #254 - SelectableLazyColumn multi selection bug --- .../jewel/foundation/lazy/tree/KeyActions.kt | 27 +++++++++++++------ .../standalone/view/component/ChipsAndTree.kt | 2 ++ .../org/jetbrains/jewel/ui/component/Link.kt | 1 - 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/tree/KeyActions.kt b/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/tree/KeyActions.kt index 448bbc44e3..7e4116e4c9 100644 --- a/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/tree/KeyActions.kt +++ b/foundation/src/main/kotlin/org/jetbrains/jewel/foundation/lazy/tree/KeyActions.kt @@ -48,6 +48,7 @@ public interface PointerEventActions { key: Any, allKeys: List, selectableLazyListState: SelectableLazyListState, + selectionMode: SelectionMode, ) public fun onExtendSelectionToKey( @@ -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 -> { @@ -99,13 +100,23 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions { key: Any, allKeys: List, 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 } } @@ -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 -> { diff --git a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/ChipsAndTree.kt b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/ChipsAndTree.kt index 1acd4141f5..c29564e3d2 100644 --- a/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/ChipsAndTree.kt +++ b/samples/standalone/src/main/kotlin/org/jetbrains/jewel/samples/standalone/view/component/ChipsAndTree.kt @@ -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 @@ -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 = { diff --git a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Link.kt b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Link.kt index 072620e5d4..ad42917dbe 100644 --- a/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Link.kt +++ b/ui/src/main/kotlin/org/jetbrains/jewel/ui/component/Link.kt @@ -260,7 +260,6 @@ private fun LinkImpl( .clickable( onClick = { linkState = linkState.copy(visited = true) - println("clicked Link") onClick() }, enabled = enabled,