comparison cagou/core/xmlui.py @ 251:1f579baf787a

xmlui: some design improvments + use ScrollView + BoxLayout instead of DropDown for ListWidget: there are some trouble with the ScrollView inside the ScrollView which need to be addressed
author Goffi <goffi@goffi.org>
date Sat, 26 Jan 2019 20:24:48 +0100
parents 50f7c000b4ae
children 1b835bcfa663
comparison
equal deleted inserted replaced
250:ff1efdeff53f 251:1f579baf787a
29 from kivy.uix.textinput import TextInput 29 from kivy.uix.textinput import TextInput
30 from kivy.uix.label import Label 30 from kivy.uix.label import Label
31 from kivy.uix.button import Button 31 from kivy.uix.button import Button
32 from kivy.uix.togglebutton import ToggleButton 32 from kivy.uix.togglebutton import ToggleButton
33 from kivy.uix.widget import Widget 33 from kivy.uix.widget import Widget
34 from kivy.uix.dropdown import DropDown
35 from kivy.uix.switch import Switch 34 from kivy.uix.switch import Switch
36 from kivy import properties 35 from kivy import properties
37 from cagou import G 36 from cagou import G
38 from cagou.core import dialog 37 from cagou.core import dialog
39 from functools import partial 38 from functools import partial
129 128
130 class ListWidgetItem(ToggleButton): 129 class ListWidgetItem(ToggleButton):
131 value = properties.StringProperty() 130 value = properties.StringProperty()
132 131
133 def on_release(self): 132 def on_release(self):
134 super(ListWidgetItem, self).on_release()
135 parent = self.parent 133 parent = self.parent
136 while parent is not None and not isinstance(parent, DropDown): 134 while parent is not None and not isinstance(parent, ListWidget):
137 parent = parent.parent 135 parent = parent.parent
138 136
139 if parent is not None and parent.attach_to is not None: 137 if parent is not None:
140 parent.select(self) 138 parent.select(self)
139 return super(ListWidgetItem, self).on_release()
141 140
142 @property 141 @property
143 def selected(self): 142 def selected(self):
144 return self.state == 'down' 143 return self.state == 'down'
145 144
146 @selected.setter 145 @selected.setter
147 def selected(self, value): 146 def selected(self, value):
148 self.state = 'down' if value else 'normal' 147 self.state = 'down' if value else 'normal'
149 148
150 149
151 class ListWidget(xmlui.ListWidget, Button): 150 class ListWidget(xmlui.ListWidget, ScrollView):
151 layout = properties.ObjectProperty()
152 152
153 def __init__(self, _xmlui_parent, options, selected, flags): 153 def __init__(self, _xmlui_parent, options, selected, flags):
154 Button.__init__(self) 154 ScrollView.__init__(self)
155 self.text = _(u"open list")
156 self._dropdown = DropDown()
157 self._dropdown.auto_dismiss = False
158 self._dropdown.bind(on_select = self.on_select)
159 self.multi = 'single' not in flags 155 self.multi = 'single' not in flags
160 self._dropdown.dismiss_on_select = not self.multi
161 self._values = [] 156 self._values = []
162 for option in options: 157 for option in options:
163 self.addValue(option) 158 self.addValue(option)
164 self._xmluiSelectValues(selected) 159 self._xmluiSelectValues(selected)
165 self._on_change = None 160 self._on_change = None
166 161
167 @property 162 @property
168 def items(self): 163 def items(self):
169 return self._dropdown.children[0].children 164 return self.layout.children
170 165
171 def on_touch_down(self, touch): 166 def select(self, item):
172 # we simulate auto-dismiss ourself because dropdown
173 # will dismiss even if attached button is touched
174 # resulting in a dismiss just before a toggle in on_release
175 # so the dropbox would always be opened, we don't want that!
176 if super(ListWidget, self).on_touch_down(touch):
177 return True
178 if self._dropdown.parent:
179 self._dropdown.dismiss()
180
181 def on_release(self):
182 if self._dropdown.parent is not None:
183 # we want to close a list already opened
184 self._dropdown.dismiss()
185 else:
186 self._dropdown.open(self)
187
188 def on_select(self, drop_down, item):
189 if not self.multi: 167 if not self.multi:
190 self._xmluiSelectValues([item.value]) 168 self._xmluiSelectValues([item.value])
191 if self._on_change is not None: 169 if self._on_change is not None:
192 self._on_change(self) 170 self._on_change(self)
193 171
198 """ 176 """
199 self._values.append(option) 177 self._values.append(option)
200 item = ListWidgetItem() 178 item = ListWidgetItem()
201 item.value, item.text = option 179 item.value, item.text = option
202 item.selected = selected 180 item.selected = selected
203 self._dropdown.add_widget(item) 181 self.layout.add_widget(item)
204 182
205 def _xmluiSelectValue(self, value): 183 def _xmluiSelectValue(self, value):
206 self._xmluiSelectValues([value]) 184 self._xmluiSelectValues([value])
207 185
208 def _xmluiSelectValues(self, values): 186 def _xmluiSelectValues(self, values):