[go: nahoru, domu]

blob: c468caa7b0fb43b488ffaa8b093d654c4be7dd77 [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,
José Figueroa9da8cec2022-05-26 14:59:39 -040096 containerColor: Color = NavigationBarDefaults.ContainerColor,
Max Yingcc44d972021-10-11 13:12:45 -040097 contentColor: Color = MaterialTheme.colorScheme.contentColorFor(containerColor),
José Figueroa9da8cec2022-05-26 14:59:39 -040098 tonalElevation: Dp = NavigationBarDefaults.Elevation,
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
José Figueroa9da8cec2022-05-26 14:59:39 -0400244/** Defaults used in [NavigationBar]. */
245object NavigationBarDefaults {
246 /** Default color for a navigation bar. */
247 val ContainerColor: Color @Composable get() = NavigationBarTokens.ContainerColor.toColor()
248
249 /** Default elevation for a navigation bar. */
250 val Elevation: Dp = NavigationBarTokens.ContainerElevation
251}
252
Max Yingcc44d972021-10-11 13:12:45 -0400253/** Defaults used in [NavigationBarItem]. */
254object NavigationBarItemDefaults {
José Figueroa9da8cec2022-05-26 14:59:39 -0400255
Max Yingcc44d972021-10-11 13:12:45 -0400256 /**
257 * Creates a [NavigationBarItemColors] with the provided colors according to the Material
258 * specification.
259 *
260 * @param selectedIconColor the color to use for the icon when the item is selected.
261 * @param unselectedIconColor the color to use for the icon when the item is unselected.
262 * @param selectedTextColor the color to use for the text label when the item is selected.
263 * @param unselectedTextColor the color to use for the text label when the item is unselected.
264 * @param indicatorColor the color to use for the indicator when the item is selected.
265 * @return the resulting [NavigationBarItemColors] used for [NavigationBarItem]
266 */
267 @Composable
268 fun colors(
Mariano15a489b2022-01-19 13:08:12 -0500269 selectedIconColor: Color = NavigationBarTokens.ActiveIconColor.toColor(),
270 unselectedIconColor: Color = NavigationBarTokens.InactiveIconColor.toColor(),
271 selectedTextColor: Color = NavigationBarTokens.ActiveLabelTextColor.toColor(),
272 unselectedTextColor: Color = NavigationBarTokens.InactiveLabelTextColor.toColor(),
273 indicatorColor: Color = NavigationBarTokens.ActiveIndicatorColor.toColor(),
Max Yingcc44d972021-10-11 13:12:45 -0400274 ): NavigationBarItemColors = remember(
275 selectedIconColor,
276 unselectedIconColor,
277 selectedTextColor,
278 unselectedTextColor,
279 indicatorColor
280 ) {
281 DefaultNavigationBarItemColors(
282 selectedIconColor = selectedIconColor,
283 unselectedIconColor = unselectedIconColor,
284 selectedTextColor = selectedTextColor,
285 unselectedTextColor = unselectedTextColor,
286 selectedIndicatorColor = indicatorColor,
287 )
288 }
289}
290
291/** Represents the colors of the various elements of a navigation item. */
292@Stable
293interface NavigationBarItemColors {
294 /**
295 * Represents the icon color for this item, depending on whether it is [selected].
296 *
297 * @param selected whether the item is selected
298 */
299 @Composable
300 fun iconColor(selected: Boolean): State<Color>
301
302 /**
303 * Represents the text color for this item, depending on whether it is [selected].
304 *
305 * @param selected whether the item is selected
306 */
307 @Composable
308 fun textColor(selected: Boolean): State<Color>
309
310 /** Represents the color of the indicator used for selected items. */
311 val indicatorColor: Color
312 @Composable get
313}
314
315@Stable
316private class DefaultNavigationBarItemColors(
317 private val selectedIconColor: Color,
318 private val unselectedIconColor: Color,
319 private val selectedTextColor: Color,
320 private val unselectedTextColor: Color,
321 private val selectedIndicatorColor: Color,
322) : NavigationBarItemColors {
323 @Composable
324 override fun iconColor(selected: Boolean): State<Color> {
325 return animateColorAsState(
326 targetValue = if (selected) selectedIconColor else unselectedIconColor,
327 animationSpec = tween(ItemAnimationDurationMillis)
328 )
329 }
330
331 @Composable
332 override fun textColor(selected: Boolean): State<Color> {
333 return animateColorAsState(
334 targetValue = if (selected) selectedTextColor else unselectedTextColor,
335 animationSpec = tween(ItemAnimationDurationMillis)
336 )
337 }
338
339 override val indicatorColor: Color
340 @Composable
341 get() = selectedIndicatorColor
342}
343
344/**
345 * Base layout for a [NavigationBarItem].
346 *
Max Ying51d3d9e2022-04-08 17:59:07 +0000347 * @param indicatorRipple indicator ripple for this item when it is selected
Max Yingcc44d972021-10-11 13:12:45 -0400348 * @param indicator indicator for this item when it is selected
349 * @param icon icon for this item
350 * @param label text label for this item
351 * @param alwaysShowLabel whether to always show the label for this item. If false, the label will
352 * only be shown when this item is selected.
353 * @param animationProgress progress of the animation, where 0 represents the unselected state of
354 * this item and 1 represents the selected state. This value controls other values such as indicator
355 * size, icon and label positions, etc.
356 */
357@Composable
358private fun NavigationBarItemBaselineLayout(
Max Ying51d3d9e2022-04-08 17:59:07 +0000359 indicatorRipple: @Composable () -> Unit,
Max Yingcc44d972021-10-11 13:12:45 -0400360 indicator: @Composable () -> Unit,
361 icon: @Composable () -> Unit,
362 label: @Composable (() -> Unit)?,
363 alwaysShowLabel: Boolean,
364 animationProgress: Float,
365) {
366 Layout({
Max Ying51d3d9e2022-04-08 17:59:07 +0000367 indicatorRipple()
Max Yingcc44d972021-10-11 13:12:45 -0400368 if (animationProgress > 0) {
369 indicator()
370 }
371
372 Box(Modifier.layoutId(IconLayoutIdTag)) { icon() }
373
374 if (label != null) {
375 Box(
376 Modifier.layoutId(LabelLayoutIdTag)
377 .alpha(if (alwaysShowLabel) 1f else animationProgress)
Max Ying51d3d9e2022-04-08 17:59:07 +0000378 .padding(horizontal = NavigationBarItemHorizontalPadding / 2)
Max Yingcc44d972021-10-11 13:12:45 -0400379 ) { label() }
380 }
381 }) { measurables, constraints ->
382 val iconPlaceable =
383 measurables.first { it.layoutId == IconLayoutIdTag }.measure(constraints)
384
385 val totalIndicatorWidth = iconPlaceable.width + (IndicatorHorizontalPadding * 2).roundToPx()
386 val animatedIndicatorWidth = (totalIndicatorWidth * animationProgress).roundToInt()
387 val indicatorHeight = iconPlaceable.height + (IndicatorVerticalPadding * 2).roundToPx()
Max Ying51d3d9e2022-04-08 17:59:07 +0000388 val indicatorRipplePlaceable =
389 measurables
390 .first { it.layoutId == IndicatorRippleLayoutIdTag }
391 .measure(
392 Constraints.fixed(
393 width = totalIndicatorWidth,
394 height = indicatorHeight
395 )
396 )
Max Yingcc44d972021-10-11 13:12:45 -0400397 val indicatorPlaceable =
398 measurables
399 .firstOrNull { it.layoutId == IndicatorLayoutIdTag }
400 ?.measure(
401 Constraints.fixed(
402 width = animatedIndicatorWidth,
403 height = indicatorHeight
404 )
405 )
406
407 val labelPlaceable =
408 label?.let {
409 measurables
410 .first { it.layoutId == LabelLayoutIdTag }
411 .measure(
Max Ying2384bc82022-04-28 20:20:58 +0000412 // Measure with loose constraints for height as we don't want the label to
413 // take up more space than it needs
Max Yingcc44d972021-10-11 13:12:45 -0400414 constraints.copy(minHeight = 0)
415 )
416 }
417
418 if (label == null) {
Max Ying51d3d9e2022-04-08 17:59:07 +0000419 placeIcon(iconPlaceable, indicatorRipplePlaceable, indicatorPlaceable, constraints)
Max Yingcc44d972021-10-11 13:12:45 -0400420 } else {
421 placeLabelAndIcon(
422 labelPlaceable!!,
423 iconPlaceable,
Max Ying51d3d9e2022-04-08 17:59:07 +0000424 indicatorRipplePlaceable,
Max Yingcc44d972021-10-11 13:12:45 -0400425 indicatorPlaceable,
426 constraints,
427 alwaysShowLabel,
428 animationProgress
429 )
430 }
431 }
432}
433
434/**
Max Ying51d3d9e2022-04-08 17:59:07 +0000435 * Places the provided [Placeable]s in the center of the provided [constraints].
Max Yingcc44d972021-10-11 13:12:45 -0400436 */
437private fun MeasureScope.placeIcon(
438 iconPlaceable: Placeable,
Max Ying51d3d9e2022-04-08 17:59:07 +0000439 indicatorRipplePlaceable: Placeable,
Max Yingcc44d972021-10-11 13:12:45 -0400440 indicatorPlaceable: Placeable?,
441 constraints: Constraints
442): MeasureResult {
443 val width = constraints.maxWidth
444 val height = constraints.maxHeight
445
446 val iconX = (width - iconPlaceable.width) / 2
447 val iconY = (height - iconPlaceable.height) / 2
448
Max Ying51d3d9e2022-04-08 17:59:07 +0000449 val rippleX = (width - indicatorRipplePlaceable.width) / 2
450 val rippleY = (height - indicatorRipplePlaceable.height) / 2
451
Max Yingcc44d972021-10-11 13:12:45 -0400452 return layout(width, height) {
453 indicatorPlaceable?.let {
454 val indicatorX = (width - it.width) / 2
455 val indicatorY = (height - it.height) / 2
456 it.placeRelative(indicatorX, indicatorY)
457 }
458 iconPlaceable.placeRelative(iconX, iconY)
Max Ying51d3d9e2022-04-08 17:59:07 +0000459 indicatorRipplePlaceable.placeRelative(rippleX, rippleY)
Max Yingcc44d972021-10-11 13:12:45 -0400460 }
461}
462
463/**
Max Ying51d3d9e2022-04-08 17:59:07 +0000464 * Places the provided [Placeable]s in the correct position, depending on [alwaysShowLabel] and
465 * [animationProgress].
Max Yingcc44d972021-10-11 13:12:45 -0400466 *
467 * When [alwaysShowLabel] is true, the positions do not move. The [iconPlaceable] will be placed
468 * near the top of the item and the [labelPlaceable] will be placed near the bottom, according to
469 * the spec.
470 *
471 * When [animationProgress] is 1 (representing the selected state), the positions will be the same
472 * as above.
473 *
474 * Otherwise, when [animationProgress] is 0, [iconPlaceable] will be placed in the center, like in
475 * [placeIcon], and [labelPlaceable] will not be shown.
476 *
477 * When [animationProgress] is animating between these values, [iconPlaceable] and [labelPlaceable]
478 * will be placed at a corresponding interpolated position.
479 *
Max Ying51d3d9e2022-04-08 17:59:07 +0000480 * [indicatorRipplePlaceable] and [indicatorPlaceable] will always be placed in such a way that to
481 * share the same center as [iconPlaceable].
Max Yingcc44d972021-10-11 13:12:45 -0400482 *
483 * @param labelPlaceable text label placeable inside this item
484 * @param iconPlaceable icon placeable inside this item
Max Ying51d3d9e2022-04-08 17:59:07 +0000485 * @param indicatorRipplePlaceable indicator ripple placeable inside this item
Max Yingcc44d972021-10-11 13:12:45 -0400486 * @param indicatorPlaceable indicator placeable inside this item, if it exists
487 * @param constraints constraints of the item
488 * @param alwaysShowLabel whether to always show the label for this item. If true, icon and label
489 * positions will not change. If false, positions transition between 'centered icon with no label'
490 * and 'top aligned icon with label'.
491 * @param animationProgress progress of the animation, where 0 represents the unselected state of
492 * this item and 1 represents the selected state. Values between 0 and 1 interpolate positions of
493 * the icon and label.
494 */
495private fun MeasureScope.placeLabelAndIcon(
496 labelPlaceable: Placeable,
497 iconPlaceable: Placeable,
Max Ying51d3d9e2022-04-08 17:59:07 +0000498 indicatorRipplePlaceable: Placeable,
Max Yingcc44d972021-10-11 13:12:45 -0400499 indicatorPlaceable: Placeable?,
500 constraints: Constraints,
501 alwaysShowLabel: Boolean,
502 animationProgress: Float,
503): MeasureResult {
504 val height = constraints.maxHeight
505
506 val baseline = labelPlaceable[LastBaseline]
507 // Label should be `ItemVerticalPadding` from the bottom
508 val labelY = height - baseline - NavigationBarItemVerticalPadding.roundToPx()
509
510 // Icon (when selected) should be `ItemVerticalPadding` from the top
511 val selectedIconY = NavigationBarItemVerticalPadding.roundToPx()
512 val unselectedIconY =
513 if (alwaysShowLabel) selectedIconY else (height - iconPlaceable.height) / 2
514
515 // How far the icon needs to move between unselected and selected states.
516 val iconDistance = unselectedIconY - selectedIconY
517
518 // The interpolated fraction of iconDistance that all placeables need to move based on
519 // animationProgress.
520 val offset = (iconDistance * (1 - animationProgress)).roundToInt()
521
522 val containerWidth = constraints.maxWidth
523
524 val labelX = (containerWidth - labelPlaceable.width) / 2
525 val iconX = (containerWidth - iconPlaceable.width) / 2
526
Max Ying51d3d9e2022-04-08 17:59:07 +0000527 val rippleX = (containerWidth - indicatorRipplePlaceable.width) / 2
528 val rippleY = selectedIconY - IndicatorVerticalPadding.roundToPx()
529
Max Yingcc44d972021-10-11 13:12:45 -0400530 return layout(containerWidth, height) {
531 indicatorPlaceable?.let {
532 val indicatorX = (containerWidth - it.width) / 2
533 val indicatorY = selectedIconY - IndicatorVerticalPadding.roundToPx()
534 it.placeRelative(indicatorX, indicatorY + offset)
535 }
536 if (alwaysShowLabel || animationProgress != 0f) {
537 labelPlaceable.placeRelative(labelX, labelY + offset)
538 }
539 iconPlaceable.placeRelative(iconX, selectedIconY + offset)
Max Ying51d3d9e2022-04-08 17:59:07 +0000540 indicatorRipplePlaceable.placeRelative(rippleX, rippleY + offset)
Max Yingcc44d972021-10-11 13:12:45 -0400541 }
542}
543
Max Ying51d3d9e2022-04-08 17:59:07 +0000544private const val IndicatorRippleLayoutIdTag: String = "indicatorRipple"
545
Max Yingcc44d972021-10-11 13:12:45 -0400546private const val IndicatorLayoutIdTag: String = "indicator"
547
548private const val IconLayoutIdTag: String = "icon"
549
550private const val LabelLayoutIdTag: String = "label"
551
Jose Alba Aguadofe2421c2021-11-02 17:37:45 +0100552private val NavigationBarHeight: Dp = NavigationBarTokens.ContainerHeight
Max Yingcc44d972021-10-11 13:12:45 -0400553
554private const val ItemAnimationDurationMillis: Int = 100
555
Max Ying51d3d9e2022-04-08 17:59:07 +0000556/*@VisibleForTesting*/
557internal val NavigationBarItemHorizontalPadding: Dp = 8.dp
Max Yingcc44d972021-10-11 13:12:45 -0400558
559/*@VisibleForTesting*/
560internal val NavigationBarItemVerticalPadding: Dp = 16.dp
561
Max Yingcc44d972021-10-11 13:12:45 -0400562private val IndicatorHorizontalPadding: Dp =
Jose Alba Aguadofe2421c2021-11-02 17:37:45 +0100563 (NavigationBarTokens.ActiveIndicatorWidth - NavigationBarTokens.IconSize) / 2
Max Yingcc44d972021-10-11 13:12:45 -0400564
565private val IndicatorVerticalPadding: Dp =
Max Ying51d3d9e2022-04-08 17:59:07 +0000566 (NavigationBarTokens.ActiveIndicatorHeight - NavigationBarTokens.IconSize) / 2
567
568private val IndicatorVerticalOffset: Dp = 12.dp