Skip to content

Commit

Permalink
feat:TreeItemList支持隐藏header
Browse files Browse the repository at this point in the history
  • Loading branch information
Yricky committed Sep 26, 2024
1 parent 37b28b4 commit 74d5fea
Showing 1 changed file with 42 additions and 53 deletions.
95 changes: 42 additions & 53 deletions abcdecoder/src/jvmMain/kotlin/me/yricky/abcde/ui/TreeItemList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fun <T> TreeItemList(
expand: (TreeStruct.TreeNode<T>) -> Boolean,
onClick: (TreeStruct.Node<T>) -> Unit = {},
applyContent: LazyListScope.(LazyListScope.() -> Unit) -> Unit = { it() },
withTreeHeader: Boolean = true,
content: @Composable RowScope.(TreeStruct.Node<T>) -> Unit = {}
) {
val state = rememberLazyListState()
Expand Down Expand Up @@ -67,63 +68,51 @@ fun <T> TreeItemList(
}
}

val firstItemIndex by remember { derivedStateOf { state.firstVisibleItemIndex } }
val headerItems = remember { mutableStateListOf<Pair<Int, TreeStruct.Node<T>>>() }
LaunchedEffect(firstItemIndex,list){
headerItems.clear()
// var item = list.getOrNull(firstItemIndex + headerItems.size)
// while (item?.second is TreeStruct.Node && item.first >= headerItems.size){
// val headerItem = (0..(firstItemIndex + headerItems.size)).reversed().asSequence()
// .map { list.getOrNull(it) }
// .filter { it?.second is TreeStruct.TreeNode &&
// expand(it.second as TreeStruct.TreeNode<T>) &&
// ((item?.second?.isMyParent(it.second) == true) || item?.second == it.second)
// }
// .firstOrNull { it?.first == headerItems.size }
// if(headerItem != null){
// headerItems.add(headerItem)
// item = list.getOrNull(firstItemIndex + headerItems.size)
// } else break
// }
var indent = 0
var item = list.getOrNull(firstItemIndex)
while (item != null && item.first >= indent){
indent++
item = list.getOrNull(firstItemIndex + indent)
}
indent = (indent - 1).coerceAtLeast(0)
var node = list.getOrNull(firstItemIndex + indent)
if(node != null){
while(node!!.first != 0){
node = Pair(node.first - 1, node.second.parent!!)
headerItems.add(0,node)
if (withTreeHeader){
val firstItemIndex by remember { derivedStateOf { state.firstVisibleItemIndex } }
val headerItems = remember { mutableStateListOf<Pair<Int, TreeStruct.Node<T>>>() }
LaunchedEffect(firstItemIndex,list){
headerItems.clear()
var indent = 0
var item = list.getOrNull(firstItemIndex)
while (item != null && item.first >= indent){
indent++
item = list.getOrNull(firstItemIndex + indent)
}
while (headerItems.size > indent){
headerItems.removeLast()
indent = (indent - 1).coerceAtLeast(0)
var node = list.getOrNull(firstItemIndex + indent)
if(node != null){
while(node!!.first != 0){
node = Pair(node.first - 1, node.second.parent!!)
headerItems.add(0,node)
}
while (headerItems.size > indent){
headerItems.removeLast()
}
}
}


}
Column(Modifier.fillMaxWidth().padding(end = LocalScrollbarStyle.current.thickness)
.align(Alignment.TopCenter).background(MaterialTheme.colorScheme.surface)
) {
headerItems.forEach { item ->
Row(
Modifier.fillMaxWidth()
.clickable { onClick(item.second) }.drawBehind {
repeat(item.first){
drawRect(
Color.hsv(((it * 40)%360).toFloat() ,1f,0.5f),
topLeft = Offset(density.density * (it * 12 + 7),0f),
size = Size(density.density * 2,size.height)
)
}
}.padding(start = (12*item.first).dp),
verticalAlignment = Alignment.CenterVertically
) {
Image(Icons.chevronDown(), "null", modifier = Modifier.size(16.dp))
content(item.second)
}
Column(Modifier.fillMaxWidth().padding(end = LocalScrollbarStyle.current.thickness)
.align(Alignment.TopCenter).background(MaterialTheme.colorScheme.surface)
) {
headerItems.forEach { item ->
Row(
Modifier.fillMaxWidth()
.clickable { onClick(item.second) }.drawBehind {
repeat(item.first){
drawRect(
Color.hsv(((it * 40)%360).toFloat() ,1f,0.5f),
topLeft = Offset(density.density * (it * 12 + 7),0f),
size = Size(density.density * 2,size.height)
)
}
}.padding(start = (12*item.first).dp),
verticalAlignment = Alignment.CenterVertically
) {
Image(Icons.chevronDown(), "null", modifier = Modifier.size(16.dp))
content(item.second)
}
}
}
}
Expand Down

0 comments on commit 74d5fea

Please sign in to comment.