fix(ui): плавный переход состояний главной кнопки

Прерванный морф стартовал новый Morph от старой цели - форма прыгала.
Угол вращения сбрасывался в 0 скачком, а scale/rotation читались
в композиции, рекомпозируя кнопку каждый кадр спина.
Тайминги сведены к motionScheme.
This commit is contained in:
samosvalishe committed 2026-07-29 02:52:14 +03:00
1 parent daea9cf2a8
commit 77084151f6
1 file changed
+92 -58
@@ -7,12 +7,7 @@ import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.LinearEasing import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut import androidx.compose.animation.fadeOut
@@ -39,11 +34,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.State
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
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.scale
import androidx.compose.ui.geometry.Size import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Matrix import androidx.compose.ui.graphics.Matrix
@@ -57,6 +52,7 @@ import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.graphics.shapes.Morph import androidx.graphics.shapes.Morph
@@ -67,6 +63,7 @@ import com.freeturn.app.ui.theme.LocalReducedMotion
import com.freeturn.app.ui.theme.extendedColorScheme import com.freeturn.app.ui.theme.extendedColorScheme
import com.freeturn.app.domain.ProxyState import com.freeturn.app.domain.ProxyState
import com.freeturn.app.ui.theme.Spacing import com.freeturn.app.ui.theme.Spacing
import kotlin.math.ceil
/** Герой главного экрана: кнопка-тоггл, строка статуса и пилюля счётчика/uptime. */ /** Герой главного экрана: кнопка-тоггл, строка статуса и пилюля счётчика/uptime. */
@Composable @Composable
@@ -123,6 +120,12 @@ private fun HeroToggleButton(
HeroKind.Error -> stringResource(R.string.proxy_error_restart) HeroKind.Error -> stringResource(R.string.proxy_error_restart)
HeroKind.Idle -> stringResource(R.string.start_proxy) HeroKind.Idle -> stringResource(R.string.start_proxy)
} }
// Форма, размер и цвет тянутся одними и теми же спеками - иначе переход распадается на слои.
val colorSpec = MaterialTheme.motionScheme.slowEffectsSpec<Color>()
val spatialSpec = MaterialTheme.motionScheme.defaultSpatialSpec<Float>()
val effectsSpec = MaterialTheme.motionScheme.defaultEffectsSpec<Float>()
val fastEffectsSpec = MaterialTheme.motionScheme.fastEffectsSpec<Float>()
val containerColor by animateColorAsState( val containerColor by animateColorAsState(
targetValue = when (kind) { targetValue = when (kind) {
HeroKind.Running -> extended.successContainer HeroKind.Running -> extended.successContainer
@@ -130,7 +133,7 @@ private fun HeroToggleButton(
HeroKind.Busy -> MaterialTheme.colorScheme.secondaryContainer HeroKind.Busy -> MaterialTheme.colorScheme.secondaryContainer
HeroKind.Idle -> MaterialTheme.colorScheme.primaryContainer HeroKind.Idle -> MaterialTheme.colorScheme.primaryContainer
}, },
animationSpec = tween(500), animationSpec = colorSpec,
label = "btn_bg" label = "btn_bg"
) )
val contentColor by animateColorAsState( val contentColor by animateColorAsState(
@@ -140,12 +143,13 @@ private fun HeroToggleButton(
HeroKind.Busy -> MaterialTheme.colorScheme.onSecondaryContainer HeroKind.Busy -> MaterialTheme.colorScheme.onSecondaryContainer
HeroKind.Idle -> MaterialTheme.colorScheme.onPrimaryContainer HeroKind.Idle -> MaterialTheme.colorScheme.onPrimaryContainer
}, },
animationSpec = tween(500), animationSpec = colorSpec,
label = "btn_fg" label = "btn_fg"
) )
val scale by animateFloatAsState( // scale/rotation читаются внутри graphicsLayer (фаза отрисовки) - иначе каждый кадр рекомпозирует кнопку.
targetValue = if (kind == HeroKind.Busy) 0.96f else 1f, val scale = animateFloatAsState(
animationSpec = spring(dampingRatio = Spring.DampingRatioMediumBouncy), targetValue = if (kind == HeroKind.Busy) 0.94f else 1f,
animationSpec = spatialSpec,
label = "btn_scale" label = "btn_scale"
) )
@@ -158,25 +162,18 @@ private fun HeroToggleButton(
}, },
reducedMotion = reducedMotion reducedMotion = reducedMotion
) )
val rotation = rememberHeroSpin(spinning = kind == HeroKind.Busy && !reducedMotion)
// Вращаем фигуру, пока идёт подключение.
val rotation = if (kind == HeroKind.Busy && !reducedMotion) {
val spin = rememberInfiniteTransition(label = "hero_spin")
val angle by spin.animateFloat(
initialValue = 0f,
targetValue = 360f,
animationSpec = infiniteRepeatable(tween(8_000, easing = LinearEasing)),
label = "hero_angle"
)
angle
} else 0f
Surface( Surface(
onClick = onClick, onClick = onClick,
modifier = Modifier modifier = Modifier
.size(148.dp) .size(148.dp)
.scale(scale) // Один слой на scale+rotation: два graphicsLayer поверх generic-outline дают лишний рендер-нод.
.graphicsLayer { rotationZ = rotation } .graphicsLayer {
scaleX = scale.value
scaleY = scale.value
rotationZ = rotation.value
}
.semantics { contentDescription = buttonLabel }, .semantics { contentDescription = buttonLabel },
shape = heroShape, shape = heroShape,
color = containerColor, color = containerColor,
@@ -186,7 +183,7 @@ private fun HeroToggleButton(
// Контр-вращение: крутится только фигура. // Контр-вращение: крутится только фигура.
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.graphicsLayer { rotationZ = -rotation }, .graphicsLayer { rotationZ = -rotation.value },
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
if (reducedMotion) { if (reducedMotion) {
@@ -195,8 +192,8 @@ private fun HeroToggleButton(
AnimatedContent( AnimatedContent(
targetState = kind, targetState = kind,
transitionSpec = { transitionSpec = {
(fadeIn(tween(220)) + scaleIn(initialScale = 0.6f, animationSpec = tween(260))) (fadeIn(effectsSpec) + scaleIn(spatialSpec, initialScale = 0.85f))
.togetherWith(fadeOut(tween(120)) + scaleOut(targetScale = 0.6f, animationSpec = tween(120))) .togetherWith(fadeOut(fastEffectsSpec) + scaleOut(fastEffectsSpec, targetScale = 0.85f))
}, },
label = "hero_icon" label = "hero_icon"
) { k -> ) { k ->
@@ -207,22 +204,43 @@ private fun HeroToggleButton(
} }
} }
/** Непрерывное вращение фигуры с доводом до полного оборота на выходе. */
@Composable
private fun rememberHeroSpin(spinning: Boolean): State<Float> {
val angle = remember { Animatable(0f) }
val settleSpec = MaterialTheme.motionScheme.slowSpatialSpec<Float>()
LaunchedEffect(spinning) {
if (spinning) {
// Один длинный проход вместо infiniteRepeatable: перезапуск tween даёт микро-рывок на стыке.
angle.animateTo(angle.value + 360f * 240, tween(240 * 8_000, easing = LinearEasing))
} else if (angle.value != 0f) {
// Пружина подхватывает текущую скорость - вместо мгновенного сброса на 0.
angle.animateTo(ceil(angle.value / 360f) * 360f, settleSpec)
angle.snapTo(0f)
}
}
return angle.asState()
}
@Composable @Composable
private fun HeroIcon(kind: HeroKind, tint: Color) { private fun HeroIcon(kind: HeroKind, tint: Color) {
when (kind) { // Фиксированный слот: смена иконки не меняет размер контента (иначе AnimatedContent тянет ещё и размер).
HeroKind.Busy -> LoadingIndicator(color = tint, modifier = Modifier.size(64.dp)) Box(Modifier.size(64.dp), contentAlignment = Alignment.Center) {
HeroKind.Running -> Icon( when (kind) {
painterResource(R.drawable.check_circle_24px), null, HeroKind.Busy -> LoadingIndicator(color = tint, modifier = Modifier.size(64.dp))
Modifier.size(52.dp), tint = tint HeroKind.Running -> Icon(
) painterResource(R.drawable.check_circle_24px), null,
HeroKind.Error -> Icon( Modifier.size(52.dp), tint = tint
painterResource(R.drawable.error_24px), null, )
Modifier.size(52.dp), tint = tint HeroKind.Error -> Icon(
) painterResource(R.drawable.error_24px), null,
HeroKind.Idle -> Icon( Modifier.size(52.dp), tint = tint
painterResource(R.drawable.play_arrow_24px), null, )
Modifier.size(52.dp), tint = tint HeroKind.Idle -> Icon(
) painterResource(R.drawable.play_arrow_24px), null,
Modifier.size(52.dp), tint = tint
)
}
} }
} }
@@ -241,9 +259,12 @@ private fun StatusLabel(state: ProxyState, reducedMotion: Boolean) {
is ProxyState.Error, is ProxyState.CaptchaRequired -> MaterialTheme.colorScheme.error is ProxyState.Error, is ProxyState.CaptchaRequired -> MaterialTheme.colorScheme.error
else -> MaterialTheme.colorScheme.onSurfaceVariant else -> MaterialTheme.colorScheme.onSurfaceVariant
}, },
animationSpec = tween(400), animationSpec = MaterialTheme.motionScheme.slowEffectsSpec(),
label = "status_color" label = "status_color"
) )
val enterSpec = MaterialTheme.motionScheme.defaultEffectsSpec<Float>()
val exitSpec = MaterialTheme.motionScheme.fastEffectsSpec<Float>()
val slideSpec = MaterialTheme.motionScheme.defaultSpatialSpec<IntOffset>()
val text: @Composable (String) -> Unit = { value -> val text: @Composable (String) -> Unit = { value ->
Text( Text(
text = value, text = value,
@@ -259,8 +280,8 @@ private fun StatusLabel(state: ProxyState, reducedMotion: Boolean) {
AnimatedContent( AnimatedContent(
targetState = label, targetState = label,
transitionSpec = { transitionSpec = {
(fadeIn(tween(220)) + slideInVertically(tween(260)) { it / 3 }) (fadeIn(enterSpec) + slideInVertically(slideSpec) { it / 3 })
.togetherWith(fadeOut(tween(120)) + slideOutVertically(tween(120)) { -it / 3 }) .togetherWith(fadeOut(exitSpec) + slideOutVertically(slideSpec) { -it / 3 })
}, },
label = "status_label" label = "status_label"
) { value -> text(value) } ) { value -> text(value) }
@@ -287,8 +308,10 @@ private fun StatsPill(state: ProxyState, kind: HeroKind, uptimeText: String?) {
if (pillText != null) lastText = pillText if (pillText != null) lastText = pillText
AnimatedVisibility( AnimatedVisibility(
visible = pillText != null, visible = pillText != null,
enter = fadeIn(tween(220)) + scaleIn(initialScale = 0.8f, animationSpec = tween(260)), enter = fadeIn(MaterialTheme.motionScheme.defaultEffectsSpec()) +
exit = fadeOut(tween(120)) + scaleOut(targetScale = 0.8f, animationSpec = tween(120)) scaleIn(MaterialTheme.motionScheme.defaultSpatialSpec(), initialScale = 0.8f),
exit = fadeOut(MaterialTheme.motionScheme.fastEffectsSpec()) +
scaleOut(MaterialTheme.motionScheme.fastEffectsSpec(), targetScale = 0.8f)
) { ) {
Surface( Surface(
shape = CircleShape, shape = CircleShape,
@@ -311,28 +334,39 @@ private fun StatsPill(state: ProxyState, kind: HeroKind, uptimeText: String?) {
private fun rememberMorphingShape(target: RoundedPolygon, reducedMotion: Boolean): Shape { private fun rememberMorphingShape(target: RoundedPolygon, reducedMotion: Boolean): Shape {
var from by remember { mutableStateOf(target) } var from by remember { mutableStateOf(target) }
var to by remember { mutableStateOf(target) } var to by remember { mutableStateOf(target) }
if (to !== target) {
from = to
to = target
}
val morph = remember(from, to) { Morph(from, to) } val morph = remember(from, to) { Morph(from, to) }
val progress = remember { Animatable(1f) } val progress = remember { Animatable(1f) }
val spec = MaterialTheme.motionScheme.slowSpatialSpec<Float>() val spec = MaterialTheme.motionScheme.slowSpatialSpec<Float>()
LaunchedEffect(to) { val catchUpSpec = MaterialTheme.motionScheme.fastSpatialSpec<Float>()
if (from !== to) {
progress.snapTo(0f) LaunchedEffect(target) {
if (reducedMotion) progress.snapTo(1f) else progress.animateTo(1f, spec) if (target === to) return@LaunchedEffect
if (reducedMotion) {
from = target
to = target
progress.snapTo(1f)
return@LaunchedEffect
} }
// Новый Morph стартует от `to`, поэтому недокрученный старый сначала доводим до конца:
// иначе форма прыгает с текущего кадра на `to` (главный источник рывка при быстрой смене состояний).
if (progress.value < 1f) progress.animateTo(1f, catchUpSpec)
from = to
to = target
progress.snapTo(0f)
progress.animateTo(1f, spec)
} }
return MorphShape(morph, progress.value) // Общий буфер пути: MorphShape пересоздаётся каждый кадр морфа, аллокация Path на кадр не нужна.
val buffer = remember { android.graphics.Path() }
return MorphShape(morph, progress.value, buffer)
} }
private class MorphShape( private class MorphShape(
private val morph: Morph, private val morph: Morph,
private val progress: Float private val progress: Float,
private val buffer: android.graphics.Path
) : Shape { ) : Shape {
override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density): Outline { override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density): Outline {
val path = morph.toPath(progress.coerceIn(0f, 1f)).asComposePath() val path = morph.toPath(progress.coerceIn(0f, 1f), buffer).asComposePath()
val matrix = Matrix() val matrix = Matrix()
matrix.scale(size.width, size.height) matrix.scale(size.width, size.height)
path.transform(matrix) path.transform(matrix)