diff cagou/plugins/plugin_wid_chat.py @ 433:aa204c813f07

chat: attachment preview: - if there is not already one, a preview is generated for images collection thumbnails, instead of using the full size image. - for images attachment, local path is now used if present, before URL.
author Goffi <goffi@goffi.org>
date Sun, 01 Mar 2020 22:11:25 +0100
parents 36c3f1c02d33
children 53bb3886b408
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_chat.py	Sun Mar 01 22:11:25 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Sun Mar 01 22:11:25 2020 +0100
@@ -128,7 +128,8 @@
     image = properties.ObjectProperty()
 
     def on_press(self):
-        gallery = ImagesGallery(sources=[self.image.source])
+        full_size_source = self.data.get('path', self.data.get('url'))
+        gallery = ImagesGallery(sources=[full_size_source])
         G.host.showExtraUI(gallery)
 
 
@@ -137,10 +138,22 @@
     chat = properties.ObjectProperty()
     mess_data = properties.ObjectProperty()
 
+    def _setPreview(self, attachment, wid, preview_path):
+        attachment['preview'] = preview_path
+        wid.source = preview_path
+
     def _setDecryptedPath(self, attachment, wid, path):
         attachment['path'] = path
         if wid is not None:
-            wid.source = path
+            # we also need a preview for the widget
+            if 'preview' in attachment:
+                wid.source = attachment['preview']
+            else:
+                G.host.bridge.imageGeneratePreview(
+                    path,
+                    self.chat.profile,
+                    callback=partial(self._setPreview, attachment, wid),
+                )
 
     def on_kv_post(self, __):
         attachments = self.attachments
@@ -187,7 +200,7 @@
     def on_press(self):
         sources = []
         for attachment in self.attachments:
-            source = attachment.get('url', attachment.get('path'))
+            source = attachment.get('path', attachment.get('url'))
             if not source:
                 log.warning(f"no source for {attachment}")
             else: