[go: nahoru, domu]

blob: 85fc603db014753c464654e659674742c5552893 [file] [log] [blame]
Max Yingcc44d972021-10-11 13:12:45 -04001/*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.compose.material3
18
19import androidx.compose.animation.animateColorAsState
20import androidx.compose.animation.core.animateFloatAsState
21import androidx.compose.animation.core.tween
22import androidx.compose.foundation.background
Max Ying51d3d9e2022-04-08 17:59:07 +000023import androidx.compose.foundation.indication
Max Yingcc44d972021-10-11 13:12:45 -040024import androidx.compose.foundation.interaction.Interaction
25import androidx.compose.foundation.interaction.MutableInteractionSource
26import androidx.compose.foundation.layout.Arrangement
27import androidx.compose.foundation.layout.Box
28import androidx.compose.foundation.layout.Row
29import androidx.compose.foundation.layout.RowScope
30import androidx.compose.foundation.layout.fillMaxWidth
31import androidx.compose.foundation.layout.height
32import androidx.compose.foundation.layout.padding
33import androidx.compose.foundation.selection.selectable
34import androidx.compose.foundation.selection.selectableGroup
35import androidx.compose.material.ripple.rememberRipple
Jose Alba Aguadofe2421c2021-11-02 17:37:45 +010036import androidx.compose.material3.tokens.NavigationBarTokens
Max Yingcc44d972021-10-11 13:12:45 -040037import androidx.compose.runtime.Composable
38import androidx.compose.runtime.CompositionLocalProvider
39import androidx.compose.runtime.Stable
40import androidx.compose.runtime.State
41import androidx.compose.runtime.getValue
Max Ying51d3d9e2022-04-08 17:59:07 +000042import androidx.compose.runtime.mutableStateOf
Max Yingcc44d972021-10-11 13:12:45 -040043import androidx.compose.runtime.remember
Max Ying51d3d9e2022-04-08 17:59:07 +000044import androidx.compose.runtime.setValue
Max Yingcc44d972021-10-11 13:12:45 -040045import androidx.compose.ui.Alignment
46import androidx.compose.ui.Modifier
47import androidx.compose.ui.draw.alpha
Max Ying51d3d9e2022-04-08 17:59:07 +000048import androidx.compose.ui.draw.clip
49import androidx.compose.ui.geometry.Offset
Max Yingcc44d972021-10-11 13:12:45 -040050import androidx.compose.ui.graphics.Color
Max Yingcc44d972021-10-11 13:12:45 -040051import androidx.compose.ui.layout.LastBaseline
52import androidx.compose.ui.layout.Layout
53import androidx.compose.ui.layout.MeasureResult
54import androidx.compose.ui.layout.MeasureScope
55import androidx.compose.ui.layout.Placeable
56import androidx.compose.ui.layout.layoutId
Max Ying51d3d9e2022-04-08 17:59:07 +000057import androidx.compose.ui.layout.onSizeChanged
58import androidx.compose.ui.platform.LocalDensity
Max Yingcc44d972021-10-11 13:12:45 -040059import androidx.compose.ui.semantics.Role
60import androidx.compose.ui.unit.Constraints
61import androidx.compose.ui.unit.Dp
62import androidx.compose.ui.unit.dp
63import kotlin.math.roundToInt
64
65/**
Shalom Giblyf42290e2022-04-12 02:12:48 -070066 * <a href="https://m3.material.io/components/navigation-bar/overview" class="external" target="_blank">Material Design bottom navigation bar</a>.
67 *
68 * Navigation bars offer a persistent and convenient way to switch between primary destinations in
69 * an app.
70 *
Nick Rout2f69d9402021-10-19 16:25:18 +020071 * ![Navigation bar image](https://developer.android.com/images/reference/androidx/compose/material3/navigation-bar.png)
72 *
Max Yingcc44d972021-10-11 13:12:45 -040073 * [NavigationBar] should contain three to five [NavigationBarItem]s, each representing a singular
74 * destination.
75 *
76 * A simple example looks like:
77 * @sample androidx.compose.material3.samples.NavigationBarSample
78 *
79 * See [NavigationBarItem] for configuration specific to each item, and not the overall
80 * [NavigationBar] component.
81 *
Max Ying2384bc82022-04-28 20:20:58 +000082 * @param modifier the [Modifier] to be applied to this navigation bar
83 * @param containerColor the color used for the background of this navigation bar. Use
84 * [Color.Transparent] to have no color.
85 * @param contentColor the preferred color for content inside this navigation bar. Defaults to
86 * either the matching content color for [containerColor], or to the current [LocalContentColor] if
87 * [containerColor] is not a color from the theme.
88 * @param tonalElevation when [containerColor] is [ColorScheme.surface], a translucent primary color
89 * overlay is applied on top of the container. A higher tonal elevation value will result in a
90 * darker color in light theme and lighter color in dark theme. See also: [Surface].
91 * @param content the content of this navigation bar, typically 3-5 [NavigationBarItem]s
Max Yingcc44d972021-10-11 13:12:45 -040092 */
93@Composable
94fun NavigationBar(
95 modifier: Modifier = Modifier,
Mariano15a489b2022-01-19 13:08:12 -050096 containerColor: Color = NavigationBarTokens.ContainerColor.toColor(),
Max Yingcc44d972021-10-11 13:12:45 -040097 contentColor: Color = MaterialTheme.colorScheme.contentColorFor(containerColor),
Jose Alba Aguadofe2421c2021-11-02 17:37:45 +010098 tonalElevation: Dp = NavigationBarTokens.ContainerElevation,
Max Yingcc44d972021-10-11 13:12:45 -040099 content: @Composable RowScope.() -> Unit
100) {
101 Surface(
102 color = containerColor,
103 contentColor = contentColor,
104 tonalElevation = tonalElevation,
105 modifier = modifier
106 ) {
107 Row(
108 modifier = Modifier.fillMaxWidth().height(NavigationBarHeight).selectableGroup(),
Max Ying51d3d9e2022-04-08 17:59:07 +0000109 horizontalArrangement = Arrangement.spacedBy(NavigationBarItemHorizontalPadding),
Max Yingcc44d972021-10-11 13:12:45 -0400110 content = content
111 )
112 }
113}
114
115/**
116 * Material Design navigation bar item.
117 *
Shalom Giblyf42290e2022-04-12 02:12:48 -0700118 * Navigation bars offer a persistent and convenient way to switch between primary destinations in
119 * an app.
120 *
Max Yingcc44d972021-10-11 13:12:45 -0400121 * The recommended configuration for a [NavigationBarItem] depends on how many items there are
122 * inside a [NavigationBar]:
123 *
124 * - Three destinations: Display icons and text labels for all destinations.
125 * - Four destinations: Active destinations display an icon and text label. Inactive destinations
126 * display icons, and text labels are recommended.
127 * - Five destinations: Active destinations display an icon and text label. Inactive destinations
128 * use icons, and use text labels if space permits.
129 *
130 * A [NavigationBarItem] always shows text labels (if it exists) when selected. Showing text
131 * labels if not selected is controlled by [alwaysShowLabel].
132 *
133 * @param selected whether this item is selected
Max Ying2384bc82022-04-28 20:20:58 +0000134 * @param onClick called when this item is clicked
135 * @param icon icon for this item, typically an [Icon]
136 * @param modifier the [Modifier] to be applied to this item
137 * @param enabled controls the enabled state of this item. When `false`, this component will not
138 * respond to user input, and it will appear visually disabled and disabled to accessibility
139 * services.
Max Yingcc44d972021-10-11 13:12:45 -0400140 * @param label optional text label for this item
Max Ying2384bc82022-04-28 20:20:58 +0000141 * @param alwaysShowLabel whether to always show the label for this item. If `false`, the label will
Max Yingcc44d972021-10-11 13:12:45 -0400142 * only be shown when this item is selected.
143 * @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
Max Ying2384bc82022-04-28 20:20:58 +0000144 * for this item. You can create and pass in your own `remember`ed instance to observe
145 * [Interaction]s and customize the appearance / behavior of this item in different states.
146 * @param colors [NavigationBarItemColors] that will be used to resolve the colors used for this
147 * item in different states. See [NavigationBarItemDefaults.colors].
Max Yingcc44d972021-10-11 13:12:45 -0400148 */
149@Composable
150fun RowScope.NavigationBarItem(
151 selected: Boolean,
152 onClick: () -> Unit,
153 icon: @Composable () -> Unit,
154 modifier: Modifier = Modifier,
155 enabled: Boolean = true,
156 label: @Composable (() -> Unit)? = null,
157 alwaysShowLabel: Boolean = true,
158 interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
159 colors: NavigationBarItemColors = NavigationBarItemDefaults.colors()
160) {
161 val styledIcon = @Composable {
162 val iconColor by colors.iconColor(selected = selected)
163 CompositionLocalProvider(LocalContentColor provides iconColor, content = icon)
164 }
165
166 val styledLabel: @Composable (() -> Unit)? = label?.let {
167 @Composable {
Jose Alba Aguadofe2421c2021-11-02 17:37:45 +0100168 val style = MaterialTheme.typography.fromToken(NavigationBarTokens.LabelTextFont)
Max Yingcc44d972021-10-11 13:12:45 -0400169 val textColor by colors.textColor(selected = selected)
170 CompositionLocalProvider(LocalContentColor provides textColor) {
171 ProvideTextStyle(style, content = label)
172 }
173 }
174 }
175
Max Ying51d3d9e2022-04-08 17:59:07 +0000176 var itemWidth by remember { mutableStateOf(0) }
177
Max Yingcc44d972021-10-11 13:12:45 -0400178 Box(
179 modifier
180 .selectable(
181 selected = selected,
182 onClick = onClick,
183 enabled = enabled,
184 role = Role.Tab,
185 interactionSource = interactionSource,
Max Ying51d3d9e2022-04-08 17:59:07 +0000186 indication = null,
Max Yingcc44d972021-10-11 13:12:45 -0400187 )
Max Ying51d3d9e2022-04-08 17:59:07 +0000188 .weight(1f)
189 .onSizeChanged {
190 itemWidth = it.width
191 },
Max Yingcc44d972021-10-11 13:12:45 -0400192 contentAlignment = Alignment.Center
193 ) {
194 val animationProgress: Float by animateFloatAsState(
195 targetValue = if (selected) 1f else 0f,
196 animationSpec = tween(ItemAnimationDurationMillis)
197 )
198
Max Ying51d3d9e2022-04-08 17:59:07 +0000199 // The entire item is selectable, but only the indicator pill shows the ripple. To achieve
200 // this, we re-map the coordinates of the item's InteractionSource into the coordinates of
201 // the indicator.
202 val deltaOffset: Offset
203 with(LocalDensity.current) {
204 val indicatorWidth = NavigationBarTokens.ActiveIndicatorWidth.roundToPx()
205 deltaOffset = Offset(
206 (itemWidth - indicatorWidth).toFloat() / 2,
207 IndicatorVerticalOffset.toPx()
208 )
209 }
210 val offsetInteractionSource = remember(interactionSource, deltaOffset) {
211 MappedInteractionSource(interactionSource, deltaOffset)
212 }
213
214 // The indicator has a width-expansion animation which interferes with the timing of the
215 // ripple, which is why they are separate composables
216 val indicatorRipple = @Composable {
217 Box(
218 Modifier.layoutId(IndicatorRippleLayoutIdTag)
219 .clip(NavigationBarTokens.ActiveIndicatorShape.toShape())
220 .indication(offsetInteractionSource, rememberRipple())
221 )
222 }
Max Yingcc44d972021-10-11 13:12:45 -0400223 val indicator = @Composable {
224 Box(
225 Modifier.layoutId(IndicatorLayoutIdTag)
226 .background(
227 color = colors.indicatorColor.copy(alpha = animationProgress),
Jose Alba Aguado32056032022-03-22 12:50:19 +0100228 shape = NavigationBarTokens.ActiveIndicatorShape.toShape(),
Max Yingcc44d972021-10-11 13:12:45 -0400229 )
230 )
231 }
232
233 NavigationBarItemBaselineLayout(
Max Ying51d3d9e2022-04-08 17:59:07 +0000234 indicatorRipple = indicatorRipple,
Max Yingcc44d972021-10-11 13:12:45 -0400235 indicator = indicator,
236 icon = styledIcon,
237 label = styledLabel,
238 alwaysShowLabel = alwaysShowLabel,
239 animationProgress = animationProgress
240 )
241 }
242}
243
244/** Defaults used in [NavigationBarItem]. */
245object NavigationBarItemDefaults {
246 /**
247 * Creates a [NavigationBarItemColors] with the provided colors according to the Material
248 * specification.
249 *
250 * @param selectedIconColor the color to use for the icon when the item is selected.
251 * @param unselectedIconColor the color to use for the icon when the item is unselected.
252 * @param selectedTextColor the color to use for the text label when the item is selected.
253 * @param unselectedTextColor the color to use for the text label when the item is unselected.
254 * @param indicatorColor the color to use for the indicator when the item is selected.
255 * @return the resulting [NavigationBarItemColors] used for [NavigationBarItem]
256 */
257 @Composable
258 fun colors(
Mariano15a489b2022-01-19 13:08:12 -0500259 selectedIconColor: Color = NavigationBarTokens.ActiveIconColor.toColor(),
260 unselectedIconColor: Color = NavigationBarTokens.InactiveIconColor.toColor(),
261 selectedTextColor: Color = NavigationBarTokens.ActiveLabelTextColor.toColor(),
262 unselectedTextColor: Color = NavigationBarTokens.InactiveLabelTextColor.toColor(),
263 indicatorColor: Color = NavigationBarTokens.ActiveIndicatorColor.toColor(),
Max Yingcc44d972021-10-11 13:12:45 -0400264 ): NavigationBarItemColors = remember(
265 selectedIconColor,
266 unselectedIconColor,
267 selectedTextColor,
268 unselectedTextColor,
269 indicatorColor
270 ) {
271 DefaultNavigationBarItemColors(
272 selectedIconColor = selectedIconColor,
273 unselectedIconColor = unselectedIconColor,
274 selectedTextColor = selectedTextColor,
275 unselectedTextColor = unselectedTextColor,
276 selectedIndicatorColor = indicatorColor,
277 )
278 }
279}
280
281/** Represents the colors of the various elements of a navigation item. */
282@Stable
283interface NavigationBarItemColors {
284 /**
285 * Represents the icon color for this item, depending on whether it is [selected].
286 *
287 * @param selected whether the item is selected
288 */
289 @Composable
290 fun iconColor(selected: Boolean): State<Color>
291
292 /**
293 * Represents the text color for this item, depending on whether it is [selected].
294 *
295 * @param selected whether the item is selected
296 */
297 @Composable
298 fun textColor(selected: Boolean): State<Color>
299
300 /** Represents the color of the indicator used for selected items. */
301 val indicatorColor: Color
302 @Composable get
303}
304
305@Stable
306private class DefaultNavigationBarItemColors(
307 private val selectedIconColor: Color,
308 private val unselectedIconColor: Color,
309 private val selectedTextColor: Color,
310 private val unselectedTextColor: Color,
311 private val selectedIndicatorColor: Color,
312) : NavigationBarItemColors {
313 @Composable
314 override fun iconColor(selected: Boolean): State<Color> {
315 return animateColorAsState(
316 targetValue = if (selected) selectedIconColor else unselectedIconColor,
317 animationSpec = tween(ItemAnimationDurationMillis)
318 )
319 }
320
321 @Composable
322 override fun textColor(selected: Boolean): State<Color> {
323 return animateColorAsState(
324 targetValue = if (selected) selectedTextColor else unselectedTextColor,
325 animationSpec = tween(ItemAnimationDurationMillis)
326 )
327 }
328
329 override val indicatorColor: Color
330 @Composable
331 get() = selectedIndicatorColor
332}
333
334/**
335 * Base layout for a [NavigationBarItem].
336 *
Max Ying51d3d9e2022-04-08 17:59:07 +0000337 * @param indicatorRipple indicator ripple for this item when it is selected
Max Yingcc44d972021-10-11 13:12:45 -0400338 * @param indicator indicator for this item when it is selected
339 * @param icon icon for this item
340 * @param label text label for this item
341 * @param alwaysShowLabel whether to always show the label for this item. If false, the label will
342 * only be shown when this item is selected.
343 * @param animationProgress progress of the animation, where 0 represents the unselected state of
344 * this item and 1 represents the selected state. This value controls other values such as indicator
345 * size, icon and label positions, etc.
346 */
347@Composable
348private fun NavigationBarItemBaselineLayout(
Max Ying51d3d9e2022-04-08 17:59:07 +0000349 indicatorRipple: @Composable () -> Unit,
Max Yingcc44d972021-10-11 13:12:45 -0400350 indicator: @Composable () -> Unit,
351 icon: @Composable () -> Unit,
352 label: @Composable (() -> Unit)?,
353 alwaysShowLabel: Boolean,
354 animationProgress: Float,
355) {
356 Layout({
Max Ying51d3d9e2022-04-08 17:59:07 +0000357 indicatorRipple()
Max Yingcc44d972021-10-11 13:12:45 -0400358 if (animationProgress > 0) {
359 indicator()
360 }
361
362 Box(Modifier.layoutId(IconLayoutIdTag)) { icon() }
363
364 if (label != null) {
365 Box(
366 Modifier.layoutId(LabelLayoutIdTag)
367 .alpha(if (alwaysShowLabel) 1f else animationProgress)
Max Ying51d3d9e2022-04-08 17:59:07 +0000368 .padding(horizontal = NavigationBarItemHorizontalPadding / 2)
Max Yingcc44d972021-10-11 13:12:45 -0400369 ) { label() }
370 }
371 }) { measurables, constraints ->
372 val iconPlaceable =
373 measurables.first { it.layoutId == IconLayoutIdTag }.measure(constraints)
374
375 val totalIndicatorWidth = iconPlaceable.width + (IndicatorHorizontalPadding * 2).roundToPx()
376 val animatedIndicatorWidth = (totalIndicatorWidth * animationProgress).roundToInt()
377 val indicatorHeight = iconPlaceable.height + (IndicatorVerticalPadding * 2).roundToPx()
Max Ying51d3d9e2022-04-08 17:59:07 +0000378 val indicatorRipplePlaceable =
379 measurables
380 .first { it.layoutId == IndicatorRippleLayoutIdTag }
381 .measure(
382 Constraints.fixed(
383 width = totalIndicatorWidth,
384 height = indicatorHeight
385 )
386 )
Max Yingcc44d972021-10-11 13:12:45 -0400387 val indicatorPlaceable =
388 measurables
389 .firstOrNull { it.layoutId == IndicatorLayoutIdTag }
390 ?.measure(
391 Constraints.fixed(
392 width = animatedIndicatorWidth,
393 height = indicatorHeight
394 )
395 )
396
397 val labelPlaceable =
398 label?.let {
399 measurables
400 .first { it.layoutId == LabelLayoutIdTag }
401 .measure(
Max Ying2384bc82022-04-28 20:20:58 +0000402 // Measure with loose constraints for height as we don't want the label to
403 // take up more space than it needs
Max Yingcc44d972021-10-11 13:12:45 -0400404 constraints.copy(minHeight = 0)
405 )
406 }
407
408 if (label == null) {
Max Ying51d3d9e2022-04-08 17:59:07 +0000409 placeIcon(iconPlaceable, indicatorRipplePlaceable, indicatorPlaceable, constraints)
Max Yingcc44d972021-10-11 13:12:45 -0400410 } else {
411 placeLabelAndIcon(
412 labelPlaceable!!,
413 iconPlaceable,
Max Ying51d3d9e2022-04-08 17:59:07 +0000414 indicatorRipplePlaceable,
Max Yingcc44d972021-10-11 13:12:45 -0400415 indicatorPlaceable,
416 constraints,
417 alwaysShowLabel,
418 animationProgress
419 )
420 }
421 }
422}
423
424/**
Max Ying51d3d9e2022-04-08 17:59:07 +0000425 * Places the provided [Placeable]s in the center of the provided [constraints].
Max Yingcc44d972021-10-11 13:12:45 -0400426 */
427private fun MeasureScope.placeIcon(
428 iconPlaceable: Placeable,
Max Ying51d3d9e2022-04-08 17:59:07 +0000429 indicatorRipplePlaceable: Placeable,
Max Yingcc44d972021-10-11 13:12:45 -0400430 indicatorPlaceable: Placeable?,
431 constraints: Constraints
432): MeasureResult {
433 val width = constraints.maxWidth
434 val height = constraints.maxHeight
435
436 val iconX = (width - iconPlaceable.width) / 2
437 val iconY = (height - iconPlaceable.height) / 2
438
Max Ying51d3d9e2022-04-08 17:59:07 +0000439 val rippleX = (width - indicatorRipplePlaceable.width) / 2
440 val rippleY = (height - indicatorRipplePlaceable.height) / 2
441
Max Yingcc44d972021-10-11 13:12:45 -0400442 return layout(width, height) {
443 indicatorPlaceable?.let {
444 val indicatorX = (width - it.width) / 2
445 val indicatorY = (height - it.height) / 2
446 it.placeRelative(indicatorX, indicatorY)
447 }
448 iconPlaceable.placeRelative(iconX, iconY)
Max Ying51d3d9e2022-04-08 17:59:07 +0000449 indicatorRipplePlaceable.placeRelative(rippleX, rippleY)
Max Yingcc44d972021-10-11 13:12:45 -0400450 }
451}
452
453/**
Max Ying51d3d9e2022-04-08 17:59:07 +0000454 * Places the provided [Placeable]s in the correct position, depending on [alwaysShowLabel] and
455 * [animationProgress].
Max Yingcc44d972021-10-11 13:12:45 -0400456 *
457 * When [alwaysShowLabel] is true, the positions do not move. The [iconPlaceable] will be placed
458 * near the top of the item and the [labelPlaceable] will be placed near the bottom, according to
459 * the spec.
460 *
461 * When [animationProgress] is 1 (representing the selected state), the positions will be the same
462 * as above.
463 *
464 * Otherwise, when [animationProgress] is 0, [iconPlaceable] will be placed in the center, like in
465 * [placeIcon], and [labelPlaceable] will not be shown.
466 *
467 * When [animationProgress] is animating between these values, [iconPlaceable] and [labelPlaceable]
468 * will be placed at a corresponding interpolated position.
469 *
Max Ying51d3d9e2022-04-08 17:59:07 +0000470 * [indicatorRipplePlaceable] and [indicatorPlaceable] will always be placed in such a way that to
471 * share the same center as [iconPlaceable].
Max Yingcc44d972021-10-11 13:12:45 -0400472 *
473 * @param labelPlaceable text label placeable inside this item
474 * @param iconPlaceable icon placeable inside this item
Max Ying51d3d9e2022-04-08 17:59:07 +0000475 * @param indicatorRipplePlaceable indicator ripple placeable inside this item
Max Yingcc44d972021-10-11 13:12:45 -0400476 * @param indicatorPlaceable indicator placeable inside this item, if it exists
477 * @param constraints constraints of the item
478 * @param alwaysShowLabel whether to always show the label for this item. If true, icon and label
479 * positions will not change. If false, positions transition between 'centered icon with no label'
480 * and 'top aligned icon with label'.
481 * @param animationProgress progress of the animation, where 0 represents the unselected state of
482 * this item and 1 represents the selected state. Values between 0 and 1 interpolate positions of
483 * the icon and label.
484 */
485private fun MeasureScope.placeLabelAndIcon(
486 labelPlaceable: Placeable,
487 iconPlaceable: Placeable,
Max Ying51d3d9e2022-04-08 17:59:07 +0000488 indicatorRipplePlaceable: Placeable,
Max Yingcc44d972021-10-11 13:12:45 -0400489 indicatorPlaceable: Placeable?,
490 constraints: Constraints,
491 alwaysShowLabel: Boolean,
492 animationProgress: Float,
493): MeasureResult {
494 val height = constraints.maxHeight
495
496 val baseline = labelPlaceable[LastBaseline]
497 // Label should be `ItemVerticalPadding` from the bottom
498 val labelY = height - baseline - NavigationBarItemVerticalPadding.roundToPx()
499
500 // Icon (when selected) should be `ItemVerticalPadding` from the top
501 val selectedIconY = NavigationBarItemVerticalPadding.roundToPx()
502 val unselectedIconY =
503 if (alwaysShowLabel) selectedIconY else (height - iconPlaceable.height) / 2
504
505 // How far the icon needs to move between unselected and selected states.
506 val iconDistance = unselectedIconY - selectedIconY
507
508 // The interpolated fraction of iconDistance that all placeables need to move based on
509 // animationProgress.
510 val offset = (iconDistance * (1 - animationProgress)).roundToInt()
511
512 val containerWidth = constraints.maxWidth
513
514 val labelX = (containerWidth - labelPlaceable.width) / 2
515 val iconX = (containerWidth - iconPlaceable.width) / 2
516
Max Ying51d3d9e2022-04-08 17:59:07 +0000517 val rippleX = (containerWidth - indicatorRipplePlaceable.width) / 2
518 val rippleY = selectedIconY - IndicatorVerticalPadding.roundToPx()
519
Max Yingcc44d972021-10-11 13:12:45 -0400520 return layout(containerWidth, height) {
521 indicatorPlaceable?.let {
522 val indicatorX = (containerWidth - it.width) / 2
523 val indicatorY = selectedIconY - IndicatorVerticalPadding.roundToPx()
524 it.placeRelative(indicatorX, indicatorY + offset)
525 }
526 if (alwaysShowLabel || animationProgress != 0f) {
527 labelPlaceable.placeRelative(labelX, labelY + offset)
528 }
529 iconPlaceable.placeRelative(iconX, selectedIconY + offset)
Max Ying51d3d9e2022-04-08 17:59:07 +0000530 indicatorRipplePlaceable.placeRelative(rippleX, rippleY + offset)
Max Yingcc44d972021-10-11 13:12:45 -0400531 }
532}
533
Max Ying51d3d9e2022-04-08 17:59:07 +0000534private const val IndicatorRippleLayoutIdTag: String = "indicatorRipple"
535
Max Yingcc44d972021-10-11 13:12:45 -0400536private const val IndicatorLayoutIdTag: String = "indicator"
537
538private const val IconLayoutIdTag: String = "icon"
539
540private const val LabelLayoutIdTag: String = "label"
541
Jose Alba Aguadofe2421c2021-11-02 17:37:45 +0100542private val NavigationBarHeight: Dp = NavigationBarTokens.ContainerHeight
Max Yingcc44d972021-10-11 13:12:45 -0400543
544private const val ItemAnimationDurationMillis: Int = 100
545
Max Ying51d3d9e2022-04-08 17:59:07 +0000546/*@VisibleForTesting*/
547internal val NavigationBarItemHorizontalPadding: Dp = 8.dp
Max Yingcc44d972021-10-11 13:12:45 -0400548
549/*@VisibleForTesting*/
550internal val NavigationBarItemVerticalPadding: Dp = 16.dp
551
Max Yingcc44d972021-10-11 13:12:45 -0400552private val IndicatorHorizontalPadding: Dp =
Jose Alba Aguadofe2421c2021-11-02 17:37:45 +0100553 (NavigationBarTokens.ActiveIndicatorWidth - NavigationBarTokens.IconSize) / 2
Max Yingcc44d972021-10-11 13:12:45 -0400554
555private val IndicatorVerticalPadding: Dp =
Max Ying51d3d9e2022-04-08 17:59:07 +0000556 (NavigationBarTokens.ActiveIndicatorHeight - NavigationBarTokens.IconSize) / 2
557
558private val IndicatorVerticalOffset: Dp = 12.dp