Skip to content

Commit

Permalink
Lazy components revamping (#109)
Browse files Browse the repository at this point in the history
* Refactor TreeView and SelectableLazyColumn to fix selection, focus, keypress handling and performance.

* resolve #106

* resolve #81
  • Loading branch information
fscarponi authored Sep 4, 2023
1 parent 0096df0 commit 15c8798
Show file tree
Hide file tree
Showing 14 changed files with 1,080 additions and 1,139 deletions.
44 changes: 22 additions & 22 deletions core/src/main/kotlin/org/jetbrains/jewel/LazyTree.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.jetbrains.jewel

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.res.ResourceLoader
import org.jetbrains.jewel.foundation.lazy.SelectableLazyItemScope
import org.jetbrains.jewel.foundation.tree.BasicLazyTree
import org.jetbrains.jewel.foundation.tree.DefaultTreeViewKeyActions
import org.jetbrains.jewel.foundation.tree.KeyBindingScopedActions
import org.jetbrains.jewel.foundation.tree.InitialNodeStatus
import org.jetbrains.jewel.foundation.tree.KeyBindingActions
import org.jetbrains.jewel.foundation.tree.Tree
import org.jetbrains.jewel.foundation.tree.TreeElementState
import org.jetbrains.jewel.foundation.tree.TreeState
Expand All @@ -22,20 +21,22 @@ import org.jetbrains.jewel.styling.LazyTreeStyle
@Composable
fun <T> LazyTree(
tree: Tree<T>,
initialNodeStatus: InitialNodeStatus = InitialNodeStatus.Close(),
resourceLoader: ResourceLoader,
modifier: Modifier = Modifier,
onElementClick: (Tree.Element<T>) -> Unit,
treeState: TreeState = rememberTreeState(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
onElementDoubleClick: (Tree.Element<T>) -> Unit = { },
keyActions: KeyBindingScopedActions = DefaultTreeViewKeyActions(treeState),
onElementDoubleClick: (Tree.Element<T>) -> Unit = {},
onSelectionChange: (List<Tree.Element<T>>) -> Unit = {},
keyActions: KeyBindingActions = DefaultTreeViewKeyActions(treeState),
style: LazyTreeStyle = IntelliJTheme.treeStyle,
nodeContent: @Composable SelectableLazyItemScope.(Tree.Element<T>) -> Unit,
nodeContent: @Composable (SelectableLazyItemScope.(Tree.Element<T>) -> Unit),
) {
val colors = style.colors
val metrics = style.metrics
BasicLazyTree(
tree = tree,
initialNodeStatus = initialNodeStatus,
onElementClick = onElementClick,
elementBackgroundFocused = colors.elementBackgroundFocused,
elementBackgroundSelectedFocused = colors.elementBackgroundSelectedFocused,
Expand All @@ -49,26 +50,25 @@ fun <T> LazyTree(
treeState = treeState,
modifier = modifier,
onElementDoubleClick = onElementDoubleClick,
interactionSource = interactionSource,
onSelectionChange = onSelectionChange,
keyActions = keyActions,
chevronContent = { elementState ->
val painterProvider = style.icons.nodeChevron(elementState.isExpanded)
val painter by painterProvider.getPainter(elementState, resourceLoader)
Icon(painter = painter, contentDescription = null)
},
nodeContent = {
CompositionLocalProvider(
LocalContentColor provides (
style.colors.contentFor(
TreeElementState.of(
isFocused,
isSelected,
false,
),
).value
.takeOrElse { LocalContentColor.current }
) {
CompositionLocalProvider(
LocalContentColor provides (
style.colors.contentFor(
TreeElementState.of(
focused = isActive,
selected = isSelected,
expanded = false,
),
) { nodeContent(it) }
},
)
).value
.takeOrElse { LocalContentColor.current }
),
) { nodeContent(it) }
}
}
2 changes: 1 addition & 1 deletion core/src/main/kotlin/org/jetbrains/jewel/Scrollbars.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun TabStripHorizontalScrollbar(
style: ScrollbarStyle = IntelliJTheme.scrollbarStyle,
) {
val shape by remember { mutableStateOf(RoundedCornerShape(style.metrics.thumbCornerSize)) }
val hoverDurationMillis by remember { mutableStateOf(style.hoverDuration.toInt(DurationUnit.MILLISECONDS)) }
val hoverDurationMillis by remember { mutableStateOf(style.hoverDuration.inWholeMilliseconds.toInt()) }

CompositionLocalProvider(
LocalScrollbarStyle provides ComposeScrollbarStyle(
Expand Down
Loading

0 comments on commit 15c8798

Please sign in to comment.