]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - music_sampler/app_blocks/actionlist.py
Use @mainthread decorator where necessary
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / app_blocks / actionlist.py
CommitLineData
60aa2bed
IB
1from kivy.uix.label import Label
2from kivy.uix.stacklayout import StackLayout
3from kivy.uix.relativelayout import RelativeLayout
4from kivy.properties import ListProperty, StringProperty
4d1dfc89 5from ..lock import Lock
60aa2bed 6
a9324e30
IB
7from kivy.clock import mainthread
8
60aa2bed
IB
9__all__ = ["ActionList",
10 "ActionListIcons", "ActionListIcon",
11 "ActionListDescriptions", "ActionListDescription"]
12
4d1dfc89
IB
13actionlist_lock = Lock("playlist")
14
60aa2bed
IB
15class ActionList(RelativeLayout):
16 action_title = StringProperty("")
17 action_list = ListProperty([])
18
a9324e30 19 @mainthread
60aa2bed 20 def update_list(self, key, action_descriptions):
814c30c6
IB
21 if key.repeat_delay > 0:
22 self.action_title = _(
23 "actions linked to key {} (repeat protection {}s):"
24 ).format(key.key_sym, key.repeat_delay)
25 else:
26 self.action_title = _(
27 "actions linked to key {}:"
28 ).format(key.key_sym)
29
60aa2bed
IB
30 action_list = []
31
32 for [action, status] in action_descriptions:
33 if status == "done":
34 icon = "✓"
35 elif status == "current":
36 icon = "✅"
37 else:
38 icon = " "
39 action_list.append([icon, action])
4d1dfc89
IB
40 with actionlist_lock:
41 self.action_list = action_list
60aa2bed
IB
42
43class ActionListIcons(StackLayout):
44 def __init__(self, **kwargs):
45 super(ActionListIcons, self).__init__(**kwargs)
46 self.icons = []
47
48 def on_parent(self, instance, parent):
49 parent.bind(action_list=self.update_actionlist_icons)
50
51 def update_actionlist_icons(self, instance, actionlist):
4d1dfc89
IB
52 icons_length = len(self.icons)
53 index = -1
54 for index, [icon, description] in enumerate(actionlist):
55 if index >= icons_length:
56 icon_label = ActionListIcon(text=icon)
57 self.add_widget(icon_label)
58 self.icons.append(icon_label)
59 else:
60 self.icons[index].text = icon
61
62 if index+1 < icons_length:
63 self.clear_widgets(children=self.icons[index+1:icons_length])
64 del(self.icons[index+1:icons_length])
60aa2bed
IB
65
66class ActionListIcon(Label):
67 def __init__(self, text='', **kwargs):
68 super(ActionListIcon, self).__init__(**kwargs)
69 self.text = text
70
71 def on_parent(self, instance, parent):
72 if parent is not None:
4d1dfc89
IB
73 parent.bind(font_size=self.update_font_size)
74 parent.bind(labels_height=self.update_height)
75
76 def update_height(self, instance, height):
77 self.height = height
78
79 def update_font_size(self, instance, font_size):
80 self.font_size = font_size
60aa2bed
IB
81
82class ActionListDescriptions(StackLayout):
83 def __init__(self, **kwargs):
84 super(ActionListDescriptions, self).__init__(**kwargs)
85 self.descriptions = []
86
87 def on_parent(self, instance, parent):
88 parent.bind(action_list=self.update_actionlist_descriptions)
89
90 def update_actionlist_descriptions(self, instance, actionlist):
4d1dfc89
IB
91 descriptions_length = len(self.descriptions)
92 index = -1
93 for index, [icon, description] in enumerate(actionlist):
94 if index >= descriptions_length:
95 description_label = ActionListDescription(text=description)
96 self.add_widget(description_label)
97 self.descriptions.append(description_label)
98 else:
99 self.descriptions[index].text = description
100
101 if index+1 < descriptions_length:
102 self.clear_widgets(
103 children=self.descriptions[index+1:descriptions_length])
104 del(self.descriptions[index+1:descriptions_length])
60aa2bed
IB
105
106class ActionListDescription(Label):
107 def __init__(self, text='', **kwargs):
108 super(ActionListDescription, self).__init__(**kwargs)
109 self.text = text
110
111 def on_parent(self, instance, parent):
112 if parent is not None:
4d1dfc89
IB
113 parent.bind(font_size=self.update_font_size)
114 parent.bind(labels_height=self.update_height)
115
116 def update_height(self, instance, height):
117 self.height = height
60aa2bed 118
4d1dfc89
IB
119 def update_font_size(self, instance, font_size):
120 self.font_size = font_size
60aa2bed 121