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 package com.ts3client.android.ui
import androidx.compose.animation.animateColorAsState 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.animation.core.tween
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
@@ -84,6 +93,44 @@ private fun flatten(tree: ServerTree, collapsed: Set<Int>, currentChannel: Int):
return rows 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 @Composable
fun ChannelTree( fun ChannelTree(
serverName: String, serverName: String,
@@ -102,6 +149,8 @@ fun ChannelTree(
found: Set<TreeRef> = emptySet(), found: Set<TreeRef> = emptySet(),
foundCurrent: TreeRef? = null, foundCurrent: TreeRef? = null,
listState: LazyListState = rememberLazyListState(), 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) } val rows = remember(tree, collapsed, currentChannel) { flatten(tree, collapsed, currentChannel) }
var flashing by remember { mutableStateOf<TreeRef?>(null) } var flashing by remember { mutableStateOf<TreeRef?>(null) }
@@ -121,29 +170,49 @@ fun ChannelTree(
flashing = null flashing = null
} }
} }
LazyColumn(modifier.fillMaxSize(), state = listState, contentPadding = PaddingValues(vertical = 4.dp)) { val anchor = remember(rows) { selfAnchor(rows) }
item(key = "server") { val offscreen by remember(anchor) {
RowLayout(depth = 0, onClick = onServer) { derivedStateOf {
PackIcon("SERVER_GREEN", null, size = ICON) if (anchor < 0) return@derivedStateOf null
Text(serverName, Modifier.weight(1f), fontWeight = FontWeight.Bold, maxLines = 1, val info = listState.layoutInfo
overflow = TextOverflow.Ellipsis) val index = anchor + 1 // past the server's row
ServerIcon(tree.serverIconId, size = ICON) 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
} }
} }
items(rows, key = { it.key }) { row -> }
val background = rowBackground( Box(modifier.fillMaxSize()) {
when (row.ref) { LazyColumn(Modifier.fillMaxSize(), state = listState, contentPadding = PaddingValues(vertical = 4.dp)) {
flashing -> FLASH item(key = "server") {
foundCurrent -> findCurrentColor() RowLayout(depth = 0, onClick = onServer) {
in found -> findMatchColor() PackIcon("SERVER_GREEN", null, size = ICON)
else -> Color.Transparent Text(serverName, Modifier.weight(1f), fontWeight = FontWeight.Bold, maxLines = 1,
}, overflow = TextOverflow.Ellipsis)
) ServerIcon(tree.serverIconId, size = ICON)
when (row) { }
is TreeRow.Channel -> ChannelRow(row, background, onToggle, onChannel) }
is TreeRow.Client -> ClientRow(row, tree.countryFlags, background, onClient) items(rows, key = { it.key }) { row ->
val background = rowBackground(
when (row.ref) {
flashing -> FLASH
foundCurrent -> findCurrentColor()
in found -> findMatchColor()
else -> Color.Transparent
},
)
when (row) {
is TreeRow.Channel -> ChannelRow(row, background, onToggle, onChannel)
is TreeRow.Client -> ClientRow(row, tree.countryFlags, background, onClient)
}
} }
} }
ScrollToSelf(offscreen, onShowSelf)
} }
} }
@@ -284,6 +284,11 @@ fun ServerScreen(
found = remember(treeHits) { treeHits.mapTo(HashSet()) { TreeRef(treeFind.channels, it) } }, found = remember(treeHits) { treeHits.mapTo(HashSet()) { TreeRef(treeFind.channels, it) } },
foundCurrent = treeAt?.takeIf { it in treeHits }?.let { TreeRef(treeFind.channels, it) }, foundCurrent = treeAt?.takeIf { it in treeHits }?.let { TreeRef(treeFind.channels, it) },
listState = treeList, 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 { } else {
ChatPane( ChatPane(