[gnome-music/wip/mschraal/rework-art-widget: 14/18] coverpaintable: Split out texture caching




commit 06267f59237904c361fdefa633317b87bbafd5a6
Author: Marinus Schraal <mschraal gnome org>
Date:   Sat Apr 2 23:50:32 2022 +0200

    coverpaintable: Split out texture caching

 gnomemusic/coverpaintable.py | 20 ++++------
 gnomemusic/texturecache.py   | 87 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+), 13 deletions(-)
---
diff --git a/gnomemusic/coverpaintable.py b/gnomemusic/coverpaintable.py
index 407f7d14f..c7dd04a93 100644
--- a/gnomemusic/coverpaintable.py
+++ b/gnomemusic/coverpaintable.py
@@ -30,8 +30,8 @@ import gi
 gi.require_versions({"Gdk": "4.0", "Gtk": "4.0", "Gsk": "4.0"})
 from gi.repository import Adw, Gsk, Gtk, GObject, Graphene, Gdk
 
-from gnomemusic.asyncqueue import AsyncQueue
 from gnomemusic.mediaartloader import MediaArtLoader
+from gnomemusic.texturecache import TextureCache
 from gnomemusic.utils import ArtSize, DefaultIconType
 if typing.TYPE_CHECKING:
     from gnomemusic.corealbum import CoreAlbum
@@ -51,19 +51,14 @@ class CoverPaintable(GObject.GObject, Gdk.Paintable):
 
     __gtype_name__ = "CoverPaintable"
 
-    _async_queue = AsyncQueue("CoverPaintable")
-
     def __init__(
             self, art_size: ArtSize, widget: Gtk.Widget,
-            icon_type: DefaultIconType = DefaultIconType.ALBUM,
-            texture: Gdk.Texture = None) -> None:
+            icon_type: DefaultIconType = DefaultIconType.ALBUM) -> None:
         """Initiliaze CoverPaintable
 
         :param ArtSize art_size: Size of the cover
         :param Gtk.Widget widget: Widget using the cover
         :param DefaultIconType icon_type: Type of cover
-        :param Gdk.Texture texture: Texture to use or None for
-            placeholder
         """
         super().__init__()
 
@@ -75,7 +70,8 @@ class CoverPaintable(GObject.GObject, Gdk.Paintable):
             widget.get_display())
         self._icon_type = icon_type
         self._style_manager = Adw.StyleManager.get_default()
-        self._texture = texture
+        self._texture = None
+        self._texture_cache = TextureCache()
         self._thumbnail_id = 0
         self._widget = widget
 
@@ -170,12 +166,10 @@ class CoverPaintable(GObject.GObject, Gdk.Paintable):
             self.invalidate_contents()
             return
 
-        self._art_loader = MediaArtLoader()
-        self._art_loading_id = self._art_loader.connect(
-            "finished", self._on_art_loading_finished)
-        self._async_queue.queue(self._art_loader, thumbnail_uri)
+        self._texture_cache.connect("texture", self._on_texture_cache)
+        self._texture_cache.lookup(thumbnail_uri)
 
-    def _on_art_loading_finished(self, art_loader, texture) -> None:
+    def _on_texture_cache(self, texture_cache, texture):
         if texture:
             self._texture = texture
             self.invalidate_contents()
diff --git a/gnomemusic/texturecache.py b/gnomemusic/texturecache.py
new file mode 100644
index 000000000..b721e2087
--- /dev/null
+++ b/gnomemusic/texturecache.py
@@ -0,0 +1,87 @@
+# Copyright 2022 The GNOME Music developers
+#
+# GNOME Music is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GNOME Music is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# The GNOME Music authors hereby grant permission for non-GPL compatible
+# GStreamer plugins to be used and distributed together with GStreamer
+# and GNOME Music.  This permission is above and beyond the permissions
+# granted by the GPL license by which GNOME Music is covered.  If you
+# modify this code, you may extend this exception to your version of the
+# code, but you are not obligated to do so.  If you do not wish to do so,
+# delete this exception statement from your version.
+
+from __future__ import annotations
+from typing import Dict, Union
+import typing
+
+from gi.repository import GObject, Gdk
+
+from gnomemusic.asyncqueue import AsyncQueue
+from gnomemusic.mediaartloader import MediaArtLoader
+if typing.TYPE_CHECKING:
+    from gnomemusic.corealbum import CoreAlbum
+    from gnomemusic.coreartist import CoreArtist
+    from gnomemusic.coresong import CoreSong
+
+if typing.TYPE_CHECKING:
+    CoreObject = Union[CoreAlbum, CoreArtist, CoreSong]
+
+
+class TextureCache(GObject.GObject):
+    """Retrieval and cache for artwork textures
+    """
+
+    __gtype_name__ = "TextureCache"
+
+    __gsignals__ = {
+        "texture": (GObject.SignalFlags.RUN_FIRST, None, (object, ))
+    }
+
+    _async_queue = AsyncQueue("TextureCache")
+
+    _textures: Dict[str, Gdk.Texture] = {}
+
+    def __init__(self) -> None:
+        """Initiliaze Texturecache
+        """
+        super().__init__()
+
+        self._art_loader = MediaArtLoader()
+        self._art_loading_id = 0
+        self._uri = ""
+
+    def lookup(self, uri: str) -> None:
+        self._uri = uri
+
+        if self._art_loading_id != 0:
+            self._art_loader.disconnect(self._art_loading_id)
+            self._art_loading_id = 0
+
+        if uri in self._textures.keys():
+            self.emit("texture", self._textures[uri])
+            return
+
+        self._art_loader = MediaArtLoader()
+        self._art_loading_id = self._art_loader.connect(
+            "finished", self._on_art_loading_finished)
+        self._async_queue.queue(self._art_loader, self._uri)
+
+    def _on_art_loading_finished(
+            self, art_loader: MediaArtLoader, texture: Gdk.Texture) -> None:
+        if texture:
+            self._textures[self._uri] = texture
+            self.emit("texture", texture)
+        else:
+            self.emit("texture", None)


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