# HG changeset patch # User Goffi # Date 1581534178 -3600 # Node ID 84ff5c9170646072f9ffaa76b836b6f033b57bda # Parent f7476818f9fb1f1407bf20fbc76619a76e03266c widgets: implemented ordering in ContactItem and CagouWidget: ordering of slides in collection_carousel were using a specific key during the sort, this has been replaced by an ordering implemented directly into the classes. Ordering has also been implemented in ContactItem to have items appearing in a consistent order notably for opened chats. diff -r f7476818f9fb -r 84ff5c917064 cagou/core/cagou_widget.py --- a/cagou/core/cagou_widget.py Wed Feb 12 20:02:58 2020 +0100 +++ b/cagou/core/cagou_widget.py Wed Feb 12 20:02:58 2020 +0100 @@ -18,6 +18,7 @@ # along with this program. If not, see . +from functools import total_ordering from sat.core import log as logging from sat.core import exceptions from kivy.uix.behaviors import ButtonBehavior @@ -83,6 +84,7 @@ menu.ExtraSideMenu().show() +@total_ordering class CagouWidget(BoxLayout): main_container = properties.ObjectProperty(None) header_input = properties.ObjectProperty(None) @@ -118,6 +120,17 @@ ) self.header_box.add_widget(self.header_input) + def __lt__(self, other): + # XXX: sorting is notably used when collection_carousel is set + try: + target = str(self.target) + except AttributeError: + target = str(list(self.targets)[0]) + other_target = str(list(other.targets)[0]) + else: + other_target = str(other.target) + return target < other_target + @property def screen_manager(self): if ((not self.global_screen_manager diff -r f7476818f9fb -r 84ff5c917064 cagou/core/common.py --- a/cagou/core/common.py Wed Feb 12 20:02:58 2020 +0100 +++ b/cagou/core/common.py Wed Feb 12 20:02:58 2020 +0100 @@ -19,7 +19,7 @@ """common simple widgets""" import json -from functools import partial +from functools import partial, total_ordering from sat.core.i18n import _ from sat.core import log as logging from kivy.uix.widget import Widget @@ -54,7 +54,7 @@ class NotifLabel(Label): pass - +@total_ordering class ContactItem(BoxLayout): """An item from ContactList @@ -86,6 +86,9 @@ self.avatar_layout.remove_widget(self.badge) self.badge = None + def __lt__(self, other): + return self.jid < other.jid + class ContactButton(ButtonBehavior, ContactItem): pass diff -r f7476818f9fb -r 84ff5c917064 cagou/core/widgets_handler.py --- a/cagou/core/widgets_handler.py Wed Feb 12 20:02:58 2020 +0100 +++ b/cagou/core/widgets_handler.py Wed Feb 12 20:02:58 2020 +0100 @@ -497,17 +497,6 @@ continue yield w - def widgets_sort(self, widget): - """method used as key to sort the widgets - - order of the widgets when changing slide is affected - @param widget(QuickWidget): widget to sort - @return: a value which will be used for sorting - """ - try: - return str(widget.target).lower() - except AttributeError: - return str(list(widget.targets)[0]).lower() def _updateHiddenSlides(self): """adjust carousel slides according to visible widgets""" @@ -522,7 +511,7 @@ # we ignore current_slide as it may not be visible yet (e.g. if an other # screen is shown hidden = list(self.hiddenList(visible_list, ignore=current_slide)) - slides_sorted = sorted(set(hidden + [current_slide]), key=self.widgets_sort) + slides_sorted = sorted(set(hidden + [current_slide])) to_remove = set(self.carousel.slides).difference({current_slide}) for w in to_remove: self.carousel.remove_widget(w)