Float a "Me" indicator over the channel tree while our own row is off screen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 21:26:20 +00:00
co-authored by Claude Opus 5.5
parent ac01d3eacd
commit ec3a43a689
2 changed files with 93 additions and 19 deletions
@@ -1,6 +1,15 @@
package com.ts3client.android.ui
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Surface
import androidx.compose.runtime.derivedStateOf
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
@@ -84,6 +93,44 @@ private fun flatten(tree: ServerTree, collapsed: Set<Int>, currentChannel: Int):
return rows
}
private fun ChannelItem.holdsSelf(): Boolean = clients.any { it.self } || children.any { it.holdsSelf() }
/** Where the list shows our own client: its row, or the folded channel hiding it; -1 when it shows nowhere. */
private fun selfAnchor(rows: List<TreeRow>): Int = rows.indexOfFirst { row ->
when (row) {
is TreeRow.Client -> row.client.self
is TreeRow.Channel -> !row.expanded && row.channel.holdsSelf()
}
}
/** Which way our own row lies off screen. */
private enum class Offscreen { ABOVE, BELOW }
/** Floats over the tree's edge on the side our own row went off. */
@Composable
private fun BoxScope.ScrollToSelf(where: Offscreen?, onClick: () -> Unit) {
AnimatedVisibility(where == Offscreen.ABOVE, Modifier.align(Alignment.TopCenter).padding(top = 8.dp),
enter = slideInVertically { -it * 2 } + fadeIn(), exit = slideOutVertically { -it * 2 } + fadeOut()) {
ScrollToSelfChip(up = true, onClick)
}
AnimatedVisibility(where == Offscreen.BELOW, Modifier.align(Alignment.BottomCenter).padding(bottom = 8.dp),
enter = slideInVertically { it * 2 } + fadeIn(), exit = slideOutVertically { it * 2 } + fadeOut()) {
ScrollToSelfChip(up = false, onClick)
}
}
@Composable
private fun ScrollToSelfChip(up: Boolean, onClick: () -> Unit) {
Surface(onClick = onClick, shape = CircleShape, color = MaterialTheme.colorScheme.secondaryContainer,
shadowElevation = 4.dp) {
Row(Modifier.padding(start = 10.dp, end = 14.dp, top = 6.dp, bottom = 6.dp),
verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp)) {
Icon(Icons.Filled.KeyboardArrowDown, null, Modifier.size(18.dp).rotate(if (up) 180f else 0f))
Text("Me", style = MaterialTheme.typography.labelLarge)
}
}
}
@Composable
fun ChannelTree(
serverName: String,
@@ -102,6 +149,8 @@ fun ChannelTree(
found: Set<TreeRef> = emptySet(),
foundCurrent: TreeRef? = null,
listState: LazyListState = rememberLazyListState(),
/** Brings our own client into view; its row may sit in folded channels. */
onShowSelf: () -> Unit = {},
) {
val rows = remember(tree, collapsed, currentChannel) { flatten(tree, collapsed, currentChannel) }
var flashing by remember { mutableStateOf<TreeRef?>(null) }
@@ -121,7 +170,25 @@ fun ChannelTree(
flashing = null
}
}
LazyColumn(modifier.fillMaxSize(), state = listState, contentPadding = PaddingValues(vertical = 4.dp)) {
val anchor = remember(rows) { selfAnchor(rows) }
val offscreen by remember(anchor) {
derivedStateOf {
if (anchor < 0) return@derivedStateOf null
val info = listState.layoutInfo
val index = anchor + 1 // past the server's row
val item = info.visibleItemsInfo.firstOrNull { it.index == index }
when {
item != null && item.offset >= info.viewportStartOffset &&
item.offset + item.size <= info.viewportEndOffset -> null
item != null -> if (item.offset < info.viewportStartOffset) Offscreen.ABOVE else Offscreen.BELOW
info.visibleItemsInfo.isEmpty() -> null
index < info.visibleItemsInfo.first().index -> Offscreen.ABOVE
else -> Offscreen.BELOW
}
}
}
Box(modifier.fillMaxSize()) {
LazyColumn(Modifier.fillMaxSize(), state = listState, contentPadding = PaddingValues(vertical = 4.dp)) {
item(key = "server") {
RowLayout(depth = 0, onClick = onServer) {
PackIcon("SERVER_GREEN", null, size = ICON)
@@ -145,6 +212,8 @@ fun ChannelTree(
}
}
}
ScrollToSelf(offscreen, onShowSelf)
}
}
private val ICON = 16.dp
@@ -284,6 +284,11 @@ fun ServerScreen(
found = remember(treeHits) { treeHits.mapTo(HashSet()) { TreeRef(treeFind.channels, it) } },
foundCurrent = treeAt?.takeIf { it in treeHits }?.let { TreeRef(treeFind.channels, it) },
listState = treeList,
onShowSelf = {
val path = tree.pathTo(state.selfClientId).orEmpty()
if (treeFiltering) filterCollapsed = filterCollapsed - path.toSet() else server.expand(path)
reveal = TreeRef(false, state.selfClientId)
},
)
} else {
ChatPane(