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, |
José Figueroa | 9da8cec | 2022-05-26 14:59:39 -0400 | [diff] [blame^] | 96 | containerColor: Color = NavigationBarDefaults.ContainerColor, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 97 | contentColor: Color = MaterialTheme.colorScheme.contentColorFor(containerColor), |
José Figueroa | 9da8cec | 2022-05-26 14:59:39 -0400 | [diff] [blame^] | 98 | tonalElevation: Dp = NavigationBarDefaults.Elevation, |
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 | |
José Figueroa | 9da8cec | 2022-05-26 14:59:39 -0400 | [diff] [blame^] | 244 | /** Defaults used in [NavigationBar]. */ |
| 245 | object 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 Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 253 | /** Defaults used in [NavigationBarItem]. */ |
| 254 | object NavigationBarItemDefaults { |
José Figueroa | 9da8cec | 2022-05-26 14:59:39 -0400 | [diff] [blame^] | 255 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 256 | /** |
| 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( |
Mariano | 15a489b | 2022-01-19 13:08:12 -0500 | [diff] [blame] | 269 | 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 Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 274 | ): 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 |
| 293 | interface 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 |
| 316 | private 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 347 | * @param indicatorRipple indicator ripple for this item when it is selected |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 348 | * @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 |
| 358 | private fun NavigationBarItemBaselineLayout( |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 359 | indicatorRipple: @Composable () -> Unit, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 360 | indicator: @Composable () -> Unit, |
| 361 | icon: @Composable () -> Unit, |
| 362 | label: @Composable (() -> Unit)?, |
| 363 | alwaysShowLabel: Boolean, |
| 364 | animationProgress: Float, |
| 365 | ) { |
| 366 | Layout({ |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 367 | indicatorRipple() |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 368 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 378 | .padding(horizontal = NavigationBarItemHorizontalPadding / 2) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 379 | ) { 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 388 | val indicatorRipplePlaceable = |
| 389 | measurables |
| 390 | .first { it.layoutId == IndicatorRippleLayoutIdTag } |
| 391 | .measure( |
| 392 | Constraints.fixed( |
| 393 | width = totalIndicatorWidth, |
| 394 | height = indicatorHeight |
| 395 | ) |
| 396 | ) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 397 | 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 Ying | 2384bc8 | 2022-04-28 20:20:58 +0000 | [diff] [blame] | 412 | // Measure with loose constraints for height as we don't want the label to |
| 413 | // take up more space than it needs |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 414 | constraints.copy(minHeight = 0) |
| 415 | ) |
| 416 | } |
| 417 | |
| 418 | if (label == null) { |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 419 | placeIcon(iconPlaceable, indicatorRipplePlaceable, indicatorPlaceable, constraints) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 420 | } else { |
| 421 | placeLabelAndIcon( |
| 422 | labelPlaceable!!, |
| 423 | iconPlaceable, |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 424 | indicatorRipplePlaceable, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 425 | indicatorPlaceable, |
| 426 | constraints, |
| 427 | alwaysShowLabel, |
| 428 | animationProgress |
| 429 | ) |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 435 | * Places the provided [Placeable]s in the center of the provided [constraints]. |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 436 | */ |
| 437 | private fun MeasureScope.placeIcon( |
| 438 | iconPlaceable: Placeable, |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 439 | indicatorRipplePlaceable: Placeable, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 440 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 449 | val rippleX = (width - indicatorRipplePlaceable.width) / 2 |
| 450 | val rippleY = (height - indicatorRipplePlaceable.height) / 2 |
| 451 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 452 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 459 | indicatorRipplePlaceable.placeRelative(rippleX, rippleY) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 464 | * Places the provided [Placeable]s in the correct position, depending on [alwaysShowLabel] and |
| 465 | * [animationProgress]. |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 466 | * |
| 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 480 | * [indicatorRipplePlaceable] and [indicatorPlaceable] will always be placed in such a way that to |
| 481 | * share the same center as [iconPlaceable]. |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 482 | * |
| 483 | * @param labelPlaceable text label placeable inside this item |
| 484 | * @param iconPlaceable icon placeable inside this item |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 485 | * @param indicatorRipplePlaceable indicator ripple placeable inside this item |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 486 | * @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 | */ |
| 495 | private fun MeasureScope.placeLabelAndIcon( |
| 496 | labelPlaceable: Placeable, |
| 497 | iconPlaceable: Placeable, |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 498 | indicatorRipplePlaceable: Placeable, |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 499 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 527 | val rippleX = (containerWidth - indicatorRipplePlaceable.width) / 2 |
| 528 | val rippleY = selectedIconY - IndicatorVerticalPadding.roundToPx() |
| 529 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 530 | 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 Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 540 | indicatorRipplePlaceable.placeRelative(rippleX, rippleY + offset) |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 544 | private const val IndicatorRippleLayoutIdTag: String = "indicatorRipple" |
| 545 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 546 | private const val IndicatorLayoutIdTag: String = "indicator" |
| 547 | |
| 548 | private const val IconLayoutIdTag: String = "icon" |
| 549 | |
| 550 | private const val LabelLayoutIdTag: String = "label" |
| 551 | |
Jose Alba Aguado | fe2421c | 2021-11-02 17:37:45 +0100 | [diff] [blame] | 552 | private val NavigationBarHeight: Dp = NavigationBarTokens.ContainerHeight |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 553 | |
| 554 | private const val ItemAnimationDurationMillis: Int = 100 |
| 555 | |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 556 | /*@VisibleForTesting*/ |
| 557 | internal val NavigationBarItemHorizontalPadding: Dp = 8.dp |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 558 | |
| 559 | /*@VisibleForTesting*/ |
| 560 | internal val NavigationBarItemVerticalPadding: Dp = 16.dp |
| 561 | |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 562 | private val IndicatorHorizontalPadding: Dp = |
Jose Alba Aguado | fe2421c | 2021-11-02 17:37:45 +0100 | [diff] [blame] | 563 | (NavigationBarTokens.ActiveIndicatorWidth - NavigationBarTokens.IconSize) / 2 |
Max Ying | cc44d97 | 2021-10-11 13:12:45 -0400 | [diff] [blame] | 564 | |
| 565 | private val IndicatorVerticalPadding: Dp = |
Max Ying | 51d3d9e | 2022-04-08 17:59:07 +0000 | [diff] [blame] | 566 | (NavigationBarTokens.ActiveIndicatorHeight - NavigationBarTokens.IconSize) / 2 |
| 567 | |
| 568 | private val IndicatorVerticalOffset: Dp = 12.dp |