-
Notifications
You must be signed in to change notification settings - Fork 2
/
toolbar.py
426 lines (324 loc) · 11.2 KB
/
toolbar.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""
Components/Toolbar
==================
Example
-------
.. code-block:: python
from kivy.uix.behaviors import ButtonBehavior
from kivy.lang.builder import Builder
import kivymd_extensions.akivymd
from kivymd.app import MDApp
from kivymd.uix.list import OneLineListItem
kv = '''
Screen:
AKToolbarLayout:
id: toolbar
AKToolbarClass:
MDToolbar:
title: 'Hide On Scroll'
left_action_items: [('menu', lambda x: None)]
AKToolbarPinClass:
id: pin
height: dp(40)
canvas.before:
Color:
rgba: app.theme_cls.accent_color
Rectangle:
pos: self.pos
size: self.size
MDLabel:
text: 'Pinned to top'
halign: 'center'
valign: 'center'
AKToolbarContent:
id: box
AKToolbarFloatingButton:
MDFloatingActionButton:
icon: 'arrow-up'
on_release: toolbar.scroll_to(1)
'''
class Main(MDApp):
def build(self):
return Builder.load_string(kv)
def on_start(self):
for x in range(30):
self.root.ids.box.add_widget(OneLineListItem(
text= f'List {x}'
))
return super().on_start()
Main().run()
"""
__all__ = (
"AKToolbarClass",
"AKToolbarPinClass",
"AKToolbarFloatingButton",
"AKToolbarContent",
"AKToolbarLayout",
)
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.effects.dampedscroll import DampedScrollEffect
from kivy.lang.builder import Builder
from kivy.properties import (
BooleanProperty,
NumericProperty,
ObjectProperty,
OptionProperty,
ReferenceListProperty,
StringProperty,
)
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.stencilview import StencilView
from kivymd.theming import ThemableBehavior
from kivy.uix.recycleview import RecycleView
Builder.load_string(
"""
<AKToolbarClass>
size_hint_y: None
height: self.minimum_height
<AKToolbarPinClass>
size_hint_y: None
height: self.minimum_height
<AKToolbarFloatingButton>
size_hint: None, None
size: self.minimum_size
<AKToolbarContent>
size_hint_y: None
do_scroll_y:True
RecycleBoxLayout:
orientation: "vertical"
size_hint_y: None
spacing:dp(20)
padding:20,20,20,20
width: self.minimum_width
height:self.minimum_height
default_size_hint:1,None
default_size:None,None
<AKToolbarLayout>
_toolbar: _toolbar
_floating_button: _floating_button
id:scroll
MDBoxLayout:
id: scrollbox
adaptive_height: True
orientation: "vertical"
BoxLayout:
size_hint_y: None
height: _toolbar.height + _pin_widget.height
MDBoxLayout:
id: _toolbar
adaptive_height: True
y:root.height-self.height
MDBoxLayout:
id: _pin_widget
top: _toolbar.y
adaptive_height: True
BoxLayout:
id: _floating_button
size_hint: None, None
size: self.minimum_size
"""
)
class AKToolbarClass(BoxLayout):
pass
class AKToolbarFloatingButton(BoxLayout):
pass
class CustomScrollViewEffect(DampedScrollEffect):
friction = 0.1
class AKToolbarContent(RecycleView):
_root = ObjectProperty()
class AKToolbarPinClass(BoxLayout):
def on_touch_down(self, touch):
pos = touch.pos
if not self.collide_point(*pos):
return False
for child in self.children:
child.on_touch_down(touch)
return True
class AKToolbarLayout(ThemableBehavior, StencilView, RelativeLayout):
duration = NumericProperty(0.3)
"""Animations duration.
:attr:`duration` is an :class:`~kivy.properties.NumericProperty`
and defaults to `0.3`.
"""
transition = StringProperty("out_quad")
"""Animations transition.
:attr:`duration` is an :class:`~kivy.properties.StringProperty`
and defaults to `'out_quad'`.
"""
show_on_stop = BooleanProperty(False)
"""Always shows toolbar after scroll stop.
:attr:`show_on_stop` is an :class:`~kivy.properties.BooleanProperty`
and defaults to `False`.
"""
auto_adjust = BooleanProperty(True)
"""Automatically adjusts the toolbar's height after scrolling stop.
:attr:`auto_adjust` is an :class:`~kivy.properties.BooleanProperty`
and defaults to `True`.
"""
float_button_pos = OptionProperty("br", options=["br", "bl", "tl", "tr"])
"""Floating button position.
:attr:`float_button_pos` is an :class:`~kivy.properties.OptionProperty`
and defaults to `br`.
"""
float_button_padding_x = NumericProperty("30dp")
"""Floating button's horizontal padding.
:attr:`float_button_padding_x` is an :class:`~kivy.properties.NumericProperty`
and defaults to `'30dp`.
"""
float_button_padding_y = NumericProperty("30dp")
"""Floating button's vertical padding.
:attr:`float_button_padding_y` is an :class:`~kivy.properties.NumericProperty`
and defaults to `'30dp`.
"""
float_button_padding = ReferenceListProperty(
float_button_padding_x, float_button_padding_y
)
float_button_show_distance = NumericProperty("150dp")
_toolbar = ObjectProperty()
_floating_button = ObjectProperty()
_last_y = False
_anim_playing = False
_float_anim_playing = False
_float_button_status = "close"
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self._update)
def _update(self, *args):
self.ids.content.bind(vbar=self._set_toolbar_pos)
self.ids.content.bind(on_scroll_stop=self.on_scroll_stop)
self.effect_cls = CustomScrollViewEffect
Window.bind(on_resize=self._update_toolbar_pos)
Window.bind(on_resize=self._update_floating_button_pos)
self._last_height = self.height
def _set_toolbar_pos(self, instance, vbar):
y = vbar[0] * self.height
if not self._last_y:
self._last_y = y
return
else:
diff = self._last_y - y
self._last_y = y
# Sometimes scrollview jumps suddenly
if diff > 100:
return
# handle floating button
scrolled_y = (1 - vbar[1] - vbar[0]) * self.height
if (
scrolled_y > self.float_button_show_distance
and self._float_button_status == "close"
):
self.show_float_button()
elif (
scrolled_y < self.float_button_show_distance
and self._float_button_status == "open"
):
self.hide_float_button()
if self._anim_playing:
Animation.stop_all(self._toolbar)
self._anim_playing = False
self._toolbar.y += diff
# hide
if self._toolbar.y > self.height:
self.hide_toolbar()
# full size
elif (
self._toolbar.top < self.height or round(vbar[0] + vbar[1], 2) == 1
):
self.show_toolbar()
def hide_toolbar(self, animate=False, *args):
if animate and not self._anim_playing:
self._anim_playing = True
anim = Animation(y=self.height, d=self.duration, t=self.transition)
anim.bind(on_complete=self._allow_anim)
anim.start(self._toolbar)
else:
self._toolbar.y = self.height
def show_toolbar(self, animate=False, *args):
if animate and not self._anim_playing:
self._anim_playing = True
anim = Animation(
top=self.height, d=self.duration, t=self.transition
)
anim.bind(on_complete=self._allow_anim)
anim.start(self._toolbar)
else:
self._toolbar.top = self.height
def _allow_anim(self, *args):
self._anim_playing = False
return
def _update_toolbar_pos(self, *args):
def update_pos(*args):
self._toolbar.top = self.height
Clock.schedule_once(update_pos)
def add_widget(self, widget, index=0, canvas=None):
if issubclass(widget.__class__, AKToolbarContent):
widget._root = self
return self.ids.scrollbox.add_widget(widget)
elif issubclass(widget.__class__, AKToolbarClass):
return self.ids._toolbar.add_widget(widget)
elif issubclass(widget.__class__, AKToolbarPinClass):
return self.ids._pin_widget.add_widget(widget)
elif issubclass(widget.__class__, AKToolbarFloatingButton):
self.ids._floating_button.add_widget(widget)
Clock.schedule_once(self._update_floating_button_pos)
else:
return super().add_widget(widget, index=index, canvas=canvas)
def on_scroll_stop(self, instance, touch):
if self.show_on_stop:
Clock.schedule_once(self.show_toolbar, 1)
elif self.auto_adjust:
Clock.schedule_once(self._auto_adjust, 1)
def _auto_adjust(self, *args):
middle_pos = self.height - self._toolbar.height / 2
if self._toolbar.y < middle_pos:
self.show_toolbar(animate=True)
else:
self.hide_toolbar(animate=True)
def _update_floating_button_pos(self, *args):
width = Window.width
padding_y = self.float_button_padding_y
if self.float_button_pos == "br":
self._floating_button.x = width + 20
self._floating_button.y = padding_y
elif self.float_button_pos == "bl":
raise NotImplementedError
elif self.float_button_pos == "tr":
raise NotImplementedError
elif self.float_button_pos == "tl":
raise NotImplementedError
def _allow_float_anim(self, *args):
self._float_anim_playing = False
def show_float_button(self):
if self._float_anim_playing:
self._float_anim_playing = True
return
self._float_button_status = "open"
Animation.stop_all(self._floating_button)
width = Window.width
anim = Animation(
right=width - self.float_button_padding_x,
d=self.duration,
t=self.transition,
)
anim.bind(on_complete=self._allow_float_anim)
anim.start(self._floating_button)
def hide_float_button(self):
if self._float_anim_playing:
self._float_anim_playing = True
return
self._float_button_status = "close"
Animation.stop_all(self._floating_button)
width = Window.width
width = Window.width
anim = Animation(x=width + 20, d=self.duration, t=self.transition)
anim.bind(on_complete=self._allow_float_anim)
anim.start(self._floating_button)
def get_float_button_status(self):
return self._float_button_status
def scroll_to(self, value):
Animation(scroll_y=value, d=self.duration, t=self.transition).start(
self.ids.scroll
)