mirror of
https://github.com/BetterAndroid/FlexiUI.git
synced 2025-09-08 11:34:18 +08:00
feat: add content and content spacing in Switch
This commit is contained in:
@@ -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(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Track { Thumb() }
|
Row(modifier = sModifier) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(end = contentSpacing)
|
||||||
|
.clickable(enabled = enabled) { onCheckedChange(!checked) }
|
||||||
|
) { content() }
|
||||||
|
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
|
||||||
|
Reference in New Issue
Block a user