Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.compose.material3 |
| 18 | |
| 19 | import androidx.compose.animation.animateColorAsState |
| 20 | import androidx.compose.animation.core.animateFloatAsState |
| 21 | import androidx.compose.animation.core.tween |
| 22 | import androidx.compose.foundation.background |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 23 | import androidx.compose.foundation.indication |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 24 | import androidx.compose.foundation.interaction.Interaction |
| 25 | import androidx.compose.foundation.interaction.MutableInteractionSource |
| 26 | import androidx.compose.foundation.layout.Arrangement |
| 27 | import androidx.compose.foundation.layout.Box |
| 28 | import androidx.compose.foundation.layout.Row |
| 29 | import androidx.compose.foundation.layout.RowScope |
| 30 | import androidx.compose.foundation.layout.fillMaxWidth |
| 31 | import androidx.compose.foundation.layout.height |
| 32 | import androidx.compose.foundation.layout.padding |
| 33 | import androidx.compose.foundation.selection.selectable |
| 34 | import androidx.compose.foundation.selection.selectableGroup |
| 35 | import androidx.compose.material.ripple.rememberRipple |
Jose Alba Aguado | fe2421c | 2021-11-02 17:37:45 +0100 | [diff] [blame] | 36 | import androidx.compose.material3.tokens.NavigationBarTokens |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 37 | import androidx.compose.runtime.Composable |
| 38 | import androidx.compose.runtime.CompositionLocalProvider |
| 39 | import androidx.compose.runtime.Stable |
| 40 | import androidx.compose.runtime.State |
| 41 | import androidx.compose.runtime.getValue |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 42 | import androidx.compose.runtime.mutableStateOf |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 43 | import androidx.compose.runtime.remember |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 44 | import androidx.compose.runtime.setValue |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 45 | import androidx.compose.ui.Alignment |
| 46 | import androidx.compose.ui.Modifier |
| 47 | import androidx.compose.ui.draw.alpha |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 48 | import androidx.compose.ui.draw.clip |
| 49 | import androidx.compose.ui.geometry.Offset |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 50 | import androidx.compose.ui.graphics.Color |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 51 | import androidx.compose.ui.layout.LastBaseline |
| 52 | import androidx.compose.ui.layout.Layout |
| 53 | import androidx.compose.ui.layout.MeasureResult |
| 54 | import androidx.compose.ui.layout.MeasureScope |
| 55 | import androidx.compose.ui.layout.Placeable |
| 56 | import androidx.compose.ui.layout.layoutId |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 57 | import androidx.compose.ui.layout.onSizeChanged |
| 58 | import androidx.compose.ui.platform.LocalDensity |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 59 | import androidx.compose.ui.semantics.Role |
| 60 | import androidx.compose.ui.unit.Constraints |
| 61 | import androidx.compose.ui.unit.Dp |
| 62 | import androidx.compose.ui.unit.dp |
| 63 | import kotlin.math.roundToInt |
| 64 | |
| 65 | /** |
Shalom Gibly | f42290e | 2022-04-12 02:12:48 -0700 | [diff] [blame] | 66 | * <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 Rout | 2f69d940 | 2021-10-19 16:25:18 +0200 | [diff] [blame] | 71 | * ![Navigation bar image](https://developer.android.com/images/reference/androidx/compose/material3/navigation-bar.png) |
| 72 | * |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 73 | * [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 Ying | 2384bc8 | 2022-04-28 20:20:58 +0000 | [diff] [blame] | 82 | * @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 Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 92 | */ |
| 93 | @Composable |
| 94 | fun NavigationBar( |
| 95 | modifier: Modifier = Modifier, |
Mariano | 15a489b | 2022-01-19 13:08:12 -0500 | [diff] [blame] | 96 | containerColor: Color = NavigationBarTokens.ContainerColor.toColor(), |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 97 | contentColor: Color = MaterialTheme.colorScheme.contentColorFor(containerColor), |
Jose Alba Aguado | fe2421c | 2021-11-02 17:37:45 +0100 | [diff] [blame] | 98 | tonalElevation: Dp = NavigationBarTokens.ContainerElevation, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 99 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 109 | horizontalArrangement = Arrangement.spacedBy(NavigationBarItemHorizontalPadding), |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 110 | content = content |
| 111 | ) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Material Design navigation bar item. |
| 117 | * |
Shalom Gibly | f42290e | 2022-04-12 02:12:48 -0700 | [diff] [blame] | 118 | * Navigation bars offer a persistent and convenient way to switch between primary destinations in |
| 119 | * an app. |
| 120 | * |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 121 | * 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 Ying | 2384bc8 | 2022-04-28 20:20:58 +0000 | [diff] [blame] | 134 | * @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 Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 140 | * @param label optional text label for this item |
Max Ying | 2384bc8 | 2022-04-28 20:20:58 +0000 | [diff] [blame] | 141 | * @param alwaysShowLabel whether to always show the label for this item. If `false`, the label will |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 142 | * only be shown when this item is selected. |
| 143 | * @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s |
Max Ying | 2384bc8 | 2022-04-28 20:20:58 +0000 | [diff] [blame] | 144 | * 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 Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 148 | */ |
| 149 | @Composable |
| 150 | fun 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 Aguado | fe2421c | 2021-11-02 17:37:45 +0100 | [diff] [blame] | 168 | val style = MaterialTheme.typography.fromToken(NavigationBarTokens.LabelTextFont) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 169 | val textColor by colors.textColor(selected = selected) |
| 170 | CompositionLocalProvider(LocalContentColor provides textColor) { |
| 171 | ProvideTextStyle(style, content = label) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 176 | var itemWidth by remember { mutableStateOf(0) } |
| 177 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 178 | Box( |
| 179 | modifier |
| 180 | .selectable( |
| 181 | selected = selected, |
| 182 | onClick = onClick, |
| 183 | enabled = enabled, |
| 184 | role = Role.Tab, |
| 185 | interactionSource = interactionSource, |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 186 | indication = null, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 187 | ) |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 188 | .weight(1f) |
| 189 | .onSizeChanged { |
| 190 | itemWidth = it.width |
| 191 | }, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 192 | contentAlignment = Alignment.Center |
| 193 | ) { |
| 194 | val animationProgress: Float by animateFloatAsState( |
| 195 | targetValue = if (selected) 1f else 0f, |
| 196 | animationSpec = tween(ItemAnimationDurationMillis) |
| 197 | ) |
| 198 | |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 199 | // 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 Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 223 | val indicator = @Composable { |
| 224 | Box( |
| 225 | Modifier.layoutId(IndicatorLayoutIdTag) |
| 226 | .background( |
| 227 | color = colors.indicatorColor.copy(alpha = animationProgress), |
Jose Alba Aguado | 3205603 | 2022-03-22 12:50:19 +0100 | [diff] [blame] | 228 | shape = NavigationBarTokens.ActiveIndicatorShape.toShape(), |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 229 | ) |
| 230 | ) |
| 231 | } |
| 232 | |
| 233 | NavigationBarItemBaselineLayout( |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 234 | indicatorRipple = indicatorRipple, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 235 | indicator = indicator, |
| 236 | icon = styledIcon, |
| 237 | label = styledLabel, |
| 238 | alwaysShowLabel = alwaysShowLabel, |
| 239 | animationProgress = animationProgress |
| 240 | ) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** Defaults used in [NavigationBarItem]. */ |
| 245 | object 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( |
Mariano | 15a489b | 2022-01-19 13:08:12 -0500 | [diff] [blame] | 259 | 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 Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 264 | ): 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 |
| 283 | interface 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 |
| 306 | private 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 337 | * @param indicatorRipple indicator ripple for this item when it is selected |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 338 | * @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 |
| 348 | private fun NavigationBarItemBaselineLayout( |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 349 | indicatorRipple: @Composable () -> Unit, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 350 | indicator: @Composable () -> Unit, |
| 351 | icon: @Composable () -> Unit, |
| 352 | label: @Composable (() -> Unit)?, |
| 353 | alwaysShowLabel: Boolean, |
| 354 | animationProgress: Float, |
| 355 | ) { |
| 356 | Layout({ |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 357 | indicatorRipple() |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 358 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 368 | .padding(horizontal = NavigationBarItemHorizontalPadding / 2) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 369 | ) { 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 378 | val indicatorRipplePlaceable = |
| 379 | measurables |
| 380 | .first { it.layoutId == IndicatorRippleLayoutIdTag } |
| 381 | .measure( |
| 382 | Constraints.fixed( |
| 383 | width = totalIndicatorWidth, |
| 384 | height = indicatorHeight |
| 385 | ) |
| 386 | ) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 387 | 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 Ying | 2384bc8 | 2022-04-28 20:20:58 +0000 | [diff] [blame] | 402 | // Measure with loose constraints for height as we don't want the label to |
| 403 | // take up more space than it needs |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 404 | constraints.copy(minHeight = 0) |
| 405 | ) |
| 406 | } |
| 407 | |
| 408 | if (label == null) { |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 409 | placeIcon(iconPlaceable, indicatorRipplePlaceable, indicatorPlaceable, constraints) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 410 | } else { |
| 411 | placeLabelAndIcon( |
| 412 | labelPlaceable!!, |
| 413 | iconPlaceable, |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 414 | indicatorRipplePlaceable, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 415 | indicatorPlaceable, |
| 416 | constraints, |
| 417 | alwaysShowLabel, |
| 418 | animationProgress |
| 419 | ) |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 425 | * Places the provided [Placeable]s in the center of the provided [constraints]. |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 426 | */ |
| 427 | private fun MeasureScope.placeIcon( |
| 428 | iconPlaceable: Placeable, |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 429 | indicatorRipplePlaceable: Placeable, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 430 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 439 | val rippleX = (width - indicatorRipplePlaceable.width) / 2 |
| 440 | val rippleY = (height - indicatorRipplePlaceable.height) / 2 |
| 441 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 442 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 449 | indicatorRipplePlaceable.placeRelative(rippleX, rippleY) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 454 | * Places the provided [Placeable]s in the correct position, depending on [alwaysShowLabel] and |
| 455 | * [animationProgress]. |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 456 | * |
| 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 470 | * [indicatorRipplePlaceable] and [indicatorPlaceable] will always be placed in such a way that to |
| 471 | * share the same center as [iconPlaceable]. |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 472 | * |
| 473 | * @param labelPlaceable text label placeable inside this item |
| 474 | * @param iconPlaceable icon placeable inside this item |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 475 | * @param indicatorRipplePlaceable indicator ripple placeable inside this item |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 476 | * @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 | */ |
| 485 | private fun MeasureScope.placeLabelAndIcon( |
| 486 | labelPlaceable: Placeable, |
| 487 | iconPlaceable: Placeable, |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 488 | indicatorRipplePlaceable: Placeable, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 489 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 517 | val rippleX = (containerWidth - indicatorRipplePlaceable.width) / 2 |
| 518 | val rippleY = selectedIconY - IndicatorVerticalPadding.roundToPx() |
| 519 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 520 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 530 | indicatorRipplePlaceable.placeRelative(rippleX, rippleY + offset) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 534 | private const val IndicatorRippleLayoutIdTag: String = "indicatorRipple" |
| 535 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 536 | private const val IndicatorLayoutIdTag: String = "indicator" |
| 537 | |
| 538 | private const val IconLayoutIdTag: String = "icon" |
| 539 | |
| 540 | private const val LabelLayoutIdTag: String = "label" |
| 541 | |
Jose Alba Aguado | fe2421c | 2021-11-02 17:37:45 +0100 | [diff] [blame] | 542 | private val NavigationBarHeight: Dp = NavigationBarTokens.ContainerHeight |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 543 | |
| 544 | private const val ItemAnimationDurationMillis: Int = 100 |
| 545 | |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 546 | /*@VisibleForTesting*/ |
| 547 | internal val NavigationBarItemHorizontalPadding: Dp = 8.dp |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 548 | |
| 549 | /*@VisibleForTesting*/ |
| 550 | internal val NavigationBarItemVerticalPadding: Dp = 16.dp |
| 551 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 552 | private val IndicatorHorizontalPadding: Dp = |
Jose Alba Aguado | fe2421c | 2021-11-02 17:37:45 +0100 | [diff] [blame] | 553 | (NavigationBarTokens.ActiveIndicatorWidth - NavigationBarTokens.IconSize) / 2 |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 554 | |
| 555 | private val IndicatorVerticalPadding: Dp = |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame^] | 556 | (NavigationBarTokens.ActiveIndicatorHeight - NavigationBarTokens.IconSize) / 2 |
| 557 | |
| 558 | private val IndicatorVerticalOffset: Dp = 12.dp |