-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspinner.py
332 lines (260 loc) · 9.43 KB
/
spinner.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
Components/Spinner
==================
.. seealso::
`Material Design spec, Menus <https://material.io/components/progress-indicators#circular-progress-indicators>`_
.. rubric:: Circular progress indicator in Google's Material Design.
Usage
-----
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDSpinner:
size_hint: None, None
size: dp(46), dp(46)
pos_hint: {'center_x': .5, 'center_y': .5}
active: True if check.active else False
MDCheckbox:
id: check
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': .5, 'center_y': .4}
active: True
'''
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
Test().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/spinner.gif
:align: center
Spinner palette
---------------
.. code-block:: kv
MDSpinner:
# The number of color values can be any.
palette:
[0.28627450980392155, 0.8431372549019608, 0.596078431372549, 1], \
[0.3568627450980392, 0.3215686274509804, 0.8666666666666667, 1], \
[0.8862745098039215, 0.36470588235294116, 0.592156862745098, 1], \
[0.8784313725490196, 0.9058823529411765, 0.40784313725490196, 1],
.. code-block:: python
MDSpinner(
size_hint=(None, None),
size=(dp(46), dp(46)),
pos_hint={'center_x': .5, 'center_y': .5},
active=True,
palette=[
[0.28627450980392155, 0.8431372549019608, 0.596078431372549, 1],
[0.3568627450980392, 0.3215686274509804, 0.8666666666666667, 1],
[0.8862745098039215, 0.36470588235294116, 0.592156862745098, 1],
[0.8784313725490196, 0.9058823529411765, 0.40784313725490196, 1],
]
)
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/spinner-palette.gif
:align: center
Determinate mode
----------------
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDSpinner:
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': .5, 'center_y': .5}
determinate: True
'''
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
Test().run()
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/spinner-determinate.gif
:align: center
"""
__all__ = ("MDSpinner",)
from typing import NoReturn, Union
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import (
BooleanProperty,
ColorProperty,
ListProperty,
NumericProperty,
)
from kivy.uix.widget import Widget
from kivymd.theming import ThemableBehavior
Builder.load_string(
"""
<MDSpinner>
canvas.before:
PushMatrix
Rotate:
angle: self._rotation_angle
origin: self.center
canvas:
Color:
rgba: self.color if self.color else self.theme_cls.primary_color
a: self._alpha
SmoothLine:
circle: self.center_x, self.center_y, self.width / 2, \
self._angle_start, self._angle_end
cap: 'square'
width: root.line_width
canvas.after:
PopMatrix
"""
)
class MDSpinner(ThemableBehavior, Widget):
"""
:class:`MDSpinner` is an implementation of the circular progress
indicator in `Google's Material Design`.
It can be used either as an indeterminate indicator that loops while
the user waits for something to happen, or as a determinate indicator.
Set :attr:`determinate` to **True** to activate determinate mode, and
:attr:`determinate_time` to set the duration of the animation.
:Events:
`on_determinate_complete`
The event is called at the end of the spinner loop in the
`determinate = True` mode.
"""
determinate = BooleanProperty(False)
"""
Determinate value.
:attr:`determinate` is a :class:`~kivy.properties.BooleanProperty`
and defaults to `False`.
"""
determinate_time = NumericProperty(2)
"""
Determinate time value.
:attr:`determinate_time` is a :class:`~kivy.properties.NumericProperty`
and defaults to `2`.
"""
line_width = NumericProperty(dp(2.25))
"""
Progress line width of spinner.
:attr:`line_width` is a :class:`~kivy.properties.NumericProperty`
and defaults to `dp(2.25)`.
"""
active = BooleanProperty(True)
"""
Use :attr:`active` to start or stop the spinner.
:attr:`active` is a :class:`~kivy.properties.BooleanProperty`
and defaults to `True`.
"""
color = ColorProperty(None, allownone=True)
"""
Spinner color.
:attr:`color` is a :class:`~kivy.properties.ColorProperty`
and defaults to `[0, 0, 0, 0]`.
"""
palette = ListProperty()
"""
A set of colors. Changes with each completed spinner cycle.
:attr:`palette` is a :class:`~kivy.properties.ListProperty`
and defaults to `[]`.
"""
_alpha = NumericProperty(0)
_rotation_angle = NumericProperty(360)
_angle_start = NumericProperty(0)
_angle_end = NumericProperty(0)
_palette = []
def __init__(self, **kwargs):
super().__init__(**kwargs)
if not self.color:
self.color = self.theme_cls.primary_color
if self.color == self.theme_cls.primary_color:
self.theme_cls.bind(primary_color=self._update_color)
self._alpha_anim_in = Animation(_alpha=1, duration=0.8, t="out_quad")
self._alpha_anim_out = Animation(_alpha=0, duration=0.3, t="out_quad")
self._alpha_anim_out.bind(
on_complete=self._reset,
on_progress=self._on_determinate_progress,
)
self.register_event_type("on_determinate_complete")
Clock.schedule_once(self.check_determinate)
def on__rotation_angle(self, *args):
if self._rotation_angle == 0:
self._rotation_angle = 360
if not self.determinate:
_rot_anim = Animation(_rotation_angle=0, duration=2)
_rot_anim.start(self)
elif self._rotation_angle == 360:
if self._palette:
try:
Animation(color=next(self._palette), duration=2).start(self)
except StopIteration:
self._palette = iter(self.palette)
Animation(color=next(self._palette), duration=2).start(self)
def on_palette(self, instance_spinner, palette_list: list) -> NoReturn:
self._palette = iter(palette_list)
def on_active(self, instance_spinner, active_value: bool) -> NoReturn:
self._reset()
if self.active:
self.check_determinate()
def on_determinate_complete(self, *args):
"""
The event is called at the end of the spinner loop in the
`determinate = True` mode.
"""
def check_determinate(self, interval: Union[float, int] = 0) -> NoReturn:
if self.active:
if self.determinate:
self._start_determinate()
else:
self._start_loop()
def _update_color(self, *args):
self.color = self.theme_cls.primary_color
def _start_determinate(self, *args):
self._alpha_anim_in.start(self)
Animation(
_rotation_angle=0,
duration=self.determinate_time * 0.7,
t="out_quad",
).start(self)
_angle_start_anim = Animation(
_angle_end=360, duration=self.determinate_time, t="in_out_quad"
)
_angle_start_anim.bind(
on_complete=lambda *x: self._alpha_anim_out.start(self)
)
_angle_start_anim.start(self)
def _start_loop(self, *args):
if self._alpha == 0:
_rot_anim = Animation(_rotation_angle=0, duration=2, t="linear")
_rot_anim.start(self)
self._alpha = 1
self._alpha_anim_in.start(self)
_angle_start_anim = Animation(
_angle_end=self._angle_end + 270, duration=0.6, t="in_out_cubic"
)
_angle_start_anim.bind(on_complete=self._anim_back)
_angle_start_anim.start(self)
def _anim_back(self, *args):
_angle_back_anim = Animation(
_angle_start=self._angle_end - 8, duration=0.6, t="in_out_cubic"
)
_angle_back_anim.bind(on_complete=self._start_loop)
_angle_back_anim.start(self)
def _reset(self, *args):
Animation.cancel_all(
self,
"_angle_start",
"_rotation_angle",
"_angle_end",
"_alpha",
"color",
)
self._angle_start = 0
self._angle_end = 0
self._rotation_angle = 360
self._alpha = 0
def _on_determinate_progress(
self, instance_animation, instance_spinner, value
):
if value == 1:
self.dispatch("on_determinate_complete")