-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathsnackbar.py
More file actions
115 lines (103 loc) · 4.14 KB
/
snackbar.py
File metadata and controls
115 lines (103 loc) · 4.14 KB
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
# -*- coding: utf-8 -*-
from collections import deque
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import ObjectProperty, StringProperty, NumericProperty
from kivy.uix.relativelayout import RelativeLayout
from kivymd.material_resources import DEVICE_TYPE
Builder.load_string('''
#:import Window kivy.core.window.Window
#:import get_color_from_hex kivy.utils.get_color_from_hex
#:import MDFlatButton kivymd.button.MDFlatButton
#:import MDLabel kivymd.label.MDLabel
#:import DEVICE_TYPE kivymd.material_resources.DEVICE_TYPE
<_SnackbarWidget>
canvas:
Color:
rgb: get_color_from_hex('323232')
Rectangle:
size: self.size
size_hint_y: None
size_hint_x: 1 if DEVICE_TYPE == 'mobile' else None
height: dp(48) if _label.texture_size[1] < dp(30) else dp(80)
width: dp(24) + _label.width + _spacer.width + root.padding_right if root.button_text == '' else dp(24) + \
_label.width + _spacer.width + _button.width + root.padding_right
top: 0
x: 0 if DEVICE_TYPE == 'mobile' else Window.width/2 - self.width/2
BoxLayout:
width: Window.width - root.padding_right - _spacer.width - dp(24) if DEVICE_TYPE == 'mobile' and \
root.button_text == '' else Window.width - root.padding_right - _button.width - _spacer.width - dp(24) \
if DEVICE_TYPE == 'mobile' else _label.texture_size[0] if (dp(568) - root.padding_right - _button.width - \
_spacer.width - _label.texture_size[0] - dp(24)) >= 0 else (dp(568) - root.padding_right - _button.width - \
_spacer.width - dp(24))
size_hint_x: None
x: dp(24)
MDLabel:
id: _label
text: root.text
size: self.texture_size
BoxLayout:
id: _spacer
size_hint_x: None
x: _label.right
width: 0
MDFlatButton:
id: _button
text: root.button_text
size_hint_x: None
x: _spacer.right if root.button_text != '' else root.right
center_y: root.height/2
on_release: root.button_callback()
''')
class _SnackbarWidget(RelativeLayout):
text = StringProperty()
button_text = StringProperty()
button_callback = ObjectProperty()
duration = NumericProperty()
padding_right = NumericProperty(dp(24))
def __init__(self, text, duration, button_text='', button_callback=None,
**kwargs):
super(_SnackbarWidget, self).__init__(**kwargs)
self.text = text
self.button_text = button_text
self.button_callback = button_callback
self.duration = duration
self.ids['_label'].text_size = (None, None)
def begin(self):
if self.button_text == '':
self.remove_widget(self.ids['_button'])
else:
self.ids['_spacer'].width = dp(16) if \
DEVICE_TYPE == "mobile" else dp(40)
self.padding_right = dp(16)
Window.add_widget(self)
anim = Animation(y=0, duration=.3, t='out_quad')
anim.start(self)
Clock.schedule_once(lambda dt: self.die(), self.duration)
def die(self):
anim = Animation(top=0, duration=.3, t='out_quad')
anim.bind(on_complete=lambda *args: _play_next(self))
anim.bind(on_complete=lambda *args: Window.remove_widget(self))
anim.start(self)
queue = deque()
playing = False
def make(text, button_text=None, button_callback=None, duration=3):
if button_text is not None and button_callback is not None:
queue.append(_SnackbarWidget(text=text,
button_text=button_text,
button_callback=button_callback,
duration=duration))
else:
queue.append(_SnackbarWidget(text=text,
duration=duration))
_play_next()
def _play_next(dying_widget=None):
global playing
if (dying_widget or not playing) and len(queue) > 0:
playing = True
queue.popleft().begin()
elif len(queue) == 0:
playing = False