changeset 397:54f6a47cc60a

core (common): added a notifications counter on ContactButton and use it in JidSelector: A notifications counter is drawned above avatar if `nofifs` key is present and not empty in `data`. This is used in JidSelector to show counter when necessary with opened chats.
author Goffi <goffi@goffi.org>
date Sun, 09 Feb 2020 23:47:29 +0100
parents ae6f7fd1cb0e
children a5457241c17f
files cagou/core/common.py cagou/kv/common.kv
diffstat 2 files changed, 64 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/common.py	Sun Feb 09 23:47:29 2020 +0100
+++ b/cagou/core/common.py	Sun Feb 09 23:47:29 2020 +0100
@@ -49,16 +49,32 @@
     pass
 
 
+class NotifLabel(Label):
+    pass
+
+
 class ContactItem(BoxLayout):
     """An item from ContactList
 
-    The item will drawn as an icon (JID avatar) with its jid below
+    The item will drawn as an icon (JID avatar) with its jid below.
+    If "notifs" are present in data, a notification counter will be drawn above the
+    avatar.
     """
     base_width = dp(150)
+    avatar_layout = properties.ObjectProperty()
+    avatar = properties.ObjectProperty()
     profile = properties.StringProperty()
     data = properties.DictProperty()
     jid = properties.StringProperty('')
 
+    def on_kv_post(self, __):
+        if self.data and self.data.get('notifs'):
+            notif = NotifLabel(
+                pos_hint={"right": 0.8, "y": 0},
+                text=str(len(self.data['notifs']))
+            )
+            self.avatar_layout.add_widget(notif)
+
 
 class ContactButton(ButtonBehavior, ContactItem):
     pass
@@ -225,10 +241,18 @@
 
         for wid in opened_chats:
             contact_list = G.host.contact_lists[wid.profile]
+            data=contact_list.getItem(wid.target)
+            notifs = list(G.host.getNotifs(wid.target, profile=wid.profile))
+            if notifs:
+                # we shallow copy the dict to have the notification displayed only with
+                # opened chats (otherwise, the counter would appear on each other
+                # instance of ContactButton for this entity, i.e. in roster too).
+                data = data.copy()
+                data['notifs'] = notifs
             try:
                 item = ContactButton(
                     jid=wid.target,
-                    data=contact_list.getItem(wid.target),
+                    data=data,
                     profile=wid.profile,
                 )
             except Exception as e:
--- a/cagou/kv/common.kv	Sun Feb 09 23:47:29 2020 +0100
+++ b/cagou/kv/common.kv	Sun Feb 09 23:47:29 2020 +0100
@@ -15,26 +15,51 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+<NotifLabel>:
+    background_color: 1, 0, 0, 1
+    size_hint: None, None
+    text_size: None, root.height
+    padding_x: sp(5)
+    size: self.texture_size
+    bold: True
+    canvas.before:
+        Color:
+            rgba: root.background_color
+        Ellipse:
+            size: self.size
+            pos: self.pos
+
+
 <ContactItem>:
     size_hint: None, None
     width: self.base_width
     height: self.minimum_height
     orientation: 'vertical'
-    Avatar:
-        id: avatar
+    avatar: avatar
+    avatar_layout: avatar_layout
+    FloatLayout:
+        id: avatar_layout
         size_hint: 1, None
         height: dp(60)
-        source: root.data.get('avatar') or app.default_avatar
-        allow_stretch: True
-    Label:
-        id: jid_label
-        size_hint: None, None
-        text_size: root.base_width, None
-        size: self.texture_size
-        text: root.data.get('nick', root.jid.node or root.jid)
-        bold: True
-        valign: 'middle'
-        halign: 'center'
+        Avatar:
+            id: avatar
+            pos_hint: {'x': 0, 'y': 0}
+            source: root.data.get('avatar') or app.default_avatar
+            allow_stretch: True
+    BoxLayout:
+        id: label_box
+        size_hint: 1, None
+        height: self.minimum_height
+        Label:
+            size_hint: 1, None
+            height: self.font_size + sp(5)
+            text_size: self.size
+            shorten: True
+            shorten_from: "right"
+            text: root.data.get('nick', root.jid.node or root.jid)
+            bold: True
+            valign: 'middle'
+            halign: 'center'
 
 
 <JidItem>: