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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
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),
}
private val Tabs = Screen.entries.filter { it.isTab }
@Composable
fun AppShell() {
var tab by rememberSaveable { mutableStateOf(Screen.HOME) }
@@ -161,30 +164,34 @@ private fun NavPill(current: Screen, onSelect: (Screen) -> Unit, modifier: Modif
.selectableGroup(),
horizontalArrangement = Arrangement.spacedBy(4.dp),
) {
Screen.entries.filter { it.isTab }.forEach { item ->
val selected = item == current
val background by animateColorAsState(
if (selected) Palette.AccentSoft else Color.Transparent,
animationSpec = Motion.color,
label = "navBackground",
)
val tint by animateColorAsState(
if (selected) Palette.Accent else Palette.NavIcon,
animationSpec = Motion.color,
label = "navTint",
)
val label = stringResource(item.label)
Box(
modifier = Modifier
.size(width = 58.dp, height = 44.dp)
.clip(RoundedCornerShape(22.dp))
.background(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))
}
}
Tabs.forEach { item -> NavItem(item, selected = item == current, onSelect = onSelect) }
}
}
// One tab of the panel. Its own composable, so a color animation recomposes only the tabs that
// change instead of the whole panel; the background is read only when drawing.
@Composable
private fun NavItem(item: Screen, selected: Boolean, onSelect: (Screen) -> Unit) {
val background by animateColorAsState(
if (selected) Palette.AccentSoft else Color.Transparent,
animationSpec = Motion.color,
label = "navBackground",
)
val tint by animateColorAsState(
if (selected) Palette.Accent else Palette.NavIcon,
animationSpec = Motion.color,
label = "navTint",
)
val label = stringResource(item.label)
Box(
modifier = Modifier
.size(width = 58.dp, height = 44.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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.CornerRadius
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.unit.Constraints
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
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)
// 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
fun HomeScreen(
bottomPadding: Dp,
@@ -356,7 +361,7 @@ private fun StatusCard(
Modifier
.size(7.dp)
.clip(CircleShape)
.background(dot)
.drawBehind { drawRect(dot) }
)
Spacer(Modifier.width(8.dp))
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
private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) {
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)
.graphicsLayer { alpha = if (enabled) 1f else 0.5f }
.clip(RoundedCornerShape(25.dp))
.background(track)
.drawBehind { drawRect(track) }
.toggleable(value = on, enabled = enabled, role = Role.Switch, onValueChange = { onToggle() })
.semantics { contentDescription = description }
.padding(5.dp),
@@ -405,7 +411,7 @@ private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) {
)
Box(
modifier = Modifier
.offset(x = knob)
.offset { IntOffset(knob.roundToPx(), 0) }
.size(40.dp)
.clip(CircleShape)
.background(Color.White),
@@ -413,7 +419,7 @@ private fun PowerToggle(on: Boolean, enabled: Boolean, onToggle: () -> Unit) {
) {
Canvas(Modifier.size(14.dp)) {
val stroke = 1.6.dp.toPx()
listOf(0.29f, 0.5f, 0.71f).forEach { fraction ->
KnobLines.forEach { fraction ->
drawLine(
color = Palette.Fg3,
start = Offset(size.width * fraction, size.height * 0.21f),
@@ -3,7 +3,6 @@ package cc.skysparkle.sparkledns
import androidx.annotation.DrawableRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
@@ -36,6 +35,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
@@ -269,7 +269,8 @@ private fun ServerRow(
Row(
modifier = Modifier
.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)
.heightIn(min = 60.dp)
.padding(start = 16.dp, end = 8.dp, top = 8.dp, bottom = 8.dp),