From 079b0b510cc918a3a2659814980175cc22188c68 Mon Sep 17 00:00:00 2001 From: Fabrizio Scarponi <36624359+fscarponi@users.noreply.github.com> Date: Mon, 27 Nov 2023 11:41:35 +0000 Subject: [PATCH] Fix #254 - SelectableLazyColumn multi selection bug (#255) Fixes #254 - SelectableLazyColumn multi selection bug --- foundation/api/foundation.api | 4 ++-- .../jewel/foundation/lazy/tree/KeyActions.kt | 21 +++++++++++++------ .../standalone/view/component/ChipsAndTree.kt | 2 ++ .../org/jetbrains/jewel/ui/component/Link.kt | 1 - 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/foundation/api/foundation.api b/foundation/api/foundation.api index 4481fe5e1..c63df0506 100644 --- a/foundation/api/foundation.api +++ b/foundation/api/foundation.api @@ -382,7 +382,7 @@ public class org/jetbrains/jewel/foundation/lazy/tree/DefaultSelectableLazyColum public fun ()V public fun handlePointerEventPress (Landroidx/compose/ui/input/pointer/PointerEvent;Lorg/jetbrains/jewel/foundation/lazy/SelectableColumnKeybindings;Lorg/jetbrains/jewel/foundation/lazy/SelectableLazyListState;Lorg/jetbrains/jewel/foundation/lazy/SelectionMode;Ljava/util/List;Ljava/lang/Object;)V public fun onExtendSelectionToKey (Ljava/lang/Object;Ljava/util/List;Lorg/jetbrains/jewel/foundation/lazy/SelectableLazyListState;Lorg/jetbrains/jewel/foundation/lazy/SelectionMode;)V - public fun toggleKeySelection (Ljava/lang/Object;Ljava/util/List;Lorg/jetbrains/jewel/foundation/lazy/SelectableLazyListState;)V + public fun toggleKeySelection (Ljava/lang/Object;Ljava/util/List;Lorg/jetbrains/jewel/foundation/lazy/SelectableLazyListState;Lorg/jetbrains/jewel/foundation/lazy/SelectionMode;)V } public class org/jetbrains/jewel/foundation/lazy/tree/DefaultSelectableLazyColumnKeyActions : org/jetbrains/jewel/foundation/lazy/tree/KeyActions { @@ -470,7 +470,7 @@ public final class org/jetbrains/jewel/foundation/lazy/tree/KeyActionsKt { public abstract interface class org/jetbrains/jewel/foundation/lazy/tree/PointerEventActions { public abstract fun handlePointerEventPress (Landroidx/compose/ui/input/pointer/PointerEvent;Lorg/jetbrains/jewel/foundation/lazy/SelectableColumnKeybindings;Lorg/jetbrains/jewel/foundation/lazy/SelectableLazyListState;Lorg/jetbrains/jewel/foundation/lazy/SelectionMode;Ljava/util/List;Ljava/lang/Object;)V public abstract fun onExtendSelectionToKey (Ljava/lang/Object;Ljava/util/List;Lorg/jetbrains/jewel/foundation/lazy/SelectableLazyListState;Lorg/jetbrains/jewel/foundation/lazy/SelectionMode;)V - public abstract fun toggleKeySelection (Ljava/lang/Object;Ljava/util/List;Lorg/jetbrains/jewel/foundation/lazy/SelectableLazyListState;)V + public abstract fun toggleKeySelection (Ljava/lang/Object;Ljava/util/List;Lorg/jetbrains/jewel/foundation/lazy/SelectableLazyListState;Lorg/jetbrains/jewel/foundation/lazy/SelectionMode;)V } public final class org/jetbrains/jewel/foundation/lazy/tree/Tree { 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 a1b0842f5..6e2ef1539 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 @@ -46,6 +46,7 @@ public interface PointerEventActions { key: Any, allKeys: List, selectableLazyListState: SelectableLazyListState, + selectionMode: SelectionMode, ) public fun onExtendSelectionToKey( @@ -78,7 +79,7 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions { } pointerEvent.keyboardModifiers.isMultiSelectionKeyPressed -> { - toggleKeySelection(key, allKeys, selectableLazyListState) + toggleKeySelection(key, allKeys, selectableLazyListState, selectionMode) } else -> { @@ -93,11 +94,18 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions { key: Any, allKeys: List, selectableLazyListState: SelectableLazyListState, + selectionMode: SelectionMode, ) { - selectableLazyListState.selectedKeys = if (selectableLazyListState.selectedKeys.contains(key)) { - selectableLazyListState.selectedKeys - key - } else { - selectableLazyListState.selectedKeys + key + when (selectionMode) { + SelectionMode.None -> return + SelectionMode.Single -> selectableLazyListState.selectedKeys = setOf(key) + SelectionMode.Multiple -> { + if (key in selectableLazyListState.selectedKeys) { + selectableLazyListState.selectedKeys -= key + } else { + selectableLazyListState.selectedKeys += key + } + } } selectableLazyListState.lastActiveItemIndex = allKeys.indexOfFirst { it == key } } @@ -161,8 +169,9 @@ public class DefaultTreeViewPointerEventAction( pointerEvent.keyboardModifiers.isMultiSelectionKeyPressed -> { selectableLazyListState.lastKeyEventUsedMouse = false - super.toggleKeySelection(key, allKeys, selectableLazyListState) + super.toggleKeySelection(key, allKeys, selectableLazyListState, selectionMode) } + else -> { selectableLazyListState.selectedKeys = setOf(key) } 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 1acd4141f..c29564e3d 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 072620e5d..ad42917db 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,