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

View File

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

View File

@@ -75,7 +75,7 @@ data class SliderColors(
@Immutable
data class SliderStyle(
val thumbDiameter: Dp,
val thumbRadius: Dp,
val thumbGain: Float,
val thumbShadowSize: Dp,
val thumbShape: Shape,
@@ -101,15 +101,15 @@ fun Slider(
onValueChangeFinished: (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
) {
val thumbDiameter = style.thumbRadius * 2
val hovered by interactionSource.collectIsHoveredAsState()
var dragging by remember { mutableStateOf(false) }
val thumbRadius = style.thumbDiameter / 2
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
var absOffsetX by remember { mutableStateOf(0f) }
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() {
val newValue = (offsetX / maxOffset) * (max - min) + min
onValueChange(newValue)
@@ -135,7 +135,7 @@ fun Slider(
@Composable
fun Thumb() {
Box(
modifier = Modifier.size(style.thumbDiameter)
modifier = Modifier.size(thumbDiameter)
.offset { IntOffset(offsetX.roundToInt(), 0) }
.scale(animatedScale)
.shadow(style.thumbShadowSize, style.thumbShape)
@@ -172,7 +172,7 @@ fun Slider(
.pointerInput(Unit) {
if (enabled) detectTapGestures(
onTap = { offset ->
val tapedOffsetX = offset.x - thumbRadius.toPx()
val tapedOffsetX = offset.x - style.thumbRadius.toPx()
offsetX = tapedOffsetX.coerceIn(0f, maxOffset)
updateValue()
onValueChangeFinished?.invoke()
@@ -208,7 +208,7 @@ private fun defaultSliderColors() = SliderColors(
@Composable
@ReadOnlyComposable
private fun defaultSliderStyle() = SliderStyle(
thumbDiameter = DefaultThumbDiameter,
thumbRadius = DefaultThumbRadius,
thumbGain = DefaultThumbGain,
thumbShadowSize = DefaultThumbShadowSize,
thumbShape = CircleShape,
@@ -223,7 +223,7 @@ private fun defaultSliderStyle() = SliderStyle(
@ReadOnlyComposable
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 val DefaultThumbShadowSize = 0.5.dp

View File

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