refactor: replace diameter to radius

This commit is contained in:
2023-11-14 02:21:28 +08:00
parent e6f1590bb1
commit 41705fb307
4 changed files with 31 additions and 27 deletions

View File

@@ -69,7 +69,7 @@ internal interface IProgressIndicatorStyle {
data class CircularIndicatorStyle( data class CircularIndicatorStyle(
override val strokeWidth: Dp, override val strokeWidth: Dp,
override val strokeCap: StrokeCap, override val strokeCap: StrokeCap,
val diameter: Dp, val radius: Dp,
val rotationDuration: Int, val rotationDuration: Int,
val rotationsPerCycle: Int, val rotationsPerCycle: Int,
val startAngleOffset: Float, val startAngleOffset: Float,
@@ -114,13 +114,14 @@ fun CircularProgressIndicator(
colors: ProgressIndicatorColors = ProgressIndicator.circularColors, colors: ProgressIndicatorColors = ProgressIndicator.circularColors,
style: CircularIndicatorStyle = ProgressIndicator.circularStyle style: CircularIndicatorStyle = ProgressIndicator.circularStyle
) { ) {
val diameter = style.radius * 2
val stroke = with(LocalDensity.current) { Stroke(width = style.strokeWidth.toPx(), cap = style.strokeCap) } val stroke = with(LocalDensity.current) { Stroke(width = style.strokeWidth.toPx(), cap = style.strokeCap) }
@Composable @Composable
fun Determinate() { fun Determinate() {
val coercedProgress = progress.coerceIn(min, max) val coercedProgress = progress.coerceIn(min, max)
val normalizedProgress = (coercedProgress - min) / (max - min) val normalizedProgress = (coercedProgress - min) / (max - min)
Canvas(modifier.progressSemantics(normalizedProgress).size(style.diameter)) { Canvas(modifier.progressSemantics(normalizedProgress).size(diameter)) {
val startAngle = 270f val startAngle = 270f
val sweep = normalizedProgress * 360f val sweep = normalizedProgress * 360f
drawCircularIndicatorBackground(colors.backgroundColor, stroke) drawCircularIndicatorBackground(colors.backgroundColor, stroke)
@@ -175,13 +176,13 @@ fun CircularProgressIndicator(
} }
) )
) )
Canvas(modifier.progressSemantics().size(style.diameter)) { Canvas(modifier.progressSemantics().size(diameter)) {
drawCircularIndicatorBackground(colors.backgroundColor, stroke) drawCircularIndicatorBackground(colors.backgroundColor, stroke)
val rotationAngleOffset = caleRotationAngleOffset(style.baseRotationAngle, style.jumpRotationAngle) val rotationAngleOffset = caleRotationAngleOffset(style.baseRotationAngle, style.jumpRotationAngle)
val currentRotationAngleOffset = (currentRotation * rotationAngleOffset) % 360f val currentRotationAngleOffset = (currentRotation * rotationAngleOffset) % 360f
val sweep = abs(endAngle - startAngle) val sweep = abs(endAngle - startAngle)
val offset = style.startAngleOffset + currentRotationAngleOffset + baseRotation val offset = style.startAngleOffset + currentRotationAngleOffset + baseRotation
drawIndeterminateCircularIndicator(startAngle + offset, style.strokeWidth, style.diameter, sweep, colors.foregroundColor, stroke) drawIndeterminateCircularIndicator(startAngle + offset, style.strokeWidth, diameter, sweep, colors.foregroundColor, stroke)
} }
} }
if (indeterminate) Indeterminate() else Determinate() if (indeterminate) Indeterminate() else Determinate()
@@ -389,7 +390,7 @@ private fun defaultLinearIndicatorColors() = ProgressIndicatorColors(
private fun defaultCircularIndicatorStyle() = CircularIndicatorStyle( private fun defaultCircularIndicatorStyle() = CircularIndicatorStyle(
strokeWidth = DefaultIndicatorStrokeWidth, strokeWidth = DefaultIndicatorStrokeWidth,
strokeCap = StrokeCap.Round, strokeCap = StrokeCap.Round,
diameter = DefaultCircularIndicatorDiameter, radius = DefaultCircularIndicatorRadius,
rotationDuration = DefaultRotationDuration, rotationDuration = DefaultRotationDuration,
rotationsPerCycle = DefaultRotationsPerCycle, rotationsPerCycle = DefaultRotationsPerCycle,
startAngleOffset = DefaultStartAngleOffset, startAngleOffset = DefaultStartAngleOffset,
@@ -425,7 +426,7 @@ private fun caleHeadAndTailAnimationDuration(rotationDuration: Int) = (rotationD
private val DefaultIndicatorStrokeWidth = 4.dp private val DefaultIndicatorStrokeWidth = 4.dp
private val DefaultLinearIndicatorWidth = 240.dp private val DefaultLinearIndicatorWidth = 240.dp
private val DefaultCircularIndicatorDiameter = 40.dp private val DefaultCircularIndicatorRadius = 20.dp
private const val DefaultLinearAnimationDuration = 1800 private const val DefaultLinearAnimationDuration = 1800
private const val DefaultFirstLineHeadDuration = 750 private const val DefaultFirstLineHeadDuration = 750

View File

@@ -65,9 +65,9 @@ data class RadioButtonColors(
@Immutable @Immutable
data class RadioButtonStyle( data class RadioButtonStyle(
val contentDiameter: Dp, val contentRadius: Dp,
val contentShadowSize: Dp, val contentShadowSize: Dp,
val strokeDiameter: Dp, val strokeRadius: Dp,
val pressedGain: Float, val pressedGain: Float,
val hoveredGain: Float, val hoveredGain: Float,
val shape: Shape, val shape: Shape,
@@ -86,6 +86,8 @@ fun RadioButton(
contentSpacing: Dp = RadioButton.contentSpacing, contentSpacing: Dp = RadioButton.contentSpacing,
content: @Composable () -> Unit = {} content: @Composable () -> Unit = {}
) { ) {
val contentDiameter = style.contentRadius * 2
val strokeDiameter = style.strokeRadius * 2
val hovered by interactionSource.collectIsHoveredAsState() val hovered by interactionSource.collectIsHoveredAsState()
val pressed by interactionSource.collectIsPressedAsState() val pressed by interactionSource.collectIsPressedAsState()
val animatedStrokeScale by animateFloatAsState(if (pressed) style.pressedGain else 1f) val animatedStrokeScale by animateFloatAsState(if (pressed) style.pressedGain else 1f)
@@ -100,13 +102,13 @@ fun RadioButton(
enabled = enabled, enabled = enabled,
role = Role.RadioButton, role = Role.RadioButton,
onClick = onClick onClick = onClick
).size(style.strokeDiameter) ).size(strokeDiameter)
.scale(animatedStrokeScale) .scale(animatedStrokeScale)
.background(animatedColor, style.shape), .background(animatedColor, style.shape),
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Box( Box(
modifier = Modifier.size(style.contentDiameter) modifier = Modifier.size(contentDiameter)
.scale(animatedContentScale) .scale(animatedContentScale)
.shadow(animatedContentShadow, style.shape) .shadow(animatedContentShadow, style.shape)
.alpha(animatedContentAlpha) .alpha(animatedContentAlpha)
@@ -146,9 +148,9 @@ private fun defaultRadioButtonColors() = RadioButtonColors(
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultRadioButtonStyle() = RadioButtonStyle( private fun defaultRadioButtonStyle() = RadioButtonStyle(
contentDiameter = DefaultContentDiameter, contentRadius = DefaultContentRadius,
contentShadowSize = DefaultContentShadowSize, contentShadowSize = DefaultContentShadowSize,
strokeDiameter = DefaultStrokeDiameter, strokeRadius = DefaultStrokeRadius,
pressedGain = DefaultPressedGain, pressedGain = DefaultPressedGain,
hoveredGain = DefaultHoveredGain, hoveredGain = DefaultHoveredGain,
shape = CircleShape, shape = CircleShape,
@@ -163,8 +165,8 @@ private fun defaultRadioButtonBorder() = BorderStroke(LocalSizes.current.borderS
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultRadioButtonContentSpacing() = LocalSizes.current.spacingSecondary private fun defaultRadioButtonContentSpacing() = LocalSizes.current.spacingSecondary
private val DefaultContentDiameter = 10.dp private val DefaultContentRadius = 5.dp
private val DefaultStrokeDiameter = 20.dp private val DefaultStrokeRadius = 10.dp
private const val DefaultPressedGain = 0.9f private const val DefaultPressedGain = 0.9f
private const val DefaultHoveredGain = 1.2f private const val DefaultHoveredGain = 1.2f

View File

@@ -75,7 +75,7 @@ data class SliderColors(
@Immutable @Immutable
data class SliderStyle( data class SliderStyle(
val thumbDiameter: Dp, val thumbRadius: Dp,
val thumbGain: Float, val thumbGain: Float,
val thumbShadowSize: Dp, val thumbShadowSize: Dp,
val thumbShape: Shape, val thumbShape: Shape,
@@ -101,15 +101,15 @@ fun Slider(
onValueChangeFinished: (() -> Unit)? = null, onValueChangeFinished: (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() } interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
) { ) {
val thumbDiameter = style.thumbRadius * 2
val hovered by interactionSource.collectIsHoveredAsState() val hovered by interactionSource.collectIsHoveredAsState()
var dragging by remember { mutableStateOf(false) } var dragging by remember { mutableStateOf(false) }
val thumbRadius = style.thumbDiameter / 2
val animatedScale by animateFloatAsState(if (hovered || dragging) style.thumbGain else 1f) val animatedScale by animateFloatAsState(if (hovered || dragging) style.thumbGain else 1f)
val maxOffset = with(LocalDensity.current) { (style.trackWidth - style.thumbDiameter).toPx() } val maxOffset = with(LocalDensity.current) { (style.trackWidth - thumbDiameter).toPx() }
val offsetXFromValue = (value.coerceIn(min, max) - min) / (max - min) * maxOffset val offsetXFromValue = (value.coerceIn(min, max) - min) / (max - min) * maxOffset
var absOffsetX by remember { mutableStateOf(0f) } var absOffsetX by remember { mutableStateOf(0f) }
var offsetX by remember { mutableStateOf(offsetXFromValue) } var offsetX by remember { mutableStateOf(offsetXFromValue) }
val draggedOffsetX = with(LocalDensity.current) { (offsetX.toDp() + thumbRadius).toPx() } val draggedOffsetX = with(LocalDensity.current) { (offsetX.toDp() + style.thumbRadius).toPx() }
fun updateValue() { fun updateValue() {
val newValue = (offsetX / maxOffset) * (max - min) + min val newValue = (offsetX / maxOffset) * (max - min) + min
onValueChange(newValue) onValueChange(newValue)
@@ -135,7 +135,7 @@ fun Slider(
@Composable @Composable
fun Thumb() { fun Thumb() {
Box( Box(
modifier = Modifier.size(style.thumbDiameter) modifier = Modifier.size(thumbDiameter)
.offset { IntOffset(offsetX.roundToInt(), 0) } .offset { IntOffset(offsetX.roundToInt(), 0) }
.scale(animatedScale) .scale(animatedScale)
.shadow(style.thumbShadowSize, style.thumbShape) .shadow(style.thumbShadowSize, style.thumbShape)
@@ -172,7 +172,7 @@ fun Slider(
.pointerInput(Unit) { .pointerInput(Unit) {
if (enabled) detectTapGestures( if (enabled) detectTapGestures(
onTap = { offset -> onTap = { offset ->
val tapedOffsetX = offset.x - thumbRadius.toPx() val tapedOffsetX = offset.x - style.thumbRadius.toPx()
offsetX = tapedOffsetX.coerceIn(0f, maxOffset) offsetX = tapedOffsetX.coerceIn(0f, maxOffset)
updateValue() updateValue()
onValueChangeFinished?.invoke() onValueChangeFinished?.invoke()
@@ -208,7 +208,7 @@ private fun defaultSliderColors() = SliderColors(
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultSliderStyle() = SliderStyle( private fun defaultSliderStyle() = SliderStyle(
thumbDiameter = DefaultThumbDiameter, thumbRadius = DefaultThumbRadius,
thumbGain = DefaultThumbGain, thumbGain = DefaultThumbGain,
thumbShadowSize = DefaultThumbShadowSize, thumbShadowSize = DefaultThumbShadowSize,
thumbShape = CircleShape, thumbShape = CircleShape,
@@ -223,7 +223,7 @@ private fun defaultSliderStyle() = SliderStyle(
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultSliderBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary) private fun defaultSliderBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)
private val DefaultThumbDiameter = 20.dp private val DefaultThumbRadius = 10.dp
private const val DefaultThumbGain = 1.1f private const val DefaultThumbGain = 1.1f
private val DefaultThumbShadowSize = 0.5.dp private val DefaultThumbShadowSize = 0.5.dp

View File

@@ -76,7 +76,7 @@ data class SwitchColors(
@Immutable @Immutable
data class SwitchStyle( data class SwitchStyle(
val padding: Dp, val padding: Dp,
val thumbDiameter: Dp, val thumbRadius: Dp,
val thumbGain: Float, val thumbGain: Float,
val thumbShadowSize: Dp, val thumbShadowSize: Dp,
val thumbShape: Shape, val thumbShape: Shape,
@@ -99,7 +99,8 @@ fun Switch(
contentSpacing: Dp = Switch.contentSpacing, contentSpacing: Dp = Switch.contentSpacing,
content: @Composable () -> Unit = {} content: @Composable () -> Unit = {}
) { ) {
val maxOffset = with(LocalDensity.current) { (style.trackWidth - style.thumbDiameter - style.padding * 2).toPx() } val thumbDiameter = style.thumbRadius * 2
val maxOffset = with(LocalDensity.current) { (style.trackWidth - thumbDiameter - style.padding * 2).toPx() }
val halfWidth = maxOffset / 2 val halfWidth = maxOffset / 2
val hovered by interactionSource.collectIsHoveredAsState() val hovered by interactionSource.collectIsHoveredAsState()
var dragging by remember { mutableStateOf(false) } var dragging by remember { mutableStateOf(false) }
@@ -141,7 +142,7 @@ fun Switch(
@Composable @Composable
fun Thumb() { fun Thumb() {
Box( Box(
modifier = Modifier.size(style.thumbDiameter) modifier = Modifier.size(thumbDiameter)
.offset { IntOffset((if (efficientDragging) offsetX else animatedOffsetX).roundToInt(), 0) } .offset { IntOffset((if (efficientDragging) offsetX else animatedOffsetX).roundToInt(), 0) }
.scale(animatedScale) .scale(animatedScale)
.shadow(style.thumbShadowSize, style.thumbShape) .shadow(style.thumbShadowSize, style.thumbShape)
@@ -216,7 +217,7 @@ private fun defaultSwitchColors() = SwitchColors(
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultSwitchStyle() = SwitchStyle( private fun defaultSwitchStyle() = SwitchStyle(
padding = DefaultSwitchPadding, padding = DefaultSwitchPadding,
thumbDiameter = DefaultThumbDiameter, thumbRadius = DefaultThumbRadius,
thumbGain = DefaultThumbGain, thumbGain = DefaultThumbGain,
thumbShadowSize = DefaultThumbShadowSize, thumbShadowSize = DefaultThumbShadowSize,
thumbShape = CircleShape, thumbShape = CircleShape,
@@ -237,7 +238,7 @@ private fun defaultSwitchContentSpacing() = LocalSizes.current.spacingSecondary
private val DefaultSwitchPadding = 3.dp private val DefaultSwitchPadding = 3.dp
private val DefaultThumbDiameter = 15.dp private val DefaultThumbRadius = 7.5.dp
private const val DefaultThumbGain = 1.1f private const val DefaultThumbGain = 1.1f
private val DefaultThumbShadowSize = 0.5.dp private val DefaultThumbShadowSize = 0.5.dp