From 7a186a0870503b2569df8bd183b1c4ab1e621c93 Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Mon, 30 Jan 2023 15:37:33 +0800 Subject: [PATCH] Align AppBar top margin with current Settings Keep the top margin always the same, this only has a difference when the title is two lines. Fix: 264213159 Test: Manually with Settings Change-Id: I805a4eb88242e31cfab709ade553e31f12df1b8d --- .../spa/widget/scaffold/CustomizedAppBar.kt | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/CustomizedAppBar.kt b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/CustomizedAppBar.kt index ae88ed7c99b9c..e8b5b19daad05 100644 --- a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/CustomizedAppBar.kt +++ b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/CustomizedAppBar.kt @@ -50,6 +50,8 @@ import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.SideEffect import androidx.compose.runtime.Stable import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -61,6 +63,7 @@ import androidx.compose.ui.layout.AlignmentLine import androidx.compose.ui.layout.LastBaseline import androidx.compose.ui.layout.Layout import androidx.compose.ui.layout.layoutId +import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.semantics.clearAndSetSemantics import androidx.compose.ui.text.TextStyle @@ -116,7 +119,7 @@ internal fun CustomizedLargeTopAppBar( actions = actions, colors = topAppBarColors(), windowInsets = TopAppBarDefaults.windowInsets, - maxHeight = 176.dp, + maxHeightWithoutTitle = 120.dp, pinnedHeight = ContainerHeight, scrollBehavior = scrollBehavior, ) @@ -258,8 +261,8 @@ private fun SingleRowTopAppBar( * A two-rows top app bar that is designed to be called by the Large and Medium top app bar * composables. * - * @throws [IllegalArgumentException] if the given [maxHeight] is equal or smaller than the - * [pinnedHeight] + * @throws [IllegalArgumentException] if the given [maxHeightWithoutTitle] is equal or smaller than + * the [pinnedHeight] */ @OptIn(ExperimentalMaterial3Api::class) @Composable @@ -274,29 +277,31 @@ private fun TwoRowsTopAppBar( actions: @Composable RowScope.() -> Unit, windowInsets: WindowInsets, colors: TopAppBarColors, - maxHeight: Dp, + maxHeightWithoutTitle: Dp, pinnedHeight: Dp, scrollBehavior: TopAppBarScrollBehavior? ) { - if (maxHeight <= pinnedHeight) { + if (maxHeightWithoutTitle <= pinnedHeight) { throw IllegalArgumentException( "A TwoRowsTopAppBar max height should be greater than its pinned height" ) } val pinnedHeightPx: Float - val maxHeightPx: Float + val density = LocalDensity.current + val maxHeightPx = density.run { + remember { mutableStateOf((maxHeightWithoutTitle + pinnedHeight).toPx()) } + } val titleBottomPaddingPx: Int - LocalDensity.current.run { + density.run { pinnedHeightPx = pinnedHeight.toPx() - maxHeightPx = maxHeight.toPx() titleBottomPaddingPx = titleBottomPadding.roundToPx() } // Sets the app bar's height offset limit to hide just the bottom title area and keep top title // visible when collapsed. SideEffect { - if (scrollBehavior?.state?.heightOffsetLimit != pinnedHeightPx - maxHeightPx) { - scrollBehavior?.state?.heightOffsetLimit = pinnedHeightPx - maxHeightPx + if (scrollBehavior?.state?.heightOffsetLimit != pinnedHeightPx - maxHeightPx.value) { + scrollBehavior?.state?.heightOffsetLimit = pinnedHeightPx - maxHeightPx.value } } @@ -366,12 +371,19 @@ private fun TwoRowsTopAppBar( ) TopAppBarLayout( modifier = Modifier.clipToBounds(), - heightPx = maxHeightPx - pinnedHeightPx + (scrollBehavior?.state?.heightOffset - ?: 0f), + heightPx = maxHeightPx.value - pinnedHeightPx + + (scrollBehavior?.state?.heightOffset ?: 0f), navigationIconContentColor = colors.navigationIconContentColor, titleContentColor = colors.titleContentColor, actionIconContentColor = colors.actionIconContentColor, - title = title, + title = { + Box(modifier = Modifier.onGloballyPositioned { coordinates -> + density.run { + maxHeightPx.value = + maxHeightWithoutTitle.toPx() + coordinates.size.height.toFloat() + } + }) { title() } + }, titleTextStyle = titleTextStyle, titleAlpha = bottomTitleAlpha, titleVerticalArrangement = Arrangement.Bottom,