refactor: merge rippleColor to rippleStyle

This commit is contained in:
2023-12-02 11:56:09 +08:00
parent 16ce052ceb
commit 61eef027a6
3 changed files with 60 additions and 52 deletions

View File

@@ -48,20 +48,21 @@ import com.highcapable.flexiui.LocalColors
import com.highcapable.flexiui.LocalShapes
import com.highcapable.flexiui.LocalSizes
import com.highcapable.flexiui.extension.borderOrNot
import com.highcapable.flexiui.extension.orElse
import com.highcapable.flexiui.extension.status
import com.highcapable.flexiui.interaction.Interaction
import com.highcapable.flexiui.interaction.RippleStyle
import com.highcapable.flexiui.interaction.rippleClickable
import com.highcapable.flexiui.interaction.rippleToggleable
@Immutable
data class ButtonColors(
val rippleColor: Color,
val contentColor: Color,
val backgroundColor: Color
)
@Immutable
data class ButtonStyle(
val rippleStyle: RippleStyle,
val padding: PaddingValues,
val shape: Shape,
val border: BorderStroke
@@ -91,9 +92,9 @@ fun Button(
style = style,
then = modifier
).rippleClickable(
rippleStyle = style.rippleStyle,
enabled = enabled,
role = Role.Button,
rippleColor = colors.rippleColor,
interactionSource = interactionSource,
onClick = onClick
)
@@ -132,8 +133,7 @@ fun IconButton(
style = style,
then = modifier
).rippleClickable(
rippleColor = colors.rippleColor,
bounded = false,
rippleStyle = style.rippleStyle,
enabled = enabled,
role = Role.Button,
interactionSource = interactionSource,
@@ -162,8 +162,7 @@ fun IconToggleButton(
then = modifier
).rippleToggleable(
value = checked,
rippleColor = colors.rippleColor,
bounded = false,
rippleStyle = style.rippleStyle,
onValueChange = onCheckedChange,
enabled = enabled,
role = Role.Checkbox,
@@ -197,10 +196,7 @@ object Button {
val colors: ButtonColors
@Composable
@ReadOnlyComposable
get() = when (LocalInAreaBox.current) {
true -> defaultButtonInBoxColors()
else -> defaultButtonOutBoxColors()
}
get() = defaultButtonColors()
val style: ButtonStyle
@Composable
@ReadOnlyComposable
@@ -220,31 +216,22 @@ object IconButton {
@Composable
@ReadOnlyComposable
private fun defaultButtonInBoxColors() = ButtonColors(
rippleColor = LocalColors.current.themeSecondary,
contentColor = LocalColors.current.textPrimary,
backgroundColor = LocalColors.current.foregroundSecondary
)
@Composable
@ReadOnlyComposable
private fun defaultButtonOutBoxColors() = ButtonColors(
rippleColor = LocalColors.current.foregroundSecondary,
contentColor = Color.White,
backgroundColor = LocalColors.current.themePrimary
)
@Composable
@ReadOnlyComposable
private fun defaultIconButtonColors() = ButtonColors(
rippleColor = LocalColors.current.themeSecondary,
contentColor = LocalIconTint.current.orElse() ?: LocalColors.current.themePrimary,
backgroundColor = Color.Transparent
)
private fun defaultButtonColors() =
when (LocalInAreaBox.current) {
true -> ButtonColors(
contentColor = LocalColors.current.textPrimary,
backgroundColor = LocalColors.current.foregroundSecondary
)
else -> ButtonColors(
contentColor = Color.White,
backgroundColor = LocalColors.current.themePrimary
)
}
@Composable
@ReadOnlyComposable
private fun defaultButtonStyle() = ButtonStyle(
rippleStyle = defaultButtonRippleStyle(),
padding = PaddingValues(
horizontal = LocalSizes.current.spacingPrimary,
vertical = LocalSizes.current.spacingSecondary
@@ -253,14 +240,30 @@ private fun defaultButtonStyle() = ButtonStyle(
border = defaultButtonBorder()
)
@Composable
@ReadOnlyComposable
private fun defaultIconButtonColors() = ButtonColors(
contentColor = LocalColors.current.themePrimary,
backgroundColor = Color.Transparent
)
@Composable
@ReadOnlyComposable
private fun defaultIconButtonStyle() = ButtonStyle(
rippleStyle = defaultButtonRippleStyle(),
padding = PaddingValues(),
shape = LocalShapes.current.tertiary,
border = defaultButtonBorder()
)
@Composable
@ReadOnlyComposable
private fun defaultButtonRippleStyle() =
Interaction.rippleStyle.copy(color = when (LocalInAreaBox.current) {
true -> LocalColors.current.themeSecondary
else -> LocalColors.current.foregroundSecondary
})
@Composable
@ReadOnlyComposable
private fun defaultButtonBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)

View File

@@ -59,6 +59,7 @@ import com.highcapable.flexiui.LocalColors
import com.highcapable.flexiui.LocalSizes
import com.highcapable.flexiui.extension.orElse
import com.highcapable.flexiui.extension.status
import com.highcapable.flexiui.interaction.Interaction
import com.highcapable.flexiui.interaction.rippleClickable
@Immutable
@@ -141,9 +142,9 @@ fun NavigationItem(
.then(modifier)
.background(animatedIndicatorColor)
.rippleClickable(
rippleStyle = Interaction.rippleStyle.copy(color = currentColors.indicatorColor),
enabled = enabled,
role = Role.Tab,
rippleColor = currentColors.indicatorColor,
interactionSource = interactionSource,
onClick = onClick
)

View File

@@ -26,6 +26,7 @@ package com.highcapable.flexiui.interaction
import androidx.compose.foundation.Indication
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.remember
@@ -34,18 +35,20 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.Dp
import com.highcapable.flexiui.LocalColors
import com.highcapable.flexiui.extension.orElse
import androidx.compose.foundation.clickable as foundationClickable
import androidx.compose.foundation.selection.selectable as foundationSelectable
import androidx.compose.foundation.selection.toggleable as foundationToggleable
import androidx.compose.material.ripple.rememberRipple as materialRememberRipple
@Immutable
data class RippleStyle(
val bounded: Boolean,
val radius: Dp,
val color: Color
)
@Composable
fun rememberRipple(
bounded: Boolean = true,
radius: Dp = Dp.Unspecified,
color: Color = Interaction.rippleColor
) = materialRememberRipple(bounded, radius, color)
fun rememberRipple(style: RippleStyle) = materialRememberRipple(style.bounded, style.radius, style.color)
@Composable
fun Modifier.clickable(
@@ -79,8 +82,7 @@ fun Modifier.selectable(
@Composable
fun Modifier.rippleClickable(
rippleColor: Color = Interaction.rippleColor,
bounded: Boolean = true,
rippleStyle: RippleStyle = Interaction.rippleStyle,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
enabled: Boolean = true,
onClickLabel: String? = null,
@@ -89,7 +91,7 @@ fun Modifier.rippleClickable(
) = clickable(
onClick = onClick,
interactionSource = interactionSource,
indication = rememberRipple(bounded = bounded, color = rippleColor),
indication = rememberRipple(rippleStyle),
enabled = enabled,
onClickLabel = onClickLabel,
role = role
@@ -98,8 +100,7 @@ fun Modifier.rippleClickable(
@Composable
fun Modifier.rippleToggleable(
value: Boolean,
rippleColor: Color = Interaction.rippleColor,
bounded: Boolean = true,
rippleStyle: RippleStyle = Interaction.rippleStyle,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
enabled: Boolean = true,
role: Role? = null,
@@ -107,7 +108,7 @@ fun Modifier.rippleToggleable(
) = toggleable(
value = value,
interactionSource = interactionSource,
indication = rememberRipple(bounded = bounded, color = rippleColor),
indication = rememberRipple(rippleStyle),
enabled = enabled,
role = role,
onValueChange = onValueChange
@@ -116,8 +117,7 @@ fun Modifier.rippleToggleable(
@Composable
fun Modifier.rippleSelectable(
selected: Boolean,
rippleColor: Color = Interaction.rippleColor,
bounded: Boolean = true,
rippleStyle: RippleStyle = Interaction.rippleStyle,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
enabled: Boolean = true,
role: Role? = null,
@@ -125,21 +125,25 @@ fun Modifier.rippleSelectable(
) = selectable(
selected = selected,
interactionSource = interactionSource,
indication = rememberRipple(bounded = bounded, color = rippleColor),
indication = rememberRipple(rippleStyle),
enabled = enabled,
role = role,
onClick = onClick
)
object Interaction {
val rippleColor: Color
val rippleStyle: RippleStyle
@Composable
@ReadOnlyComposable
get() = LocalRippleColor.current.orElse() ?: defaultInteractionRippleColor()
get() = LocalRippleStyle.current ?: defaultRippleStyle()
}
val LocalRippleColor = compositionLocalOf { Color.Unspecified }
val LocalRippleStyle = compositionLocalOf<RippleStyle?> { null }
@Composable
@ReadOnlyComposable
private fun defaultInteractionRippleColor() = LocalColors.current.themeSecondary
private fun defaultRippleStyle() = RippleStyle(
bounded = true,
radius = Dp.Unspecified,
color = LocalColors.current.themeSecondary
)