Skip to content

Commit

Permalink
contiguous selection appearance fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
fscarponi committed Nov 21, 2023
1 parent 9265541 commit 5f941fe
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface SelectableColumnOnKeyEvent {
for (index in allKeys.indices) {
val key = allKeys[index]
if (key is Selectable) {
state.selectedKeys = listOf(key.key)
state.selectedKeys = setOf(key.key)
state.lastActiveItemIndex = index
return
}
Expand All @@ -34,7 +34,7 @@ public interface SelectableColumnOnKeyEvent {
state: SelectableLazyListState,
) {
val initialIndex = state.lastActiveItemIndex ?: return
val newSelection = ArrayList<Any>(max(initialIndex, state.selectedKeys.size)).apply {
val newSelection = LinkedHashSet<Any>(max(initialIndex, state.selectedKeys.size)).apply {
addAll(state.selectedKeys)
}
var lastActiveItemIndex = initialIndex
Expand All @@ -59,7 +59,7 @@ public interface SelectableColumnOnKeyEvent {
for (index in keys.lastIndex downTo 0) {
val key = keys[index]
if (key is Selectable) {
state.selectedKeys = listOf(key.key)
state.selectedKeys = setOf(key.key)
state.lastActiveItemIndex = index
return
}
Expand All @@ -75,7 +75,7 @@ public interface SelectableColumnOnKeyEvent {
state: SelectableLazyListState,
) {
val initialIndex = state.lastActiveItemIndex ?: return
val newSelection = ArrayList<Any>(max(keys.size - initialIndex, state.selectedKeys.size)).apply {
val newSelection = LinkedHashSet<Any>(max(keys.size - initialIndex, state.selectedKeys.size)).apply {
addAll(state.selectedKeys)
}
var lastActiveItemIndex = initialIndex
Expand All @@ -101,7 +101,7 @@ public interface SelectableColumnOnKeyEvent {
for (index in initialIndex - 1 downTo 0) {
val key = keys[index]
if (key is Selectable) {
state.selectedKeys = listOf(key.key)
state.selectedKeys = linkedSetOf(key.key)
state.lastActiveItemIndex = index
return
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public interface SelectableColumnOnKeyEvent {
for (index in initialIndex + 1..keys.lastIndex) {
val key = keys[index]
if (key is Selectable) {
state.selectedKeys = listOf(key.key)
state.selectedKeys = linkedSetOf(key.key)
state.lastActiveItemIndex = index
return
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public interface SelectableColumnOnKeyEvent {
) {
val visibleSize = state.layoutInfo.visibleItemsInfo.size
val targetIndex = max((state.lastActiveItemIndex ?: 0) - visibleSize, 0)
state.selectedKeys = listOf(keys[targetIndex].key)
state.selectedKeys = linkedSetOf(keys[targetIndex].key)
state.lastActiveItemIndex = targetIndex
}

Expand Down Expand Up @@ -206,7 +206,7 @@ public interface SelectableColumnOnKeyEvent {
) {
val visibleSize = state.layoutInfo.visibleItemsInfo.size
val targetIndex = min((state.lastActiveItemIndex ?: 0) + visibleSize, keys.lastIndex)
state.selectedKeys = listOf(keys[targetIndex].key)
state.selectedKeys = setOf(keys[targetIndex].key)
state.lastActiveItemIndex = targetIndex
}

Expand Down Expand Up @@ -240,7 +240,7 @@ public interface SelectableColumnOnKeyEvent {
* Select All.
*/
public fun onSelectAll(keys: List<SelectableLazyListKey>, state: SelectableLazyListState) {
state.selectedKeys = keys.filterIsInstance<Selectable>().map { it.key }
state.selectedKeys = keys.filterIsInstance<Selectable>().map { it.key }.toCollection(LinkedHashSet())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public val SelectableLazyListState.visibleItemsRange: IntRange

public interface SelectableScope {

public var selectedKeys: List<Any>
public var selectedKeys: Set<Any>
}

/**
Expand All @@ -36,7 +36,7 @@ public class SelectableLazyListState(

internal var lastKeyEventUsedMouse: Boolean = false

override var selectedKeys: List<Any> by mutableStateOf(emptyList<Any>())
override var selectedKeys: Set<Any> by mutableStateOf(emptySet())

public var lastActiveItemIndex: Int? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -150,8 +150,19 @@ public fun <T> BasicLazyTree(
?: false,
)

val backgroundShape by remember {
mutableStateOf(RoundedCornerShape(elementBackgroundCornerSize))
val backgroundShape by derivedStateOf {
val upperCorners = flattenedTree.getOrNull(index - 1)?.id?.let {
it !in treeState.delegate.selectedKeys
} ?: false
val bottomCorners = flattenedTree.getOrNull(index + 1)?.id?.let {
it !in treeState.delegate.selectedKeys
} ?: false
RoundedCornerShape(
topStart = if (upperCorners) elementBackgroundCornerSize else CornerSize(0.dp),
topEnd = if (upperCorners) elementBackgroundCornerSize else CornerSize(0.dp),
bottomEnd = if (bottomCorners) elementBackgroundCornerSize else CornerSize(0.dp),
bottomStart = if (bottomCorners) elementBackgroundCornerSize else CornerSize(0.dp),
)
}
Row(
verticalAlignment = Alignment.CenterVertically,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public open class DefaultTreeViewOnKeyEvent(
?.let {
state.lastActiveItemIndex =
keys.indexOfFirst { selectableKey -> selectableKey.key == parentNodeKey }
state.selectedKeys = listOf(parentNodeKey)
state.selectedKeys = setOf(parentNodeKey)
}
}
}
Expand All @@ -59,7 +59,7 @@ public open class DefaultTreeViewOnKeyEvent(
if (keys[i].key in keyNodeList) {
if (keys[i] is SelectableLazyListKey.Selectable) {
state.lastActiveItemIndex = i
state.selectedKeys = listOf(keys[i].key)
state.selectedKeys = setOf(keys[i].key)
}
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions {

else -> {
Log.i("single click")
selectableLazyListState.selectedKeys = listOf(key)
selectableLazyListState.selectedKeys = setOf(key)
selectableLazyListState.lastActiveItemIndex = allKeys.indexOfFirst { it.key == key }
}
}
Expand All @@ -100,12 +100,10 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions {
allKeys: List<SelectableLazyListKey>,
selectableLazyListState: SelectableLazyListState,
) {
if (selectableLazyListState.selectedKeys.contains(key)) {
selectableLazyListState.selectedKeys =
selectableLazyListState.selectedKeys.toMutableList().also { it.remove(key) }
selectableLazyListState.selectedKeys = if (selectableLazyListState.selectedKeys.contains(key)) {
selectableLazyListState.selectedKeys - key
} else {
selectableLazyListState.selectedKeys =
selectableLazyListState.selectedKeys.toMutableList().also { it.add(key) }
selectableLazyListState.selectedKeys + key
}
selectableLazyListState.lastActiveItemIndex = allKeys.indexOfFirst { it == key }
}
Expand All @@ -118,7 +116,7 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions {
) {
if (selectionMode == SelectionMode.None) return
if (selectionMode == SelectionMode.Single) {
state.selectedKeys = listOf(key)
state.selectedKeys = setOf(key)
} else {
val currentIndex = allKeys.indexOfFirst { it.key == key }.coerceAtLeast(0)
val lastFocussed = state.lastActiveItemIndex ?: currentIndex
Expand All @@ -139,7 +137,7 @@ public open class DefaultSelectableLazyColumnEventAction : PointerEventActions {
}
}
}
state.selectedKeys = state.selectedKeys.toMutableList().also { it.addAll(keys) }
state.selectedKeys = state.selectedKeys + keys
state.lastActiveItemIndex = allKeys.indexOfFirst { it.key == key }
}
}
Expand Down Expand Up @@ -173,9 +171,8 @@ public class DefaultTreeViewPointerEventAction(
selectableLazyListState.lastKeyEventUsedMouse = false
super.toggleKeySelection(key, allKeys, selectableLazyListState)
}

else -> {
selectableLazyListState.selectedKeys = listOf(key)
selectableLazyListState.selectedKeys = setOf(key)
}
}
}
Expand Down

0 comments on commit 5f941fe

Please sign in to comment.