diff cagou/core/simple_xhtml.py @ 422:efee0e0afb78

core (common): moved simple_xhtml's image code to a generic "SizedImage" widget
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 16:41:27 +0100
parents 1da3c379205b
children 2d277a3d9cec
line wrap: on
line diff
--- a/cagou/core/simple_xhtml.py	Wed Feb 26 16:39:17 2020 +0100
+++ b/cagou/core/simple_xhtml.py	Wed Feb 26 16:41:27 2020 +0100
@@ -22,13 +22,12 @@
 from kivy.uix.stacklayout import StackLayout
 from kivy.uix.label import Label
 from kivy.utils import escape_markup
-from kivy.metrics import sp, dp
+from kivy.metrics import sp
 from kivy import properties
 from sat.core import log as logging
 from sat_frontends.tools import css_color, strings as sat_strings
 from cagou import G
-from cagou.core.image import AsyncImage
-from cagou.core.constants import Const as C
+from cagou.core.common  import SizedImage
 
 
 log = logging.getLogger(__name__)
@@ -89,53 +88,6 @@
             self.font_size = parent.font_size
 
 
-class SimpleXHTMLWidgetImage(AsyncImage):
-    # following properties are desired height/width
-    # i.e. the ones specified in height/width attributes of <img>
-    # (or wanted for whatever reason)
-    # set to None to ignore them
-    target_height = properties.NumericProperty(allownone=True)
-    target_width = properties.NumericProperty(allownone=True)
-
-    def __init__(self, **kwargs):
-        # best calculated size
-        self._best_width = self._best_height = 100
-        super().__init__(**kwargs)
-
-    def on_texture(self, instance, texture):
-        """Adapt the size according to max size and target_*"""
-        if texture is None:
-            return
-        max_width, max_height = dp(C.IMG_MAX_WIDTH), dp(C.IMG_MAX_HEIGHT)
-        width, height = texture.size
-        if self.target_width:
-            width = min(width, self.target_width)
-        if width > max_width:
-            width = C.IMG_MAX_WIDTH
-
-        height = width / self.image_ratio
-
-        if self.target_height:
-            height = min(height, self.target_height)
-
-        if height > max_height:
-            height = max_height
-            width = height * self.image_ratio
-
-        self.width, self.height = self._best_width, self._best_height = width, height
-
-    def on_parent(self, instance, parent):
-        if parent is not None:
-            parent.bind(width=self.on_parent_width)
-
-    def on_parent_width(self, instance, width):
-        if self._best_width > width:
-            self.width = width
-            self.height = width / self.image_ratio
-        else:
-            self.width, self.height = self._best_width, self._best_height
-
-
 class SimpleXHTMLWidget(StackLayout):
     """widget handling simple XHTML parsing"""
     xhtml = properties.StringProperty()
@@ -435,7 +387,8 @@
             log.warning(f"Can't parse image width: {elem.get('width')}")
             target_width = None
 
-        img = SimpleXHTMLWidgetImage(source=src, target_height=target_height, target_width=target_width)
+        img = SizedImage(
+            source=src, target_height=target_height, target_width=target_width)
         self.current_wid = img
         self.add_widget(img)