diff --git a/app/src/main/java/it/panov/musyx/tv/ui/MusyxApp.kt b/app/src/main/java/it/panov/musyx/tv/ui/MusyxApp.kt index bfcf4b7..ee2d03f 100644 --- a/app/src/main/java/it/panov/musyx/tv/ui/MusyxApp.kt +++ b/app/src/main/java/it/panov/musyx/tv/ui/MusyxApp.kt @@ -6,9 +6,12 @@ import androidx.activity.compose.BackHandler import androidx.annotation.DrawableRes import androidx.annotation.StringRes import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.core.FastOutSlowInEasing +import androidx.compose.animation.core.animateDpAsState +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.animation.core.tween import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut -import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.background import androidx.compose.foundation.focusGroup import androidx.compose.foundation.layout.Arrangement @@ -19,38 +22,44 @@ import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.selection.selectableGroup import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveableStateHolder +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clipToBounds +import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusProperties import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.selected +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp -import androidx.tv.material3.Border -import androidx.tv.material3.Icon import androidx.tv.material3.LocalContentColor -import androidx.tv.material3.NavigationDrawer -import androidx.tv.material3.NavigationDrawerItem import androidx.tv.material3.NavigationDrawerItemDefaults -import androidx.tv.material3.NavigationDrawerScope import androidx.tv.material3.Text import it.panov.musyx.tv.R import it.panov.musyx.tv.state.AppState import it.panov.musyx.tv.state.Route import it.panov.musyx.tv.state.Session import it.panov.musyx.tv.ui.components.FocusEntry +import it.panov.musyx.tv.ui.components.Focusable +import it.panov.musyx.tv.ui.components.MusyxIcon import it.panov.musyx.tv.ui.screens.AlbumScreen import it.panov.musyx.tv.ui.screens.ArtistScreen import it.panov.musyx.tv.ui.screens.ChartScreen @@ -72,6 +81,9 @@ import it.panov.musyx.tv.ui.screens.WaveScreen import it.panov.musyx.tv.ui.theme.MusyxTheme private const val WORDMARK = "musyx" +private const val DRAWER_MS = 150 +private const val DRAWER_ITEM_SCALE = 1.05f +private const val DRAWER_DIM = 0.4f @Composable fun MusyxApp(state: AppState, onExit: () -> Unit) { @@ -119,11 +131,10 @@ private fun Shell(state: AppState, onExit: () -> Unit) { BackHandler { if (!state.back()) onExit() } - NavigationDrawer( - drawerContent = { Drawer(current = route.section(), onNavigate = state::navigate) }, - modifier = Modifier.background(c.bg), - ) { - Column(Modifier.fillMaxSize().background(c.bg)) { + Box(Modifier.fillMaxSize().background(c.bg)) { + // The page sits beside the rail and keeps its size: the open drawer covers it rather + // than pushing it, so opening never re-lays out the page. + Column(Modifier.fillMaxSize().padding(start = drawerWidth(open = false))) { Box(Modifier.weight(1f).fillMaxWidth()) { pages.SaveableStateProvider(route.toString()) { Page(state, route, entry) @@ -131,6 +142,7 @@ private fun Shell(state: AppState, onExit: () -> Unit) { } if (route != Route.NowPlaying && state.playback.state.hasTrack) MiniPlayer(state) } + Drawer(current = route.section(), onNavigate = state::navigate) } SheetHost(state) } @@ -153,71 +165,79 @@ private fun Page(state: AppState, route: Route, entry: FocusEntry) { } } +/** The drawer's width: the icon rail while closed, labels too while open. */ @Composable -private fun NavigationDrawerScope.Drawer(current: Route, onNavigate: (Route) -> Unit) { +private fun drawerWidth(open: Boolean): Dp = + (if (open) NavigationDrawerItemDefaults.ExpandedDrawerItemWidth else NavigationDrawerItemDefaults.CollapsedDrawerItemWidth) + + MusyxTheme.space.m * 2 + +/** The rail opens while it holds focus: one short tween, and only the drawer itself moves. */ +@Composable +private fun Drawer(current: Route, onNavigate: (Route) -> Unit) { val c = MusyxTheme.colors val space = MusyxTheme.space val selectedItem = remember { FocusRequester() } + var open by remember { mutableStateOf(false) } + val motion = tween(DRAWER_MS, easing = FastOutSlowInEasing) + val width by animateDpAsState(drawerWidth(open), tween(DRAWER_MS, easing = FastOutSlowInEasing), label = "drawer") + val dim by animateFloatAsState(if (open) DRAWER_DIM else 0f, motion, label = "drawer dim") + // Dims the covered page; drawn, not composed, so it costs nothing per frame. + Box(Modifier.fillMaxSize().drawBehind { if (dim > 0f) drawRect(Color.Black, alpha = dim) }) Column( modifier = Modifier .fillMaxHeight() + .width(width) + .clipToBounds() .background(c.panel) .padding(horizontal = space.m, vertical = space.safeY) .selectableGroup() + .onFocusChanged { open = it.hasFocus } // Entering the drawer lands on the current destination, not the nearest item. .focusProperties { onEnter = { selectedItem.requestFocus() } } .focusGroup(), verticalArrangement = Arrangement.spacedBy(space.xxs), ) { - Wordmark(expanded = hasFocus, modifier = Modifier.padding(start = space.s10, bottom = space.xl)) - NAV.forEach { DrawerItem(it, current, selectedItem, onNavigate) } + Wordmark(expanded = open, modifier = Modifier.padding(start = space.s10, bottom = space.xl)) + NAV.forEach { DrawerItem(it, current, open, selectedItem, onNavigate) } Spacer(Modifier.weight(1f)) - DrawerItem(SETTINGS, current, selectedItem, onNavigate) + DrawerItem(SETTINGS, current, open, selectedItem, onNavigate) } } @Composable -private fun NavigationDrawerScope.DrawerItem(item: NavItem, current: Route, selectedItem: FocusRequester, onNavigate: (Route) -> Unit) { +private fun DrawerItem(item: NavItem, current: Route, open: Boolean, selectedItem: FocusRequester, onNavigate: (Route) -> Unit) { val c = MusyxTheme.colors - val shape = RoundedCornerShape(MusyxTheme.radius.control) val selected = item.route == current - val ring = Border(BorderStroke(2.dp, c.accent), shape = shape) - NavigationDrawerItem( - selected = selected, + val iconSize = NavigationDrawerItemDefaults.IconSize + Focusable( onClick = { onNavigate(item.route) }, - modifier = if (selected) Modifier.focusRequester(selectedItem) else Modifier, - leadingContent = { - Icon( - painter = painterResource(item.icon), - contentDescription = null, - tint = if (selected) c.accent else LocalContentColor.current, - ) - }, - shape = NavigationDrawerItemDefaults.shape(shape), - colors = NavigationDrawerItemDefaults.colors( - containerColor = Color.Transparent, - contentColor = c.muted, - inactiveContentColor = c.muted, - focusedContainerColor = c.raised, - focusedContentColor = c.fg, - pressedContainerColor = c.raised, - pressedContentColor = c.fg, - selectedContainerColor = c.raised, - selectedContentColor = c.fg, - focusedSelectedContainerColor = c.raised, - focusedSelectedContentColor = c.fg, - pressedSelectedContainerColor = c.raised, - pressedSelectedContentColor = c.fg, - ), - border = NavigationDrawerItemDefaults.border( - focusedBorder = ring, - focusedSelectedBorder = ring, - pressedBorder = ring, - pressedSelectedBorder = ring, - ), - glow = NavigationDrawerItemDefaults.glow(), + modifier = Modifier + .fillMaxWidth() + .height(NavigationDrawerItemDefaults.ContainerHeightOneLine) + .semantics { this.selected = selected } + .then(if (selected) Modifier.focusRequester(selectedItem) else Modifier), + container = if (selected) c.raised else Color.Transparent, + contentColor = if (selected) c.fg else c.muted, + focusedContentColor = c.fg, + scale = DRAWER_ITEM_SCALE, ) { - Text(stringResource(item.label), style = MusyxTheme.type.bodyMedium, maxLines = 1) + Row( + Modifier.fillMaxSize().padding(start = (NavigationDrawerItemDefaults.CollapsedDrawerItemWidth - iconSize) / 2), + verticalAlignment = Alignment.CenterVertically, + ) { + MusyxIcon(item.icon, tint = if (selected) c.accent else LocalContentColor.current, size = iconSize) + if (open) { + // One unwrapped line, clipped while the drawer is still widening. + Text( + stringResource(item.label), + style = MusyxTheme.type.bodyMedium, + maxLines = 1, + softWrap = false, + overflow = TextOverflow.Clip, + modifier = Modifier.padding(start = MusyxTheme.space.m).weight(1f, fill = false), + ) + } + } } } @@ -229,7 +249,15 @@ fun Wordmark(modifier: Modifier = Modifier, expanded: Boolean = true) { val dot = (7 * scale).dp Row(modifier = modifier) { if (expanded) { - Text(WORDMARK, style = MusyxTheme.type.wordmark, color = c.fg, modifier = Modifier.alignByBaseline()) + // Weighted, so the dot keeps its place while a widening drawer still clips the word. + Text( + WORDMARK, + style = MusyxTheme.type.wordmark, + color = c.fg, + softWrap = false, + overflow = TextOverflow.Clip, + modifier = Modifier.alignByBaseline().weight(1f, fill = false), + ) Spacer(Modifier.width((3 * scale).dp)) } // The dot sits on the baseline, as in the desktop sidebar.