Skip to content

Commit bd955dd

Browse files
authored
library: TopAppBar: Opt largeTitle anim (#67)
1 parent 0329f33 commit bd955dd

File tree

1 file changed

+33
-20
lines changed
  • miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic

1 file changed

+33
-20
lines changed

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/TopAppBar.kt

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.compose.foundation.layout.displayCutout
2121
import androidx.compose.foundation.layout.fillMaxWidth
2222
import androidx.compose.foundation.layout.heightIn
2323
import androidx.compose.foundation.layout.navigationBars
24+
import androidx.compose.foundation.layout.offset
2425
import androidx.compose.foundation.layout.only
2526
import androidx.compose.foundation.layout.padding
2627
import androidx.compose.foundation.layout.statusBars
@@ -52,6 +53,7 @@ import androidx.compose.ui.platform.LocalDensity
5253
import androidx.compose.ui.text.font.FontWeight
5354
import androidx.compose.ui.unit.Constraints
5455
import androidx.compose.ui.unit.Dp
56+
import androidx.compose.ui.unit.IntOffset
5557
import androidx.compose.ui.unit.Velocity
5658
import androidx.compose.ui.unit.dp
5759
import androidx.compose.ui.util.fastFirst
@@ -90,7 +92,7 @@ fun TopAppBar(
9092
) {
9193
val density = LocalDensity.current
9294
val expandedHeightPx by rememberUpdatedState(
93-
with(density) { TopAppBarExpandedHeight.toPx().coerceAtLeast(0f) }
95+
with(density) { (TopAppBarExpandedHeight).toPx().coerceAtLeast(0f) }
9496
)
9597
SideEffect {
9698
// Sets the app bar's height offset to collapse the entire bar's height when content is
@@ -560,6 +562,10 @@ private fun TopAppBarLayout(
560562
targetValue = if (extOffset > 1f) 0f else 10f,
561563
animationSpec = tween(durationMillis = 250)
562564
)
565+
// Subtract the scrolledOffset from the maxHeight. The scrolledOffset is expected to be
566+
// equal or smaller than zero.
567+
val scrolledOffsetValue = scrolledOffset.offset()
568+
val heightOffset = if (scrolledOffsetValue.isNaN()) 0 else scrolledOffsetValue.roundToInt()
563569

564570
Layout(
565571
{
@@ -595,15 +601,25 @@ private fun TopAppBarLayout(
595601
Modifier
596602
.layoutId("largeTitle")
597603
.fillMaxWidth()
598-
.padding(horizontal = horizontalPadding)
599-
.graphicsLayer(alpha = 1f - (abs(scrolledOffset.offset()) / expandedHeightPx * 2).coerceIn(0f, 1f))
600604
) {
601-
Text(
602-
text = largeTitle,
603-
maxLines = 1,
604-
fontSize = MiuixTheme.textStyles.title1.fontSize,
605-
fontWeight = FontWeight.Normal
606-
)
605+
Box(
606+
Modifier
607+
.fillMaxWidth()
608+
.padding(top = 56.dp)
609+
.padding(horizontal = horizontalPadding)
610+
.graphicsLayer(alpha = 1f - (abs(scrolledOffset.offset()) / expandedHeightPx * 2).coerceIn(0f, 1f))
611+
.clipToBounds()
612+
) {
613+
Text(
614+
modifier = Modifier.offset { IntOffset(0, heightOffset) },
615+
text = largeTitle,
616+
maxLines = 1,
617+
fontSize = MiuixTheme.textStyles.title1.fontSize,
618+
fontWeight = FontWeight.Normal
619+
)
620+
}
621+
622+
607623
}
608624
},
609625
modifier = Modifier
@@ -640,16 +656,6 @@ private fun TopAppBarLayout(
640656
.fastFirst { it.layoutId == "title" }
641657
.measure(constraints.copy(minWidth = 0, maxWidth = maxTitleWidth, minHeight = 0))
642658

643-
val largeTitlePlaceable =
644-
measurables
645-
.fastFirst { it.layoutId == "largeTitle" }
646-
.measure(constraints.copy(minWidth = 0, minHeight = 0))
647-
648-
// Subtract the scrolledOffset from the maxHeight. The scrolledOffset is expected to be
649-
// equal or smaller than zero.
650-
val scrolledOffsetValue = scrolledOffset.offset()
651-
val heightOffset = if (scrolledOffsetValue.isNaN()) 0 else scrolledOffsetValue.roundToInt()
652-
653659

654660
val layoutHeight =
655661
(if (constraints.maxHeight == Constraints.Infinity) {
@@ -658,6 +664,13 @@ private fun TopAppBarLayout(
658664
constraints.maxHeight + heightOffset
659665
}).coerceAtLeast(0)
660666

667+
val largeTitlePlaceable =
668+
measurables
669+
.fastFirst { it.layoutId == "largeTitle" }
670+
.measure(
671+
constraints.copy(minWidth = 0, minHeight = 0)
672+
)
673+
661674
layout(constraints.maxWidth, layoutHeight) {
662675
val verticalCenter = 60.dp.roundToPx() / 2
663676

@@ -688,7 +701,7 @@ private fun TopAppBarLayout(
688701
// Large title
689702
largeTitlePlaceable.placeRelative(
690703
x = 0,
691-
y = layoutHeight - largeTitlePlaceable.height
704+
y = 0
692705
)
693706
}
694707
}

0 commit comments

Comments
 (0)