[gnome-music/wip/mschraal/artstack-pass-widget-around: 2/2] defaulticon: Pass widget to DefaultIcon




commit 235b36271c926298f8781f76ed88eaf05ed7170e
Author: Marinus Schraal <mschraal gnome org>
Date:   Fri Feb 4 10:02:29 2022 +0100

    defaulticon: Pass widget to DefaultIcon
    
    No langer pass scale, but pass the owning widget directly and derive the
    scale from it.

 gnomemusic/artcache.py         |  9 ++++-----
 gnomemusic/defaulticon.py      | 20 ++++++++++++++------
 gnomemusic/widgets/artstack.py |  6 ++----
 3 files changed, 20 insertions(+), 15 deletions(-)
---
diff --git a/gnomemusic/artcache.py b/gnomemusic/artcache.py
index 7d19a4adf..7d51bbc46 100644
--- a/gnomemusic/artcache.py
+++ b/gnomemusic/artcache.py
@@ -65,15 +65,14 @@ class ArtCache(GObject.GObject):
         """
         self._coreobject = coreobject
         self._size = size
-        scale = self._widget.props.scale_factor
 
         if isinstance(coreobject, CoreArtist):
-            self._default_icon = DefaultIcon().get(
-                DefaultIcon.Type.ARTIST, self._size, scale)
+            self._default_icon = DefaultIcon(self._widget).get(
+                DefaultIcon.Type.ARTIST, self._size)
         elif (isinstance(coreobject, CoreAlbum)
                 or isinstance(coreobject, CoreSong)):
-            self._default_icon = DefaultIcon().get(
-                DefaultIcon.Type.ALBUM, self._size, scale)
+            self._default_icon = DefaultIcon(self._widget).get(
+                DefaultIcon.Type.ALBUM, self._size)
 
         thumbnail_uri = coreobject.props.thumbnail
         if thumbnail_uri == "generic":
diff --git a/gnomemusic/defaulticon.py b/gnomemusic/defaulticon.py
index 519b2910d..b40a0c215 100644
--- a/gnomemusic/defaulticon.py
+++ b/gnomemusic/defaulticon.py
@@ -110,16 +110,23 @@ class DefaultIcon(GObject.GObject):
         ARTIST = "avatar-default-symbolic"
 
     _cache: Dict[
-        Tuple[DefaultIcon.Type, ArtSize, int, bool, bool], cairo.Surface] = {}
+        Tuple[DefaultIcon.Type, ArtSize, int, bool, bool],
+        cairo.ImageSurface] = {}
 
     _default_theme = Gtk.IconTheme.get_default()
 
-    def __init__(self):
+    def __init__(self, widget: Gtk.Widget) -> None:
+        """Initialize DefaultIcon
+
+        :param Gtk.Widget widget: The widget of the icon
+        """
         super().__init__()
 
+        self._widget = widget
+
     def _make_default_icon(
             self, icon_type: DefaultIcon.Type, art_size: ArtSize, scale: int,
-            round_shape: bool, dark: bool) -> cairo.Surface:
+            round_shape: bool, dark: bool) -> cairo.ImageSurface:
         icon_info = self._default_theme.lookup_icon_for_scale(
             icon_type.value, art_size.width / 3, scale, 0)
         icon = icon_info.load_surface()
@@ -129,7 +136,8 @@ class DefaultIcon(GObject.GObject):
 
         return icon_surface
 
-    def get(self, icon_type, art_size, scale=1):
+    def get(self, icon_type: DefaultIcon.Type,
+            art_size: ArtSize) -> cairo.ImageSurface:
         """Returns the requested symbolic icon
 
         Returns a cairo surface of the requested symbolic icon in the
@@ -137,10 +145,9 @@ class DefaultIcon(GObject.GObject):
 
         :param enum icon_type: The DefaultIcon.Type of the icon
         :param enum art_size: The ArtSize requested
-        :param int scale: The scale
 
         :return: The symbolic icon
-        :rtype: cairo.Surface
+        :rtype: cairo.ImageSurface
         """
         if icon_type == DefaultIcon.Type.ALBUM:
             round_shape = False
@@ -148,6 +155,7 @@ class DefaultIcon(GObject.GObject):
             round_shape = True
 
         dark = Handy.StyleManager.get_default().props.dark
+        scale = self._widget.props.scale_factor
 
         if (icon_type, art_size,
                 scale, round_shape, dark) not in self._cache.keys():
diff --git a/gnomemusic/widgets/artstack.py b/gnomemusic/widgets/artstack.py
index 97e33fd7c..81357e41d 100644
--- a/gnomemusic/widgets/artstack.py
+++ b/gnomemusic/widgets/artstack.py
@@ -121,8 +121,7 @@ class ArtStack(Gtk.Stack):
         """
         self._art_type = value
 
-        default_icon = DefaultIcon().get(
-            self._art_type, self._size, self.props.scale_factor)
+        default_icon = DefaultIcon(self).get(self._art_type, self._size)
 
         self._swap_thumbnails(default_icon, False)
 
@@ -146,8 +145,7 @@ class ArtStack(Gtk.Stack):
     def _on_dark_changed(
             self, style_manager: Handy.StyleManager,
             pspec: GObject.ParamSpecBoolean) -> None:
-        default_icon = DefaultIcon().get(
-            self._art_type, self._size, self.props.scale_factor)
+        default_icon = DefaultIcon(self).get(self._art_type, self._size)
 
         if self._coreobject:
             if self._coreobject.props.thumbnail == "generic":


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]