Read animated colors and offsets when drawing

This commit is contained in:
2026-10-31 11:50:14 +02:00
parent 87723f0a8f
commit caf498d783
3 changed files with 46 additions and 32 deletions
@@ -39,6 +39,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.shadow import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@@ -59,6 +60,8 @@ private enum class Screen(@StringRes val label: Int, @DrawableRes val icon: Int,
ABOUT(R.string.tab_about, R.drawable.ic_tab_info, false), ABOUT(R.string.tab_about, R.drawable.ic_tab_info, false),
} }
private val Tabs = Screen.entries.filter { it.isTab }
@Composable @Composable
fun AppShell() { fun AppShell() {
var tab by rememberSaveable { mutableStateOf(Screen.HOME) } var tab by rememberSaveable { mutableStateOf(Screen.HOME) }
@@ -161,30 +164,34 @@ private fun NavPill(current: Screen, onSelect: (Screen) -> Unit, modifier: Modif
.selectableGroup(), .selectableGroup(),
horizontalArrangement = Arrangement.spacedBy(4.dp), horizontalArrangement = Arrangement.spacedBy(4.dp),
) { ) {
Screen.entries.filter { it.isTab }.forEach { item -> Tabs.forEach { item -> NavItem(item, selected = item == current, onSelect = onSelect) }
val selected = item == current }
val background by animateColorAsState( }
if (selected) Palette.AccentSoft else Color.Transparent,
animationSpec = Motion.color, // One tab of the panel. Its own composable, so a color animation recomposes only the tabs that
label = "navBackground", // change instead of the whole panel; the background is read only when drawing.
) @Composable
val tint by animateColorAsState( private fun NavItem(item: Screen, selected: Boolean, onSelect: (Screen) -> Unit) {
if (selected) Palette.Accent else Palette.NavIcon, val background by animateColorAsState(
animationSpec = Motion.color, if (selected) Palette.AccentSoft else Color.Transparent,
label = "navTint", animationSpec = Motion.color,
) label = "navBackground",
val label = stringResource(item.label) )
Box( val tint by animateColorAsState(
modifier = Modifier if (selected) Palette.Accent else Palette.NavIcon,
.size(width = 58.dp, height = 44.dp) animationSpec = Motion.color,
.clip(RoundedCornerShape(22.dp)) label = "navTint",
.background(background) )
.selectable(selected = selected, onClick = { onSelect(item) }, role = Role.Tab) val label = stringResource(item.label)
.semantics { contentDescription = label }, Box(
contentAlignment = Alignment.Center, modifier = Modifier
) { .size(width = 58.dp, height = 44.dp)
Icon(painterResource(item.icon), contentDescription = null, tint = tint, modifier = Modifier.size(22.dp)) .clip(RoundedCornerShape(22.dp))
} .drawBehind { drawRect(background) }
} .selectable(selected = selected, onClick = { onSelect(item) }, role = Role.Tab)
.semantics { contentDescription = label },
contentAlignment = Alignment.Center,
) {
Icon(painterResource(item.icon), contentDescription = null, tint = tint, modifier = Modifier.size(22.dp))
} }
} }
@@ -56,6 +56,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.drawWithCache import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.CornerRadius import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
@@ -82,6 +83,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
@@ -105,6 +107,9 @@ private data class Chart(val period: StatsPeriod, val bars: List<Bar>, val label
private val ChartLabelStyle = TextStyle(color = Palette.Fg3, fontSize = 10.sp) private val ChartLabelStyle = TextStyle(color = Palette.Fg3, fontSize = 10.sp)
// Where the three grip lines on the knob of the power switch sit, as fractions of its width
private val KnobLines = floatArrayOf(0.29f, 0.5f, 0.71f)
@Composable @Composable
fun HomeScreen( fun HomeScreen(
bottomPadding: Dp, bottomPadding: Dp,
@@ -356,7 +361,7 @@ private fun StatusCard(
Modifier Modifier
.size(7.dp) .size(7.dp)
.clip(CircleShape) .clip(CircleShape)
.background(dot) .drawBehind { drawRect(dot) }
) )
Spacer(Modifier.width(8.dp)) Spacer(Modifier.width(8.dp))
val via = stringResource(R.string.home_via, providerName) + if (http3) " · HTTP/3" else "" val via = stringResource(R.string.home_via, providerName) + if (http3) " · HTTP/3" else ""
@@ -377,7 +382,8 @@ private fun StatusCard(
} }
} }
// The big ON / OFF switch; the white knob slides across // The big ON / OFF switch; the white knob slides across. The animated color and position are
// read only when drawing and placing, so the animation does not recompose the switch every frame.
@Composable @Composable
private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) { private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) {
val track by animateColorAsState(if (on) Palette.Accent else Palette.Track, Motion.color, label = "track") val track by animateColorAsState(if (on) Palette.Accent else Palette.Track, Motion.color, label = "track")
@@ -388,7 +394,7 @@ private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) {
.size(width = 120.dp, height = 50.dp) .size(width = 120.dp, height = 50.dp)
.graphicsLayer { alpha = if (enabled) 1f else 0.5f } .graphicsLayer { alpha = if (enabled) 1f else 0.5f }
.clip(RoundedCornerShape(25.dp)) .clip(RoundedCornerShape(25.dp))
.background(track) .drawBehind { drawRect(track) }
.toggleable(value = on, enabled = enabled, role = Role.Switch, onValueChange = { onToggle() }) .toggleable(value = on, enabled = enabled, role = Role.Switch, onValueChange = { onToggle() })
.semantics { contentDescription = description } .semantics { contentDescription = description }
.padding(5.dp), .padding(5.dp),
@@ -405,7 +411,7 @@ private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) {
) )
Box( Box(
modifier = Modifier modifier = Modifier
.offset(x = knob) .offset { IntOffset(knob.roundToPx(), 0) }
.size(40.dp) .size(40.dp)
.clip(CircleShape) .clip(CircleShape)
.background(Color.White), .background(Color.White),
@@ -413,7 +419,7 @@ private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) {
) { ) {
Canvas(Modifier.size(14.dp)) { Canvas(Modifier.size(14.dp)) {
val stroke = 1.6.dp.toPx() val stroke = 1.6.dp.toPx()
listOf(0.29f, 0.5f, 0.71f).forEach { fraction -> KnobLines.forEach { fraction ->
drawLine( drawLine(
color = Palette.Fg3, color = Palette.Fg3,
start = Offset(size.width * fraction, size.height * 0.21f), start = Offset(size.width * fraction, size.height * 0.21f),
@@ -3,7 +3,6 @@ package cc.skysparkle.sparkledns
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
@@ -36,6 +35,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
@@ -269,7 +269,8 @@ private fun ServerRow(
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.background(highlight) // Read when drawing, so the fade does not recompose the row every frame
.drawBehind { drawRect(highlight) }
.selectable(selected = selected, onClick = onClick, role = Role.RadioButton) .selectable(selected = selected, onClick = onClick, role = Role.RadioButton)
.heightIn(min = 60.dp) .heightIn(min = 60.dp)
.padding(start = 16.dp, end = 8.dp, top = 8.dp, bottom = 8.dp), .padding(start = 16.dp, end = 8.dp, top = 8.dp, bottom = 8.dp),