forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CircularProgressIndicator.kt
151 lines (148 loc) · 5.55 KB
/
CircularProgressIndicator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.microsoft.fluentui.tokenized.progress
import androidx.compose.animation.core.*
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.progressSemantics
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.ControlTokens
import com.microsoft.fluentui.theme.token.FluentStyle
import com.microsoft.fluentui.theme.token.controlTokens.CircularProgressIndicatorInfo
import com.microsoft.fluentui.theme.token.controlTokens.CircularProgressIndicatorSize
import com.microsoft.fluentui.theme.token.controlTokens.CircularProgressIndicatorTokens
import com.microsoft.fluentui.util.dpToPx
/**
* Create a Determinate Circular Progress Indicator
*
* @param progress Progress of the progress indicator. 0.0 represents no progress and 1.0 represents full progress.
* @param size Optional size of the circular progress indicator
* @param modifier Modifier for circular progress indicator
* @param style Style of progress indicator. Default: [FluentStyle.Neutral]
* @param circularProgressIndicatorTokens Token values for circular progress indicator
*
*/
@Composable
fun CircularProgressIndicator(
progress: Float,
size: CircularProgressIndicatorSize = CircularProgressIndicatorSize.XXSmall,
modifier: Modifier = Modifier,
style: FluentStyle = FluentStyle.Neutral,
circularProgressIndicatorTokens: CircularProgressIndicatorTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val tokens = circularProgressIndicatorTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.CircularProgressIndicatorControlType] as CircularProgressIndicatorTokens
val circularProgressIndicatorInfo = CircularProgressIndicatorInfo(
circularProgressIndicatorSize = size,
style = style
)
val currentProgress = animateFloatAsState(
targetValue = progress.coerceIn(0f..1f),
animationSpec = tween(
delayMillis = 0,
durationMillis = 750,
easing = LinearOutSlowInEasing
)
)
val circularProgressIndicatorColor =
tokens.brush(
circularProgressIndicatorInfo
)
val circularProgressIndicatorSize =
tokens.size(
circularProgressIndicatorInfo
)
val circularProgressIndicatorStrokeWidth =
tokens.strokeWidth(
circularProgressIndicatorInfo
)
val indicatorSizeInPx = dpToPx(circularProgressIndicatorSize)
Canvas(
modifier = modifier
.requiredSize(circularProgressIndicatorSize)
.progressSemantics(progress)
) {
drawArc(
circularProgressIndicatorColor,
-90f,
currentProgress.value * 360,
false,
size = Size(
indicatorSizeInPx,
indicatorSizeInPx
),
style = Stroke(dpToPx(circularProgressIndicatorStrokeWidth), cap = StrokeCap.Round)
)
}
}
/**
* Create an Indeterminate Circular Progress indicator
*
* @param size Optional size of the circular progress indicator
* @param modifier Modifier for circular progress indicator
* @param style Style of progress indicator. Default: [FluentStyle.Neutral]
* @param circularProgressIndicatorTokens Token values for circular progress indicator
*
*/
@Composable
fun CircularProgressIndicator(
size: CircularProgressIndicatorSize = CircularProgressIndicatorSize.XXSmall,
modifier: Modifier = Modifier,
style: FluentStyle = FluentStyle.Neutral,
circularProgressIndicatorTokens: CircularProgressIndicatorTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val tokens = circularProgressIndicatorTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.CircularProgressIndicatorControlType] as CircularProgressIndicatorTokens
val circularProgressIndicatorInfo = CircularProgressIndicatorInfo(
circularProgressIndicatorSize = size,
style = style
)
val circularProgressIndicatorColor =
tokens.brush(
circularProgressIndicatorInfo
)
val circularProgressIndicatorSize =
tokens.size(
circularProgressIndicatorInfo
)
val circularProgressIndicatorStrokeWidth =
tokens.strokeWidth(
circularProgressIndicatorInfo
)
val infiniteTransition = rememberInfiniteTransition()
val startAngle by infiniteTransition.animateFloat(
0f,
360f,
infiniteRepeatable(
animation = tween(
durationMillis = 1000,
easing = LinearEasing
)
)
)
val indicatorSizeInPx = dpToPx(circularProgressIndicatorSize)
Canvas(
modifier = modifier
.requiredSize(circularProgressIndicatorSize)
.progressSemantics()
) {
drawArc(
circularProgressIndicatorColor,
startAngle,
270f,
false,
size = Size(
indicatorSizeInPx, indicatorSizeInPx
),
style = Stroke(dpToPx(circularProgressIndicatorStrokeWidth), cap = StrokeCap.Round)
)
}
}