Fix disabled colors for navigation bar/rail.
Fixes: b/258867034
Test: new screenshot tests
Relnote: Added disabled colors for navigation bar and rail.
Change-Id: Ia78923757cb5c72813789a09f4fabb9a64c615af
diff --git a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/NavigationBar.kt b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/NavigationBar.kt
index 0844cfd..2746436 100644
--- a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/NavigationBar.kt
+++ b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/NavigationBar.kt
@@ -169,7 +169,7 @@
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
) {
val styledIcon = @Composable {
- val iconColor by colors.iconColor(selected = selected)
+ val iconColor by colors.iconColor(selected = selected, enabled = enabled)
// If there's a label, don't have a11y services repeat the icon description.
val clearSemantics = label != null && (alwaysShowLabel || selected)
Box(modifier = if (clearSemantics) Modifier.clearAndSetSemantics {} else Modifier) {
@@ -180,7 +180,7 @@
val styledLabel: @Composable (() -> Unit)? = label?.let {
@Composable {
val style = MaterialTheme.typography.fromToken(NavigationBarTokens.LabelTextFont)
- val textColor by colors.textColor(selected = selected)
+ val textColor by colors.textColor(selected = selected, enabled = enabled)
CompositionLocalProvider(LocalContentColor provides textColor) {
ProvideTextStyle(style, content = label)
}
@@ -286,6 +286,8 @@
* @param indicatorColor the color to use for the indicator when the item is selected.
* @param unselectedIconColor the color to use for the icon when the item is unselected.
* @param unselectedTextColor the color to use for the text label when the item is unselected.
+ * @param disabledIconColor the color to use for the icon when the item is disabled.
+ * @param disabledTextColor the color to use for the text label when the item is disabled.
* @return the resulting [NavigationBarItemColors] used for [NavigationBarItem]
*/
@Composable
@@ -295,12 +297,37 @@
indicatorColor: Color = NavigationBarTokens.ActiveIndicatorColor.toColor(),
unselectedIconColor: Color = NavigationBarTokens.InactiveIconColor.toColor(),
unselectedTextColor: Color = NavigationBarTokens.InactiveLabelTextColor.toColor(),
+ disabledIconColor: Color = unselectedIconColor.copy(alpha = DisabledAlpha),
+ disabledTextColor: Color = unselectedTextColor.copy(alpha = DisabledAlpha),
+ ): NavigationBarItemColors = NavigationBarItemColors(
+ selectedIconColor = selectedIconColor,
+ selectedTextColor = selectedTextColor,
+ selectedIndicatorColor = indicatorColor,
+ unselectedIconColor = unselectedIconColor,
+ unselectedTextColor = unselectedTextColor,
+ disabledIconColor = disabledIconColor,
+ disabledTextColor = disabledTextColor,
+ )
+
+ @Deprecated(
+ "Use overload with disabledIconColor and disabledTextColor",
+ level = DeprecationLevel.HIDDEN
+ )
+ @Composable
+ fun colors(
+ selectedIconColor: Color = NavigationBarTokens.ActiveIconColor.toColor(),
+ selectedTextColor: Color = NavigationBarTokens.ActiveLabelTextColor.toColor(),
+ indicatorColor: Color = NavigationBarTokens.ActiveIndicatorColor.toColor(),
+ unselectedIconColor: Color = NavigationBarTokens.InactiveIconColor.toColor(),
+ unselectedTextColor: Color = NavigationBarTokens.InactiveLabelTextColor.toColor(),
): NavigationBarItemColors = NavigationBarItemColors(
selectedIconColor = selectedIconColor,
selectedTextColor = selectedTextColor,
selectedIndicatorColor = indicatorColor,
unselectedIconColor = unselectedIconColor,
unselectedTextColor = unselectedTextColor,
+ disabledIconColor = unselectedIconColor.copy(alpha = DisabledAlpha),
+ disabledTextColor = unselectedTextColor.copy(alpha = DisabledAlpha),
)
}
@@ -311,16 +338,24 @@
private val selectedIndicatorColor: Color,
private val unselectedIconColor: Color,
private val unselectedTextColor: Color,
+ private val disabledIconColor: Color,
+ private val disabledTextColor: Color,
) {
/**
* Represents the icon color for this item, depending on whether it is [selected].
*
* @param selected whether the item is selected
+ * @param enabled whether the item is enabled
*/
@Composable
- internal fun iconColor(selected: Boolean): State<Color> {
+ internal fun iconColor(selected: Boolean, enabled: Boolean): State<Color> {
+ val targetValue = when {
+ !enabled -> disabledIconColor
+ selected -> selectedIconColor
+ else -> unselectedIconColor
+ }
return animateColorAsState(
- targetValue = if (selected) selectedIconColor else unselectedIconColor,
+ targetValue = targetValue,
animationSpec = tween(ItemAnimationDurationMillis)
)
}
@@ -329,11 +364,17 @@
* Represents the text color for this item, depending on whether it is [selected].
*
* @param selected whether the item is selected
+ * @param enabled whether the item is enabled
*/
@Composable
- internal fun textColor(selected: Boolean): State<Color> {
+ internal fun textColor(selected: Boolean, enabled: Boolean): State<Color> {
+ val targetValue = when {
+ !enabled -> disabledTextColor
+ selected -> selectedTextColor
+ else -> unselectedTextColor
+ }
return animateColorAsState(
- targetValue = if (selected) selectedTextColor else unselectedTextColor,
+ targetValue = targetValue,
animationSpec = tween(ItemAnimationDurationMillis)
)
}
@@ -351,6 +392,8 @@
if (selectedTextColor != other.selectedTextColor) return false
if (unselectedTextColor != other.unselectedTextColor) return false
if (selectedIndicatorColor != other.selectedIndicatorColor) return false
+ if (disabledIconColor != other.disabledIconColor) return false
+ if (disabledTextColor != other.disabledTextColor) return false
return true
}
@@ -360,6 +403,8 @@
result = 31 * result + selectedTextColor.hashCode()
result = 31 * result + unselectedTextColor.hashCode()
result = 31 * result + selectedIndicatorColor.hashCode()
+ result = 31 * result + disabledIconColor.hashCode()
+ result = 31 * result + disabledTextColor.hashCode()
return result
}