refactor: merge LocalIconTint to LocalIconStyle and use style instead tint

This commit is contained in:
2023-12-02 21:45:01 +08:00
parent 9efb190230
commit 5ea0c7db42
8 changed files with 80 additions and 31 deletions

View File

@@ -261,7 +261,7 @@ class BasicActionBar internal constructor(
content: @Composable () -> Unit content: @Composable () -> Unit
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalIconTint provides color, LocalIconStyle provides LocalIconStyle.current.copy(tint = color),
LocalTextStyle provides LocalTextStyle.current.merge(textStyle ?: LocalTextStyle.current).copy(color = color), LocalTextStyle provides LocalTextStyle.current.merge(textStyle ?: LocalTextStyle.current).copy(color = color),
content = content content = content
) )

View File

@@ -101,7 +101,7 @@ fun Button(
) )
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalIconTint provides colors.contentColor, LocalIconStyle provides LocalIconStyle.current.copy(tint = colors.contentColor),
LocalTextStyle provides localTextStyle, LocalTextStyle provides localTextStyle,
LocalProgressIndicatorColors provides localProgressIndicatorColors LocalProgressIndicatorColors provides localProgressIndicatorColors
) { ) {
@@ -141,7 +141,12 @@ fun IconButton(
onClick = onClick onClick = onClick
).padding(style.padding), ).padding(style.padding),
contentAlignment = Alignment.Center, contentAlignment = Alignment.Center,
) { CompositionLocalProvider(LocalIconTint provides colors.contentColor, content = content) } ) {
CompositionLocalProvider(
LocalIconStyle provides LocalIconStyle.current.copy(tint = colors.contentColor),
content = content
)
}
} }
@Composable @Composable
@@ -170,7 +175,12 @@ fun IconToggleButton(
interactionSource = interactionSource interactionSource = interactionSource
).padding(style.padding), ).padding(style.padding),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { CompositionLocalProvider(LocalIconTint provides colors.contentColor, content = content) } ) {
CompositionLocalProvider(
LocalIconStyle provides LocalIconStyle.current.copy(tint = colors.contentColor),
content = content
)
}
} }
private fun Modifier.button( private fun Modifier.button(
@@ -252,7 +262,7 @@ private fun defaultButtonRippleStyle() =
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultIconButtonColors() = ButtonColors( private fun defaultIconButtonColors() = ButtonColors(
contentColor = LocalIconTint.current.orElse() ?: LocalColors.current.themePrimary, contentColor = LocalIconStyle.current.tint.orElse() ?: LocalColors.current.themePrimary,
backgroundColor = Color.Transparent backgroundColor = Color.Transparent
) )

View File

@@ -119,7 +119,7 @@ fun CheckBox(
scaleY = animatedContentLayer scaleY = animatedContentLayer
), ),
imageVector = Icons.CheckMark, imageVector = Icons.CheckMark,
tint = colors.contentColor style = Icon.style.copy(tint = colors.contentColor)
) )
} }
content?.also { content -> content?.also { content ->

View File

@@ -221,7 +221,7 @@ fun DropdownList(
rotationZ = animatedDirection rotationZ = animatedDirection
}.size(style.endIconSize), }.size(style.endIconSize),
imageVector = Icons.Dropdown, imageVector = Icons.Dropdown,
tint = animatedEndIconTint style = Icon.style.copy(tint = animatedEndIconTint)
) )
} }
DropdownMenu( DropdownMenu(
@@ -345,7 +345,7 @@ fun DropdownMenuItem(
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalIconTint provides currentColor, LocalIconStyle provides LocalIconStyle.current.copy(tint = currentColor),
LocalTextStyle provides LocalTextStyle.current.copy(color = currentColor) LocalTextStyle provides LocalTextStyle.current.copy(color = currentColor)
) { content() } ) { content() }
} }

View File

