perf(ui): не рекомпозировать всю кнопку героя на каждом кадре морфа

This commit is contained in:
samosvalishe committed 2026-09-21 14:11:06 +03:00
1 parent 66513a243b
commit 03ec6b0eb5
1 file changed
+75 -39
@@ -53,6 +53,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.Dp
import androidx.compose.ui.unit.IntOffset 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
@@ -141,8 +142,6 @@ private fun HeroToggleButton(
// Форма, размер и цвет тянутся одними и теми же спеками - иначе переход распадается на слои. // Форма, размер и цвет тянутся одними и теми же спеками - иначе переход распадается на слои.
val colorSpec = MaterialTheme.motionScheme.slowEffectsSpec<Color>() val colorSpec = MaterialTheme.motionScheme.slowEffectsSpec<Color>()
val spatialSpec = MaterialTheme.motionScheme.defaultSpatialSpec<Float>() 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) {
@@ -171,18 +170,16 @@ private fun HeroToggleButton(
label = "btn_scale" label = "btn_scale"
) )
val heroShape = rememberMorphingShape( val heroTarget = when (kind) {
target = when (kind) { HeroKind.Idle -> MaterialShapes.Cookie12Sided
HeroKind.Idle -> MaterialShapes.Cookie12Sided HeroKind.Busy -> MaterialShapes.Sunny
HeroKind.Busy -> MaterialShapes.Sunny HeroKind.Running -> MaterialShapes.Circle
HeroKind.Running -> MaterialShapes.Circle HeroKind.Error -> MaterialShapes.SoftBurst
HeroKind.Error -> MaterialShapes.SoftBurst }
},
reducedMotion = reducedMotion
)
val rotation = rememberHeroSpin(spinning = kind == HeroKind.Busy && !reducedMotion) val rotation = rememberHeroSpin(spinning = kind == HeroKind.Busy && !reducedMotion)
val handSpin = remember { Animatable(0f) } val handSpin = remember { Animatable(0f) }
val handSpinState = handSpin.asState()
val spinnable = decor && kind == HeroKind.Idle val spinnable = decor && kind == HeroKind.Idle
val settleSpec = MaterialTheme.motionScheme.slowSpatialSpec<Float>() val settleSpec = MaterialTheme.motionScheme.slowSpatialSpec<Float>()
LaunchedEffect(spinnable) { LaunchedEffect(spinnable) {
@@ -201,8 +198,12 @@ private fun HeroToggleButton(
) )
} }
Surface( MorphingHeroSurface(
target = heroTarget,
reducedMotion = reducedMotion,
onClick = onClick, onClick = onClick,
color = containerColor,
tonalElevation = if (kind == HeroKind.Running) 3.dp else 1.dp,
modifier = Modifier modifier = Modifier
.size(HeroButtonSize) .size(HeroButtonSize)
.leafSpin(handSpin, spinnable) .leafSpin(handSpin, spinnable)
@@ -212,42 +213,77 @@ private fun HeroToggleButton(
scaleY = scale.value scaleY = scale.value
rotationZ = rotation.value + handSpin.value rotationZ = rotation.value + handSpin.value
} }
.semantics { contentDescription = buttonLabel }, .semantics { contentDescription = buttonLabel }
shape = heroShape,
color = containerColor,
tonalElevation = if (kind == HeroKind.Running) 3.dp else 1.dp
) { ) {
Box( HeroFace(
// Контр-вращение: крутится только фигура. kind = kind,
modifier = Modifier tint = contentColor,
.fillMaxSize() reducedMotion = reducedMotion,
.graphicsLayer { rotationZ = -(rotation.value + handSpin.value) }, rotation = rotation,
contentAlignment = Alignment.Center handSpin = handSpinState
) { )
if (reducedMotion) {
HeroIcon(kind = kind, tint = contentColor)
} else {
AnimatedContent(
targetState = kind,
transitionSpec = {
(fadeIn(effectsSpec) + scaleIn(spatialSpec, initialScale = 0.85f))
.togetherWith(fadeOut(fastEffectsSpec) + scaleOut(fastEffectsSpec, targetScale = 0.85f))
},
label = "hero_icon"
) { k ->
HeroIcon(kind = k, tint = contentColor)
}
}
}
} }
// Поверх кнопки: слой без pointerInput, тап проходит насквозь в Surface.
if (decor) { if (decor) {
AutumnLeafBurst(burstKey = burstKey, buttonSize = HeroButtonSize) AutumnLeafBurst(burstKey = burstKey, buttonSize = HeroButtonSize)
} }
} }
} }
@Composable
private fun MorphingHeroSurface(
target: RoundedPolygon,
reducedMotion: Boolean,
onClick: () -> Unit,
color: Color,
tonalElevation: Dp,
modifier: Modifier = Modifier,
content: @Composable () -> Unit
) {
Surface(
onClick = onClick,
modifier = modifier,
shape = rememberMorphingShape(target, reducedMotion),
color = color,
tonalElevation = tonalElevation,
content = content
)
}
@Composable
private fun HeroFace(
kind: HeroKind,
tint: Color,
reducedMotion: Boolean,
rotation: State<Float>,
handSpin: State<Float>
) {
Box(
modifier = Modifier
.fillMaxSize()
.graphicsLayer { rotationZ = -(rotation.value + handSpin.value) },
contentAlignment = Alignment.Center
) {
if (reducedMotion) {
HeroIcon(kind = kind, tint = tint)
} else {
val spatialSpec = MaterialTheme.motionScheme.defaultSpatialSpec<Float>()
val effectsSpec = MaterialTheme.motionScheme.defaultEffectsSpec<Float>()
val fastEffectsSpec = MaterialTheme.motionScheme.fastEffectsSpec<Float>()
AnimatedContent(
targetState = kind,
transitionSpec = {
(fadeIn(effectsSpec) + scaleIn(spatialSpec, initialScale = 0.85f))
.togetherWith(fadeOut(fastEffectsSpec) + scaleOut(fastEffectsSpec, targetScale = 0.85f))
},
label = "hero_icon"
) { k ->
HeroIcon(kind = k, tint = tint)
}
}
}
}
/** Непрерывное вращение фигуры с доводом до полного оборота на выходе. */ /** Непрерывное вращение фигуры с доводом до полного оборота на выходе. */
@Composable @Composable
private fun rememberHeroSpin(spinning: Boolean): State<Float> { private fun rememberHeroSpin(spinning: Boolean): State<Float> {