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 0e6b986
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions foundation/api/foundation.api
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public class org/jetbrains/jewel/foundation/lazy/tree/DefaultSelectableLazyColum
public fun <init> ()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 {
Expand Down Expand Up @@ -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 {
Expand Down
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 0e6b986

Please sign in to comment.