]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - music_sampler/app_blocks/actionlist.py
Use @mainthread decorator where necessary
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / app_blocks / actionlist.py
1 from kivy.uix.label import Label
2 from kivy.uix.stacklayout import StackLayout
3 from kivy.uix.relativelayout import RelativeLayout
4 from kivy.properties import ListProperty, StringProperty
5 from ..lock import Lock
6
7 from kivy.clock import mainthread
8
9 __all__ = ["ActionList",
10 "ActionListIcons", "ActionListIcon",
11 "ActionListDescriptions", "ActionListDescription"]
12
13 actionlist_lock = Lock("playlist")
14
15 class ActionList(RelativeLayout):
16 action_title = StringProperty("")
17 action_list = ListProperty([])
18
19 @mainthread
20 def update_list(self, key, action_descriptions):
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
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])
40 with actionlist_lock:
41 self.action_list = action_list
42
43 class 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):
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])
65
66 class 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:
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
81
82 class 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):
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])
105
106 class 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:
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
118
119 def update_font_size(self, instance, font_size):
120 self.font_size = font_size
121