mirror of
https://github.com/BetterAndroid/FlexiUI.git
synced 2025-09-08 19:44:25 +08:00
refactor: merge rippleColor to rippleStyle
This commit is contained in:
@@ -48,20 +48,21 @@ import com.highcapable.flexiui.LocalColors
|
|||||||
import com.highcapable.flexiui.LocalShapes
|
import com.highcapable.flexiui.LocalShapes
|
||||||
import com.highcapable.flexiui.LocalSizes
|
import com.highcapable.flexiui.LocalSizes
|
||||||
import com.highcapable.flexiui.extension.borderOrNot
|
import com.highcapable.flexiui.extension.borderOrNot
|
||||||
import com.highcapable.flexiui.extension.orElse
|
|
||||||
import com.highcapable.flexiui.extension.status
|
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.rippleClickable
|
||||||
import com.highcapable.flexiui.interaction.rippleToggleable
|
import com.highcapable.flexiui.interaction.rippleToggleable
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
data class ButtonColors(
|
data class ButtonColors(
|
||||||
val rippleColor: Color,
|
|
||||||
val contentColor: Color,
|
val contentColor: Color,
|
||||||
val backgroundColor: Color
|
val backgroundColor: Color
|
||||||
)
|
)
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
data class ButtonStyle(
|
data class ButtonStyle(
|
||||||
|
val rippleStyle: RippleStyle,
|
||||||
val padding: PaddingValues,
|
val padding: PaddingValues,
|
||||||
val shape: Shape,
|
val shape: Shape,
|
||||||
val border: BorderStroke
|
val border: BorderStroke
|
||||||
@@ -91,9 +92,9 @@ fun Button(
|
|||||||
style = style,
|
style = style,
|
||||||
then = modifier
|
then = modifier
|
||||||
).rippleClickable(
|
).rippleClickable(
|
||||||
|
rippleStyle = style.rippleStyle,
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
role = Role.Button,
|
role = Role.Button,
|
||||||
rippleColor = colors.rippleColor,
|
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
onClick = onClick
|
onClick = onClick
|
||||||
)
|
)
|
||||||
@@ -132,8 +133,7 @@ fun IconButton(
|
|||||||
style = style,
|
style = style,
|
||||||
then = modifier
|
then = modifier
|
||||||
).rippleClickable(
|
).rippleClickable(
|
||||||
rippleColor = colors.rippleColor,
|
rippleStyle = style.rippleStyle,
|
||||||
bounded = false,
|
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
role = Role.Button,
|
role = Role.Button,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
@@ -162,8 +162,7 @@ fun IconToggleButton(
|
|||||||
then = modifier
|
then = modifier
|
||||||
).rippleToggleable(
|
).rippleToggleable(
|
||||||
value = checked,
|
value = checked,
|
||||||
rippleColor = colors.rippleColor,
|
rippleStyle = style.rippleStyle,
|
||||||
bounded = false,
|
|
||||||
onValueChange = onCheckedChange,
|
onValueChange = onCheckedChange,
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
role = Role.Checkbox,
|
role = Role.Checkbox,
|
||||||
@@ -197,10 +196,7 @@ object Button {
|
|||||||
val colors: ButtonColors
|
val colors: ButtonColors
|
||||||
@Composable
|
@Composable
|
||||||
@ReadOnlyComposable
|
@ReadOnlyComposable
|
||||||
get() = when (LocalInAreaBox.current) {
|
get() = defaultButtonColors()
|
||||||
true -> defaultButtonInBoxColors()
|
|
||||||
else -> defaultButtonOutBoxColors()
|
|
||||||
}
|
|
||||||
val style: ButtonStyle
|
val style: ButtonStyle
|
||||||
@Composable
|
@Composable
|
||||||
@ReadOnlyComposable
|
@ReadOnlyComposable
|
||||||
@@ -220,31 +216,22 @@ object IconButton {
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@ReadOnlyComposable
|
@ReadOnlyComposable
|
||||||
private fun defaultButtonInBoxColors() = ButtonColors(
|
private fun defaultButtonColors() =
|
||||||
rippleColor = LocalColors.current.themeSecondary,
|
when (LocalInAreaBox.current) {
|
||||||
contentColor = LocalColors.current.textPrimary,
|
true -> ButtonColors(
|
||||||
backgroundColor = LocalColors.current.foregroundSecondary
|
contentColor = LocalColors.current.textPrimary,
|
||||||
)
|
backgroundColor = LocalColors.current.foregroundSecondary
|
||||||
|
)
|
||||||
@Composable
|
else -> ButtonColors(
|
||||||
@ReadOnlyComposable
|
contentColor = Color.White,
|
||||||
private fun defaultButtonOutBoxColors() = ButtonColors(
|
backgroundColor = LocalColors.current.themePrimary
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@ReadOnlyComposable
|
@ReadOnlyComposable
|
||||||
private fun defaultButtonStyle() = ButtonStyle(
|
private fun defaultButtonStyle() = ButtonStyle(
|
||||||
|
rippleStyle = defaultButtonRippleStyle(),
|
||||||
padding = PaddingValues(
|
padding = PaddingValues(
|
||||||
horizontal = LocalSizes.current.spacingPrimary,
|
horizontal = LocalSizes.current.spacingPrimary,
|
||||||
vertical = LocalSizes.current.spacingSecondary
|
vertical = LocalSizes.current.spacingSecondary
|
||||||
@@ -253,14 +240,30 @@ private fun defaultButtonStyle() = ButtonStyle(
|
|||||||
border = defaultButtonBorder()
|
border = defaultButtonBorder()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
@ReadOnlyComposable
|
||||||
|
private fun defaultIconButtonColors() = ButtonColors(
|
||||||
|
contentColor = LocalColors.current.themePrimary,
|
||||||
|
backgroundColor = Color.Transparent
|
||||||
|
)
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@ReadOnlyComposable
|
@ReadOnlyComposable
|
||||||
private fun defaultIconButtonStyle() = ButtonStyle(
|
private fun defaultIconButtonStyle() = ButtonStyle(
|
||||||
|
rippleStyle = defaultButtonRippleStyle(),
|
||||||
padding = PaddingValues(),
|
padding = PaddingValues(),
|
||||||
shape = LocalShapes.current.tertiary,
|
shape = LocalShapes.current.tertiary,
|
||||||
border = defaultButtonBorder()
|
border = defaultButtonBorder()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
@ReadOnlyComposable
|
||||||
|
private fun defaultButtonRippleStyle() =
|
||||||
|
Interaction.rippleStyle.copy(color = when (LocalInAreaBox.current) {
|
||||||
|
true -> LocalColors.current.themeSecondary
|
||||||
|
else -> LocalColors.current.foregroundSecondary
|
||||||
|
})
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@ReadOnlyComposable
|
@ReadOnlyComposable
|
||||||
private fun defaultButtonBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)
|
private fun defaultButtonBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)
|
@@ -59,6 +59,7 @@ import com.highcapable.flexiui.LocalColors
|
|||||||
import com.highcapable.flexiui.LocalSizes
|
import com.highcapable.flexiui.LocalSizes
|
||||||
import com.highcapable.flexiui.extension.orElse
|
import com.highcapable.flexiui.extension.orElse
|
||||||
import com.highcapable.flexiui.extension.status
|
import com.highcapable.flexiui.extension.status
|
||||||
|
import com.highcapable.flexiui.interaction.Interaction
|
||||||
import com.highcapable.flexiui.interaction.rippleClickable
|
import com.highcapable.flexiui.interaction.rippleClickable
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@@ -141,9 +142,9 @@ fun NavigationItem(
|
|||||||
.then(modifier)
|
.then(modifier)
|
||||||
.background(animatedIndicatorColor)
|
.background(animatedIndicatorColor)
|
||||||
.rippleClickable(
|
.rippleClickable(
|
||||||
|
rippleStyle = Interaction.rippleStyle.copy(color = currentColors.indicatorColor),
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
role = Role.Tab,
|
role = Role.Tab,
|
||||||
rippleColor = currentColors.indicatorColor,
|
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
onClick = onClick
|
onClick = onClick
|
||||||
)
|
)
|
||||||
|
@@ -26,6 +26,7 @@ package com.highcapable.flexiui.interaction
|
|||||||
import androidx.compose.foundation.Indication
|
import androidx.compose.foundation.Indication
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
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.runtime.remember
|
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.semantics.Role
|
||||||
import androidx.compose.ui.unit.Dp
|
import androidx.compose.ui.unit.Dp
|
||||||
import com.highcapable.flexiui.LocalColors
|
import com.highcapable.flexiui.LocalColors
|
||||||
import com.highcapable.flexiui.extension.orElse
|
|
||||||
import androidx.compose.foundation.clickable as foundationClickable
|
import androidx.compose.foundation.clickable as foundationClickable
|
||||||
import androidx.compose.foundation.selection.selectable as foundationSelectable
|
import androidx.compose.foundation.selection.selectable as foundationSelectable
|
||||||
import androidx.compose.foundation.selection.toggleable as foundationToggleable
|
import androidx.compose.foundation.selection.toggleable as foundationToggleable
|
||||||
import androidx.compose.material.ripple.rememberRipple as materialRememberRipple
|
import androidx.compose.material.ripple.rememberRipple as materialRememberRipple
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
data class RippleStyle(
|
||||||
|
val bounded: Boolean,
|
||||||
|
val radius: Dp,
|
||||||
|
val color: Color
|
||||||
|
)
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun rememberRipple(
|
fun rememberRipple(style: RippleStyle) = materialRememberRipple(style.bounded, style.radius, style.color)
|
||||||
bounded: Boolean = true,
|
|
||||||
radius: Dp = Dp.Unspecified,
|
|
||||||
color: Color = Interaction.rippleColor
|
|
||||||
) = materialRememberRipple(bounded, radius, color)
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Modifier.clickable(
|
fun Modifier.clickable(
|
||||||
@@ -79,8 +82,7 @@ fun Modifier.selectable(
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Modifier.rippleClickable(
|
fun Modifier.rippleClickable(
|
||||||
rippleColor: Color = Interaction.rippleColor,
|
rippleStyle: RippleStyle = Interaction.rippleStyle,
|
||||||
bounded: Boolean = true,
|
|
||||||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
||||||
enabled: Boolean = true,
|
enabled: Boolean = true,
|
||||||
onClickLabel: String? = null,
|
onClickLabel: String? = null,
|
||||||
@@ -89,7 +91,7 @@ fun Modifier.rippleClickable(
|
|||||||
) = clickable(
|
) = clickable(
|
||||||
onClick = onClick,
|
onClick = onClick,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
indication = rememberRipple(bounded = bounded, color = rippleColor),
|
indication = rememberRipple(rippleStyle),
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
onClickLabel = onClickLabel,
|
onClickLabel = onClickLabel,
|
||||||
role = role
|
role = role
|
||||||
@@ -98,8 +100,7 @@ fun Modifier.rippleClickable(
|
|||||||
@Composable
|
@Composable
|
||||||
fun Modifier.rippleToggleable(
|
fun Modifier.rippleToggleable(
|
||||||
value: Boolean,
|
value: Boolean,
|
||||||
rippleColor: Color = Interaction.rippleColor,
|
rippleStyle: RippleStyle = Interaction.rippleStyle,
|
||||||
bounded: Boolean = true,
|
|
||||||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
||||||
enabled: Boolean = true,
|
enabled: Boolean = true,
|
||||||
role: Role? = null,
|
role: Role? = null,
|
||||||
@@ -107,7 +108,7 @@ fun Modifier.rippleToggleable(
|
|||||||
) = toggleable(
|
) = toggleable(
|
||||||
value = value,
|
value = value,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
indication = rememberRipple(bounded = bounded, color = rippleColor),
|
indication = rememberRipple(rippleStyle),
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
role = role,
|
role = role,
|
||||||
onValueChange = onValueChange
|
onValueChange = onValueChange
|
||||||
@@ -116,8 +117,7 @@ fun Modifier.rippleToggleable(
|
|||||||
@Composable
|
@Composable
|
||||||
fun Modifier.rippleSelectable(
|
fun Modifier.rippleSelectable(
|
||||||
selected: Boolean,
|
selected: Boolean,
|
||||||
rippleColor: Color = Interaction.rippleColor,
|
rippleStyle: RippleStyle = Interaction.rippleStyle,
|
||||||
bounded: Boolean = true,
|
|
||||||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
||||||
enabled: Boolean = true,
|
enabled: Boolean = true,
|
||||||
role: Role? = null,
|
role: Role? = null,
|
||||||
@@ -125,21 +125,25 @@ fun Modifier.rippleSelectable(
|
|||||||
) = selectable(
|
) = selectable(
|
||||||
selected = selected,
|
selected = selected,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
indication = rememberRipple(bounded = bounded, color = rippleColor),
|
indication = rememberRipple(rippleStyle),
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
role = role,
|
role = role,
|
||||||
onClick = onClick
|
onClick = onClick
|
||||||
)
|
)
|
||||||
|
|
||||||
object Interaction {
|
object Interaction {
|
||||||
val rippleColor: Color
|
val rippleStyle: RippleStyle
|
||||||
@Composable
|
@Composable
|
||||||
@ReadOnlyComposable
|
@ReadOnlyComposable
|
||||||
get() = LocalRippleColor.current.orElse() ?: defaultInteractionRippleColor()
|
get() = LocalRippleStyle.current ?: defaultRippleStyle()
|
||||||
}
|
}
|
||||||
|
|
||||||
val LocalRippleColor = compositionLocalOf { Color.Unspecified }
|
val LocalRippleStyle = compositionLocalOf<RippleStyle?> { null }
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@ReadOnlyComposable
|
@ReadOnlyComposable
|
||||||
private fun defaultInteractionRippleColor() = LocalColors.current.themeSecondary
|
private fun defaultRippleStyle() = RippleStyle(
|
||||||
|
bounded = true,
|
||||||
|
radius = Dp.Unspecified,
|
||||||
|
color = LocalColors.current.themeSecondary
|
||||||
|
)
|
Reference in New Issue
Block a user