feat: add single icon button colors and style and some tweaks

This commit is contained in:
2023-11-19 00:17:09 +08:00
parent 07b7f53dfd
commit d0c468692f

View File

@@ -30,6 +30,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable import androidx.compose.runtime.Immutable
@@ -42,6 +43,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape import androidx.compose.ui.graphics.Shape
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 androidx.compose.ui.unit.dp
import com.highcapable.flexiui.LocalColors 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
@@ -87,30 +89,27 @@ fun Button(
backgroundColor = Color.Transparent backgroundColor = Color.Transparent
) )
Box( Box(
modifier = Modifier.status(enabled) modifier = Modifier.button(
.clip(style.shape) enabled = enabled,
.background(colors.backgroundColor, style.shape) colors = colors,
.borderOrNot(style.border, style.shape) style = style,
.then(modifier) modifier = modifier
.rippleClickable( ).rippleClickable(
enabled = enabled, enabled = enabled,
role = Role.Button, role = Role.Button,
rippleColor = colors.rippleColor, rippleColor = colors.rippleColor,
interactionSource = interactionSource, interactionSource = interactionSource,
onClick = onClick onClick = onClick
) )
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalIconTint provides colors.contentColor,
LocalTextStyle provides localTextStyle, LocalTextStyle provides localTextStyle,
LocalProgressIndicatorColors provides localProgressIndicatorColors LocalProgressIndicatorColors provides localProgressIndicatorColors
) { ) {
Row( Row(
modifier = Modifier.padding( modifier = Modifier.buttonPadding(style),
top = style.topPadding.orElse() ?: style.padding, verticalAlignment = Alignment.CenterVertically
start = style.startPadding.orElse() ?: style.padding,
bottom = style.bottomPadding.orElse() ?: style.padding,
end = style.endPadding.orElse() ?: style.padding
)
) { ) {
header() header()
content() content()
@@ -124,22 +123,28 @@ fun Button(
fun IconButton( fun IconButton(
onClick: () -> Unit, onClick: () -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
rippleColor: Color = Button.colors.rippleColor, colors: ButtonColors = IconButton.colors,
style: ButtonStyle = IconButton.style,
enabled: Boolean = true, enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit content: @Composable () -> Unit
) { ) {
Box( Box(
modifier = modifier.rippleClickable( modifier = Modifier.button(
rippleColor = rippleColor, enabled = enabled,
colors = colors,
style = style,
modifier = modifier
).rippleClickable(
rippleColor = colors.rippleColor,
bounded = false, bounded = false,
enabled = enabled, enabled = enabled,
role = Role.Button, role = Role.Button,
interactionSource = interactionSource, interactionSource = interactionSource,
onClick = onClick onClick = onClick
), ).buttonPadding(style),
contentAlignment = Alignment.Center, contentAlignment = Alignment.Center,
) { content() } ) { CompositionLocalProvider(LocalIconTint provides colors.contentColor, content = content) }
} }
@Composable @Composable
@@ -147,25 +152,49 @@ fun IconToggleButton(
checked: Boolean, checked: Boolean,
onCheckedChange: (Boolean) -> Unit, onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
rippleColor: Color = Button.colors.rippleColor, colors: ButtonColors = IconButton.colors,
style: ButtonStyle = IconButton.style,
enabled: Boolean = true, enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit content: @Composable () -> Unit
) { ) {
Box( Box(
modifier = modifier.rippleToggleable( modifier = Modifier.button(
enabled = enabled,
colors = colors,
style = style,
modifier = modifier
).rippleToggleable(
value = checked, value = checked,
rippleColor = rippleColor, rippleColor = colors.rippleColor,
bounded = false, bounded = false,
onValueChange = onCheckedChange, onValueChange = onCheckedChange,
enabled = enabled, enabled = enabled,
role = Role.Checkbox, role = Role.Checkbox,
interactionSource = interactionSource interactionSource = interactionSource
), ).buttonPadding(style),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { content() } ) { CompositionLocalProvider(LocalIconTint provides colors.contentColor, content = content) }
} }
private fun Modifier.button(
enabled: Boolean,
colors: ButtonColors,
style: ButtonStyle,
modifier: Modifier
) = status(enabled)
.clip(style.shape)
.background(colors.backgroundColor, style.shape)
.borderOrNot(style.border, style.shape)
.then(modifier)
private fun Modifier.buttonPadding(style: ButtonStyle) = padding(
top = style.topPadding.orElse() ?: style.padding,
start = style.startPadding.orElse() ?: style.padding,
bottom = style.bottomPadding.orElse() ?: style.padding,
end = style.endPadding.orElse() ?: style.padding
)
object Button { object Button {
val colors: ButtonColors val colors: ButtonColors
@Composable @Composable
@@ -180,6 +209,17 @@ object Button {
get() = defaultButtonStyle() get() = defaultButtonStyle()
} }
object IconButton {
val colors: ButtonColors
@Composable
@ReadOnlyComposable
get() = defaultIconButtonColors()
val style: ButtonStyle
@Composable
@ReadOnlyComposable
get() = defaultIconButtonStyle()
}
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultButtonInBoxColors() = ButtonColors( private fun defaultButtonInBoxColors() = ButtonColors(
@@ -196,6 +236,14 @@ private fun defaultButtonOutBoxColors() = ButtonColors(
backgroundColor = LocalColors.current.themePrimary backgroundColor = LocalColors.current.themePrimary
) )
@Composable
@ReadOnlyComposable
private fun defaultIconButtonColors() = ButtonColors(
rippleColor = LocalColors.current.themeSecondary,
contentColor = LocalColors.current.themePrimary,
backgroundColor = Color.Transparent
)
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultButtonStyle() = ButtonStyle( private fun defaultButtonStyle() = ButtonStyle(
@@ -211,6 +259,18 @@ private fun defaultButtonStyle() = ButtonStyle(
border = defaultButtonBorder() border = defaultButtonBorder()
) )
@Composable
@ReadOnlyComposable
private fun defaultIconButtonStyle() = ButtonStyle(
padding = 0.dp,
topPadding = Dp.Unspecified,
startPadding = Dp.Unspecified,
bottomPadding = Dp.Unspecified,
endPadding = Dp.Unspecified,
shape = CircleShape,
border = defaultButtonBorder()
)
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultButtonBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary) private fun defaultButtonBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)