Skip to content

Commit

Permalink
Fix #254 - SelectableLazyColumn multi selection bug (#255)
Browse files Browse the repository at this point in the history
Fixes #254 - SelectableLazyColumn multi selection bug
  • Loading branch information
fscarponi authored Nov 27, 2023
1 parent a789ab1 commit 079b0b5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 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 @@ -46,6 +46,7 @@ public interface PointerEventActions {
key: Any,
allKeys: List<SelectableLazyListKey>,
selectableLazyListState: SelectableLazyListState,
selectionMode: SelectionMode,
)

public fun onExtendSelectionToKey(
Expand Down Expand Up @@ -78,7 +79,7 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions {
}

pointerEvent.keyboardModifiers.isMultiSelectionKeyPressed -> {
toggleKeySelection(key, allKeys, selectableLazyListState)
toggleKeySelection(key, allKeys, selectableLazyListState, selectionMode)
}

else -> {
Expand All @@ -93,11 +94,18 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions {
key: Any,
allKeys: List<SelectableLazyListKey>,
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 }
}
Expand Down Expand Up @@ -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)
}
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 079b0b5

Please sign in to comment.