feat: add content and content spacing in Switch

This commit is contained in:
2023-11-10 23:02:44 +08:00
parent bcc3e18382
commit 9000855f80

View File

@@ -95,7 +95,9 @@ fun Switch(
colors: SwitchColors = Switch.colors, colors: SwitchColors = Switch.colors,
style: SwitchStyle = Switch.style, style: SwitchStyle = Switch.style,
enabled: Boolean = true, enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() } interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
contentSpacing: Dp = Switch.contentSpacing,
content: @Composable () -> Unit = {}
) { ) {
val maxOffset = with(LocalDensity.current) { (style.trackWidth - style.thumbDiameter - padding * 2).toPx() } val maxOffset = with(LocalDensity.current) { (style.trackWidth - style.thumbDiameter - padding * 2).toPx() }
val halfWidth = maxOffset / 2 val halfWidth = maxOffset / 2
@@ -113,11 +115,12 @@ fun Switch(
updateTrackColor() updateTrackColor()
val animatedTrackColor by animateColorAsState(trackColor) val animatedTrackColor by animateColorAsState(trackColor)
val efficientDragging = dragging && distance > 5 val efficientDragging = dragging && distance > 5
val sModifier = if (enabled) modifier else modifier.alpha(0.5f)
@Composable @Composable
fun Track(content: @Composable RowScope.() -> Unit) { fun Track(content: @Composable RowScope.() -> Unit) {
val sModifier = if (enabled) val cModifier = if (enabled)
modifier.clickable( Modifier.clickable(
interactionSource = interactionSource, interactionSource = interactionSource,
enabled = enabled, enabled = enabled,
role = Role.Switch role = Role.Switch
@@ -126,9 +129,9 @@ fun Switch(
offsetX = if (checked) 0f else maxOffset offsetX = if (checked) 0f else maxOffset
onCheckedChange(!checked) onCheckedChange(!checked)
} }
else modifier.alpha(0.5f) else Modifier.alpha(0.5f)
Row( Row(
modifier = sModifier modifier = cModifier
.background(if (efficientDragging) trackColor else animatedTrackColor, style.trackShape) .background(if (efficientDragging) trackColor else animatedTrackColor, style.trackShape)
.borderOrNot(style.trackBorder, style.trackShape) .borderOrNot(style.trackBorder, style.trackShape)
.size(style.trackWidth, style.trackHeight) .size(style.trackWidth, style.trackHeight)
@@ -171,7 +174,13 @@ fun Switch(
) )
) )
} }
Row(modifier = sModifier) {
Box(
modifier = Modifier.padding(end = contentSpacing)
.clickable(enabled = enabled) { onCheckedChange(!checked) }
) { content() }
Track { Thumb() } Track { Thumb() }
}
} }
object Switch { object Switch {
@@ -187,6 +196,10 @@ object Switch {
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = defaultSwitchStyle() get() = defaultSwitchStyle()
val contentSpacing: Dp
@Composable
@ReadOnlyComposable
get() = defaultSwitchContentSpacing()
} }
@Composable @Composable
@@ -215,6 +228,10 @@ private fun defaultSwitchStyle() = SwitchStyle(
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultSwitchBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary) private fun defaultSwitchBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)
@Composable
@ReadOnlyComposable
private fun defaultSwitchContentSpacing() = LocalSizes.current.spacingSecondary
private val DefaultSwitchPadding = 4.dp private val DefaultSwitchPadding = 4.dp
private val DefaultThumbDiameter = 12.dp private val DefaultThumbDiameter = 12.dp