@@ -26,6 +26,7 @@ package com.highcapable.flexiui.component
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.ReadOnlyComposable import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.compositionLocalOf import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@@ -34,26 +35,37 @@ import androidx.compose.ui.draw.paint
import androidx.compose.ui.geometry.Size import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.isUnspecified
import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.toolingGraphicsLayer import androidx.compose.ui.graphics.toolingGraphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.rememberVectorPainter import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.role import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.isSpecified
import com.highcapable.flexiui.LocalSizes import com.highcapable.flexiui.LocalSizes
import com.highcapable.flexiui.extension.orElse
@Immutable
data class IconStyle(
val size: Dp,
val tint: Color
)
@Composable @Composable
fun Icon( fun Icon(
imageVector: ImageVector, imageVector: ImageVector,
contentDescription: String? = null, contentDescription: String? = null,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
tint: Color = LocalIconTint.current style: IconStyle = Icon.style
) { ) {
val painter = rememberVectorPainter(imageVector) val painter = rememberVectorPainter(imageVector)
Icon(painter, contentDescription, modifier, tint) Icon(painter, contentDescription, modifier, style)
} }
@Composable @Composable
@@ -61,10 +73,10 @@ fun Icon(
painter: Painter, painter: Painter,
contentDescription: String? = null, contentDescription: String? = null,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
tint: Color = LocalIconTint.current style: IconStyle = Icon.style
) { ) {
// TODO: b/149735981 semantics for content description // TODO: b/149735981 semantics for content description
val colorFilter = if (tint == Color.Unspecified) null else ColorFilter.tint(tint) val colorFilter = if (style.tint.isUnspecified) null else ColorFilter.tint(style.tint)
val semantics = if (contentDescription != null) val semantics = if (contentDescription != null)
Modifier.semantics { Modifier.semantics {
this.contentDescription = contentDescription this.contentDescription = contentDescription
@@ -73,7 +85,7 @@ fun Icon(
else Modifier else Modifier
Box( Box(
modifier = modifier.toolingGraphicsLayer() modifier = modifier.toolingGraphicsLayer()
.defaultSizeFor(painter) .defaultSizeFor(style, painter)
.paint( .paint(
painter, painter,
colorFilter = colorFilter, colorFilter = colorFilter,
@@ -83,16 +95,39 @@ fun Icon(
) )
} }
internal val LocalIconTint = compositionLocalOf { Color.Unspecified } private fun Modifier.defaultSizeFor(
style: IconStyle,
private fun Modifier.defaultSizeFor(painter: Painter) = composed { painter: Painter
then( ) = composed(
if (painter.intrinsicSize == Size.Unspecified || painter.intrinsicSize.isInfinite()) inspectorInfo = debugInspectorInfo {
Modifier.size(defaultIconSize()) name = "defaultSizeFor"
else Modifier properties["style"] = style
) properties["painter"] = painter
}
) {
then(when {
style.size.isSpecified ||
painter.intrinsicSize == Size.Unspecified ||
painter.intrinsicSize.isInfinite() ->
Modifier.size(style.size.orElse() ?: defaultIconSize())
else -> Modifier
})
} }
object Icon {
val style: IconStyle
@Composable
@ReadOnlyComposable
get() = LocalIconStyle.current
}
internal val LocalIconStyle = compositionLocalOf { DefaultIconStyle }
private val DefaultIconStyle = IconStyle(
size = Dp.Unspecified,
tint = Color.Unspecified
)
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultIconSize() = LocalSizes.current.iconSizePrimary private fun defaultIconSize() = LocalSizes.current.iconSizePrimary

View File

@@ -135,7 +135,8 @@ fun NavigationItem(
val currentContentShape = contentShape ?: LocalNavigationContentShape.current ?: Navigation.style.contentShape val currentContentShape = contentShape ?: LocalNavigationContentShape.current ?: Navigation.style.contentShape
val animatedIndicatorColor by animateColorAsState(if (selected) currentColors.indicatorColor else Color.Transparent) val animatedIndicatorColor by animateColorAsState(if (selected) currentColors.indicatorColor else Color.Transparent)
val animatedContentColor by animateColorAsState(if (selected) currentColors.selectedContentColor else currentColors.unselectedContentColor) val animatedContentColor by animateColorAsState(if (selected) currentColors.selectedContentColor else currentColors.unselectedContentColor)
val currentContentStyle = LocalTextStyle.current.copy(color = animatedContentColor) val currentIconStyle = LocalIconStyle.current.copy(tint = animatedContentColor)
val currentTextStyle = LocalTextStyle.current.copy(color = animatedContentColor)
Box( Box(
modifier = Modifier.status(enabled) modifier = Modifier.status(enabled)
.clip(currentContentShape) .clip(currentContentShape)
@@ -151,8 +152,8 @@ fun NavigationItem(
.padding(currentContentPadding) .padding(currentContentPadding)
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalIconTint provides animatedContentColor, LocalIconStyle provides currentIconStyle,
LocalTextStyle provides currentContentStyle LocalTextStyle provides currentTextStyle
) { ) {
if (currentHorizontal) if (currentHorizontal)
Row( Row(

View File

@@ -210,10 +210,11 @@ fun Tab(
val currentContentPadding = contentPadding ?: LocalTabContentPadding.current ?: Tab.style.contentPadding val currentContentPadding = contentPadding ?: LocalTabContentPadding.current ?: Tab.style.contentPadding
val currentContentShape = contentShape ?: LocalTabContentShape.current ?: Tab.style.contentShape val currentContentShape = contentShape ?: LocalTabContentShape.current ?: Tab.style.contentShape
val contentColor by animateColorAsState(if (selected) currentSelectedContentColor else currentUnselectedContentColor) val contentColor by animateColorAsState(if (selected) currentSelectedContentColor else currentUnselectedContentColor)
val contentStyle = LocalTextStyle.current.copy(color = contentColor) val contentIconStyle = LocalIconStyle.current.copy(tint = contentColor)
val contentTextStyle = LocalTextStyle.current.copy(color = contentColor)
CompositionLocalProvider( CompositionLocalProvider(
LocalIconTint provides contentColor, LocalIconStyle provides contentIconStyle,
LocalTextStyle provides contentStyle LocalTextStyle provides contentTextStyle
) { ) {
Row( Row(
modifier = Modifier.status(enabled) modifier = Modifier.status(enabled)

View File

@@ -667,9 +667,11 @@ private fun InnerDecorationBox(
content: @Composable () -> Unit content: @Composable () -> Unit
) { ) {
Box(modifier = Modifier.textFieldPadding(style, fitStart = header, fitEnd = footer)) { Box(modifier = Modifier.textFieldPadding(style, fitStart = header, fitEnd = footer)) {
CompositionLocalProvider(LocalIconTint provides decorTint) { CompositionLocalProvider(
content() LocalIconStyle provides LocalIconStyle.current.copy(tint = decorTint),
} LocalTextStyle provides LocalTextStyle.current.copy(color = decorTint),
content = content
)
} }
} }
@@ -683,7 +685,7 @@ private fun TextFieldDecorationBox(
val animatedAlpha by animateFloatAsState(if (value.isNotEmpty()) 0f else 1f) val animatedAlpha by animateFloatAsState(if (value.isNotEmpty()) 0f else 1f)
Box(modifier = Modifier.alpha(animatedAlpha)) { Box(modifier = Modifier.alpha(animatedAlpha)) {
CompositionLocalProvider( CompositionLocalProvider(
LocalIconTint provides placeholderContentColor, LocalIconStyle provides LocalIconStyle.current.copy(tint = placeholderContentColor),
LocalTextStyle provides LocalTextStyle.current.copy(color = placeholderContentColor), LocalTextStyle provides LocalTextStyle.current.copy(color = placeholderContentColor),
content = placeholder content = placeholder
) )